如何在JENA中添加合格的基数

时间:2013-12-13 08:29:22

标签: java rdf jena owl description-logic

如何在Jena中添加合格的基数限制?我无法使用createCardinalityQRestriction,因为OntModelSpec适用于OWL的第一个版本,而不是OWL2。在ModelFactory的createOntologyModel中,有没有办法创建OWL2本体?我需要一个像

这样的类表达式
  

JeVysledkom 完全 1 Kolik_Fazovy

我尝试过使用此代码:

OntModel ontModel = ModelFactory.createOntologyModel();
OntClass ret = ontModel.createCardinalityQRestriction(null, ontProperty, cardinality,    ontClass2 );
ontClass.addSuperClass(ret);

但我得到了这个例外:

  

com.hp.hpl.jena.ontology.ProfileException:尝试使用当前语言配置文件不支持的语言构造CARDINALITY_Q:OWL Full

3 个答案:

答案 0 :(得分:5)

我实际上在处理另一个问题Adding more complicated subclass axiom时遇到了这个问题。在Jena中创建这个有点棘手,因为对合格基数限制的支持是OWL2功能,而Jena对OWL2的支持有限:

  

Jena Ontology API

     

请注意,目前,Jena本体API仅提供有限的支持   对于OWL2的合格基数限制(即基数Q,   minCardinalityQ和maxCardinalityQ)。合格的基数   限制被封装在接口中   基数QRestriction,MinCardinalityQRestriction和   CardinalityQRestriction。 OntModel还提供了创建方法   并获得合格的基数限制。既然他们不是   OWL 1.0语言定义的一部分,合格的基数   OWL本体不支持限制。合格   基数限制被添加到OWL 2更新中。 OWL2支持   耶拿将在适当的时候加入。

此外,Javadoc for the OWL2 vocabulary class说:

  

OWL2词汇。注意:Jena不提供OWL2推理或OntModel支持。提供这些常量是为了方便那些正在使用当前OWL1支持进行OWL2工作且需要一组合适名称的用户。

您可能还会在Jena邮件列表中看到有关类似问题Re: Owl maxCardinality restriction的回复。

但是你想创建一个呢?那么你就是那些“正在使用当前OWL1支持并且需要一组合适名称的OWL2工作的用户之一。”为了找出如何在RDF中序列化OWL2构造,我们需要看一下{{ 3}},尤其是第OWL 2 Web Ontology Language Mapping to RDF Graphs (Second Edition)节,它告诉我们类表达式

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是该类的资源。耶拿已经处理的不合格案件转为

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代码:

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é:

subclass axiom in Protege

答案 1 :(得分:0)

OntModel ontModel = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);
OntClass task = ontModel.createClass(OWL.NS + "Task");
OntClass actor = ontModel.createClass(OWL.NS + "Actor");
OntProperty propTask = ontModel.createObjectProperty( OWL.NS + "Task-performedBy-Actor");
OntProperty propActor = ontModel.createObjectProperty( OWL.NS + "Actor-performs-Task");

然后你可以这样做:

   createOneToMany(ontModel, task, prop, actor);
   createZeroToMany(ontModel, task, prop, actor);
   createOneToOne(ontModel, task, propTask, actor);
   createZeroToOne(ontModel, actor, propActor, task);

致电:

<rdf:RDF
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:owl="http://www.w3.org/2002/07/owl#"
    xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema#">
  <owl:Class rdf:about="http://www.w3.org/2002/07/owl#Task">
    <rdfs:subClassOf>
      <owl:Restriction rdf:nodeID="A0">
        <owl:minCardinality rdf:datatype="http://www.w3.org/2001/XMLSchema#int"
        >1</owl:minCardinality>
        <owl:onProperty>
          <owl:ObjectProperty rdf:about="http://www.w3.org/2002/07/owl#Actor-performs-Task"/>
        </owl:onProperty>
      </owl:Restriction>
    </rdfs:subClassOf>
  </owl:Class>
  <owl:Class rdf:about="http://www.w3.org/2002/07/owl#Actor">
    <rdfs:subClassOf>
      <owl:Restriction>
        <owl:maxCardinality rdf:datatype="http://www.w3.org/2001/XMLSchema#int"
        >1</owl:maxCardinality>
        <owl:onProperty rdf:resource="http://www.w3.org/2002/07/owl#Actor-performs-Task"/>
      </owl:Restriction>
    </rdfs:subClassOf>
    <rdfs:subClassOf rdf:nodeID="A0"/>
  </owl:Class>
  <owl:ObjectProperty rdf:about="http://www.w3.org/2002/07/owl#Task-performedBy-Actor"/>
</rdf:RDF>

结果样本:

date <= DATE_ADD(CURRENT_DATE(), -3, 'DAY')

答案 2 :(得分:0)

现在还有ONT-API,它是OWL2的耶拿扩展名:

    String ns = "https://stackoverflow.com/q/20562107/1281433/";
    OntGraphModel model = OntModelFactory.createModel()
            .setNsPrefixes(OntModelFactory.STANDARD)
            .setNsPrefix("test", ns);

    OntClass c = model.createOntClass(ns + "Kolik_Fazovy");
    OntNOP p = model.createObjectProperty(ns + "JeVysledkom");
    model.createOntClass(ns + "Test").addSuperClass(model.createObjectCardinality(p, 1, c));

    // list all class expressions (2 owl classes, 1 restriction):
    Assert.assertEquals(3, model.ontObjects(OntCE.class).count());
    // print model
    model.write(System.out, "ttl");

出局:

@prefix test:  <https://stackoverflow.com/q/20562107/1281433/> .
@prefix rdf:   <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix owl:   <http://www.w3.org/2002/07/owl#> .
@prefix xsd:   <http://www.w3.org/2001/XMLSchema#> .
@prefix rdfs:  <http://www.w3.org/2000/01/rdf-schema#> .

test:Kolik_Fazovy  a  owl:Class .

test:JeVysledkom  a  owl:ObjectProperty .

test:Test  a             owl:Class ;
        rdfs:subClassOf  [ a                         owl:Restriction ;
                           owl:onClass               test:Kolik_Fazovy ;
                           owl:onProperty            test:JeVysledkom ;
                           owl:qualifiedCardinality  "1"^^xsd:nonNegativeInteger
                         ] .