Jena Reasoner 在执行 SPARQL 查询时不遵循推断语句
我最近一直在学习语义网并使用 Jena 进行测试。我有一个包含 2 个本体和 2 组实例数据的模型。一个Ontology实际上是FOAF(Friend-of-a-Friend),另一个也代表人。我尝试添加语句来对齐这两个本体,这些语句似乎有效 - 将它们添加到模型并从模型中打印有关它们的语句后,我可以看到它们已添加。但是,当我执行 SPARQL 查询时,它似乎没有考虑到它们。
为了简单起见,我只在代码中写出要点。
我正在 FOAF 本体上执行以下 SPARQL 查询,其中 swp2:me
是我的实例数据中的一个实例:
select distinct ?name where {
swp2:me foaf:knows ?friend .
?friend foaf:name ?name .
}
上面的查询正确地获取了 swp2:me
的所有友元资源。接下来,我在此模型中有另一个自定义本体(将本体和实例数据放在下面):
@prefix : <http://www.semanticweb.org/luka/ontologies/2022/0/ont#> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix xml: <http://www.w3.org/XML/1998/namespace> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@base <http://www.semanticweb.org/luka/ontologies/2022/0/ont> .
<http://www.semanticweb.org/luka/ontologies/2022/0/ont> rdf:type owl:Ontology ;
owl:versionIRI <http://www.semanticweb.org/luka/ontologies/2022/0/ont/1.0.0> .
:hasFriend rdf:type owl:ObjectProperty ,
owl:SymmetricProperty .
:hasName rdf:type owl:DatatypeProperty .
:Individual rdf:type owl:Class .
:Individual_5 rdf:type :Individual ;
:hasFriend :Individual_6 ,
:Individual_7 ;
:hasName "Sem Web" .
:Individual_6 rdf:type :Individual ;
:hasFriend :Individual_5 ;
:hasName "Web O. Data" .
:Individual_7 rdf:type :Individual ;
:hasFriend :Individual_5 ;
:hasName "Mr. Owl" .
现在,我尝试对齐这两个本体,并表达 swp2:me
(来自第一个本体的实例) 和 :Individual_5
(即来自第二个本体的实例)是相同的实例。我在 Java 中使用以下方法执行此操作:
private void alignTwoOntologies()
{
System.out.println("Aligning People and FOAF Ontologies...");
Resource resource;
Property prop;
Resource obj;
resource = friendsModel.createResource(personNamespace + "Individual");
prop = friendsModel.createProperty("owl:equivalentClass");
obj = friendsModel.createResource("foaf:Person");
friendsModel.add(resource, prop, obj);
resource = friendsModel.createResource(personNamespace + "hasName");
prop = friendsModel.createProperty("owl:equivalentProperty");
obj = friendsModel.createResource("foaf:name");
friendsModel.add(resource, prop, obj);
resource = friendsModel.createResource(personNamespace + "hasFriend");
prop = friendsModel.createProperty("rdfs:subPropertyOf");
obj = friendsModel.createResource("foaf:knows");
friendsModel.add(resource, prop, obj);
resource = friendsModel.createResource(defaultNamespace + "me");
prop = friendsModel.createProperty("owl:sameAs");
obj = friendsModel.createResource(personNamespace + "Individual_5");
friendsModel.add(resource, prop, obj);
}
模型似乎受到了正确的影响 - 当我在 Protege 中打开生成的模型时,我可以看到,例如,在 swp2:me
实例上,它具有 <代码>owl:与Individual_5相同。 Same 代表类和属性对齐。 但是,当我执行我在开始时所说的相同查询时,它不会获取 individual_5 的其他朋友(Individual_6、Individual_7)(与 swp2:me
实例相同)并且我希望它能接住他们。 我在这里错过了什么吗?提前致谢!
编辑:放置完整代码以供参考:
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.apache.jena.ontology.OntModelSpec;
import org.apache.jena.query.Query;
import org.apache.jena.query.QueryExecution;
import org.apache.jena.query.QueryExecutionFactory;
import org.apache.jena.query.QueryFactory;
import org.apache.jena.query.QuerySolution;
import org.apache.jena.query.ResultSet;
import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.ModelFactory;
import org.apache.jena.rdf.model.Property;
import org.apache.jena.rdf.model.RDFNode;
import org.apache.jena.rdf.model.Resource;
import org.apache.jena.vocabulary.OWL;
public class HelloSemanticWeb {
static String defaultNamespace = "http://semwebprogramming.org/2009/ont/chp2#";
static String personNamespace = "http://www.semanticweb.org/luka/ontologies/2022/0/ont#";
Model friendsModel = null;
public static void main(String[] args) throws IOException {
HelloSemanticWeb hello = new HelloSemanticWeb();
// Initialize model
hello.friendsModel = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM_RULE_INF);
// Instance data
hello.populateInstanceData();
// Populate Schemas
hello.populateSchemas();
// Align ontologies
hello.alignTwoOntologies();
// Print friends
hello.myFriends(hello.friendsModel);
}
private void populateInstanceData() throws FileNotFoundException
{
// FOAF Friends
this.friendsModel.read(new FileInputStream("Ontologies/FOAFFriends_turtle.ttl"), null, "TTL");
// People ontology instances
this.friendsModel.read(new FileInputStream("Ontologies/additionalFriends.owl"), null, "TTL");
}
private void populateSchemas() throws FileNotFoundException
{
// FOAF Schema
this.friendsModel.read("http://xmlns.com/foaf/spec/index.rdf");
// People Schema
this.friendsModel.read(new FileInputStream("Ontologies/additionalFriendsSchema.owl"), null, "TTL");
}
private void myFriends(Model model)
{
System.out.println("\n-- Say hello to my friends-- ");
runQuery("select distinct ?name where {"
+ "swp2:me foaf:knows ?friend ."
+ "?friend foaf:name ?name ."
+ "}", model);
}
private void runQuery(String queryRequest, Model model)
{
StringBuffer queryStr = new StringBuffer();
// Prefixes
queryStr.append("PREFIX swp2: <" + defaultNamespace + "> ");
queryStr.append("PREFIX foaf: <http://xmlns.com/foaf/0.1/> ");
// Query
queryStr.append(queryRequest);
Query query = QueryFactory.create(queryStr.toString());
QueryExecution qexec = QueryExecutionFactory.create(query, model);
try
{
ResultSet response = qexec.execSelect();
boolean hasFriends = response.hasNext();
while(response.hasNext())
{
QuerySolution soln = response.nextSolution();
RDFNode name = soln.get("?name");
if(name != null)
System.out.println("Hello to " + name.toString());
}
if(!hasFriends)
System.out.println("No Friends found!");
}
catch(Exception ex)
{
System.out.println("Error executing query: " + ex.getMessage());
}
finally
{
qexec.close();
}
}
private void alignTwoOntologies()
{
System.out.println("Aligning People and FOAF Ontologies...");
Resource resource;
Property prop;
Resource obj;
resource = friendsModel.createResource(personNamespace + "Individual");
prop = OWL.equivalentClass; //friendsModel.createProperty("owl:equivalentClass");
obj = friendsModel.createResource("foaf:Person");
friendsModel.add(resource, prop, obj);
resource = friendsModel.createResource(personNamespace + "hasName");
prop = OWL.equivalentProperty; //friendsModel.createProperty("owl:equivalentProperty");
obj = friendsModel.createResource("foaf:name");
friendsModel.add(resource, prop, obj);
resource = friendsModel.createResource(personNamespace + "hasFriend");
prop = OWL.equivalentProperty; //friendsModel.createProperty("rdfs:subPropertyOf");
obj = friendsModel.createResource("foaf:knows");
friendsModel.add(resource, prop, obj);
resource = friendsModel.createResource(defaultNamespace + "me");
prop = OWL.sameAs; //friendsModel.createProperty("owl:sameAs");
obj = friendsModel.createResource(personNamespace + "Individual_5");
friendsModel.add(resource, prop, obj);
}
}
文件FOAFFriends_turtle.ttl
:
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
@prefix admin: <http://webns.net/mvcb/> .
@prefix owl: <http://w3.org/2002/07/owl#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix swp2: <http://semwebprogramming.org/2009/ont/chp2#> .
swp2:me
rdf:type foaf:Person ;
foaf:depiction <http://semwebprogramming.org/semweb.jpg> ;
foaf:family_name "Web" ;
foaf:givenname "Semantic" ;
foaf:homepage <http://semwebprogramming.org> ;
foaf:knows swp2:Reasoner , swp2:Statement , swp2:Ontology ;
foaf:name "Semantic Web" ;
foaf:nick "Webby" ;
foaf:phone <tel:410-679-8999> ;
foaf:schoolHomepage <http://www.web.edu> ;
foaf:title "Dr" ;
foaf:workInfoHomepage <http://semwebprogramming.com/dataweb.html> ;
foaf:workplaceHomepage <http://semwebprogramming.com> .
swp2:Reasoner
rdf:type foaf:Person ;
rdfs:seeAlso <http://reasoner.com> ;
foaf:mbox <mailto:[email protected]> ;
foaf:name "Ican Reason" .
swp2:Statement
rdf:type foaf:Person ;
rdfs:seeAlso <http://statement.com> ;
foaf:mbox <mailto:[email protected]> ;
foaf:name "Makea Statement" .
swp2:Ontology
rdf:type foaf:Person ;
rdfs:seeAlso <http://ont.com> ;
foaf:mbox <mailto:[email protected]> ;
foaf:name "I. M. Ontology" .
文件additionalFriends.owl
:
@prefix : <http://www.semanticweb.org/luka/ontologies/2022/0/ont#> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix xml: <http://www.w3.org/XML/1998/namespace> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@base <http://www.semanticweb.org/luka/ontologies/2022/0/ont> .
:Individual_5 rdf:type :Individual ;
:hasFriend :Individual_6 ,
:Individual_7 ;
:hasName "Sem Web" .
:Individual_6 rdf:type :Individual ;
:hasFriend :Individual_5 ;
:hasName "Web O. Data" .
:Individual_7 rdf:type :Individual ;
:hasFriend :Individual_5 ;
:hasName "Mr. Owl" .
文件additionalFriendsSchema.owl
>:
@prefix : <http://www.semanticweb.org/luka/ontologies/2022/0/ont#> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix xml: <http://www.w3.org/XML/1998/namespace> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@base <http://www.semanticweb.org/luka/ontologies/2022/0/ont> .
<http://www.semanticweb.org/luka/ontologies/2022/0/ont> rdf:type owl:Ontology ;
owl:versionIRI <http://www.semanticweb.org/luka/ontologies/2022/0/ont/1.0.0> .
:hasFriend rdf:type owl:ObjectProperty ,
owl:SymmetricProperty .
:hasName rdf:type owl:DatatypeProperty .
:Individual rdf:type owl:Class .
I have been learning about Semantic Web recently and testing stuff with Jena. I have a model with 2 Ontologies and 2 sets of Instance data. One Ontology is actually FOAF (Friend-of-a-Friend), and the other also represents people. I tried adding statements to align these two Ontologies and the statements seem to have effect - after adding them to the model and printing statements about them from the model, I can see that they are added. However, when I perform SPARQL query, it does not seem to take them into account.
For the sake of simplicity, I will just write the main points in the code.
I am performing the following SPARQL query on a FOAF ontology, where swp2:me
is an instance in my instance data:
select distinct ?name where {
swp2:me foaf:knows ?friend .
?friend foaf:name ?name .
}
The above query properly grabs all the friends of swp2:me
resource. Next, I have another custom Ontology in this model (putting both Ontology and instance data below):
@prefix : <http://www.semanticweb.org/luka/ontologies/2022/0/ont#> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix xml: <http://www.w3.org/XML/1998/namespace> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@base <http://www.semanticweb.org/luka/ontologies/2022/0/ont> .
<http://www.semanticweb.org/luka/ontologies/2022/0/ont> rdf:type owl:Ontology ;
owl:versionIRI <http://www.semanticweb.org/luka/ontologies/2022/0/ont/1.0.0> .
:hasFriend rdf:type owl:ObjectProperty ,
owl:SymmetricProperty .
:hasName rdf:type owl:DatatypeProperty .
:Individual rdf:type owl:Class .
:Individual_5 rdf:type :Individual ;
:hasFriend :Individual_6 ,
:Individual_7 ;
:hasName "Sem Web" .
:Individual_6 rdf:type :Individual ;
:hasFriend :Individual_5 ;
:hasName "Web O. Data" .
:Individual_7 rdf:type :Individual ;
:hasFriend :Individual_5 ;
:hasName "Mr. Owl" .
Now, I try to align these two Ontologies, as well as express that swp2:me
(instance from first Ontology) and :Individual_5
(that is, instance from second Ontology) are the same instance. I do this using the following method in Java:
private void alignTwoOntologies()
{
System.out.println("Aligning People and FOAF Ontologies...");
Resource resource;
Property prop;
Resource obj;
resource = friendsModel.createResource(personNamespace + "Individual");
prop = friendsModel.createProperty("owl:equivalentClass");
obj = friendsModel.createResource("foaf:Person");
friendsModel.add(resource, prop, obj);
resource = friendsModel.createResource(personNamespace + "hasName");
prop = friendsModel.createProperty("owl:equivalentProperty");
obj = friendsModel.createResource("foaf:name");
friendsModel.add(resource, prop, obj);
resource = friendsModel.createResource(personNamespace + "hasFriend");
prop = friendsModel.createProperty("rdfs:subPropertyOf");
obj = friendsModel.createResource("foaf:knows");
friendsModel.add(resource, prop, obj);
resource = friendsModel.createResource(defaultNamespace + "me");
prop = friendsModel.createProperty("owl:sameAs");
obj = friendsModel.createResource(personNamespace + "Individual_5");
friendsModel.add(resource, prop, obj);
}
The model seems to be affected properly - when I open the resulting model in Protege, I can see, for example, on swp2:me
instance that it has owl:sameAs Individual_5
. Same stands for Classes and Properties that are aligned.
However, when I execute the same query that I stated at the start, it does not pick up additional friends (Individual_6, Individual_7) of Individual_5 (which is same as swp2:me
instance) and I expect it to pick them up.
Am I missing something here? Thanks in advance!
EDIT: Placing the full code for reference:
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.apache.jena.ontology.OntModelSpec;
import org.apache.jena.query.Query;
import org.apache.jena.query.QueryExecution;
import org.apache.jena.query.QueryExecutionFactory;
import org.apache.jena.query.QueryFactory;
import org.apache.jena.query.QuerySolution;
import org.apache.jena.query.ResultSet;
import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.ModelFactory;
import org.apache.jena.rdf.model.Property;
import org.apache.jena.rdf.model.RDFNode;
import org.apache.jena.rdf.model.Resource;
import org.apache.jena.vocabulary.OWL;
public class HelloSemanticWeb {
static String defaultNamespace = "http://semwebprogramming.org/2009/ont/chp2#";
static String personNamespace = "http://www.semanticweb.org/luka/ontologies/2022/0/ont#";
Model friendsModel = null;
public static void main(String[] args) throws IOException {
HelloSemanticWeb hello = new HelloSemanticWeb();
// Initialize model
hello.friendsModel = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM_RULE_INF);
// Instance data
hello.populateInstanceData();
// Populate Schemas
hello.populateSchemas();
// Align ontologies
hello.alignTwoOntologies();
// Print friends
hello.myFriends(hello.friendsModel);
}
private void populateInstanceData() throws FileNotFoundException
{
// FOAF Friends
this.friendsModel.read(new FileInputStream("Ontologies/FOAFFriends_turtle.ttl"), null, "TTL");
// People ontology instances
this.friendsModel.read(new FileInputStream("Ontologies/additionalFriends.owl"), null, "TTL");
}
private void populateSchemas() throws FileNotFoundException
{
// FOAF Schema
this.friendsModel.read("http://xmlns.com/foaf/spec/index.rdf");
// People Schema
this.friendsModel.read(new FileInputStream("Ontologies/additionalFriendsSchema.owl"), null, "TTL");
}
private void myFriends(Model model)
{
System.out.println("\n-- Say hello to my friends-- ");
runQuery("select distinct ?name where {"
+ "swp2:me foaf:knows ?friend ."
+ "?friend foaf:name ?name ."
+ "}", model);
}
private void runQuery(String queryRequest, Model model)
{
StringBuffer queryStr = new StringBuffer();
// Prefixes
queryStr.append("PREFIX swp2: <" + defaultNamespace + "> ");
queryStr.append("PREFIX foaf: <http://xmlns.com/foaf/0.1/> ");
// Query
queryStr.append(queryRequest);
Query query = QueryFactory.create(queryStr.toString());
QueryExecution qexec = QueryExecutionFactory.create(query, model);
try
{
ResultSet response = qexec.execSelect();
boolean hasFriends = response.hasNext();
while(response.hasNext())
{
QuerySolution soln = response.nextSolution();
RDFNode name = soln.get("?name");
if(name != null)
System.out.println("Hello to " + name.toString());
}
if(!hasFriends)
System.out.println("No Friends found!");
}
catch(Exception ex)
{
System.out.println("Error executing query: " + ex.getMessage());
}
finally
{
qexec.close();
}
}
private void alignTwoOntologies()
{
System.out.println("Aligning People and FOAF Ontologies...");
Resource resource;
Property prop;
Resource obj;
resource = friendsModel.createResource(personNamespace + "Individual");
prop = OWL.equivalentClass; //friendsModel.createProperty("owl:equivalentClass");
obj = friendsModel.createResource("foaf:Person");
friendsModel.add(resource, prop, obj);
resource = friendsModel.createResource(personNamespace + "hasName");
prop = OWL.equivalentProperty; //friendsModel.createProperty("owl:equivalentProperty");
obj = friendsModel.createResource("foaf:name");
friendsModel.add(resource, prop, obj);
resource = friendsModel.createResource(personNamespace + "hasFriend");
prop = OWL.equivalentProperty; //friendsModel.createProperty("rdfs:subPropertyOf");
obj = friendsModel.createResource("foaf:knows");
friendsModel.add(resource, prop, obj);
resource = friendsModel.createResource(defaultNamespace + "me");
prop = OWL.sameAs; //friendsModel.createProperty("owl:sameAs");
obj = friendsModel.createResource(personNamespace + "Individual_5");
friendsModel.add(resource, prop, obj);
}
}
File FOAFFriends_turtle.ttl
:
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
@prefix admin: <http://webns.net/mvcb/> .
@prefix owl: <http://w3.org/2002/07/owl#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix swp2: <http://semwebprogramming.org/2009/ont/chp2#> .
swp2:me
rdf:type foaf:Person ;
foaf:depiction <http://semwebprogramming.org/semweb.jpg> ;
foaf:family_name "Web" ;
foaf:givenname "Semantic" ;
foaf:homepage <http://semwebprogramming.org> ;
foaf:knows swp2:Reasoner , swp2:Statement , swp2:Ontology ;
foaf:name "Semantic Web" ;
foaf:nick "Webby" ;
foaf:phone <tel:410-679-8999> ;
foaf:schoolHomepage <http://www.web.edu> ;
foaf:title "Dr" ;
foaf:workInfoHomepage <http://semwebprogramming.com/dataweb.html> ;
foaf:workplaceHomepage <http://semwebprogramming.com> .
swp2:Reasoner
rdf:type foaf:Person ;
rdfs:seeAlso <http://reasoner.com> ;
foaf:mbox <mailto:[email protected]> ;
foaf:name "Ican Reason" .
swp2:Statement
rdf:type foaf:Person ;
rdfs:seeAlso <http://statement.com> ;
foaf:mbox <mailto:[email protected]> ;
foaf:name "Makea Statement" .
swp2:Ontology
rdf:type foaf:Person ;
rdfs:seeAlso <http://ont.com> ;
foaf:mbox <mailto:[email protected]> ;
foaf:name "I. M. Ontology" .
File additionalFriends.owl
:
@prefix : <http://www.semanticweb.org/luka/ontologies/2022/0/ont#> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix xml: <http://www.w3.org/XML/1998/namespace> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@base <http://www.semanticweb.org/luka/ontologies/2022/0/ont> .
:Individual_5 rdf:type :Individual ;
:hasFriend :Individual_6 ,
:Individual_7 ;
:hasName "Sem Web" .
:Individual_6 rdf:type :Individual ;
:hasFriend :Individual_5 ;
:hasName "Web O. Data" .
:Individual_7 rdf:type :Individual ;
:hasFriend :Individual_5 ;
:hasName "Mr. Owl" .
File additionalFriendsSchema.owl
:
@prefix : <http://www.semanticweb.org/luka/ontologies/2022/0/ont#> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix xml: <http://www.w3.org/XML/1998/namespace> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@base <http://www.semanticweb.org/luka/ontologies/2022/0/ont> .
<http://www.semanticweb.org/luka/ontologies/2022/0/ont> rdf:type owl:Ontology ;
owl:versionIRI <http://www.semanticweb.org/luka/ontologies/2022/0/ont/1.0.0> .
:hasFriend rdf:type owl:ObjectProperty ,
owl:SymmetricProperty .
:hasName rdf:type owl:DatatypeProperty .
:Individual rdf:type owl:Class .
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论