// an ontology, append a # and then a local name. For example we can
// create the individual 'John', using the ontology URI and appending
// #John. Note however, that there is no reuqirement that a URI of a
// class, property or individual that is used in an ontology have a
// correspondance with the URI of the ontology.
OWLIndividual john = factory.getOWLNamedIndividual(IRI
.create(ontologyIRI + "#John"));
OWLIndividual mary = factory.getOWLNamedIndividual(IRI
.create(ontologyIRI + "#Mary"));
OWLIndividual susan = factory.getOWLNamedIndividual(IRI
.create(ontologyIRI + "#Susan"));
OWLIndividual bill = factory.getOWLNamedIndividual(IRI
.create(ontologyIRI + "#Bill"));
// The ontologies that we created aren't contained in any ontology at
// the moment. Individuals (or classes or properties) can't directly be
// added to an ontology, they have to be used in axioms, and then the
// axioms are added to an ontology. We now want to add some facts to the
// ontology. These facts are otherwise known as property assertions. In
// our case, we want to say that John has a wife Mary. To do this we
// need to have a reference to the hasWife object property (object
// properties link an individual to an individual, and data properties
// link and individual to a constant - here, we need an object property
// because John and Mary are individuals).
OWLObjectProperty hasWife = factory.getOWLObjectProperty(IRI
.create(ontologyIRI + "#hasWife"));
// Now we need to create the assertion that John hasWife Mary. To do
// this we need an axiom, in this case an object property assertion
// axiom. This can be thought of as a "triple" that has a subject, john,
// a predicate, hasWife and an object Mary
OWLObjectPropertyAssertionAxiom axiom1 = factory
.getOWLObjectPropertyAssertionAxiom(hasWife, john, mary);
// We now need to add this assertion to our ontology. To do this, we
// apply an ontology change to the ontology via the OWLOntologyManager.
// First we create the change object that will tell the manager that we
// want to add the axiom to the ontology
AddAxiom addAxiom1 = new AddAxiom(ont, axiom1);
// Now we apply the change using the manager.
manager.applyChange(addAxiom1);
// Now we want to add the other facts/assertions to the ontology John
// hasSon Bill Get a refernece to the hasSon property
OWLObjectProperty hasSon = factory.getOWLObjectProperty(IRI
.create(ontologyIRI + "#hasSon"));
// Create the assertion, John hasSon Bill
OWLAxiom axiom2 = factory.getOWLObjectPropertyAssertionAxiom(hasSon,
john, bill);
// Apply the change
manager.applyChange(new AddAxiom(ont, axiom2));
// John hasDaughter Susan
OWLObjectProperty hasDaughter = factory.getOWLObjectProperty(IRI
.create(ontologyIRI + "#hasDaughter"));
OWLAxiom axiom3 = factory.getOWLObjectPropertyAssertionAxiom(
hasDaughter, john, susan);
manager.applyChange(new AddAxiom(ont, axiom3));
// John hasAge 33 In this case, hasAge is a data property, which we need
// a reference to
OWLDataProperty hasAge = factory.getOWLDataProperty(IRI
.create(ontologyIRI + "#hasAge"));
// We create a data property assertion instead of an object property
// assertion
OWLAxiom axiom4 = factory.getOWLDataPropertyAssertionAxiom(hasAge,
john, 33);
manager.applyChange(new AddAxiom(ont, axiom4));
// In the above code, 33 is an integer, so we can just pass 33 into the
// data factory method. Behind the scenes the OWL API will create a
// typed constant that it will use as the value of the data property
// assertion. We could have manually created the constant as follows:
OWLDatatype intDatatype = factory.getIntegerOWLDatatype();
OWLLiteral thirtyThree = factory.getOWLLiteral("33", intDatatype);
// We would then create the axiom as follows:
factory.getOWLDataPropertyAssertionAxiom(hasAge, john, thirtyThree);
// However, the convenice method is much shorter! We can now create the
// other facts/assertion for Mary. The OWL API uses a change object
// model, which means we can stack up changes (or sets of axioms) and
// apply the changes (or add the axioms) in one go. We will do this for
// Mary
Set<OWLAxiom> axioms = new HashSet<OWLAxiom>();
axioms.add(factory.getOWLObjectPropertyAssertionAxiom(hasSon, mary,
bill));
axioms.add(factory.getOWLObjectPropertyAssertionAxiom(hasDaughter,
mary, susan));
axioms.add(factory.getOWLDataPropertyAssertionAxiom(hasAge, mary, 31));
// Add facts/assertions for Bill and Susan
axioms.add(factory.getOWLDataPropertyAssertionAxiom(hasAge, bill, 13));
axioms.add(factory.getOWLDataPropertyAssertionAxiom(hasAge, mary, 8));
// Now add all the axioms in one go - there is a convenience method on
// OWLOntologyManager that will automatically generate the AddAxiom
// change objects for us. We need to specify the ontology that the
// axioms should be added to and the axioms to add.
manager.addAxioms(ont, axioms);
// Now specify the genders of John, Mary, Bill and Susan. To do this we
// need the male and female individuals and the hasGender object
// property.
OWLIndividual male = factory.getOWLNamedIndividual(IRI
.create(ontologyIRI + "#male"));
OWLIndividual female = factory.getOWLNamedIndividual(IRI
.create(ontologyIRI + "#female"));
OWLObjectProperty hasGender = factory.getOWLObjectProperty(IRI
.create(ontologyIRI + "#hasGender"));
Set<OWLAxiom> genders = new HashSet<OWLAxiom>();
genders.add(factory.getOWLObjectPropertyAssertionAxiom(hasGender, john,