我用protégé5构建了一个小型本体,其中包含4个类:类Person及其子类(Student,Lecturer)和Module及其子类(MathModule和CSModule),我有两个对象属性:教学和研究。我仍然是OWL API的初学者,我要做的是加载此本体并在不同的类(包括子类)上进行迭代,以针对对象属性创建和插入个人。我仅针对一个班级开始这样做,但是我不确定如何在其余班级上做到这一点。
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();
}
}
}
要遍历所有类,请使用ontology.classesInSignature()
(在OWLAPI 5中,否则为ontology.getClassesInSignature()
)。
但是,我假设您将需要一些更精确的标准来选择要使用的类-对所有类进行迭代将包括对所有Persons
和Modules
进行迭代。我认为您需要更具选择性。