用jena列出qualifiedCardinality限制的资源

时间:2016-12-26 21:05:00

标签: java jena ontology

我是jena库的新手,想要列出qualifiedCardinality限制的所有资源,属性和数据类型。

我的限制:

...
<rdfs:subClassOf>
  <owl:Restriction>
    <owl:onProperty         rdf:resource="http://purl.oclc.org/NET/ssnx/ssn#sensingMethodUsed"/>
    <owl:onClass rdf:resource="http://purl.oclc.org/NET/ssnx/ssn#Sensing"/>
    <owl:qualifiedCardinality rdf:datatype="http://www.w3.org/2001/XMLSchema#nonNegativeInteger">1</owl:qualifiedCardinality>
  </owl:Restriction>
</rdfs:subClassOf>
<rdfs:subClassOf>
  <owl:Restriction>
    <owl:onProperty rdf:resource="http://purl.oclc.org/NET/ssnx/ssn#featureOfInterest"/>
    <owl:onClass rdf:resource="http://purl.oclc.org/NET/ssnx/ssn#FeatureOfInterest"/>
    <owl:qualifiedCardinality rdf:datatype="http://www.w3.org/2001/XMLSchema#nonNegativeInteger">1</owl:qualifiedCardinality>
  </owl:Restriction>
</rdfs:subClassOf>
...

所需的字符串输出:

qualcard sensingMethodUsed nonNegativeIteger 1 Sensing                        
qualcard featureOfInterest nonNegativeIteger 1 FeatureOfInterest

请有人帮助我。

1 个答案:

答案 0 :(得分:0)

该片段显示它是OWL2本体。 Jena仅支持OWL1(请参阅包 org.apache.jena.ontology )。 但作为选项,您可以使用ONT-API中的 ru.avicomp.ontapi.jena.model.OntGraphModel ,它完全类似于 org.apache.jena.ontology.OntModel 但是对于OWL2规范。它是基于apache-jena的库。

一个例子:

    // build model with qualified exact cardinality object property restrictions:
    OntGraphModel m = OntModelFactory.createModel();
    m.setNsPrefixes(OntModelFactory.STANDARD);
    OntNOP op1 = m.createObjectProperty("http://purl.oclc.org/NET/ssnx/ssn#sensingMethodUsed");
    OntClass c1 = m.createOntClass("http://purl.oclc.org/NET/ssnx/ssn#Sensing");
    OntNOP op2 = m.createObjectProperty("http://purl.oclc.org/NET/ssnx/ssn#featureOfInterest");
    OntClass c2 = m.createOntClass("http://purl.oclc.org/NET/ssnx/ssn#FeatureOfInterest");
    m.createOntClass("http://example.com#clazz")
            .addSuperClass(m.createObjectCardinality(op1, 1, c1))
            .addSuperClass(m.createObjectCardinality(op2, 1, c2));

    // printing:
    m.write(System.out, "rdf/xml");
    System.out.println();
    // list all exact cardinality restrictions:
    m.ontObjects(OntCE.ObjectCardinality.class).forEach(c -> {
        int cardinality = c.getCardinality();
        OntCE onClass = c.getValue(); // cannot be null, owl:Thing if it is unqualified restriction
        OntOPE onProperty = c.getProperty();
        OntDT dt = m.getDatatype(XSD.nonNegativeInteger); // only this DT is allowed
        System.out.printf("qualcard %s %s %d %s%n",
                onProperty.getLocalName(), dt.getLocalName(), cardinality, onClass.getLocalName());
    });

此示例的输出:

<rdf:RDF
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:dc="http://purl.org/dc/elements/1.1/"
    xmlns:eg="http://www.example.org/"
    xmlns:vcard="http://www.w3.org/2001/vcard-rdf/3.0#"
    xmlns:owl="http://www.w3.org/2002/07/owl#"
    xmlns:ja="http://jena.hpl.hp.com/2005/11/Assembler#"
    xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
    xmlns:rss="http://purl.org/rss/1.0/"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema#">
  <owl:Ontology/>
  <owl:Class rdf:about="http://purl.oclc.org/NET/ssnx/ssn#FeatureOfInterest"/>
  <owl:Class rdf:about="http://purl.oclc.org/NET/ssnx/ssn#Sensing"/>
  <owl:Class rdf:about="http://example.com#clazz">
    <rdfs:subClassOf>
      <owl:Restriction>
        <owl:onClass rdf:resource="http://purl.oclc.org/NET/ssnx/ssn#FeatureOfInterest"/>
        <owl:qualifiedCardinality rdf:datatype="http://www.w3.org/2001/XMLSchema#nonNegativeInteger"
        >1</owl:qualifiedCardinality>
        <owl:onProperty>
          <owl:ObjectProperty rdf:about="http://purl.oclc.org/NET/ssnx/ssn#featureOfInterest"/>
        </owl:onProperty>
      </owl:Restriction>
    </rdfs:subClassOf>
    <rdfs:subClassOf>
      <owl:Restriction>
        <owl:onClass rdf:resource="http://purl.oclc.org/NET/ssnx/ssn#Sensing"/>
        <owl:qualifiedCardinality rdf:datatype="http://www.w3.org/2001/XMLSchema#nonNegativeInteger"
        >1</owl:qualifiedCardinality>
        <owl:onProperty>
          <owl:ObjectProperty rdf:about="http://purl.oclc.org/NET/ssnx/ssn#sensingMethodUsed"/>
        </owl:onProperty>
      </owl:Restriction>
    </rdfs:subClassOf>
  </owl:Class>
</rdf:RDF>

qualcard featureOfInterest nonNegativeInteger 1 FeatureOfInterest
qualcard sensingMethodUsed nonNegativeInteger 1 Sensing