0
votes

I would like to add this assertion (from Protégé "Individual Description") with java owl-api :

person hasProperty only (ClassA or ClassB )

As :

<ClassAssertion>
  <ObjectAllValuesFrom>
    <ObjectProperty IRI=“#hasProperty”/>
      <ObjectUnionOf>
        <Class IRI=“#ClassA”/>
        <Class IRI=“#ClassB”/>
       </ObjectUnionOf>
   </ObjectAllValuesFrom>
  <NamedIndividual IRI=“#Person”/>
</ClassAssertion

How can I do it?

OWLClass c1 = factory.getOWLClass(":ClassA", prefix );

OWLClass c2 = factory.getOWLClass(":ClassB", prefix );

OWLObjectProperty p = factory.getOWLObjectProperty("hasProperty", prefix);

OWLObjectIntersectionOf u = factory.getOWLObjectUnionOf(c1, c2);

OWLClassExpression e = factory.getOWLClass(property, u);

manager.addAxiom(owlOntology, factory.getOWLClassAssertionAxiom(p, person, e));
1
factory.getOWLClass(property, u); <- that's clearly wrong if you look at the Javadoc of this method! use factory.getOWLObjectAllValuesFrom(p, u) instead.UninformedUser
You have the order of class name and prefix wrong. The prefix comes first. See response below.Ignazio
Note: the output example that you have pasted looks like OWL/XML. If you're editing a file in place, when saving the file you do not need to specify a format, but if you're creating an ontology and do not want the default RDF/XML format you will have to specify a format argument to the saveOntology method.Ignazio

1 Answers

0
votes

A few issues with the code you've written:

OWLOntologyManager manager=OWLManager.createOWLOntologyManager();
OWLDataFactory factory=manager.getOWLDataFactory();
String str = "urn:test:ontology";
OWLOntology owlOntology=manager.createOntology(IRI.create(str));
String prefix=str+"#";

OWLClass c1 = factory.getOWLClass( prefix ,"ClassA");
OWLClass c2 = factory.getOWLClass( prefix, "ClassB" );
OWLObjectProperty p = factory.getOWLObjectProperty( prefix, "hasProperty");
OWLObjectUnionOf u = factory.getOWLObjectUnionOf(c1, c2);
OWLClassExpression e = factory.getOWLObjectAllValuesFrom(p, u);
OWLNamedIndividual person=factory.getOWLNamedIndividual(prefix, "Person");
manager.addAxiom(owlOntology, factory.getOWLClassAssertionAxiom(e, person));
// don't forget to save the ontology
manager.saveOntology(owlOntology, ... output stream here...);