i have rdf graph of hierarchy 3 levels deep. want retrieve paths starting root of class hierarchy (i.e., owl:thing) down classes in third level without using reasoner. instance, path c1 → c2 → c3 path, each ci class @ ith level of hierarchy.
i need retrieve paths in rdf graph using breadth first search algorithm no considerations object properties in graph.
given data (where length of class name indication of depth of class in hierarchy):
@prefix : <http://example.org/> . @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . :a rdfs:class . :aa rdfs:subclassof :a . :ab rdfs:subclassof :a . :ac rdfs:subclassof :a . :aaa rdfs:subclassof :aa . :aab rdfs:subclassof :aa . :aac rdfs:subclassof :aa . :aaba rdfs:subclassof :aab . :aabb rdfs:subclassof :aab . :aba rdfs:subclassof :ab . :abb rdfs:subclassof :ab . you can use sparql query select paths you're looking for.
using sparql query
you can write sparql query following results:
prefix : <http://example.org/> prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> select ?c1 ?c2 ?c3 { values ?c1 { :a } ?c1 ^rdfs:subclassof ?c2 . optional { ?c2 ^rdfs:subclassof ?c3 . } } order ?c3 ?c2 ?c1 ------------------- | c1 | c2 | c3 | =================== | :a | :ac | | | :a | :aa | :aaa | | :a | :aa | :aab | | :a | :aa | :aac | | :a | :ab | :aba | | :a | :ab | :abb | ------------------- using camera ontology
this approach works camera ontology has been mentioned in comments, though query requires little extension handle deeper class paths. thus:
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> prefix owl: <http://www.w3.org/2002/07/owl#> prefix : <http://www.xfront.com/owl/ontologies/camera/#> select * { values ?c1 { owl:thing } ?c1 ^rdfs:subclassof ?c2 . optional { ?c2 ^rdfs:subclassof ?c3 . optional { ?c3 ^rdfs:subclassof ?c4 . } } } order ?c4 ?c3 ?c2 ----------------------------------------------------------- | c1 | c2 | c3 | c4 | =========================================================== | owl:thing | :money | | | | owl:thing | :range | | | | owl:thing | :window | | | | owl:thing | :purchaseableitem | :body | | | owl:thing | :purchaseableitem | :lens | | | owl:thing | :purchaseableitem | :camera | :digital | | owl:thing | :purchaseableitem | :camera | :large-format | ----------------------------------------------------------- using jena api
while above sparql query produces paths in order expected breadth first traversal, there no guarantee on how arq generates results. can implement breadth first search directly, using jena model api retrieve subclasses. here's straightforward implementation:
import java.io.ioexception; import java.io.inputstream; import java.util.arraylist; import java.util.linkedlist; import java.util.list; import java.util.queue; import com.hp.hpl.jena.rdf.model.model; import com.hp.hpl.jena.rdf.model.modelfactory; import com.hp.hpl.jena.rdf.model.resource; import com.hp.hpl.jena.rdf.model.stmtiterator; import com.hp.hpl.jena.vocabulary.owl; import com.hp.hpl.jena.vocabulary.rdfs; public class bfsinrdfwithjena { public static list<list<resource>> bfs( final model model, final queue<list<resource>> queue, final int depth ) { final list<list<resource>> results = new arraylist<>(); while ( !queue.isempty() ) { final list<resource> path = queue.poll(); results.add( path ); if ( path.size() < depth ) { final resource last = path.get( path.size() - 1 ); final stmtiterator stmt = model.liststatements( null, rdfs.subclassof, last ); while ( stmt.hasnext() ) { final list<resource> extpath = new arraylist<>( path ); extpath.add( stmt.next().getsubject().asresource() ); queue.offer( extpath ); } } } return results; } public static void main( final string[] args ) throws ioexception { final model model = modelfactory.createdefaultmodel(); try ( final inputstream in = bfsinrdfwithjena.class.getclassloader().getresourceasstream( "camera.owl" ) ) { model.read( in, null ); } // setup initial queue final queue<list<resource>> queue = new linkedlist<>(); final list<resource> thingpath = new arraylist<>(); thingpath.add( owl.thing ); queue.offer( thingpath ); // paths, , display them final list<list<resource>> paths = bfs( model, queue, 4 ); ( list<resource> path : paths ) { system.out.println( path ); } } } [http://www.w3.org/2002/07/owl#thing] [http://www.w3.org/2002/07/owl#thing, http://www.xfront.com/owl/ontologies/camera/#purchaseableitem] [http://www.w3.org/2002/07/owl#thing, http://www.xfront.com/owl/ontologies/camera/#window] [http://www.w3.org/2002/07/owl#thing, http://www.xfront.com/owl/ontologies/camera/#range] [http://www.w3.org/2002/07/owl#thing, http://www.xfront.com/owl/ontologies/camera/#money] [http://www.w3.org/2002/07/owl#thing, http://www.xfront.com/owl/ontologies/camera/#purchaseableitem, http://www.xfront.com/owl/ontologies/camera/#camera] [http://www.w3.org/2002/07/owl#thing, http://www.xfront.com/owl/ontologies/camera/#purchaseableitem, http://www.xfront.com/owl/ontologies/camera/#lens] [http://www.w3.org/2002/07/owl#thing, http://www.xfront.com/owl/ontologies/camera/#purchaseableitem, http://www.xfront.com/owl/ontologies/camera/#body] [http://www.w3.org/2002/07/owl#thing, http://www.xfront.com/owl/ontologies/camera/#purchaseableitem, http://www.xfront.com/owl/ontologies/camera/#camera, http://www.xfront.com/owl/ontologies/camera/#digital] [http://www.w3.org/2002/07/owl#thing, http://www.xfront.com/owl/ontologies/camera/#purchaseableitem, http://www.xfront.com/owl/ontologies/camera/#camera, http://www.xfront.com/owl/ontologies/camera/#large-format]
Comments
Post a Comment