use of org.semanticweb.owlapi.model.OWLOntologyManager in project stanbol by apache.
the class HermitReasoningServiceTest method testEnrichWithRules.
private void testEnrichWithRules(String testID, String rulesID, String testExpectedID) {
log.info("Testing the task ENRICH with rules");
OWLOntologyManager manager = TestData.manager;
// We prepare the input ontology
try {
OWLOntology testOntology = manager.createOntology();
OWLOntologyID testOntologyID = testOntology.getOntologyID();
log.debug("Created test ontology with ID: {}", testOntologyID);
OWLImportsDeclaration importTest = TestData.factory.getOWLImportsDeclaration(IRI.create(testID));
manager.applyChange(new AddImport(testOntology, importTest));
Set<SWRLRule> rules = manager.getOntology(IRI.create(rulesID)).getAxioms(AxiomType.SWRL_RULE);
// Maybe we want to see the list of rules
if (log.isDebugEnabled()) {
log.debug("List of {} rules: ", rules.size());
TestUtils.debug(rules, log);
}
log.debug("We add the rules to the ontology");
manager.addAxioms(manager.getOntology(testOntologyID), rules);
// Maybe we want to see what is in before
if (log.isDebugEnabled())
log.debug("Content of the input is:");
TestUtils.debug(manager.getOntology(testOntologyID), log);
// Now we test the method
log.debug("Running HermiT");
Set<OWLAxiom> inferred = this.theinstance.runTask(ReasoningService.Tasks.ENRICH, manager.getOntology(testOntologyID));
// Maybe we want to see the inferred axiom list
if (log.isDebugEnabled()) {
log.debug("{} inferred axioms:", inferred.size());
TestUtils.debug(inferred, log);
}
Set<OWLLogicalAxiom> expectedAxioms = manager.getOntology(IRI.create(testExpectedID)).getLogicalAxioms();
Set<OWLAxiom> missing = new HashSet<OWLAxiom>();
for (OWLAxiom expected : expectedAxioms) {
// We consider here all kind of axioms
if (!inferred.contains(expected)) {
log.error("missing expected axiom: {}", expected);
missing.add(expected);
}
}
assertTrue(missing.isEmpty());
// We want to remove the ontology from the manager
manager.removeOntology(testOntology);
} catch (OWLOntologyCreationException e) {
log.error("An {} have been thrown while creating the input ontology for test", e.getClass());
assertTrue(false);
} catch (ReasoningServiceException e) {
log.error("An {} have been thrown while executing the reasoning", e.getClass());
assertTrue(false);
} catch (InconsistentInputException e) {
log.error("An {} have been thrown while executing the reasoning", e.getClass());
assertTrue(false);
} catch (UnsupportedTaskException e) {
log.error("An {} have been thrown while executing the reasoning", e.getClass());
assertTrue(false);
}
}
use of org.semanticweb.owlapi.model.OWLOntologyManager in project stanbol by apache.
the class RegistryManagerImpl method activate.
protected void activate(Dictionary<String, Object> configuration) {
// Parse configuration.
try {
lazyLoading = (Boolean) (configuration.get(RegistryManager.LAZY_LOADING));
} catch (Exception ex) {
lazyLoading = _LAZY_LOADING_DEFAULT;
}
try {
retainIncomplete = (Boolean) (configuration.get(RegistryManager.RETAIN_INCOMPLETE));
} catch (Exception ex) {
retainIncomplete = _RETAIN_INCOMPLETE_DEFAULT;
}
Object obj = configuration.get(RegistryManager.REGISTRY_LOCATIONS);
if (obj instanceof String[])
locations = (String[]) obj;
else if (obj instanceof String)
locations = new String[] { (String) obj };
if (locations == null)
locations = new String[] { "stanbol_network.owl" };
Object cachingPolicy = configuration.get(RegistryManager.CACHING_POLICY);
if (cachingPolicy == null) {
this.cachingPolicyString = _CACHING_POLICY_DEFAULT.name();
} else {
this.cachingPolicyString = cachingPolicy.toString();
}
final IRI[] offlineResources;
if (this.offline != null) {
List<IRI> paths = offline.getOntologySourceLocations();
if (paths != null)
offlineResources = paths.toArray(new IRI[0]);
else
// There are no offline paths.
offlineResources = new IRI[0];
} else
// There's no offline configuration at all.
offlineResources = new IRI[0];
// Used only for creating the registry model, do not use for caching.
OWLOntologyManager mgr = OWLOntologyManagerFactory.createOWLOntologyManager(offlineResources);
OWLOntologyLoaderConfiguration conf = new OWLOntologyLoaderConfiguration();
// If we are retaining incomplete registries, do not throw exceptions if imports fail.
conf.setSilentMissingImportsHandling(retainIncomplete);
// Load registries
Set<OWLOntology> regOnts = new HashSet<OWLOntology>();
for (String loc : locations) {
try {
IRI iri = IRI.create(loc);
OWLOntologyDocumentSource src = null;
OWLOntology o = null;
if (iri.isAbsolute())
src = new IRIDocumentSource(iri);
else {
// Relative IRI : use data file provider
log.debug("Found relative IRI {} . Will try to retrieve from data file providers.", iri);
Map<String, String> info = new HashMap<String, String>();
if (dataFileProvider != null && dataFileProvider.isAvailable(null, loc, info))
src = new StreamDocumentSource(dataFileProvider.getInputStream(null, loc, info));
}
if (src != null)
o = mgr.loadOntologyFromOntologyDocument(src, conf);
if (o != null)
regOnts.add(o);
else
log.warn("Failed to obtain OWL ontology from resource {}", loc);
} catch (OWLOntologyAlreadyExistsException e) {
log.info("Skipping cached ontology {}.", e.getOntologyID());
continue;
} catch (OWLOntologyCreationException e) {
log.warn("Failed to load ontology " + loc + " - Skipping...", e);
continue;
} catch (Exception e) {
log.warn("Invalid registry configuration " + loc + " - Skipping...", e);
continue;
}
}
// Create and set the cache.
if (cachingPolicyString.equals(CachingPolicy.CENTRALISED.name())) {
// this.cache = OWLOntologyManagerFactory.createOWLOntologyManager(offlineResources);
if (cache == null) {
log.warn("Caching policy is set as Centralised, but no ontology provider is supplied. Will use new in-memory tcProvider.");
cache = new ClerezzaOntologyProvider(TcManager.getInstance(), offline, Parser.getInstance());
}
// else sta bene cosi'
} else if (cachingPolicyString.equals(CachingPolicy.DISTRIBUTED.name())) {
this.cache = null;
}
riFactory = new RegistryItemFactoryImpl(cache);
// Build the model.
createModel(regOnts);
// Set the cache on libraries.
Set<RegistryItem> visited = new HashSet<RegistryItem>();
for (Registry reg : getRegistries()) for (RegistryItem child : reg.getChildren()) if (!visited.contains(child)) {
if (child instanceof Library) {
if (this.cache != null)
((Library) child).setCache(this.cache);
else
((Library) child).setCache(new ClerezzaOntologyProvider(TcManager.getInstance(), offline, Parser.getInstance()));
}
visited.add(child);
}
if (isLazyLoading()) {
// Nothing to do about it at the moment.
} else {
loadEager();
}
}
use of org.semanticweb.owlapi.model.OWLOntologyManager in project stanbol by apache.
the class SessionImpl method attachScopeImportsOwlApi.
private void attachScopeImportsOwlApi(OWLOntology target, org.semanticweb.owlapi.model.IRI prefix) {
if (!attachedScopes.isEmpty()) {
String scopePrefix = prefix.toString();
scopePrefix = scopePrefix.substring(0, scopePrefix.lastIndexOf("/" + shortName + "/")) + "/ontology/";
List<OWLOntologyChange> changes = new LinkedList<OWLOntologyChange>();
OWLOntologyManager ontologyManager = target.getOWLOntologyManager();
OWLDataFactory df = ontologyManager.getOWLDataFactory();
// Add import declarations for attached scopes.
for (String scopeID : attachedScopes) {
org.semanticweb.owlapi.model.IRI physIRI = org.semanticweb.owlapi.model.IRI.create(scopePrefix + scopeID);
changes.add(new AddImport(target, df.getOWLImportsDeclaration(physIRI)));
}
// Commit
ontologyManager.applyChanges(changes);
}
}
use of org.semanticweb.owlapi.model.OWLOntologyManager in project stanbol by apache.
the class TestOntologyCollectors method spacePreservesImports.
@Test
public void spacePreservesImports() throws Exception {
InputStream content = getClass().getResourceAsStream("/ontologies/characters_all.owl");
URL url = getClass().getResource("/ontologies/characters_all.owl");
OWLOntologyManager mgr = OWLManager.createOWLOntologyManager();
mgr.addOntologyLoaderListener(new OWLOntologyLoaderListener() {
@Override
public void startedLoadingOntology(LoadingStartedEvent arg0) {
}
@Override
public void finishedLoadingOntology(LoadingFinishedEvent arg0) {
log.info((arg0.isSuccessful() ? "Loaded" : "Failed") + (arg0.isImported() ? " imported " : " ") + "ontology " + arg0.getDocumentIRI());
}
});
mgr.addIRIMapper(new PhonyIRIMapper(null));
File f = new File(url.toURI());
OntologyInputSource<OWLOntology> src = new ParentPathInputSource(f, mgr);
// OntologyInputSource<OWLOntology> src = new RootOntologyIRISource(IRI.create(f), mgr);
// OntologyInputSource<OWLOntology> src = new OntologyContentInputSource(content,mgr);
OWLOntology original = src.getRootOntology();
Assert.assertNotNull(original);
OntologySpace spc = new CustomSpaceImpl("Test", scopeNs, ontologyProvider);
spc.addOntology(src);
}
use of org.semanticweb.owlapi.model.OWLOntologyManager in project stanbol by apache.
the class TestOntologyReconciliation method anonymousFromURL.
/*
* Anonymous ontologies loaded from a URL must reconcile with a public key that matches the resource URL
* in its ontology IRI.
*/
@Test
public void anonymousFromURL() throws Exception {
URL in = getClass().getResource(location_nameless);
OWLOntologyManager onMgr = OWLManager.createOWLOntologyManager();
OWLOntology o1 = onMgr.loadOntologyFromOntologyDocument(IRI.create(in));
assertTrue(o1.isAnonymous());
// in = getClass().getResourceAsStream(location); // use if stream cannot be reset
OWLOntologyID key = ontologyProvider.loadInStore(IRI.create(in), RDF_XML, false);
assertNotNull(key);
assertFalse(key.isAnonymous());
log.info("Anonymous ontology loaded with non-anonymous public key {}", key);
OWLOntology o2 = ontologyProvider.getStoredOntology(key, OWLOntology.class, false);
assertTrue(o2.isAnonymous());
// Cannot equal OWLOntology objects
assertEquals(o1.getAxioms(), o2.getAxioms());
}
Aggregations