use of org.semanticweb.owlapi.model.OWLDataFactory in project stanbol by apache.
the class OWLAPIToClerezzaConverterTest method setupClass.
@BeforeClass
public static void setupClass() {
/*
* Set-up the OWL ontology for the test. Simply add the axioms: AndreaNuzzolese isA Person -> class
* assertion axiom EnricoDaga isA Person -> class assertion axiom AndreaNuzzolese knows EnricoDaga ->
* object property assertion axiom
*/
OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
OWLDataFactory factory = manager.getOWLDataFactory();
try {
ontology = manager.createOntology(org.semanticweb.owlapi.model.IRI.create(ns + "testOntology"));
} catch (OWLOntologyCreationException e) {
log.error(e.getMessage());
}
if (ontology != null) {
OWLClass personClass = factory.getOWLClass(org.semanticweb.owlapi.model.IRI.create(foaf + "Person"));
OWLNamedIndividual andreaNuzzoleseOWL = factory.getOWLNamedIndividual(org.semanticweb.owlapi.model.IRI.create(ns + "AndreaNuzzolese"));
OWLNamedIndividual enricoDagaOWL = factory.getOWLNamedIndividual(org.semanticweb.owlapi.model.IRI.create(ns + "EnricoDaga"));
OWLObjectProperty knowsOWL = factory.getOWLObjectProperty(org.semanticweb.owlapi.model.IRI.create(foaf + "knows"));
OWLAxiom axiom = factory.getOWLClassAssertionAxiom(personClass, andreaNuzzoleseOWL);
manager.addAxiom(ontology, axiom);
axiom = factory.getOWLClassAssertionAxiom(personClass, enricoDagaOWL);
manager.addAxiom(ontology, axiom);
axiom = factory.getOWLObjectPropertyAssertionAxiom(knowsOWL, andreaNuzzoleseOWL, enricoDagaOWL);
manager.addAxiom(ontology, axiom);
}
/*
* Set-up the Clerezza model for the test. As before simply add the triples: AndreaNuzzolese isA
* Person EnricoDaga isA Person AndreaNuzzolese knows EnricoDaga
*/
mGraph = new SimpleGraph();
IRI knowsInClerezza = new IRI(ns + "knows");
IRI rdfType = new IRI(RDF.getURI() + "type");
IRI foafPersonInClerezza = new IRI(foaf + "Person");
BlankNodeOrIRI andreaNuzzoleseInClerezza = new IRI(ns + "AndreaNuzzolese");
BlankNodeOrIRI enricoDagaInClerezza = new IRI(ns + "EnricoDaga");
Triple triple = new TripleImpl(andreaNuzzoleseInClerezza, rdfType, foafPersonInClerezza);
mGraph.add(triple);
triple = new TripleImpl(enricoDagaInClerezza, rdfType, foafPersonInClerezza);
mGraph.add(triple);
triple = new TripleImpl(andreaNuzzoleseInClerezza, knowsInClerezza, enricoDagaInClerezza);
mGraph.add(triple);
}
use of org.semanticweb.owlapi.model.OWLDataFactory in project stanbol by apache.
the class RootResource method getOWLOntology.
private OWLOntology getOWLOntology(String ontologyId, boolean merge, URI requestUri) {
long before = System.currentTimeMillis();
IRI iri = URIUtils.sanitize(IRI.create(ontologyId));
log.debug("Will try to retrieve ontology {} from provider.", iri);
// TODO be selective: if the ontology is small enough, use OWLOntology otherwise export to ImmutableGraph.
OWLOntology o = null;
try {
// XXX Guarantee that there MUST always be an entry for any decoded ontology ID submitted.
OWLOntologyID id = OntologyUtils.decode(ontologyId);
o = ontologyProvider.getStoredOntology(id, OWLOntology.class, merge);
} catch (Exception ex) {
log.warn("Retrieval of ontology with ID " + iri + " failed.", ex);
}
if (o == null) {
log.debug("Ontology {} missing from provider. Trying libraries...", iri);
// See if we can touch a library. TODO: replace with event model on the ontology provider.
int minSize = -1;
IRI smallest = null;
for (Library lib : registryManager.getLibraries(iri)) {
int size = lib.getChildren().length;
if (minSize < 1 || size < minSize) {
smallest = lib.getIRI();
minSize = size;
}
}
if (smallest != null) {
log.debug("Selected library for ontology {} is {} .", iri, smallest);
try {
o = registryManager.getLibrary(smallest).getOntology(iri, OWLOntology.class);
} catch (RegistryContentException e) {
log.warn("The content of library " + smallest + " could not be accessed.", e);
}
}
}
if (o == null) {
log.debug("Ontology {} not found in any ontology provider or library.", iri);
return null;
}
log.debug("Retrieved ontology {} .", iri);
// Rewrite import statements - no ontology collector to do it for us here.
URI base = URI.create(getPublicBaseUri() + "ontonet/");
List<OWLOntologyChange> changes = new ArrayList<OWLOntologyChange>();
OWLDataFactory df = o.getOWLOntologyManager().getOWLDataFactory();
// TODO manage import rewrites better once the container ID is fully configurable.
for (OWLImportsDeclaration oldImp : o.getImportsDeclarations()) {
changes.add(new RemoveImport(o, oldImp));
String s = oldImp.getIRI().toString();
if (s.contains("::")) {
s = s.substring(s.indexOf("::") + 2, s.length());
}
IRI target = IRI.create(base + s);
changes.add(new AddImport(o, df.getOWLImportsDeclaration(target)));
}
// Versioning.
OWLOntologyID id = o.getOntologyID();
if (!id.isAnonymous() && id.getVersionIRI() == null) {
IRI viri = IRI.create(requestUri);
log.debug("Setting version IRI for export : {}", viri);
changes.add(new SetOntologyID(o, new OWLOntologyID(id.getOntologyIRI(), viri)));
}
o.getOWLOntologyManager().applyChanges(changes);
log.debug("Exported as Clerezza ImmutableGraph in {} ms. Handing over to writer.", System.currentTimeMillis() - before);
return o;
}
use of org.semanticweb.owlapi.model.OWLDataFactory in project stanbol by apache.
the class OntologyImportUtils method buildImportTree.
/**
* Non-recursively adds import statements to the root ontology so that it is directly linked to all the
* ontologies in the subtrees set.
*
* @param parent
* the ontology to which import subtrees should be appended. If null, a runtime exception will
* be thrown.
* @param subtrees
* the set of target ontologies for import statements. These can in turn be importing other
* ontologies, hence the "subtree" notation. A single statement will be added for
* each member of this set.
* @param mgr
* the OWL ontology manager to use for constructing the import tree. If null, an internal one
* will be used instead, otherwise an existing ontology manager can be used e.g. for extracting
* import statements from its IRI mappers or known ontologies. Note that the supplied manager
* will <i>never</i> try to load any ontologies, even when they are unknown.
* @return the same input ontology as defined in <code>root</code>, but with the added import statements.
*/
public static OWLOntology buildImportTree(OWLOntology parent, Set<OWLOntology> subtrees, OWLOntologyManager mgr) {
if (parent == null)
throw new NullPointerException("Cannot append import trees to a nonexistent ontology.");
// If no manager was supplied, use a temporary one.
if (mgr == null)
mgr = OWLManager.createOWLOntologyManager();
OWLDataFactory owlFactory = mgr.getOWLDataFactory();
List<OWLOntologyChange> changes = new LinkedList<OWLOntologyChange>();
for (OWLOntology o : subtrees) {
IRI importIri = null;
try {
/*
* First query the manager, as it could know the physical location of anonymous ontologies, if
* previously loaded or IRI-mapped.
*/
importIri = mgr.getOntologyDocumentIRI(o);
} catch (UnknownOWLOntologyException ex) {
/*
* Otherwise, ask the ontology itself (the location of an anonymous ontology may have been
* known at creation/loading time, even if another manager built it.)
*/
importIri = o.getOntologyID().getDefaultDocumentIRI();
} catch (Exception ex) {
logger.error("Exception caught during tree building. Skipping import of ontology " + o.getOntologyID(), ex);
} finally {
/*
* It is still possible that an imported ontology is anonymous but has no physical document
* IRI (for example, because it was only generated in-memory but not stored). In this case it
* is necessary (and generally safe) to copy all its axioms and import statements to the
* parent ontology, or else it is lost.
*/
if (o.isAnonymous() && importIri == null) {
logger.warn("Anonymous import target " + o.getOntologyID() + " not mapped to physical IRI. Will add extracted axioms to parent ontology.");
for (OWLImportsDeclaration im : o.getImportsDeclarations()) changes.add(new AddImport(parent, im));
for (OWLAxiom im : o.getAxioms()) changes.add(new AddAxiom(parent, im));
} else if (importIri != null) {
// An anonymous ontology can still be imported if it has a
// valid document IRI.
changes.add(new AddImport(parent, owlFactory.getOWLImportsDeclaration(importIri)));
}
}
}
// apply the changes one by one, just in case.
for (OWLOntologyChange im : changes) try {
mgr.applyChange(im);
} catch (Exception ex) {
logger.error("KReS :: Exception caught during tree building. Skipping import", ex);
continue;
}
return parent;
}
use of org.semanticweb.owlapi.model.OWLDataFactory in project stanbol by apache.
the class DatavaluedPropertyAtom method getOWLTypedLiteral.
private OWLLiteral getOWLTypedLiteral(Object argument) {
OWLDataFactory factory = OWLManager.createOWLOntologyManager().getOWLDataFactory();
OWLLiteral owlLiteral;
if (argument instanceof String) {
owlLiteral = factory.getOWLTypedLiteral((String) argument);
} else if (argument instanceof Integer) {
owlLiteral = factory.getOWLTypedLiteral(((Integer) argument).intValue());
} else if (argument instanceof Double) {
owlLiteral = factory.getOWLTypedLiteral(((Double) argument).doubleValue());
} else if (argument instanceof Float) {
owlLiteral = factory.getOWLTypedLiteral(((Float) argument).floatValue());
} else if (argument instanceof Boolean) {
owlLiteral = factory.getOWLTypedLiteral(((Boolean) argument).booleanValue());
} else {
owlLiteral = factory.getOWLStringLiteral(argument.toString());
}
return owlLiteral;
}
use of org.semanticweb.owlapi.model.OWLDataFactory in project stanbol by apache.
the class GreaterEqualThanAtom method getOWLTypedLiteral.
private OWLLiteral getOWLTypedLiteral(Object argument) {
OWLDataFactory factory = OWLManager.createOWLOntologyManager().getOWLDataFactory();
OWLLiteral owlLiteral;
if (argument instanceof String) {
owlLiteral = factory.getOWLTypedLiteral((String) argument);
} else if (argument instanceof Integer) {
owlLiteral = factory.getOWLTypedLiteral(((Integer) argument).intValue());
} else if (argument instanceof Double) {
owlLiteral = factory.getOWLTypedLiteral(((Double) argument).doubleValue());
} else if (argument instanceof Float) {
owlLiteral = factory.getOWLTypedLiteral(((Float) argument).floatValue());
} else if (argument instanceof Boolean) {
owlLiteral = factory.getOWLTypedLiteral(((Boolean) argument).booleanValue());
} else {
owlLiteral = factory.getOWLStringLiteral(argument.toString());
}
return owlLiteral;
}
Aggregations