use of org.semanticweb.owlapi.model.OWLOntologyID in project stanbol by apache.
the class AbstractOntologyCollectorImpl method exportToOWLOntology.
/**
* This method has no conversion calls, to it can be invoked by subclasses that wish to modify it
* afterwards.
*
* FIXME not merging yet FIXME not including imported ontologies unless they are merged *before* storage.
*
* @param merge
* @return
*/
protected OWLOntology exportToOWLOntology(boolean merge, org.semanticweb.owlapi.model.IRI prefix) {
long before = System.currentTimeMillis();
// Create a new ontology
OWLOntology root;
OWLOntologyManager ontologyManager = OWLManager.createOWLOntologyManager();
org.semanticweb.owlapi.model.IRI iri = org.semanticweb.owlapi.model.IRI.create(prefix + _id);
try {
root = ontologyManager.createOntology(iri);
} catch (OWLOntologyAlreadyExistsException e) {
// It should be impossible, but just in case.
ontologyManager.removeOntology(ontologyManager.getOntology(iri));
try {
root = ontologyManager.createOntology(iri);
} catch (OWLOntologyAlreadyExistsException e1) {
root = ontologyManager.getOntology(iri);
} catch (OWLOntologyCreationException e1) {
log.error("Failed to assemble root ontology for scope " + iri, e);
root = null;
}
} catch (OWLOntologyCreationException e) {
log.error("Failed to assemble root ontology for scope " + _id, e);
root = null;
}
// Add the import declarations for directly managed ontologies.
if (root != null) {
if (merge) {
final Set<OWLOntology> set = new HashSet<OWLOntology>();
log.debug("Merging {} with its imports.", root);
set.add(root);
for (OWLOntologyID ontologyId : managedOntologies) {
log.debug("Merging {} with {}.", ontologyId, root);
set.add(getOntology(ontologyId, OWLOntology.class, true));
}
OWLOntologySetProvider provider = new OWLOntologySetProvider() {
@Override
public Set<OWLOntology> getOntologies() {
return set;
}
};
OWLOntologyMerger merger = new OWLOntologyMerger(provider);
try {
root = merger.createMergedOntology(OWLManager.createOWLOntologyManager(), iri);
} catch (OWLOntologyCreationException e) {
log.error("Failed to merge imports for ontology " + iri, e);
root = null;
}
} else {
// Add the import declarations for directly managed ontologies.
List<OWLOntologyChange> changes = new LinkedList<OWLOntologyChange>();
OWLDataFactory df = ontologyManager.getOWLDataFactory();
String base = prefix + getID();
for (int i = 0; i < backwardPathLength; i++) base = URIUtils.upOne(URI.create(base)).toString();
base += "/";
// The key set of managedOntologies contains the ontology IRIs, not their storage keys.
for (OWLOntologyID ontologyId : managedOntologies) {
// XXX some day the versionIRI will be the only physical reference for the ontology
org.semanticweb.owlapi.model.IRI physIRI = org.semanticweb.owlapi.model.IRI.create(base + OntologyUtils.encode(ontologyId));
changes.add(new AddImport(root, df.getOWLImportsDeclaration(physIRI)));
}
ontologyManager.applyChanges(changes);
}
}
log.debug("OWL export of {} completed in {} ms.", getID(), System.currentTimeMillis() - before);
return root;
}
use of org.semanticweb.owlapi.model.OWLOntologyID 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.OWLOntologyID in project stanbol by apache.
the class GraphMultiplexer method getHandles.
@Override
public Set<OntologyCollector> getHandles(OWLOntologyID publicKey) {
Set<OntologyCollector> handles = new HashSet<OntologyCollector>();
Set<OWLOntologyID> aliases = listAliases(publicKey);
aliases.add(publicKey);
for (OWLOntologyID alias : aliases) {
IRI ontologyId = buildResource(alias);
for (Iterator<Triple> it = meta.filter(null, MANAGES_URIREF, ontologyId); it.hasNext(); ) {
BlankNodeOrIRI sub = it.next().getSubject();
if (sub instanceof IRI)
checkHandle((IRI) sub, handles);
else
throw new InvalidMetaGraphStateException(sub + " is not a valid ontology collector identifer.");
}
for (Iterator<Triple> it = meta.filter(ontologyId, IS_MANAGED_BY_URIREF, null); it.hasNext(); ) {
RDFTerm obj = it.next().getObject();
if (obj instanceof IRI)
checkHandle((IRI) obj, handles);
else
throw new InvalidMetaGraphStateException(obj + " is not a valid ontology collector identifer.");
}
}
return handles;
// throw new UnsupportedOperationException("Not implemented yet.");
}
use of org.semanticweb.owlapi.model.OWLOntologyID in project stanbol by apache.
the class GraphMultiplexer method clearDependencies.
@Override
public void clearDependencies(OWLOntologyID dependent) {
if (dependent == null)
throw new IllegalArgumentException("dependent cannot be null");
log.debug("Clearing dependencies for {}", dependent);
Set<Triple> dependencies = new HashSet<Triple>();
synchronized (meta) {
Set<OWLOntologyID> aliases = listAliases(dependent);
aliases.add(dependent);
for (OWLOntologyID depalias : aliases) {
IRI dep = buildResource(depalias);
Iterator<Triple> it = meta.filter(dep, DEPENDS_ON_URIREF, null);
while (it.hasNext()) {
Triple t = it.next();
dependencies.add(t);
log.debug(" ... Set {} as a dependency to remove.", t.getObject());
}
it = meta.filter(null, HAS_DEPENDENT_URIREF, dep);
while (it.hasNext()) {
Triple t = it.next();
dependencies.add(t);
log.debug(" ... Set {} as a dependency to remove.", t.getSubject());
}
}
meta.removeAll(dependencies);
}
log.debug(" ... DONE clearing dependencies.");
}
use of org.semanticweb.owlapi.model.OWLOntologyID in project stanbol by apache.
the class GraphMultiplexer method listAliases.
/*
* XXX see if we can use reasoners, either live or by caching materialisations.
*/
protected Set<OWLOntologyID> listAliases(OWLOntologyID publicKey) {
if (publicKey == null || publicKey.isAnonymous())
throw new IllegalArgumentException("Cannot locate aliases for null or anonymous public keys.");
Set<OWLOntologyID> aliases = new HashSet<OWLOntologyID>();
IRI ont = buildResource(publicKey);
// Forwards
for (Iterator<Triple> it = meta.filter(ont, OWL.sameAs, null); it.hasNext(); ) {
RDFTerm r = it.next().getObject();
if (r instanceof IRI)
aliases.add(buildPublicKey((IRI) r));
}
// Backwards
for (Iterator<Triple> it = meta.filter(null, OWL.sameAs, ont); it.hasNext(); ) {
RDFTerm r = it.next().getSubject();
if (r instanceof IRI)
aliases.add(buildPublicKey((IRI) r));
}
return aliases;
}
Aggregations