use of org.apache.stanbol.commons.owl.PhonyIRIMapper in project stanbol by apache.
the class ClerezzaOntologyProvider method toOWLOntology.
/**
* @param graphName
* @param forceMerge
* if set to false, the selected import management policy will be applied.
* @return
* @throws OWLOntologyCreationException
*/
protected OWLOntology toOWLOntology(IRI graphName, boolean forceMerge) throws OWLOntologyCreationException {
log.debug("Exporting graph to OWLOntology");
log.debug(" -- ImmutableGraph name : {}", graphName);
OWLOntologyManager mgr = OWLManager.createOWLOntologyManager();
// Never try to import
mgr.addIRIMapper(new PhonyIRIMapper(Collections.<org.semanticweb.owlapi.model.IRI>emptySet()));
Set<OWLOntologyID> loaded = new HashSet<OWLOntologyID>();
Graph graph = store.getGraph(graphName);
IRI ontologyId = null;
// Get the id of this ontology.
Iterator<Triple> itt = graph.filter(null, RDF.type, OWL.Ontology);
if (itt.hasNext()) {
BlankNodeOrIRI nl = itt.next().getSubject();
if (nl instanceof IRI)
ontologyId = (IRI) nl;
}
List<OWLOntologyID> revImps = new Stack<OWLOntologyID>();
List<OWLOntologyID> lvl1 = new Stack<OWLOntologyID>();
fillImportsReverse(keymap.getReverseMapping(graphName), revImps, lvl1);
// If not set to merge (either by policy of by force), adopt the set import policy.
if (!forceMerge && !ImportManagementPolicy.MERGE.equals(getImportManagementPolicy())) {
OWLOntology o = OWLAPIToClerezzaConverter.clerezzaGraphToOWLOntology(graph, mgr);
// TODO make it not flat.
// Examining the reverse imports stack will flatten all imports.
List<OWLOntologyChange> changes = new ArrayList<OWLOntologyChange>();
OWLDataFactory df = OWLManager.getOWLDataFactory();
List<OWLOntologyID> listToUse;
switch(getImportManagementPolicy()) {
case FLATTEN:
listToUse = revImps;
break;
case PRESERVE:
listToUse = lvl1;
break;
default:
listToUse = lvl1;
break;
}
for (OWLOntologyID ref : listToUse) if (!loaded.contains(ref) && !ref.equals(keymap.getReverseMapping(graphName))) {
changes.add(new AddImport(o, df.getOWLImportsDeclaration(ref.getOntologyIRI())));
loaded.add(ref);
}
o.getOWLOntologyManager().applyChanges(changes);
return o;
} else {
// If there is just the root ontology, convert it straight away.
if (revImps.size() == 1 && revImps.contains(graphName)) {
OWLOntology o = OWLAPIToClerezzaConverter.clerezzaGraphToOWLOntology(graph, mgr);
return o;
}
// FIXME when there's more than one ontology, this way of merging them seems inefficient...
Graph tempGraph = new IndexedGraph();
// The set of triples that will be excluded from the merge
Set<Triple> exclusions = new HashSet<Triple>();
// Examine all reverse imports
for (OWLOntologyID ref : revImps) if (!loaded.contains(ref)) {
// Get the triples
Graph imported = // store.getTriples(ref);
getStoredOntology(getKey(ref), Graph.class, false);
// For each owl:Ontology
Iterator<Triple> remove = imported.filter(null, RDF.type, OWL.Ontology);
while (remove.hasNext()) {
BlankNodeOrIRI subj = remove.next().getSubject();
/*
* If it's not the root ontology, trash all its triples. If the root ontology is
* anonymous, all ontology annotations are to be trashed without distinction.
*/
if (ontologyId == null || !subj.equals(ontologyId)) {
Iterator<Triple> it = imported.filter(subj, null, null);
while (it.hasNext()) {
Triple t = it.next();
exclusions.add(t);
}
}
}
Iterator<Triple> it = imported.iterator();
while (it.hasNext()) {
Triple t = it.next();
if (!exclusions.contains(t))
tempGraph.add(t);
}
loaded.add(ref);
}
// online.
return OWLAPIToClerezzaConverter.clerezzaGraphToOWLOntology(tempGraph, mgr);
}
}
use of org.apache.stanbol.commons.owl.PhonyIRIMapper 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.apache.stanbol.commons.owl.PhonyIRIMapper in project stanbol by apache.
the class OWLAPIToClerezzaConverter method clerezzaGraphToOWLOntology.
/**
* Converts a Clerezza {@link Graph} to an OWL API {@link OWLOntology}.
*
* @param mGraph
* {@link org.apache.clerezza.commons.rdf.Graph}
* @return the equivalent OWL API {@link OWLOntology}.
*/
public static OWLOntology clerezzaGraphToOWLOntology(org.apache.clerezza.commons.rdf.Graph graph) {
OWLOntologyManager mgr = OWLManager.createOWLOntologyManager();
// Never try to import
mgr.addIRIMapper(new PhonyIRIMapper(Collections.<IRI>emptySet()));
return clerezzaGraphToOWLOntology(graph, mgr);
}
Aggregations