如何在JENA中添加合格的基数 [英] How to add qualified cardinality in JENA
问题描述
如何在Jena中添加合格的基数限制?我不能使用 createCardinalityQRestriction
,因为 OntModelSpec
是针对OWL的第一个版本,而不是OWL2。在ModelFactory的createOntologyModel中,有没有办法创建OWL2本体?我需要一个类表达式,如
How can I add qualified cardinality restriction in Jena? I can not use createCardinalityQRestriction
because the OntModelSpec
is for the first version of OWL, not OWL2. In ModelFactory's createOntologyModel, is there a way to create an OWL2 ontology? I need a class expression like
JeVysledkom 完全 1 Kolik_Fazovy
JeVysledkom exactly 1 Kolik_Fazovy
我尝试过使用此代码:
OntModel ontModel = ModelFactory.createOntologyModel();
OntClass ret = ontModel.createCardinalityQRestriction(null, ontProperty, cardinality, ontClass2 );
ontClass.addSuperClass(ret);
但是我得到了这个例外:
but I get this exception:
com.hp.hpl.jena.ontology.ProfileException:尝试使用当前语言配置文件不支持的语言构造CARDINALITY_Q:OWL Full
com.hp.hpl.jena.ontology.ProfileException: Attempted to use language construct CARDINALITY_Q that is not supported in the current language profile: OWL Full
推荐答案
我在处理另一个问题时遇到了这个问题,添加更复杂的子类公理。在Jena中创建这个有点棘手,因为支持合格的基数限制是一个OWL2功能,而Jena对OWL2的支持有限:
I actually just ran into this while handling another question, Adding more complicated subclass axiom. Creating this in Jena is a little bit tricky because support for the qualified cardinality restrictions is an OWL2 feature, and Jena has limited support for OWL2:
Jena Ontology API
请注意,目前,对于OWL2的合格基数限制(即cardinalityQ,
minCardinalityQ和maxCardinalityQ),Jena本体API仅支持
。合格的基数
限制封装在接口
CardinalityQRestriction,MinCardinalityQRestriction和
CardinalityQRestriction中。 OntModel还提供了创建
和访问限定基数限制的方法。由于它们不是OWL 1.0语言定义的
部分,因此OWL本体不支持合格的基数
限制。 OWL 2更新中添加了合格的
基数限制。 Jena的OWL2支持
将在适当的时候添加。
Jena Ontology API
Note that, at present, the Jena ontology API has only limited support for OWL2's qualified cardinality restrictions (i.e. cardinalityQ, minCardinalityQ and maxCardinalityQ). Qualified cardinality restrictions are encapsulated in the interfaces CardinalityQRestriction, MinCardinalityQRestriction and CardinalityQRestriction. OntModel also provides methods for creating and accessing qualified cardinality restrictions. Since they are not part of the OWL 1.0 language definition, qualified cardinality restrictions are not supported in OWL ontologies. Qualified cardinality restrictions were added to the OWL 2 update. OWL2 support in Jena will be added in due course.
此外, OWL2词汇类的Javadoc 说:
OWL2词汇。注意:Jena不提供OWL2推理或OntModel支持。提供这些常量是为了方便那些正在使用当前OWL1支持进行OWL2工作且需要一组合适名称的用户。
OWL2 vocabulary. NOTE: Jena does not provide OWL2 inference or OntModel support. These constants are provided for the convenience of users who are doing OWL2 work with the current OWL1 support and desire a suitable set of names.
您可能还会看到我在Jena邮件列表中发布的关于类似问题的回复, Re:Owl maxCardinality restriction 。
You might also see a response that I posted to the Jena mailing list about a similar question, Re: Owl maxCardinality restriction.
但是你想要创建一个呢?那么你就是那些正在使用当前OWL1支持并且需要一组合适名称的OWL2工作的用户之一。为了找出如何在RDF中序列化OWL2结构,我们需要看一下 OWL 2 Web本体语言映射到RDF图表(第二版) ,特别是 2从结构规范到RDF图的映射,它告诉我们类表达式
But you want to create one anyway? Then you're one of those "users who are doing OWL2 work with the current OWL1 support and desire a suitable set of names." To find out how the OWL2 construction should be serialized in RDF, we need to take a look at OWL 2 Web Ontology Language Mapping to RDF Graphs (Second Edition), particularly section 2 Mapping from the Structural Specification to RDF Graphs, which tells us that the class expression
ObjectExactCardinality( n OPE CE )
被序列化为以下三元组
_:x rdf:type owl:Restriction .
_:x owl:onProperty T(OPE) .
_:x owl:qualifiedCardinality "n"^^xsd:nonNegativeInteger .
_:x owl:onClass T(CE) .
其中 _:x
是资源是班级。 Jena已经处理的不合格案例转为
where _:x
is the resource that is the class. The non-qualified case, which Jena already handles, turns
ObjectExactCardinality( n OPE )
进入
_:x rdf:type owl:Restriction .
_:x owl:onProperty T(OPE) .
_:x owl:cardinality "n"^^xsd:nonNegativeInteger .
如果我们有后者之一,我们可以替换它的 owl:cardinality
具有 owl:qualifiedCardinality
属性的属性,并添加相应的 owl:onClass
属性。下面是一些Java代码:
If we had one of the latter, we could replace its owl:cardinality
property with an owl:qualifiedCardinality
property, and add the appropriate owl:onClass
property. Here's some Java code that does just that:
import com.hp.hpl.jena.ontology.OntClass;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.ontology.OntModelSpec;
import com.hp.hpl.jena.ontology.OntProperty;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.vocabulary.OWL;
import com.hp.hpl.jena.vocabulary.OWL2;
public class QualifiedRestrictionExample {
public static OntClass createCardinalityQRestriction(
OntModel model,
String uri,
Property prop,
int cardinality,
OntClass clas ) {
OntClass klass = model.createCardinalityRestriction( uri, prop, cardinality );
klass.removeAll( OWL.cardinality );
klass.addLiteral( OWL2.qualifiedCardinality, cardinality );
klass.addProperty( OWL2.onClass, clas );
return klass;
}
public static void main(String[] args) {
String NS = "https://stackoverflow.com/q/20562107/1281433/";
OntModel model = ModelFactory.createOntologyModel( OntModelSpec.OWL_DL_MEM );
OntClass test = model.createClass( NS+"Test" );
OntProperty j = model.createObjectProperty( NS+"JeVysledkom" );
OntClass k = model.createClass( NS+"Kolik_Fazovy" );
OntClass x = createCardinalityQRestriction(model, null, j, 1, k);
test.addSuperClass( x );
model.write( System.out, "RDF/XML-ABBREV" );
}
}
输出:
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:owl="http://www.w3.org/2002/07/owl#"
xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#">
<owl:Class rdf:about="https://stackoverflow.com/q/20562107/1281433/Kolik_Fazovy"/>
<owl:Class rdf:about="https://stackoverflow.com/q/20562107/1281433/Test">
<rdfs:subClassOf>
<owl:Restriction>
<owl:onClass rdf:resource="https://stackoverflow.com/q/20562107/1281433/Kolik_Fazovy"/>
<owl:qualifiedCardinality rdf:datatype="http://www.w3.org/2001/XMLSchema#long"
>1</owl:qualifiedCardinality>
<owl:onProperty>
<owl:ObjectProperty rdf:about="https://stackoverflow.com/q/20562107/1281433/JeVysledkom"/>
</owl:onProperty>
</owl:Restriction>
</rdfs:subClassOf>
</owl:Class>
</rdf:RDF>
在Protégé中:
这篇关于如何在JENA中添加合格的基数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!