我正在获得Student .i的所有成员.i使用以下代码,但我得到的结果只是我的查询是正确的。当我在protege 4.3中运行该查询时,他们给出了正确的结果但是java代码中的问题。我不明白我做错了什么。请在我的java类中提出错误。
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();
}
}
}
我的本体是
<?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>
答案 0 :(得分:2)
由于您在model1
中包含所有结果时查询空model
,因此无法获得结果。只需更改:
OntModel model1=ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM);
...到......
OntModel model1=ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM, model);
...然后您的后续查询将有权访问OntModel
内的三元组。证明可在您创建QueryExecution
实例的行中找到:
QueryExecution exe=QueryExecutionFactory.create(query, model1);
在此行中,您将创建一个引用新模型的查询,该模型没有您从文件中读取的三元组。