use of org.semanticweb.owlapi.model.OWLOntology in project stanbol by apache.
the class RegistryManagerImpl method populateOntology.
protected RegistryOntology populateOntology(OWLNamedObject ind, Set<OWLOntology> registries) throws RegistryContentException {
IRI ontId = ind.getIRI();
RegistryItem ront = null;
if (population.containsKey(ontId)) {
// We are not allowing multityping either.
ront = population.get(ontId);
if (!(ront instanceof RegistryOntology))
throw new RegistryContentException("Inconsistent multityping: for item " + ontId + " : {" + RegistryOntology.class + ", " + ront.getClass() + "}");
} else {
ront = riFactory.createRegistryOntology(ind);
try {
population.put(ront.getIRI(), ront);
} catch (Exception e) {
log.error("Invalid identifier for library item " + ront, e);
return null;
}
}
// EXIT nodes.
Set<OWLNamedObject> libs = new HashSet<OWLNamedObject>();
OWLDataFactory df = OWLManager.getOWLDataFactory();
for (OWLOntology o : registries) {
if (ind instanceof OWLIndividual) {
// Get usages of isOntologyOf as an object property
for (OWLIndividual value : ((OWLIndividual) ind).getObjectPropertyValues(isOntologyOf, o)) if (value.isNamed())
libs.add(value.asOWLNamedIndividual());
// Get usages of isOntologyOf as an annotation property
for (OWLAnnotationAssertionAxiom ann : o.getAnnotationAssertionAxioms(ind.getIRI())) if (isOntologyOfAnn.equals(ann.getProperty())) {
OWLAnnotationValue value = ann.getValue();
if (value instanceof OWLNamedObject)
libs.add((OWLNamedObject) value);
else if (value instanceof IRI)
libs.add(df.getOWLNamedIndividual((IRI) value));
}
}
}
for (OWLNamedObject ilib : libs) {
IRI parentId = ilib.getIRI();
// If some populate*() method has created it, it will be there.
RegistryItem rlib = population.get(parentId);
// Otherwise populating it will also put it in population.
if (rlib == null)
rlib = populateLibrary(ilib, registries);
ront.addParent(rlib);
if (ontologyIndex.get(ontId) == null)
ontologyIndex.put(ontId, new HashSet<IRI>());
ontologyIndex.get(ontId).add(parentId);
}
return (RegistryOntology) ront;
}
use of org.semanticweb.owlapi.model.OWLOntology in project stanbol by apache.
the class RegistryManagerImpl method createModel.
@Override
public Set<Registry> createModel(Set<OWLOntology> registryOntologies) {
Set<Registry> results = new HashSet<Registry>();
// Reset population
population.clear();
// Build the transitive imports closure of the union.
Set<OWLOntology> closure = new HashSet<OWLOntology>();
for (OWLOntology rego : registryOntologies) closure.addAll(rego.getOWLOntologyManager().getImportsClosure(rego));
/*
* For each value in this map, index 0 is the score of the library class, while 1 is the score of the
* ontology class.
*/
final Map<IRI, int[]> candidateTypes = new HashMap<IRI, int[]>();
/*
* Scans class assertions and object property values and tries to determine the type of each
* individual it finds.
*/
OWLAxiomVisitor scanner = new OWLAxiomVisitorAdapter() {
/*
* For a given identifier, returns the array of integers whose value determine the likelihood if
* the corresponding entity being a library or an ontology. If no such array exists, it is
* created.
*/
private int[] checkScores(IRI key) {
int[] scores;
if (candidateTypes.containsKey(key))
scores = candidateTypes.get(key);
else {
scores = new int[] { 0, 0 };
candidateTypes.put(key, scores);
}
return scores;
}
@Override
public void visit(OWLAnnotationAssertionAxiom axiom) {
/*
* Works like object property assertions, in case hasOntology and isOntologyOf are not
* detected to be object properties (e.g. due to a failure to load codolight or the registry
* metamodel).
*/
OWLAnnotationProperty prop = axiom.getProperty();
if (hasOntologyAnn.equals(prop)) {
IRI iri;
// The axiom subject gets a +1 in its library score.
OWLObject ind = axiom.getSubject();
if (ind instanceof IRI) {
iri = (IRI) ind;
checkScores(iri)[0]++;
}
// The axiom object gets a +1 in its ontology score.
ind = axiom.getValue();
if (ind instanceof IRI) {
iri = (IRI) ind;
checkScores(iri)[1]++;
}
} else if (isOntologyOfAnn.equals(prop)) {
IRI iri;
// The axiom subject gets a +1 in its ontology score.
OWLObject ind = axiom.getSubject();
if (ind instanceof IRI) {
iri = (IRI) ind;
checkScores(iri)[1]++;
}
// The axiom object gets a +1 in its library score.
ind = axiom.getValue();
if (ind instanceof IRI) {
iri = (IRI) ind;
checkScores(iri)[0]++;
}
}
}
@Override
public void visit(OWLClassAssertionAxiom axiom) {
OWLIndividual ind = axiom.getIndividual();
// Do not accept anonymous registry items.
if (ind.isAnonymous())
return;
IRI iri = ind.asOWLNamedIndividual().getIRI();
int[] scores = checkScores(iri);
OWLClassExpression type = axiom.getClassExpression();
// If the type is stated to be a library, increase its library score.
if (cRegistryLibrary.equals(type)) {
scores[0]++;
} else // If the type is stated to be an ontology, increase its ontology score.
if (cOntology.equals(type)) {
scores[1]++;
}
}
@Override
public void visit(OWLObjectPropertyAssertionAxiom axiom) {
OWLObjectPropertyExpression prop = axiom.getProperty();
if (hasOntology.equals(prop)) {
IRI iri;
// The axiom subject gets a +1 in its library score.
OWLIndividual ind = axiom.getSubject();
if (!ind.isAnonymous()) {
iri = ind.asOWLNamedIndividual().getIRI();
checkScores(iri)[0]++;
}
// The axiom object gets a +1 in its ontology score.
ind = axiom.getObject();
if (!ind.isAnonymous()) {
iri = ind.asOWLNamedIndividual().getIRI();
checkScores(iri)[1]++;
}
} else if (isOntologyOf.equals(prop)) {
IRI iri;
// The axiom subject gets a +1 in its ontology score.
OWLIndividual ind = axiom.getSubject();
if (!ind.isAnonymous()) {
iri = ind.asOWLNamedIndividual().getIRI();
checkScores(iri)[1]++;
}
// The axiom object gets a +1 in its library score.
ind = axiom.getObject();
if (!ind.isAnonymous()) {
iri = ind.asOWLNamedIndividual().getIRI();
checkScores(iri)[0]++;
}
}
}
};
// First pass to determine the types.
for (OWLOntology o : closure) for (OWLAxiom ax : o.getAxioms()) ax.accept(scanner);
// Then populate on the registry
OWLDataFactory df = OWLManager.getOWLDataFactory();
for (IRI iri : candidateTypes.keySet()) {
int[] scores = candidateTypes.get(iri);
if (scores != null && (scores[0] > 0 || scores[1] > 0)) {
if (scores[0] > 0 && scores[1] == 0)
population.put(iri, riFactory.createLibrary(df.getOWLNamedIndividual(iri)));
else if (scores[0] == 0 && scores[1] > 0)
population.put(iri, riFactory.createRegistryOntology(df.getOWLNamedIndividual(iri)));
}
// else log.warn("Unable to determine type for registry item {}", iri);
}
for (OWLOntology oReg : registryOntologies) {
try {
results.add(populateRegistry(oReg));
} catch (RegistryContentException e) {
log.error("An error occurred while populating an ontology registry.", e);
}
}
return results;
}
use of org.semanticweb.owlapi.model.OWLOntology in project stanbol by apache.
the class ODPRegistryCacheManager method retrieveRemoteResource.
/**
* Gets the remote ontology and saves it locally
*
* @param uri
* @return
* @throws OWLOntologyCreationException
* @throws UnknownOWLOntologyException
* @throws OWLOntologyStorageException
*/
private static synchronized OWLOntology retrieveRemoteResource(URI uri) throws OWLOntologyCreationException, UnknownOWLOntologyException, OWLOntologyStorageException {
manager.setSilentMissingImportsHandling(true);
manager.addMissingImportListener(new MissingImportListener() {
public void importMissing(MissingImportEvent arg0) {
if (!getUnresolvedURIs().contains(arg0.getImportedOntologyURI()))
getUnresolvedURIs().add(arg0.getImportedOntologyURI().toURI());
}
});
manager.addOntologyLoaderListener(new OWLOntologyLoaderListener() {
@Override
public void startedLoadingOntology(LoadingStartedEvent event) {
// Nothing to do
}
@Override
public void finishedLoadingOntology(LoadingFinishedEvent event) {
URI onturi = event.getDocumentIRI().toURI();
if (event.getException() != null) {
getUnresolvedURIs().add(onturi);
LoggerFactory.getLogger(ODPRegistryCacheManager.class).warn("Failed to resolve ontology at " + onturi + " . Skipping.", event.getException());
return;
}
try {
if (!uris.containsKey(onturi)) {
cacheOntology(onturi, newFile(), manager.getOntology(event.getOntologyID()));
}
} catch (UnknownOWLOntologyException e) {
LoggerFactory.getLogger(ODPRegistryCacheManager.class).warn("Failed to cache ontology at " + onturi + " . Skipping.", e);
getUnresolvedURIs().add(onturi);
} catch (OWLOntologyStorageException e) {
LoggerFactory.getLogger(ODPRegistryCacheManager.class).warn("Failed to cache ontology at " + onturi + " . Skipping.", e);
getUnresolvedURIs().add(onturi);
}
}
});
OWLOntology ont;
try {
ont = manager.loadOntologyFromOntologyDocument(IRI.create(uri));
} catch (OWLOntologyAlreadyExistsException e) {
ont = manager.getOntology(e.getOntologyID().getOntologyIRI());
}
File file = newFile();
cacheOntology(uri, file, ont);
return ont;
}
use of org.semanticweb.owlapi.model.OWLOntology in project stanbol by apache.
the class TestClerezzaProvider method testVersionIRISplit.
@Test
public void testVersionIRISplit() throws Exception {
// Check the first version
InputStream data = getClass().getResourceAsStream(fn1);
OWLOntologyID key1 = ontologyProvider.loadInStore(data, RDF_XML, true);
assertNotNull(key1);
assertFalse(key1.isAnonymous());
// Check the second version
data = getClass().getResourceAsStream(fn2);
OWLOntologyID key2 = ontologyProvider.loadInStore(data, RDF_XML, true);
assertNotNull(key2);
assertFalse(key2.isAnonymous());
// Must be 2 different graphs
assertFalse(key1.equals(key2));
assertEquals(2, ontologyProvider.listPrimaryKeys().size());
// Ontologies must not be tainting each other
// Don't use keys any more here. They're not the real public keys!
Set<OWLOntology> oAll = new HashSet<OWLOntology>();
for (OWLOntologyID key : ontologyProvider.listPrimaryKeys()) {
log.info("Found public key {}", key);
oAll.add(ontologyProvider.getStoredOntology(key, OWLOntology.class, true));
}
Iterator<OWLOntology> it = oAll.iterator();
OWLOntology o1 = it.next();
OWLOntology o2 = it.next();
for (OWLNamedIndividual i : o1.getIndividualsInSignature()) {
Set<OWLClassExpression> tAll = i.getTypes(oAll), t1 = i.getTypes(o1), t2 = i.getTypes(o2);
// Should be obvious from the OWL API
assertTrue(tAll.containsAll(t1));
// Should be obvious from the OWL API
assertTrue(tAll.containsAll(t2));
assertFalse(t1.containsAll(t2));
assertFalse(t2.containsAll(t1));
}
}
use of org.semanticweb.owlapi.model.OWLOntology in project stanbol by apache.
the class TestOntologyReconciliation method anonymousFromURLWithCustomKeys.
/*
* If an anonymous ontology is loaded from a URL and at least one override is provided, the first override
* should be the primary key, while everything else, including the URL, should be an alias for that key.
*/
@Test
public void anonymousFromURLWithCustomKeys() throws Exception {
OWLOntologyID myKey = new OWLOntologyID(IRI.create("nameless"), IRI.create(getClass().getCanonicalName() + "#anonymousFromURLWithCustomKeys()"));
OWLOntologyID alias = new OWLOntologyID(IRI.create("nameless"), IRI.create(getClass().getCanonicalName() + "#anonymousFromURLWithCustomKeys().alias"));
URL url = getClass().getResource(location_nameless);
OWLOntologyManager onMgr = OWLManager.createOWLOntologyManager();
OWLOntology o1 = onMgr.loadOntologyFromOntologyDocument(IRI.create(url));
assertTrue(o1.isAnonymous());
OWLOntologyID key = ontologyProvider.loadInStore(IRI.create(url), RDF_XML, false, Origin.create(myKey), Origin.create(alias));
assertNotNull(key);
assertFalse(key.isAnonymous());
assertEquals(myKey, key);
log.info("Anonymous ontology loaded with non-anonymous public key {} (submitted)", key);
// should have 2 aliases: the physical location and the submitted alias.
assertEquals(2, ontologyProvider.listAliases(key).size());
for (OWLOntologyID al : ontologyProvider.listAliases(key)) {
assertFalse(al.isAnonymous());
log.info("Named alias detected {}", al);
}
// Now retrieve using the alias...
OWLOntology o2 = ontologyProvider.getStoredOntology(alias, OWLOntology.class, false);
assertTrue(o2.isAnonymous());
// Cannot equal OWLOntology objects
assertEquals(o1.getAxioms(), o2.getAxioms());
// ... and using the physical IRI
o2 = ontologyProvider.getStoredOntology(new OWLOntologyID(IRI.create(url)), OWLOntology.class, false);
assertTrue(o2.isAnonymous());
// Cannot equal OWLOntology objects
assertEquals(o1.getAxioms(), o2.getAxioms());
}
Aggregations