SPARQL 查询在 jena 中的 运行 时未在导入的 ontology 中找到个人

SPARQL query does not find individual in imported ontology when run in jena

我有 3 个 ontology 文件,其中第一个导入第二个,第二个导入第三个:

第一个ontology导入第二个:

<?xml version="1.0"?>
<rdf:RDF xmlns="http://www.example.com/user/rainer/ontologies/2016/1/usecase_individuals#"
     xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
     xmlns:owl="http://www.w3.org/2002/07/owl#"
     xmlns:xml="http://www.w3.org/XML/1998/namespace"
     xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
     xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
     xmlns:uc="http://www.example.com/user/rainer/ontologies/2016/1/usecase#">
    <owl:Ontology rdf:about="http://www.example.com/user/rainer/ontologies/2016/1/usecase_individuals">
        <owl:imports rdf:resource="http://www.example.com/user/rainer/ontologies/2016/1/usecase"/>
    </owl:Ontology>
....

第二个ontology导入第三个:

<?xml version="1.0"?>
<rdf:RDF xmlns="http://www.example.com/user/rainer/ontologies/2016/1/usecase#"
     xml:base="http://www.example.com/user/rainer/ontologies/2016/1/usecase"
     xmlns:fgcm="http://www.example.com/user/rainer/ontologies/2016/1/fgcm#"
     xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
     xmlns:owl="http://www.w3.org/2002/07/owl#"
     xmlns:xml="http://www.w3.org/XML/1998/namespace"
     xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
     xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
     xmlns:uc="http://www.example.com/user/rainer/ontologies/2016/1/usecase#">
    <owl:Ontology rdf:about="http://www.example.com/user/rainer/ontologies/2016/1/usecase">
        <owl:imports rdf:resource="http://www.boeing.com/user/rainer/ontologies/2016/1/fgcm"/>
    </owl:Ontology>

....

第三个 ontology(在 Protégé 中创建)断言个人:

<?xml version="1.0"?>
<rdf:RDF xmlns="http://www.boeing.com/user/rainer/ontologies/2016/1/fgcm#"

...

    <owl:NamedIndividual rdf:about="http://www.boeing.com/user/rainer/ontologies/2016/1/fgcm#admin">
        <rdf:type rdf:resource="http://www.boeing.com/user/rainer/ontologies/2016/1/fgcm#User"/>
        <userName>admin</userName>
    </owl:NamedIndividual>
...

当我在 Protégé 中打开第一个 ontology 并执行 SPARQL 查询时

PREFIX fgcm: <http://www.example.com/user/rainer/ontologies/2016/1/fgcm#>
SELECT ?subject ?name WHERE { ?subject fgcm:userName ?name}

它找到第三个 ontology 中的个人没有问题。当我 运行 来自耶拿代码的相同 SPARQL 查询时,我没有得到那个人。查询是 运行 针对使用默认设置创建的 OntModel。

我知道 Jena 能够加载和导入本体,因为我可以在 SPARQL 查询中和直接使用 Jena API 访问 类 和导入本体中的属性。我的问题似乎仅限于在导入 ontology.

中声明的个人

我搜索了可能会改变此行为但未找到任何解决方案的设置(在加载 ontology 时,例如不同的 OntModelSpecs 或在创建/ 运行 查询时)。

原来是我误会了Jena成功加载导入的本体。 (没有报错并不代表应该导入的ontology确实找到了)。

SPARQL 查询在使用 OntDocumentManager 并告诉它在哪里可以找到需要导入的 ontology 文件后返回了预期的结果。这是对我有用的代码片段:

OntDocumentManager mgr = new OntDocumentManager ();
mgr.addAltEntry("http://www.boeing.com/user/rainer/ontologies/2016/1/usecase", "file:C:\Dev\luna_workspace\fgcm_translate\usecase.owl");
mgr.addAltEntry("http://www.boeing.com/user/rainer/ontologies/2016/1/fgcm", "file:C:\Dev\luna_workspace\fgcm_translate\fgcm.owl");
OntModelSpec spec = new OntModelSpec ( OntModelSpec .OWL_DL_MEM_TRANS_INF);
spec.setDocumentManager(mgr);

OntModel model = ModelFactory.createOntologyModel(spec);

我希望这对 运行 遇到类似问题的人有所帮助。