use of org.semanticweb.owlapi.model.OWLImportsDeclaration in project stanbol by apache.
the class HermitReasoningServiceTest method testEnrich.
/**
* We may want to test this method with more then 1 ontology. This is why the implementation is in
* aprivate method. This method tests if all the logical axioms in testExpectedID ontology are inferences
* of the testID ontology.
*
* TODO: This method is the same as testClassify(String,String), with the only difference - the task
* called. We may want to have this procedure isolated.
*
* @param testID
* // The ID of the ontology to be the input (loaded in the TestData.manager)
* @param testExpectedID
* // The ID of the ontology which contains logical axioms expected in the result
*/
private void testEnrich(String testID, String testExpectedID) {
log.info("Testing the ENRICH task");
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));
// Maybe we want to see what is in before
if (log.isDebugEnabled())
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()) {
TestUtils.debug(inferred, log);
}
Set<OWLLogicalAxiom> expectedAxioms = manager.getOntology(IRI.create(testExpectedID)).getLogicalAxioms();
Set<OWLAxiom> missing = new HashSet<OWLAxiom>();
for (OWLAxiom expected : expectedAxioms) {
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.OWLImportsDeclaration 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.OWLImportsDeclaration 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.OWLImportsDeclaration 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.OWLImportsDeclaration in project stanbol by apache.
the class TestOntologyLibrary method testLibrarySourceCreation.
/**
* Tests the creation of an ontology input source from a single library. Because the test is run offline,
* import statements might be file URIs, so tests should not fail on this.
*
* @throws Exception
*/
@Test
public void testLibrarySourceCreation() throws Exception {
IRI localTestRegistry = IRI.create(getClass().getResource(registryResourcePath));
Dictionary<String, Object> regmanConf = new Hashtable<String, Object>();
regmanConf.put(RegistryManager.REGISTRY_LOCATIONS, new String[] { localTestRegistry.toString() });
// Instantiating the registry manager will also load the registry data.
regMgr = new RegistryManagerImpl(offline, new ClerezzaOntologyProvider(tcManager, offline, parser), regmanConf);
assertNotNull(regMgr);
// Now use this registry manager to instantiate an input source.
OntologyInputSource<OWLOntology> src = new LibrarySource(Locations.LIBRARY_TEST1, regMgr, virginOntologyManager);
OWLOntology o = src.getRootOntology();
boolean hasImporting = false, hasImported = false;
for (OWLImportsDeclaration ax : o.getImportsDeclarations()) {
// Since we added a local IRI mapping, import statements might be using file: IRIs instead of
// HTTP, in which case IRI equality would fail. So it is enough here to just check the filename.
String tmpstr = ax.getIRI().toString();
if (!hasImporting && tmpstr.endsWith("characters_all.owl"))
hasImporting = true;
else if (!hasImported && tmpstr.endsWith("maincharacters.owl"))
hasImported = true;
if (hasImporting && hasImported)
break;
}
assertTrue(hasImporting);
assertTrue(hasImported);
}
Aggregations