java - multiple filter in openrdf sesame Model -
i filter model, triples has specific predicate , subject of type c. below code not return result, 1 has idea how implement it?
return triples.filter(null, new uriimpl(property.getfulliri()), null).filter (null, rdf.type,new uriimpl(c.getfulliri()));
the problem applying second filter
on result of first - result of first filter only contains triples property on filtered - second filter can never return empty result (since no triple in intermediate result have rdf:type
predicate).
since expressing secondary constraint 'non-sequential' in fashion, can not solve filtering alone. need construct new model
, fill data go along. along these lines:
// use valuefactory, avoid instantiating uriimpl directly. valuefactory vf = valuefactoryimpl().getinstance(); uri c = vf.createuri(c.getfulliri()); uri prop = vf.createuri(property.getfulliri()) // create new model resulting triple collection model result = new linkedhashmodel(); // filter on supplied property model propmatches = triples.filter(null, prop, null); for(resource subject: propmatches.subjects()) { // check if selected subject of supplied type if (triples.contains(subject, rdf.type, c)) { // add type triple result result.add(subject, rdf.type, c); // add property triple(s) result result.addall(propmatches.filter(subject, null, null)); } } return result;
the above works sesame 2. if using sesame 4 (which supports java 8 , stream api), can more easily, so:
return triples.stream().filter(st -> { if (prop.equals(st.getpredicate()) { // add triple if subject has correct type return triples.contains(st.getsubject(), rdf.type, c)); } else if (rdf.type.equals(st.getpredicate()) && c.equals(st.getobject()) { // add triple if subject has correct prop return triples.contains(st.getsubject(), prop, null); } return false; }).collect(collectors.tocollection(linkedhashmodel::new));
Comments
Post a Comment