Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Is my humble opinion that you don't know RDF altogether, and your are struggling to get things working by asking random questions here on SO when you are not able to figure out what's wrong by yourself. There's nothing wrong with this attitude, however it would be way better if you tried to <strong>carefully read the documentation (OWL, RDF and Jena)</strong>.</p> <p>I suspect that the problem is <strong>not on the Java side</strong>, but instead it's an issue with your ontology (unless you can prove it's not true).</p> <p>I never used RDF and Jena at all, however I spent a couple of hours reading the docs, so even if the following may not be the holy way to do things, it's certainly a way better than yours. Feel free to ask questions (to me or to the SO community) when you can't understand something. First, let's write our ontology in OWL (I'm new to this, so this may contain errors) <strong>Note: You are not supposed to build your ontology in Java code at every program run. Likely you'll load your ontology from some serialized form like the following</strong></p> <pre><code>&lt;?xml version="1.0"?&gt; &lt;!-- See http://www.w3.org/TR/2004/REC-owl-guide-20040210/#Namespaces --&gt; &lt;!DOCTYPE rdf:RDF [ &lt;!ENTITY fam "http://zybnet.com/fam#" &gt; &lt;!ENTITY xsd "http://www.w3.org/2001/XMLSchema#" &gt; ]&gt; &lt;rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#" xmlns:owl="http://www.w3.org/2002/07/owl#" xmlns:xsd="&amp;xsd;" xmlns ="&amp;fam;" xmlns:fam="&amp;fam;" xml:base ="&amp;fam;" &gt; &lt;owl:Class rdf:about="Person" /&gt; &lt;!-- Properties name, gender, and birth --&gt; &lt;owl:DatatypeProperty rdf:about="&amp;fam;name"&gt; &lt;rdfs:domain rdf:resource="Person" /&gt; &lt;rdfs:range rdf:resource="&amp;xsd;string" /&gt; &lt;/owl:DatatypeProperty&gt; &lt;owl:DatatypeProperty rdf:about="&amp;fam;birth"&gt; &lt;rdfs:domain rdf:resource="Person" /&gt; &lt;rdfs:range rdf:resource="&amp;xsd;date" /&gt; &lt;/owl:DatatypeProperty&gt; &lt;owl:DatatypeProperty rdf:about="&amp;fam;gender"&gt; &lt;rdfs:domain rdf:resource="Person" /&gt; &lt;rdfs:range rdf:resource="&amp;xsd;string"/&gt; &lt;/owl:DatatypeProperty&gt; &lt;rdf:Description rdf:about="user/7192"&gt; &lt;fam:name&gt;Bruno&lt;/fam:name&gt; &lt;fam:gender&gt;M&lt;/fam:gender&gt; &lt;fam:birth&gt;19/08/1987&lt;/fam:birth&gt; &lt;/rdf:Description&gt; &lt;rdf:Description rdf:about="user/3023"&gt; &lt;fam:name&gt;Raffaele&lt;/fam:name&gt; &lt;fam:gender&gt;M&lt;/fam:gender&gt; &lt;fam:birth&gt;09/02/1927&lt;/fam:birth&gt; &lt;/rdf:Description&gt; &lt;rdf:Description rdf:about="user/9283"&gt; &lt;fam:name&gt;Angela&lt;/fam:name&gt; &lt;fam:gender&gt;F&lt;/fam:gender&gt; &lt;fam:birth&gt;01/06/1957&lt;/fam:birth&gt; &lt;/rdf:Description&gt; &lt;/rdf:RDF&gt; </code></pre> <p>Then comes the Java code</p> <pre><code>public class RDF { public static void main(String[] args) { Model model = ModelFactory.createOntologyModel(); model.read(RDF.class.getResourceAsStream("family.rdf"), null); System.out.println("All triples in file"); outQuery("SELECT ?subject ?predicate ?object " + "WHERE { ?subject ?predicate ?object }", model); System.out.println("A simple query"); outQuery("SELECT ?subject ?name ?gender " + "WHERE { " + " ?subject &lt;http://zybnet.com/fam#name&gt; ?name ." + " ?subject &lt;http://zybnet.com/fam#gender&gt; ?gender ." + " }", model); } private static void outQuery(String q, Model model) { Query query = QueryFactory.create(q); QueryExecution execution = QueryExecutionFactory.create(query, model); ResultSet results = execution.execSelect(); ResultSetFormatter.out(System.out, results, query); execution.close(); } } </code></pre> <p>And finally a bit of log4j configuration (this must be put in a <code>log4j.properties</code> on the classpath, or loaded from Java code with <a href="http://zybnet.com/java-logging-with-log4j/" rel="nofollow"><code>PropertyConfigurator.configure</code></a>)</p> <pre><code>log4j.logger.com.hp.hpl.jena=debug, console log4j.appender.console=org.apache.log4j.ConsoleAppender log4j.appender.console.layout=org.apache.log4j.PatternLayout log4j.appender.console.layout.ConversionPattern=[%-5p] %m%n </code></pre> <p>This should be your starting code. By running this, you'll see what's going on. By understanding this code, you'll be able to refactor your application and find ouy what to study to get it up and running. Good luck!</p> <p><em>Sample output: (text removed from brevity)</em></p> <pre><code>[Lots of text here...] A simple query [DEBUG] Lock : main ------------------------------------------------------- | uri | name | gender | ======================================================= | &lt;http://zybnet.com/user/9230&gt; | "Angela" | "F" | | &lt;http://zybnet.com/user/0239&gt; | "Raffaele" | "M" | | &lt;http://zybnet.com/user/0001&gt; | "Bruno" | "M" | ------------------------------------------------------- </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