Search in sources :

Example 1 with PhonyIRIMapper

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);
    }
}
Also used : IRI(org.apache.clerezza.commons.rdf.IRI) BlankNodeOrIRI(org.apache.clerezza.commons.rdf.BlankNodeOrIRI) ArrayList(java.util.ArrayList) BlankNodeOrIRI(org.apache.clerezza.commons.rdf.BlankNodeOrIRI) AddImport(org.semanticweb.owlapi.model.AddImport) Stack(java.util.Stack) Triple(org.apache.clerezza.commons.rdf.Triple) IndexedGraph(org.apache.stanbol.commons.indexedgraph.IndexedGraph) Graph(org.apache.clerezza.commons.rdf.Graph) OWLOntologyChange(org.semanticweb.owlapi.model.OWLOntologyChange) OWLOntologyID(org.semanticweb.owlapi.model.OWLOntologyID) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) PhonyIRIMapper(org.apache.stanbol.commons.owl.PhonyIRIMapper) Iterator(java.util.Iterator) OWLOntologyManager(org.semanticweb.owlapi.model.OWLOntologyManager) OWLDataFactory(org.semanticweb.owlapi.model.OWLDataFactory) IndexedGraph(org.apache.stanbol.commons.indexedgraph.IndexedGraph) HashSet(java.util.HashSet)

Example 2 with PhonyIRIMapper

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);
}
Also used : InputStream(java.io.InputStream) URL(java.net.URL) CustomSpaceImpl(org.apache.stanbol.ontologymanager.multiplexer.clerezza.impl.CustomSpaceImpl) OWLOntologyLoaderListener(org.semanticweb.owlapi.model.OWLOntologyLoaderListener) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) OntologySpace(org.apache.stanbol.ontologymanager.servicesapi.scope.OntologySpace) PhonyIRIMapper(org.apache.stanbol.commons.owl.PhonyIRIMapper) ParentPathInputSource(org.apache.stanbol.ontologymanager.sources.owlapi.ParentPathInputSource) OWLOntologyManager(org.semanticweb.owlapi.model.OWLOntologyManager) File(java.io.File) Test(org.junit.Test)

Example 3 with PhonyIRIMapper

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);
}
Also used : IRI(org.semanticweb.owlapi.model.IRI) PhonyIRIMapper(org.apache.stanbol.commons.owl.PhonyIRIMapper) OWLOntologyManager(org.semanticweb.owlapi.model.OWLOntologyManager)

Aggregations

PhonyIRIMapper (org.apache.stanbol.commons.owl.PhonyIRIMapper)3 OWLOntologyManager (org.semanticweb.owlapi.model.OWLOntologyManager)3 OWLOntology (org.semanticweb.owlapi.model.OWLOntology)2 File (java.io.File)1 InputStream (java.io.InputStream)1 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 Iterator (java.util.Iterator)1 Stack (java.util.Stack)1 BlankNodeOrIRI (org.apache.clerezza.commons.rdf.BlankNodeOrIRI)1 Graph (org.apache.clerezza.commons.rdf.Graph)1 IRI (org.apache.clerezza.commons.rdf.IRI)1 Triple (org.apache.clerezza.commons.rdf.Triple)1 IndexedGraph (org.apache.stanbol.commons.indexedgraph.IndexedGraph)1 CustomSpaceImpl (org.apache.stanbol.ontologymanager.multiplexer.clerezza.impl.CustomSpaceImpl)1 OntologySpace (org.apache.stanbol.ontologymanager.servicesapi.scope.OntologySpace)1 ParentPathInputSource (org.apache.stanbol.ontologymanager.sources.owlapi.ParentPathInputSource)1 Test (org.junit.Test)1 AddImport (org.semanticweb.owlapi.model.AddImport)1