OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
IRI ontologyIRI = IRI.create("http://example.com/owl/families/");
OWLOntology ontology = manager.createOntology(ontologyIRI);
// Get hold of a data factory from the manager and set up a prefix
// manager to make things easier
OWLDataFactory factory = manager.getOWLDataFactory();
PrefixManager pm = new DefaultPrefixManager(null, null,
ontologyIRI.toString());
// Let's specify the :John has a wife :Mary Get hold of the necessary
// individuals and object property
OWLNamedIndividual john = factory.getOWLNamedIndividual(":John", pm);
OWLNamedIndividual mary = factory.getOWLNamedIndividual(":Mary", pm);
OWLObjectProperty hasWife = factory
.getOWLObjectProperty(":hasWife", pm);
// To specify that :John is related to :Mary via the :hasWife property
// we create an object property assertion and add it to the ontology
OWLObjectPropertyAssertionAxiom propertyAssertion = factory
.getOWLObjectPropertyAssertionAxiom(hasWife, john, mary);
manager.addAxiom(ontology, propertyAssertion);
// Now let's specify that :John is aged 51. Get hold of a data property
// called :hasAge
OWLDataProperty hasAge = factory.getOWLDataProperty(":hasAge", pm);
// To specify that :John has an age of 51 we create a data property
// assertion and add it to the ontology
OWLDataPropertyAssertionAxiom dataPropertyAssertion = factory
.getOWLDataPropertyAssertionAxiom(hasAge, john, 51);
manager.addAxiom(ontology, dataPropertyAssertion);
// Note that the above is a shortcut for creating a typed literal and
// specifying this typed literal as the value of the property assertion.
// That is, Get hold of the xsd:integer datatype
OWLDatatype integerDatatype = factory
.getOWLDatatype(OWL2Datatype.XSD_INTEGER.getIRI());
// Create a typed literal. We type the literal "51" with the datatype
OWLLiteral literal = factory.getOWLLiteral("51", integerDatatype);
// Create the property assertion and add it to the ontology
OWLAxiom ax = factory.getOWLDataPropertyAssertionAxiom(hasAge, john,
literal);
manager.addAxiom(ontology, ax);
// Dump the ontology to System.out
manager.saveOntology(ontology, new StreamDocumentTarget(
new ByteArrayOutputStream()));