Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Assuming that this builds on the answer to your <a href="https://stackoverflow.com/a/17751328/1281433">previous question</a> about constructing the type of paths that you mention in the question, can you just use:</p> <pre class="lang-java prettyprint-override"><code>public static int classSize( final Resource klass ) { return klass.getModel().listSubjectsWithProperty( RDFS.subClassOf, klass ).toList().size(); } public static double pathSize( final List&lt;Resource&gt; path ) { int prevSize = classSize( path.get( 0 )); double pathSum = prevSize; for ( int i = 1; i &lt; path.size(); i++ ) { int currSize = classSize( path.get( i )); double linkWeight = currSize &lt; prevSize ? 0.5 : 1.0; pathSum += linkWeight + currSize; prevSize = currSize; } return pathSum; } </code></pre> <p>Then we get the following sums for the paths. The complete code follows this output. Notice the <code>owl:Thing</code> has a size of four here, not five, as you gave in the question. if the idea was to count the number of times that a class appeared as the object in a triple of <code>rdfs:subClassOf</code>, there are only four triples of the form <code>something rdfs:subClassOf owl:Thing</code> in the OWL file that you linked to, so it seems like its size should be four, not five. Given that consideration, note that the “Thing–PurchaseableItem–Lens” path has weight eight, as expected (one less than nine, as you mentioned in the question).</p> <pre><code>4.0 [http://www.w3.org/2002/07/owl#Thing] 7.5 [http://www.w3.org/2002/07/owl#Thing, http://www.xfront.com/owl/ontologies/camera/#PurchaseableItem] 4.5 [http://www.w3.org/2002/07/owl#Thing, http://www.xfront.com/owl/ontologies/camera/#Window] 4.5 [http://www.w3.org/2002/07/owl#Thing, http://www.xfront.com/owl/ontologies/camera/#Range] 4.5 [http://www.w3.org/2002/07/owl#Thing, http://www.xfront.com/owl/ontologies/camera/#Money] 10.0 [http://www.w3.org/2002/07/owl#Thing, http://www.xfront.com/owl/ontologies/camera/#PurchaseableItem, http://www.xfront.com/owl/ontologies/camera/#Camera] 8.0 [http://www.w3.org/2002/07/owl#Thing, http://www.xfront.com/owl/ontologies/camera/#PurchaseableItem, http://www.xfront.com/owl/ontologies/camera/#Lens] 8.0 [http://www.w3.org/2002/07/owl#Thing, http://www.xfront.com/owl/ontologies/camera/#PurchaseableItem, http://www.xfront.com/owl/ontologies/camera/#Body] 10.5 [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] 10.5 [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] </code></pre> <p>Complete code:</p> <pre class="lang-java prettyprint-override"><code>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&lt;List&lt;Resource&gt;&gt; BFS( final Model model, final Queue&lt;List&lt;Resource&gt;&gt; queue, final int depth ) { final List&lt;List&lt;Resource&gt;&gt; results = new ArrayList&lt;&gt;(); while ( !queue.isEmpty() ) { final List&lt;Resource&gt; path = queue.poll(); results.add( path ); if ( path.size() &lt; depth ) { final Resource last = path.get( path.size() - 1 ); final StmtIterator stmt = model.listStatements( null, RDFS.subClassOf, last ); while ( stmt.hasNext() ) { final List&lt;Resource&gt; extPath = new ArrayList&lt;&gt;( path ); extPath.add( stmt.next().getSubject().asResource() ); queue.offer( extPath ); } } } return results; } public static int classSize( final Resource klass ) { return klass.getModel().listSubjectsWithProperty( RDFS.subClassOf, klass ).toList().size(); } public static double pathSize( final List&lt;Resource&gt; path ) { int prevSize = classSize( path.get( 0 )); double pathSum = prevSize; for ( int i = 1; i &lt; path.size(); i++ ) { int currSize = classSize( path.get( i )); double linkWeight = currSize &lt; prevSize ? 0.5 : 1.0; pathSum += linkWeight + currSize; prevSize = currSize; } return pathSum; } 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 the initial queue final Queue&lt;List&lt;Resource&gt;&gt; queue = new LinkedList&lt;&gt;(); final List&lt;Resource&gt; thingPath = new ArrayList&lt;&gt;(); thingPath.add( OWL.Thing.inModel( model )); queue.offer( thingPath ); // Get the paths, and display them final List&lt;List&lt;Resource&gt;&gt; paths = BFS( model, queue, 4 ); for ( List&lt;Resource&gt; path : paths ) { System.out.println( pathSize( path ) + " " + path ); } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload