list resources of qualifiedCardinality restriction with jena

274 Views Asked by At

I am new with jena libraries, and want to list all resources, properties, datatypes of qualifiedCardinality restriction.

My restriction:

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

Desired String output:

qualcard sensingMethodUsed nonNegativeIteger 1 Sensing                        
qualcard featureOfInterest nonNegativeIteger 1 FeatureOfInterest

Please can some one help me please.

1

There are 1 best solutions below

0
On

The snippet shows it is OWL2 ontology. Jena supports only OWL1 (see package org.apache.jena.ontology). But as an option you can use com.github.owlcs.ontapi.jena.model.OntModel from ONT-API, which is full analogue of org.apache.jena.ontology.OntModel but for OWL2-specification. ONT-API is a jena based library.

See example:

    // build the model with qualified exact cardinality object property restrictions:
    String ns = "http://purl.oclc.org/NET/ssnx/ssn#";
    OntModel m = OntModelFactory.createModel();
    m.setNsPrefixes(OntModelFactory.STANDARD);
    OntObjectProperty op1 = m.createObjectProperty(ns + "sensingMethodUsed");
    OntClass c1 = m.createOntClass(ns + "Sensing");
    OntObjectProperty op2 = m.createObjectProperty(ns + "featureOfInterest");
    OntClass c2 = m.createOntClass(ns + "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(OntClass.ObjectCardinality.class).forEach(c -> {
        int cardinality = c.getCardinality();
        OntClass onClass = c.getValue(); // cannot be null, owl:Thing if it is unqualified restriction
        OntObjectProperty onProperty = c.getProperty();
        OntDataRange 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());
    });

The output for this example is:

<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