Question

i am getting all members of Student .i use the following code but i getting result only ind my query is correct . when i run that query in protege 4.3 they give correct result but problem in java code.i dont understand what i do wrong .please suggest what i wrong in my java class.

class sparql1
    {  public static void main(String[] args) {

            String filename="modified2.owl";
            Model model=ModelFactory.createDefaultModel();
            OntModel model1=ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM);

            try
            {
                File file=new File(filename);
                FileInputStream reader=new FileInputStream(file);
                model.read(reader,null);

                String query1=" PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX my: <http://www.semanticweb.org/rohit/ontologies/2014/4/untitled-ontology-10#> SELECT  ?ind WHERE { ?ind rdf:type my:Student .}";

                com.hp.hpl.jena.query.Query query=QueryFactory.create(query1);
                QueryExecution exe=QueryExecutionFactory.create(query, model1);
                ResultSet RES=exe.execSelect();
                ResultSetFormatter.out(System.out, RES, query);
            }catch(Exception e)
            {
                e.printStackTrace();
            }
        }
    }

my ontology is

<?xml version="1.0"?>


<!DOCTYPE rdf:RDF [
    <!ENTITY owl "http://www.w3.org/2002/07/owl#" >
    <!ENTITY xsd "http://www.w3.org/2001/XMLSchema#" >
    <!ENTITY rdfs "http://www.w3.org/2000/01/rdf-schema#" >
    <!ENTITY rdf "http://www.w3.org/1999/02/22-rdf-syntax-ns#" >
    <!ENTITY untitled-ontology-10 "http://www.semanticweb.org/rohit/ontologies/2014/4/untitled-ontology-10#" >
]>


<rdf:RDF xmlns="http://www.semanticweb.org/rohit/ontologies/2014/4/untitled-ontology-10#"
     xml:base="http://www.semanticweb.org/rohit/ontologies/2014/4/untitled-ontology-10"
     xmlns:untitled-ontology-10="http://www.semanticweb.org/rohit/ontologies/2014/4/untitled-ontology-10#"
     xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
     xmlns:owl="http://www.w3.org/2002/07/owl#"
     xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
     xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
    <owl:Ontology rdf:about="http://www.semanticweb.org/rohit/ontologies/2014/4/untitled-ontology-10"/>
     <!-- http://www.semanticweb.org/rohit/ontologies/2014/4/untitled-ontology-10#hasAddress -->

    <owl:DatatypeProperty rdf:about="&untitled-ontology-10;hasAddress"/>



    <!-- http://www.semanticweb.org/rohit/ontologies/2014/4/untitled-ontology-10#hasEmail -->

    <owl:DatatypeProperty rdf:about="&untitled-ontology-10;hasEmail"/>
   <owl:Class rdf:about="&untitled-ontology-10;Student"/>
    <!-- http://www.semanticweb.org/rohit/ontologies/2014/4/untitled-ontology-10#Student2 -->

  <owl:NamedIndividual rdf:about="&untitled-ontology-10;Student2">
        <rdf:type rdf:resource="&untitled-ontology-10;Student"/>
        <hasEmail rdf:datatype="&xsd;string">asd</hasEmail>
        <hasAddress rdf:datatype="&xsd;string">sdsad</hasAddress>
    </owl:NamedIndividual>



    <!-- http://www.semanticweb.org/rohit/ontologies/2014/4/untitled-ontology-10#student1 -->

    <owl:NamedIndividual rdf:about="&untitled-ontology-10;student1">
        <rdf:type rdf:resource="&untitled-ontology-10;Student"/>
        <hasEmail rdf:datatype="&xsd;string">fhgchg</hasEmail>
        <hasAddress rdf:datatype="&xsd;string">me</hasAddress>
    </owl:NamedIndividual>
    <!-- http://www.semanticweb.org/rohit/ontologies/2014/4/untitled-ontology-10#student3 -->

    <owl:NamedIndividual rdf:about="&untitled-ontology-10;student3">
        <rdf:type rdf:resource="&untitled-ontology-10;Student"/>
        <hasEmail rdf:datatype="&xsd;string">dsfdsf</hasEmail>
        <hasAddress rdf:datatype="&xsd;string">sdfds</hasAddress>
    </owl:NamedIndividual>
</rdf:RDF>
Was it helpful?

Solution

You get no results because you are querying the empty model1 while all of your results are contained in model. Simply change:

OntModel model1=ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM);

... to ...

OntModel model1=ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM, model);

... and then your subsequent query will have access to the triples inside of the OntModel. The proof is found on the line where you create your QueryExecution instance:

QueryExecution exe=QueryExecutionFactory.create(query, model1);

On this line, you create a query that references the new model, which has none of the triples that you read in from your file.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top