use of org.semanticweb.owlapi.model.OWLOntologyManager in project stanbol by apache.
the class TestOntologyReconciliation method versioned.
/*
* Two versioned ontologies that share their ontology IRI are stored with separate public keys that manage
* their full ontology IDs.
*/
@Test
public void versioned() throws Exception {
OWLOntologyManager onMgr = OWLManager.createOWLOntologyManager();
// Load the first ontology
String location = "/ontologies/versiontest_v1.owl";
InputStream in = getClass().getResourceAsStream(location);
in.mark(Integer.MAX_VALUE);
// Keep tack of the original in a separate ontology.
OWLOntology o1 = onMgr.loadOntologyFromOntologyDocument(in);
assertFalse(o1.isAnonymous());
OWLOntologyID id1 = o1.getOntologyID();
in.reset();
// in = getClass().getResourceAsStream(location); // use if stream cannot be reset
OWLOntologyID key = ontologyProvider.loadInStore(in, RDF_XML, false);
assertNotNull(key);
assertFalse(key.isAnonymous());
log.info("Named ontology loaded with public key {}", key);
assertEquals(id1, key);
log.info(" -- (matches actual ontology ID).");
// The unversioned ID should return no match...
OWLOntologyID unversioned = new OWLOntologyID(key.getOntologyIRI());
assertSame(Status.NO_MATCH, ontologyProvider.getStatus(unversioned));
// ...but a query on the available versions should return only the public key.
Set<OWLOntologyID> versions = ontologyProvider.listVersions(key.getOntologyIRI());
assertFalse(versions.isEmpty());
assertSame(1, versions.size());
assertTrue(versions.contains(id1));
// Now load the second version.
location = "/ontologies/versiontest_v2.owl";
in = getClass().getResourceAsStream(location);
in.mark(Integer.MAX_VALUE);
// Keep tack of the original in a separate ontology.
OWLOntology o2 = onMgr.loadOntologyFromOntologyDocument(in);
assertFalse(o2.isAnonymous());
OWLOntologyID id2 = o2.getOntologyID();
in.reset();
// in = getClass().getResourceAsStream(location); // use if stream cannot be reset
key = ontologyProvider.loadInStore(in, RDF_XML, false);
assertNotNull(key);
assertFalse(key.isAnonymous());
log.info("Named ontology loaded with public key {}", key);
assertEquals(id2, key);
log.info(" -- (matches actual ontology ID).");
// The unversioned ID should still return no match...
assertSame(Status.NO_MATCH, ontologyProvider.getStatus(unversioned));
// ...but a query on the available versions should return both public keys now.
versions = ontologyProvider.listVersions(key.getOntologyIRI());
assertFalse(versions.isEmpty());
assertSame(2, versions.size());
assertTrue(versions.contains(id1));
assertTrue(versions.contains(id2));
// Check that axioms match for version 1
log.info("Version 1:");
OWLOntology o1_1 = ontologyProvider.getStoredOntology(id1, OWLOntology.class, false);
assertFalse(o1_1.isAnonymous());
// Cannot equal OWLOntology objects
assertEquals(o1.getOntologyID(), o1_1.getOntologyID());
log.warn("Plain OWL API seems to be failing to preserve owl:versionInfo. Will test non-annotation axioms only.");
assertEquals(o1.getTBoxAxioms(false), o1_1.getTBoxAxioms(false));
log.info(" -- TBox axiom check successful.");
assertEquals(o1.getABoxAxioms(false), o1_1.getABoxAxioms(false));
log.info(" -- ABox axiom check successful.");
// Check that axioms match for version 2 (therefore differ from each other)
log.info("Version 2:");
OWLOntology o2_1 = ontologyProvider.getStoredOntology(id2, OWLOntology.class, false);
assertFalse(o2_1.isAnonymous());
// Cannot equal OWLOntology objects
assertEquals(o2.getOntologyID(), o2_1.getOntologyID());
log.warn("Plain OWL API seems to be failing to preserve owl:versionInfo. Will test non-annotation axioms only.");
assertEquals(o2.getTBoxAxioms(false), o2_1.getTBoxAxioms(false));
log.info(" -- TBox axiom check successful.");
assertEquals(o2.getABoxAxioms(false), o2_1.getABoxAxioms(false));
log.info(" -- ABox axiom check successful.");
// There should be no aliases.
assertSame(0, ontologyProvider.listAliases(unversioned).size());
assertSame(0, ontologyProvider.listAliases(id1).size());
assertSame(0, ontologyProvider.listAliases(id2).size());
}
use of org.semanticweb.owlapi.model.OWLOntologyManager in project stanbol by apache.
the class TestOntologyReconciliation method versionedOnlyFromStream.
/*
* If an ontology has no ontology IRI but does have a version IRI, it should still be possible to load it,
* but the version IRI must be erased.
*/
@Test
public void versionedOnlyFromStream() throws Exception {
String location = "/ontologies/naming/versionedonly.owl";
InputStream in = getClass().getResourceAsStream(location);
in.mark(Integer.MAX_VALUE);
OWLOntologyManager onMgr = OWLManager.createOWLOntologyManager();
OWLOntology o1 = onMgr.loadOntologyFromOntologyDocument(in);
// Ensure that the OWL API erases the version IRI.
assertTrue(o1.isAnonymous());
assertNull(o1.getOntologyID().getVersionIRI());
in.reset();
// in = getClass().getResourceAsStream(location); // use if stream cannot be reset
// The public key must be non-anonymous nonetheless.
OWLOntologyID key = ontologyProvider.loadInStore(in, RDF_XML, false);
assertNotNull(key);
assertFalse(key.isAnonymous());
assertNull(key.getVersionIRI());
log.info("Wrongly versioned ontology loaded with public key {}", key);
assertFalse(o1.equals(key));
OWLOntology o1_1 = ontologyProvider.getStoredOntology(key, OWLOntology.class, false);
assertNotNull(o1_1);
assertTrue(o1_1.isAnonymous());
assertNull(o1_1.getOntologyID().getVersionIRI());
// Cannot equal two OWLOntology objects, especially if anonymous.
// Check that they match axiom-wise.
log.warn("Plain OWL API seems to be failing to preserve owl:versionInfo. Will test non-annotation axioms only.");
assertEquals(o1.getTBoxAxioms(false), o1_1.getTBoxAxioms(false));
log.info(" -- TBox axiom check successful.");
assertEquals(o1.getABoxAxioms(false), o1_1.getABoxAxioms(false));
log.info(" -- ABox axiom check successful.");
// No aliases should have been created.
assertSame(0, ontologyProvider.listAliases(key).size());
}
use of org.semanticweb.owlapi.model.OWLOntologyManager in project stanbol by apache.
the class ScopeManagerImpl method activate.
/**
* Called within both OSGi and non-OSGi environments.
*
* @param configuration
* @throws IOException
*/
protected void activate(Dictionary<String, Object> configuration) throws IOException {
long before = System.currentTimeMillis();
// Assign singleton instance
me = this;
// Parse configuration
if (offline != null)
ontonetNS = offline.getDefaultOntologyNetworkNamespace();
scopeRegistryId = (String) configuration.get(ScopeManager.ID_SCOPE_REGISTRY);
if (scopeRegistryId == null)
scopeRegistryId = _ID_SCOPE_REGISTRY_DEFAULT;
configPath = (String) configuration.get(ScopeManager.CONFIG_ONTOLOGY_PATH);
if (configPath == null)
configPath = _CONFIG_ONTOLOGY_PATH_DEFAULT;
// Bind components, starting with the local directories.
List<String> dirs = new ArrayList<String>();
try {
for (IRI iri : offline.getOntologySourceLocations()) dirs.add(iri.toString());
} catch (NullPointerException ex) {
// Ok, go empty
}
bindResources();
// String tfile = (String) configuration.get(CONFIG_FILE_PATH);
// if (tfile != null) this.configPath = tfile;
// String tns = (String) configuration.get(KRES_NAMESPACE);
// if (tns != null) this.kresNs = tns;
// configPath = (String) configuration.get(CONFIG_FILE_PATH);
// If there is no configuration file, just start with an empty scope set
Object connectivityPolicy = configuration.get(ScopeManager.CONNECTIVITY_POLICY);
if (connectivityPolicy == null) {
this.connectivityPolicyString = _CONNECTIVITY_POLICY_DEFAULT;
} else {
this.connectivityPolicyString = connectivityPolicy.toString();
}
String configPath = getOntologyNetworkConfigurationPath();
if (configPath != null && !configPath.trim().isEmpty()) {
OWLOntology oConf = null;
OWLOntologyManager tempMgr = OWLOntologyManagerFactory.createOWLOntologyManager(offline.getOntologySourceLocations().toArray(new IRI[0]));
OWLOntologyDocumentSource oConfSrc = null;
try {
log.debug("Try to load the configuration ontology from a local bundle relative path");
InputStream is = this.getClass().getResourceAsStream(configPath);
oConfSrc = new StreamDocumentSource(is);
} catch (Exception e1) {
try {
log.debug("Cannot load from a local bundle relative path", e1);
log.debug("Try to load the configuration ontology resolving the given IRI");
IRI iri = IRI.create(configPath);
if (!iri.isAbsolute())
throw new Exception("IRI seems to be not absolute! value was: " + iri.toQuotedString());
oConfSrc = new IRIDocumentSource(iri);
} catch (Exception e) {
try {
log.debug("Cannot load from the web", e1);
log.debug("Try to load the configuration ontology as full local file path");
oConfSrc = new FileDocumentSource(new File(configPath));
} catch (Exception e2) {
log.error("Cannot load the configuration ontology from parameter value: " + configPath, e2);
}
}
}
if (oConfSrc == null) {
log.warn("No ONM configuration file found at path " + configPath + ". Starting with blank scope set.");
} else {
try {
oConf = tempMgr.loadOntologyFromOntologyDocument(oConfSrc);
} catch (OWLOntologyCreationException e) {
log.error("Cannot create the configuration ontology", e);
}
}
// Create and populate the scopes from the config ontology.
bootstrapOntologyNetwork(oConf);
} else {
// No ontology supplied. Access the local graph
rebuildScopes();
}
log.debug(ScopeManager.class + " activated. Time : {} ms.", System.currentTimeMillis() - before);
}
use of org.semanticweb.owlapi.model.OWLOntologyManager in project webprotege by protegeproject.
the class RevisionManagerImpl method getOntologyManagerForRevision.
@Nonnull
@Override
public OWLOntologyManager getOntologyManagerForRevision(@Nonnull RevisionNumber revision) {
try {
OWLOntologyManager manager = WebProtegeOWLManager.createOWLOntologyManager();
final OWLOntologyID singletonOntologyId = new OWLOntologyID();
for (Revision rev : revisionStore.getRevisions()) {
if (rev.getRevisionNumber().compareTo(revision) <= 0) {
for (OWLOntologyChangeRecord record : rev) {
// Anonymous ontologies are not handled nicely at all.
OWLOntologyChangeRecord normalisedChangeRecord = normaliseChangeRecord(record, singletonOntologyId);
OWLOntologyID ontologyId = normalisedChangeRecord.getOntologyID();
if (!manager.contains(ontologyId)) {
manager.createOntology(ontologyId);
}
OWLOntologyChange change = normalisedChangeRecord.createOntologyChange(manager);
manager.applyChange(change);
}
}
}
if (manager.getOntologies().isEmpty()) {
// No revisions exported. Just create an empty ontology
manager.createOntology();
}
return manager;
} catch (OWLOntologyCreationException e) {
throw new RuntimeException("Problem creating ontology: " + e);
}
}
use of org.semanticweb.owlapi.model.OWLOntologyManager in project webprotege by protegeproject.
the class ProjectDownloader method exportProjectRevision.
private void exportProjectRevision(@Nonnull String projectDisplayName, @Nonnull RevisionNumber revisionNumber, @Nonnull OutputStream outputStream, @Nonnull DownloadFormat format) throws IOException, OWLOntologyStorageException {
RevisionManager revisionManager = project.getRevisionManager();
OWLOntologyManager manager = revisionManager.getOntologyManagerForRevision(revisionNumber);
OWLOntologyID rootOntologyId = project.getRootOntology().getOntologyID();
Optional<OWLOntology> revisionRootOntology = getOntologyFromManager(manager, rootOntologyId);
if (revisionRootOntology.isPresent()) {
saveImportsClosureToStream(projectDisplayName, revisionRootOntology.get(), format, outputStream, revisionNumber);
} else {
// An error - no flipping ontology!
logger.info("Project download failed because the root ontology could not be retrived from the manager. Project: ", project.getProjectId());
throw new RuntimeException("The ontology could not be downloaded from " + applicationName + ". Please contact the administrator.");
}
}
Aggregations