2016-12-26 53 views
1

我是jena圖書館的新成員,希望列出合格證書限制的所有資源,屬性,數據類型。jena限定合格證書限制名單資源

我的限制:

... 
<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 

請可有一個人幫我請。

回答

0

該片段顯示它是OWL2本體。 Jena僅支持OWL1(請參閱包org.apache.jena.ontology)。 但作爲選項,您可以使用ru.avicomp.ontapi.jena.model.OntGraphModelONT-API,這是org.apache.jena.ontology.OntModel但對OWL2規格完全模擬。這是基於apache-jena的庫。

一個例子:

public static void main(String ... args) { 
    // build model with qualified exact cardinality object property restrictions: 
    OntGraphModel m = OntManagers.createONT().createOntology().asGraphModel(); 
    m.setNsPrefixes(PrefixMapping.Extended); 

    OntNOP op1 = m.createOntEntity(OntNOP.class, "http://purl.oclc.org/NET/ssnx/ssn#sensingMethodUsed"); 
    OntClass c1 = m.createOntEntity(OntClass.class, "http://purl.oclc.org/NET/ssnx/ssn#Sensing"); 
    OntNOP op2 = m.createOntEntity(OntNOP.class, "http://purl.oclc.org/NET/ssnx/ssn#featureOfInterest"); 
    OntClass c2 = m.createOntEntity(OntClass.class, "http://purl.oclc.org/NET/ssnx/ssn#FeatureOfInterest"); 

    OntClass clazz = m.createOntEntity(OntClass.class, "http://example.com#clazz"); 
    clazz.addSubClassOf(m.createObjectCardinality(op1, 1, c1)); 
    clazz.addSubClassOf(m.createObjectCardinality(op2, 1, c2)); 
    m.write(System.out, OntFormat.RDF_XML.getID()); 

    // printing: 
    System.out.println(); 
    m.ontObjects(OntCE.ObjectCardinality.class).forEach(c -> { 
     String datatype = Stream.of(OWL.qualifiedCardinality, OWL.cardinality) 
       .map(p -> c.objects(p, Literal.class)) 
       .flatMap(Function.identity()) 
       .map(Literal::getDatatypeURI) 
       .map(m::shortForm) 
       .map(s -> s.replaceAll("^.*:", "")) 
       .findFirst().orElse(null); 
     int cardinality = c.getCardinality(); 
     String onClass = Optional.ofNullable(c.getValue()).map(Resource::getLocalName).orElse(null); 
     String onProperty = c.getOnProperty().getLocalName(); 
     System.out.printf("qualcard %s %s %d %s%n", onProperty, datatype, cardinality, onClass); 
    }); 
} 

輸出這個例子:

<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