简体   繁体   中英

OWLAPI 5: how to iterate over the classes of my ontology to insert individuals?

I had built a small ontology with protégé 5 that contains 4 classes: class Person and its sub-classes ( Student, Lecturer) and class Module and its sub-classes (MathModule and CSModule), I have two object properties: teaches and studies. I am still a beginner with OWL API and what I want to do is to load this ontology and iterate over the different classes ( including the sub-classes) in order to create and insert individuals with respect to the object properties. I started doing this for only one class but I am not sure how I could do it for the rest of the classes.

public class adding_individuals {
    public static void main(String[] args) throws OWLOntologyCreationException, OWLOntologyStorageException {

        OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
        try {

            OWLOntology ontology = manager.loadOntologyFromOntologyDocument(new File("C:\\..\\university.owl"));
            OWLDataFactory dataFactory = manager.getOWLDataFactory();
            //:lecturer 3 is an instance of the class :Lecturer and CS101 is an instance of CSModule
            OWLClass Lecturer = dataFactory.getOWLClass(":Lecturer");
            OWLClass CSModule = dataFactory.getOWLClass(":CSModule");
            OWLNamedIndividual lecturer3 = dataFactory.getOWLNamedIndividual(":lecturer3");
            OWLNamedIndividual CS101 = dataFactory.getOWLNamedIndividual(":CS101");
            // create a property "teaches"
            OWLObjectProperty teaches = dataFactory.getOWLObjectProperty(":teaches");
            // To specify that :lecturer3 is related to :CS101 via the :teaches property, we create an object property
            // assertion and add it to the ontology
            OWLObjectPropertyAssertionAxiom propertyAssertion = dataFactory.getOWLObjectPropertyAssertionAxiom(teaches, lecturer3, CS101);
            manager.addAxiom(ontology, propertyAssertion);

            // Dump the ontology
            StreamDocumentTarget target = new StreamDocumentTarget(new ByteArrayOutputStream());

            manager.saveOntology(ontology);
        } catch (OWLOntologyCreationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (OWLOntologyStorageException eo) {
            // TODO Auto-generated catch block
            eo.printStackTrace();
        }
    }
}

To iterate over all classes, use ontology.classesInSignature() (in OWLAPI 5, otherwise ontology.getClassesInSignature() ).

However, I assume you'll need some more precise criterion to choose which classes to use - iterating over all classes will include iterating over all Persons and Modules . I assume you need to be more selective.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM