如果您可以提供我们可以用于测试的整个本体,通常会更有帮助。这个不是太大,所以重建起来并不难。无论如何,我已经复制了它,它包含在这个答案的末尾。
推理在 OWL 中有效
您正在寻找的推理在 OWL 中是有效的,我们可以通过使用逻辑上完整的 OWL 推理器(例如 Pellet)来看到这一点。我们将在 Protégé 中看到这一点(但您也可以将 Pellet 与 Jena 一起使用。)这是在 Protégé 中重新创建的本体的样子:
然后,当我们启用 Pellet 推理器并请求ApolloMission的实例时,我们得到Mission11,正如预期的那样:
既然您说您要询问Mission11的类型,也许您使用了类似询问超类{Mission11}的查询。这也会产生预期的类:
复制本体
@prefix : <http://stackoverflow.com/q/21223545/1281433/space.owl#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix owl: <http://www.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 space: <http://stackoverflow.com/q/21223545/1281433/space.owl#> .
<http://stackoverflow.com/q/21223545/1281433/space.owl>
a owl:Ontology .
space:Mission11 a owl:NamedIndividual , space:SpaceMission ;
space:shuttleUsed space:Apollo11 .
space:shuttleUsed a owl:ObjectProperty ;
rdfs:domain space:SpaceMission .
space:Apollo13 a owl:Thing , owl:NamedIndividual .
space:ApolloMission a owl:Class ;
owl:equivalentClass [ a owl:Class ;
owl:intersectionOf ( space:SpaceMission [ a owl:Restriction ;
owl:onProperty space:shuttleUsed ;
owl:someValuesFrom [ a owl:Class ;
owl:oneOf ( space:Apollo13 space:Apollo11 )
]
] )
] .
space:SpaceMission a owl:Class .
space:Apollo11 a owl:Thing , owl:NamedIndividual .
为什么耶拿的推理器没有得到一些结果
Jena 的推理并不完整。这并不意味着它们还没有完成。Complete 是形式推理中的一个技术术语,描述推理器(或算法等),这意味着根据推理器不会产生的语言语义存在正确的推理。Jena 的推理器不完整的原因与实施策略(使用基于规则的推理器)和效率考虑有关(我们可以接受速度和我们可以获得的推理之间的权衡)。
有关 Jena 推理器的更多信息,您应该查看Reasoners 和规则引擎:文档中的 Jena 推理支持。正如它所说,它并不完全是最新的,例如:
Jena OWL 推理器不支持超出 OWL/lite 的关键结构是补全和 oneOf。如上所述,对 unionOf 的支持是部分的(由于基于规则的方法的限制),但对于遍历类层次结构很有用。
但正如下面的代码所示,事实上,owl:oneOf
在一些推理器中支持,所以一些推理器可以做出你想要的 ApolloMission 推理。
Jena 提供了许多推理器,获取连接到它们的 OntModel 的最简单方法是使用在 OntModelSpec 中声明的静态 OntModelSpecs。以下 Java 代码显示,使用您提供的最终本体,不同的推理器提供不同的结果。(用于获取不同 OntModelSpecs 的反射代码有点骇人听闻,但举个简单的例子,这很好。)该代码为每个以“OWL_”开头的声明规范构造一个 OntModel,并针对它们运行查询。
import java.lang.reflect.Field;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.ontology.OntModelSpec;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.ResultSetFormatter;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
public class JenaReasonersSpaceExample {
private final static String QUERY =
"PREFIX : <http://stackoverflow.com/q/21223545/1281433/space.owl#>\n" +
"select ?type where {\n" +
" :Mission11 a ?type .\n" +
"}\n";
private final static Model base =
ModelFactory.createDefaultModel()
.read( "file:///home/taylorj/tmp/ontologies/space/space.owl" );
public static void main(final String[] args) throws IllegalArgumentException, IllegalAccessException {
// Iterate through the fields of OntModelSpec and for each one whose name
// begins with "OWL_", assume that it's a static field (so that getField
// can accept null), and that its value is an OntModelSpec (so that we
// can cast and create an OntModel with it and the base model).
// that begin with "OWL_", and assume t
for ( final Field field : OntModelSpec.class.getFields() ) {
if ( field.getName().startsWith("OWL_") ) {
final OntModelSpec spec = (OntModelSpec) field.get(null);
final OntModel model = ModelFactory.createOntologyModel( spec, base );
// Run the query against the model (that will use the specified reasoner)
// and show the field that we used and the results that we get.
System.out.println( "\n=== "+field.getName()+" ===" );
ResultSetFormatter.out(QueryExecutionFactory.create(QUERY, model).execSelect());
}
}
}
}
输出如下。一些推理者可以推断 Mission11 是 ApolloMission。这些是规范使用的:OWL_MEM_RULE_INF
、OWL_MEM_MINI_RULE_INF
、OWL_DL_MEM_RULE_INF
和OWL_LITE_MEM_RULES_INF
。看起来您可能想要坚持RULE
名称中包含的推理器。
=== OWL_MEM ===
------------------------------------------------------------------------
| type |
========================================================================
| <http://stackoverflow.com/q/21223545/1281433/space.owl#SpaceMission> |
| <http://www.w3.org/2002/07/owl#NamedIndividual> |
------------------------------------------------------------------------
=== OWL_MEM_RDFS_INF ===
------------------------------------------------------------------------
| type |
========================================================================
| <http://stackoverflow.com/q/21223545/1281433/space.owl#SpaceMission> |
| <http://www.w3.org/2002/07/owl#NamedIndividual> |
| <http://www.w3.org/2000/01/rdf-schema#Resource> |
------------------------------------------------------------------------
=== OWL_MEM_TRANS_INF ===
------------------------------------------------------------------------
| type |
========================================================================
| <http://stackoverflow.com/q/21223545/1281433/space.owl#SpaceMission> |
| <http://www.w3.org/2002/07/owl#NamedIndividual> |
------------------------------------------------------------------------
=== OWL_MEM_RULE_INF ===
-------------------------------------------------------------------------
| type |
=========================================================================
| <http://stackoverflow.com/q/21223545/1281433/space.owl#SpaceMission> |
| <http://www.w3.org/2002/07/owl#NamedIndividual> |
| <http://www.w3.org/2002/07/owl#Thing> |
| _:b0 |
| <http://www.w3.org/2000/01/rdf-schema#Resource> |
| _:b1 |
| <http://stackoverflow.com/q/21223545/1281433/space.owl#ApolloMission> |
-------------------------------------------------------------------------
=== OWL_MEM_MICRO_RULE_INF ===
------------------------------------------------------------------------
| type |
========================================================================
| <http://stackoverflow.com/q/21223545/1281433/space.owl#SpaceMission> |
| <http://www.w3.org/2002/07/owl#NamedIndividual> |
| <http://www.w3.org/2002/07/owl#Thing> |
| <http://www.w3.org/2000/01/rdf-schema#Resource> |
------------------------------------------------------------------------
=== OWL_MEM_MINI_RULE_INF ===
-------------------------------------------------------------------------
| type |
=========================================================================
| <http://stackoverflow.com/q/21223545/1281433/space.owl#SpaceMission> |
| <http://www.w3.org/2002/07/owl#NamedIndividual> |
| <http://www.w3.org/2002/07/owl#Thing> |
| _:b0 |
| <http://www.w3.org/2000/01/rdf-schema#Resource> |
| _:b1 |
| <http://stackoverflow.com/q/21223545/1281433/space.owl#ApolloMission> |
-------------------------------------------------------------------------
=== OWL_DL_MEM ===
------------------------------------------------------------------------
| type |
========================================================================
| <http://stackoverflow.com/q/21223545/1281433/space.owl#SpaceMission> |
| <http://www.w3.org/2002/07/owl#NamedIndividual> |
------------------------------------------------------------------------
=== OWL_DL_MEM_RDFS_INF ===
------------------------------------------------------------------------
| type |
========================================================================
| <http://stackoverflow.com/q/21223545/1281433/space.owl#SpaceMission> |
| <http://www.w3.org/2002/07/owl#NamedIndividual> |
| <http://www.w3.org/2000/01/rdf-schema#Resource> |
------------------------------------------------------------------------
=== OWL_DL_MEM_TRANS_INF ===
------------------------------------------------------------------------
| type |
========================================================================
| <http://stackoverflow.com/q/21223545/1281433/space.owl#SpaceMission> |
| <http://www.w3.org/2002/07/owl#NamedIndividual> |
------------------------------------------------------------------------
=== OWL_DL_MEM_RULE_INF ===
-------------------------------------------------------------------------
| type |
=========================================================================
| <http://stackoverflow.com/q/21223545/1281433/space.owl#SpaceMission> |
| <http://www.w3.org/2002/07/owl#NamedIndividual> |
| <http://www.w3.org/2002/07/owl#Thing> |
| _:b0 |
| <http://www.w3.org/2000/01/rdf-schema#Resource> |
| <http://stackoverflow.com/q/21223545/1281433/space.owl#ApolloMission> |
| _:b1 |
-------------------------------------------------------------------------
=== OWL_LITE_MEM ===
------------------------------------------------------------------------
| type |
========================================================================
| <http://stackoverflow.com/q/21223545/1281433/space.owl#SpaceMission> |
| <http://www.w3.org/2002/07/owl#NamedIndividual> |
------------------------------------------------------------------------
=== OWL_LITE_MEM_TRANS_INF ===
------------------------------------------------------------------------
| type |
========================================================================
| <http://stackoverflow.com/q/21223545/1281433/space.owl#SpaceMission> |
| <http://www.w3.org/2002/07/owl#NamedIndividual> |
------------------------------------------------------------------------
=== OWL_LITE_MEM_RDFS_INF ===
------------------------------------------------------------------------
| type |
========================================================================
| <http://stackoverflow.com/q/21223545/1281433/space.owl#SpaceMission> |
| <http://www.w3.org/2002/07/owl#NamedIndividual> |
| <http://www.w3.org/2000/01/rdf-schema#Resource> |
------------------------------------------------------------------------
=== OWL_LITE_MEM_RULES_INF ===
-------------------------------------------------------------------------
| type |
=========================================================================
| <http://stackoverflow.com/q/21223545/1281433/space.owl#SpaceMission> |
| <http://www.w3.org/2002/07/owl#NamedIndividual> |
| <http://www.w3.org/2002/07/owl#Thing> |
| _:b0 |
| <http://www.w3.org/2000/01/rdf-schema#Resource> |
| <http://stackoverflow.com/q/21223545/1281433/space.owl#ApolloMission> |
| _:b1 |
-------------------------------------------------------------------------