java - Reach the same object with a set of predicates in Sesame -


assuming (s,p,o) , list, use model structure in sesame , check if possible use subject s, list of predicates , reach o @ end.

for example there path, if (s,p,o) , {p1,p2,p3}, there exists following triples : (s,p1,o1) , (o1,p2,o2), (o2,p3,o)

is there possibility in sesame or model structure?

probably easiest way means of sparql query.

the query using property path expression express path. since want know if there exists path given starting point , ending point, assume "yes" or "no" answer enough. can boolean ask query:

ask { :s (:p1|:p2|:p3)+ :o . } 

this return true if path of length exists between :s , :o consists of combination of properties :p1, :p2, , :p3.

since can't execute sparql query directly on model, need create in-memory repository , add model before doing query, so:

model model = ...; // model  // create , initialize temporary in-memory store repository rep = new sailrepository(new memorystore()); rep.initialize();  try (repositoryconnection conn = rep.getconnection()) {    // load model repository    conn.add(model);    // execute query   string query = "ask { :s (:p1|:p2|:p3)+ :o . }";   boolean pathexists = conn.preparebooleanquery(query).evaluate(); } 

alternatively, can implement path traversal means of simple recursive method.

model model = ...; // model  iri start = ... ; // start point :s iri end = ...; // end point :o . iri p1 = ...; iri p2 =  ...; iri p3 = ... ;  boolean pathexists = pathexists(model, start, end, p1, p2, p3); 

with actual method being this:

boolean pathexists(model m, iri start, iri end, iri... properties) {     for(iri p: properties) {         model fromstart = m.filter(start, p, null);           if (fromstart.contains(start, p, end)) {                return true;         }         else {              (value obj: fromstart.objects()) {                 if (obj instanceof resource) {                      if(pathexists(m, obj, end, properties)) {                             return true;                      }                               }             }          }     }     return false; }       

...you may need extend little bit (with list of visited intermediate nodes avoid infinite loops), illustrates basic principle hope. in case, using sparql query far easier.


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -