You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@jena.apache.org by Gi-Won Chung <gi...@gmail.com> on 2010/12/27 17:38:18 UTC

Beginner problem with simple example

Dear Jena developers!
I have a somewhat simple example, but I cant see why it doesnt work.
The code is running, but I get no results.
It would be nice if you could help me out, thank you!!!




	OntModel model = ModelFactory.createOntologyModel();
		try {
			InputStream in;
			in = new FileInputStream("lib.owl");
			String lib = "http://www.semanticweb.org/ontologies/2010/10/library.owl#";
			model.read(in, null);
			String prolog = "PREFIX lib: <" + lib + ">";
			String queryString = prolog + NL
					+ "SELECT ?y WHERE {?x lib:speaks ?y}";

			Query query = QueryFactory.create(queryString);
			QueryExecution qexec = QueryExecutionFactory.create(query, model);
			ResultSet rs = qexec.execSelect();
			
			while (rs.hasNext()) {
				QuerySolution rb1 = rs.nextSolution();

				// Get title - variable names do not include the '?' (or '$')
				RDFNode x1 = rb1.get("y");
				print("x: " + x1);
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}



<?xml version="1.0"?>


<!DOCTYPE rdf:RDF [
    <!ENTITY owl "http://www.w3.org/2002/07/owl#" >
    <!ENTITY dc "http://purl.org/dc/elements/1.1/" >
    <!ENTITY xsd "http://www.w3.org/2001/XMLSchema#" >
    <!ENTITY owl2xml "http://www.w3.org/2006/12/owl2-xml#" >
    <!ENTITY rdfs "http://www.w3.org/2000/01/rdf-schema#" >
    <!ENTITY rdf "http://www.w3.org/1999/02/22-rdf-syntax-ns#" >
    <!ENTITY library
"http://www.semanticweb.org/ontologies/2010/10/library.owl#" >
]>


<rdf:RDF xmlns="http://www.semanticweb.org/ontologies/2010/10/library.owl#"
     xml:base="http://www.semanticweb.org/ontologies/2010/10/library.owl"
     xmlns:owl2xml="http://www.w3.org/2006/12/owl2-xml#"
     xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
     xmlns:dc="http://purl.org/dc/elements/1.1/"
     xmlns:library="http://www.semanticweb.org/ontologies/2010/10/library.owl#"
     xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
     xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
     xmlns:owl="http://www.w3.org/2002/07/owl#">

    <owl:ObjectProperty rdf:about="#speaks">
        <rdfs:domain rdf:resource="#Author"/>
        <rdfs:range rdf:resource="#Language"/>
    </owl:ObjectProperty>

    <owl:Class rdf:about="#Author">
        <rdfs:subClassOf rdf:resource="#DomainConcept"/>
    </owl:Class>


    <owl:Class rdf:about="#DomainConcept"/>

    <owl:Class rdf:about="#English">
        <rdfs:subClassOf rdf:resource="#Language"/>
    </owl:Class>

    <owl:Class rdf:about="#German">
        <rdfs:subClassOf rdf:resource="#Language"/>
    </owl:Class>

    <owl:Class rdf:about="#Language">
        <rdfs:subClassOf rdf:resource="#DomainConcept"/>
    </owl:Class>

    <owl:Class rdf:about="#S.Meyer">
        <rdfs:subClassOf>
            <owl:Class>
                <owl:intersectionOf rdf:parseType="Collection">
                    <rdf:Description rdf:about="#Author"/>
                    <owl:Restriction>
                        <owl:onProperty rdf:resource="#speaks"/>
                        <owl:someValuesFrom>
                            <owl:Class>
                                <owl:intersectionOf rdf:parseType="Collection">
                                    <rdf:Description rdf:about="#English"/>
                                    <rdf:Description rdf:about="#German"/>
                                </owl:intersectionOf>
                            </owl:Class>
                        </owl:someValuesFrom>
                    </owl:Restriction>
                </owl:intersectionOf>
            </owl:Class>
        </rdfs:subClassOf>
        <rdfs:comment
            >quantifier restriction defined</rdfs:comment>
    </owl:Class>


    <owl:Class rdf:about="#T.Canavan">
        <rdfs:subClassOf>
            <owl:Class>
                <owl:intersectionOf rdf:parseType="Collection">
                    <rdf:Description rdf:about="#Author"/>
                    <owl:Restriction>
                        <owl:onProperty rdf:resource="#speaks"/>
                        <owl:someValuesFrom rdf:resource="#English"/>
                    </owl:Restriction>
                </owl:intersectionOf>
            </owl:Class>
        </rdfs:subClassOf>
        <rdfs:comment
            >quantifier restriction defined</rdfs:comment>
    </owl:Class>
 </rdf:RDF>

Re: Beginner problem with simple example

Posted by Ian Dickinson <ia...@epimorphics.com>.
Hi Gi-Won Chung,

On 27/12/10 16:38, Gi-Won Chung wrote:
> Dear Jena developers!
> I have a somewhat simple example, but I cant see why it doesnt work.
> The code is running, but I get no results.
> It would be nice if you could help me out, thank you!!!
There are two basic problems with your example.

First, your query asks for objects of the triple pattern

?x lib:speaks ?y

but there are no such triples in your ontology. I think what you are 
tryng to do is find the set of languages spoken by (at a guess) authors 
of novels. However, you haven't asserted any information about authors. 
What you have defined is a class of authors:

<owl:Class rdf:about="#S.Meyer">
  <rdfs:subClassOf>
   <owl:Class>
    <owl:intersectionOf rdf:parseType="Collection">
     <rdf:Description rdf:about="#Author"/>
     <owl:Restriction>
      <owl:onProperty rdf:resource="#speaks"/>
      <owl:someValuesFrom>
        <owl:Class>
         <owl:intersectionOf rdf:parseType="Collection">
          <rdf:Description rdf:about="#English"/>
          <rdf:Description rdf:about="#German"/>
         </owl:intersectionOf>
        </owl:Class>
       </owl:someValuesFrom>
      </owl:Restriction>
     </owl:intersectionOf>
    </owl:Class>
   </rdfs:subClassOf>
</owl:Class>

This defines #S.Meyer as the class - i.e. set of possible instances - of 
authors who speak both English and German. The fact that you have named 
this class using a name suggesting a single author hints to me that 
you've not quite got the modelling right yet.  Irrespective of that, 
just defining the class does not mean that you have any instances yet: 
the set of German and English speaking authors could be empty.

If all you want to do is assert that S.Meyer is an author who speaks 
both English and German, you want something rather simpler:

<lib:Author rdf:ID="S.Meyer">
   <lib:speaks rdf:about="#English"/>
   <lib:speaks rdf:about="#German"/>
</lib:Author>

This would then provide two solutions to your query.

The second potential problem that you may run into in future, is that in 
Jena you need to enable a reasoner (aka inference engine) if you want to 
be able get results from complex expressions like intersection class 
descriptions. You would need to change this line:

OntModel model = ModelFactory.createOntologyModel();

to, for example:

OntModel model = ModelFactory.createOntologyModel( 
OntModelSpec.OWL_MEM_MICRO_RULE_INF);

which enables one of the OWL reasoners (and MICRO RULE INF is typically 
the best one to start with)

Ian

-- 
____________________________________________________________
Ian Dickinson                   Epimorphics Ltd, Bristol, UK
mailto:ian@epimorphics.com        http://www.epimorphics.com
cell: +44-7786-850536              landline: +44-1275-399069
------------------------------------------------------------
Epimorphics Ltd.  is a limited company registered in England
(no. 7016688). Registered address: Court Lodge, 105 High St,
               Portishead, Bristol BS20 6PT, UK