4

I've an ontology file and I can obtain all classes in its (I'm using OWL-API). Well, I should retrieve, for each classes, data properties and object properties present into my file .owl, there is any way to get them with OWL-API?

Claudio Pomo
  • 2,392
  • 7
  • 42
  • 71
  • You'll have to decide what you mean by "for each class". OWL is not a (single dispatch) object oriented programming language and properties do not belong to classes, so "the properties of a class" doesn't have a well defined meaning. – Joshua Taylor May 14 '14 at 11:05
  • If you were working in Jena, this would be a duplicate of [object property for a class](http://stackoverflow.com/q/23371640/1281433). Also see [Parsing schema.org ttl/owl file using Jena](http://stackoverflow.com/q/22797424/1281433). You're probably looking for a counterpart to Jena's [listDeclaredProperties](http://jena.apache.org/documentation/javadoc/jena/com/hp/hpl/jena/ontology/OntClass.html#listDeclaredProperties%28boolean%29). – Joshua Taylor May 14 '14 at 11:07
  • It's a little counter intuitive, unless you are a logician, but restrictions are anonymous superclasses of the class, so you just call "getSuperClasses". To get, for example, just existential restrictions you then filter on type or use a Visitor. – Phil Lord May 14 '14 at 11:40
  • Oh, yes, and Joshua is correct, it's "the super classes of an Class in a given ontology" -- if you just have one ontology this distinction is probably not important, but it can trip you up otherwise. – Phil Lord May 14 '14 at 11:41
  • @PhilLord You left it implicit, but I assume you're talking about inferring that p is a property of a class C when we have `C subClassOf (p some OtherClass)`. Those may be important (but we have to be careful with restrictions like `(p max 0)` or `(p only Nothing)`. Restrictions are only part of the picture here, though. If I declare that property P has domain D, then any time I have p(x,y), I can infer that x is a D, as well as every superclass of D. Depending on what's expected, this means p should be listed as a property of D and all its superclasses. – Joshua Taylor May 14 '14 at 14:24
  • No, I am not talking about inference. The OP asks "how to get object properties for a Class". Answer, use the "getSuperClass" method. My experience is that people expect getSuperClass to return, well the superclasses (i.e. parent classes), while actually they return OWLClassExpressions as well. When I first used the OWL API, I tried searching for methods like "getRestrictions" on OWLClass, and they are not there. We need clarification from the OP to know if I am answering the question or not now! – Phil Lord May 15 '14 at 09:19

2 Answers2

5
public void test(){

        File file = new File("Ontology.owl");
        OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
        OWLOntology ontology;

        try {
            ontology = manager.loadOntologyFromOntologyDocument(file);

            Set<OWLClass> classes;
            Set<OWLObjectProperty> prop;
            Set<OWLDataProperty> dataProp;
            Set<OWLNamedIndividual> individuals;

            classes = ontology.getClassesInSignature();
            prop = ontology.getObjectPropertiesInSignature();
            dataProp = ontology.getDataPropertiesInSignature();
            individuals = ontology.getIndividualsInSignature();
            //configurator = new OWLAPIOntologyConfigurator(this);            

            System.out.println("Classes");
            System.out.println("--------------------------------");
            for (OWLClass cls : classes) {
                System.out.println("+: " + cls.getIRI().getShortForm());

                System.out.println(" \tObject Property Domain");
                for (OWLObjectPropertyDomainAxiom op : ontology.getAxioms(AxiomType.OBJECT_PROPERTY_DOMAIN)) {                        
                        if (op.getDomain().equals(cls)) {   
                            for(OWLObjectProperty oop : op.getObjectPropertiesInSignature()){
                                 System.out.println("\t\t +: " + oop.getIRI().getShortForm());
                            }
                            //System.out.println("\t\t +: " + op.getProperty().getNamedProperty().getIRI().getShortForm());
                        }
                    }

                    System.out.println(" \tData Property Domain");
                    for (OWLDataPropertyDomainAxiom dp : ontology.getAxioms(AxiomType.DATA_PROPERTY_DOMAIN)) {
                        if (dp.getDomain().equals(cls)) {   
                            for(OWLDataProperty odp : dp.getDataPropertiesInSignature()){
                                 System.out.println("\t\t +: " + odp.getIRI().getShortForm());
                            }
                            //System.out.println("\t\t +:" + dp.getProperty());
                        }
                    }

            }

        } catch (OWLOntologyCreationException ex) {
            Logger.getLogger(OntologyAPI.class.getName()).log(Level.SEVERE, null, ex);
        }
}
Rick Smith
  • 9,031
  • 15
  • 81
  • 85
Jose Juarez
  • 51
  • 1
  • 2
0

This should be an answer, I think, rather than a comment.

To get the properties of a class, you just use getSuperClasses. So, if you have a class like so

A
  :subClassOf (r some B)

then (getSuperClasses A) will return a set with a single OWLClassExpression which will be an instance of OWLObjectSomeValuesFrom. In turn, you can get the property with getProperty.

Phil Lord
  • 2,917
  • 1
  • 20
  • 31
  • Thank you Phil! However I've changed API and I'm using now JENA API. With them is very simple parse an owl file and retrieve all classes and properties for each class. But some properties are equivalent and I would obtain them just once. There is some way? – Claudio Pomo May 19 '14 at 09:25
  • Fraid I am don't fully understand the question, nor am I particularly experienced with JENA. – Phil Lord May 19 '14 at 21:40
  • @PhilLord Could you given a simplified code example of what you mean by "To get the properties of a class, you just use getSuperClasses". Say I have a class `A` which has a `rdfs:label` and `ro:located_in` class `B`. – F Lekschas Jul 17 '15 at 19:29
  • So `(.getSuperClasses A o)` gets you the superclasses, then search the superclasses for the OWLClassExpression one of which will be ro:located_in. The rdfs:label is an annotation so you need `(.getAnnotations o A)`. In each case "o" is the ontology you care about. If you care about more than one, I think, you need to make multiple calls or pass a set. – Phil Lord Aug 04 '15 at 19:13