Search in sources :

Example 31 with OWLOntologyID

use of org.semanticweb.owlapi.model.OWLOntologyID in project stanbol by apache.

the class AbstractOntologyCollectorImpl method exportToOWLOntology.

/**
 * This method has no conversion calls, to it can be invoked by subclasses that wish to modify it
 * afterwards.
 *
 * FIXME not merging yet FIXME not including imported ontologies unless they are merged *before* storage.
 *
 * @param merge
 * @return
 */
protected OWLOntology exportToOWLOntology(boolean merge, org.semanticweb.owlapi.model.IRI prefix) {
    long before = System.currentTimeMillis();
    // Create a new ontology
    OWLOntology root;
    OWLOntologyManager ontologyManager = OWLManager.createOWLOntologyManager();
    org.semanticweb.owlapi.model.IRI iri = org.semanticweb.owlapi.model.IRI.create(prefix + _id);
    try {
        root = ontologyManager.createOntology(iri);
    } catch (OWLOntologyAlreadyExistsException e) {
        // It should be impossible, but just in case.
        ontologyManager.removeOntology(ontologyManager.getOntology(iri));
        try {
            root = ontologyManager.createOntology(iri);
        } catch (OWLOntologyAlreadyExistsException e1) {
            root = ontologyManager.getOntology(iri);
        } catch (OWLOntologyCreationException e1) {
            log.error("Failed to assemble root ontology for scope " + iri, e);
            root = null;
        }
    } catch (OWLOntologyCreationException e) {
        log.error("Failed to assemble root ontology for scope " + _id, e);
        root = null;
    }
    // Add the import declarations for directly managed ontologies.
    if (root != null) {
        if (merge) {
            final Set<OWLOntology> set = new HashSet<OWLOntology>();
            log.debug("Merging {} with its imports.", root);
            set.add(root);
            for (OWLOntologyID ontologyId : managedOntologies) {
                log.debug("Merging {} with {}.", ontologyId, root);
                set.add(getOntology(ontologyId, OWLOntology.class, true));
            }
            OWLOntologySetProvider provider = new OWLOntologySetProvider() {

                @Override
                public Set<OWLOntology> getOntologies() {
                    return set;
                }
            };
            OWLOntologyMerger merger = new OWLOntologyMerger(provider);
            try {
                root = merger.createMergedOntology(OWLManager.createOWLOntologyManager(), iri);
            } catch (OWLOntologyCreationException e) {
                log.error("Failed to merge imports for ontology " + iri, e);
                root = null;
            }
        } else {
            // Add the import declarations for directly managed ontologies.
            List<OWLOntologyChange> changes = new LinkedList<OWLOntologyChange>();
            OWLDataFactory df = ontologyManager.getOWLDataFactory();
            String base = prefix + getID();
            for (int i = 0; i < backwardPathLength; i++) base = URIUtils.upOne(URI.create(base)).toString();
            base += "/";
            // The key set of managedOntologies contains the ontology IRIs, not their storage keys.
            for (OWLOntologyID ontologyId : managedOntologies) {
                // XXX some day the versionIRI will be the only physical reference for the ontology
                org.semanticweb.owlapi.model.IRI physIRI = org.semanticweb.owlapi.model.IRI.create(base + OntologyUtils.encode(ontologyId));
                changes.add(new AddImport(root, df.getOWLImportsDeclaration(physIRI)));
            }
            ontologyManager.applyChanges(changes);
        }
    }
    log.debug("OWL export of {} completed in {} ms.", getID(), System.currentTimeMillis() - before);
    return root;
}
Also used : OWLOntologyMerger(org.semanticweb.owlapi.util.OWLOntologyMerger) AddImport(org.semanticweb.owlapi.model.AddImport) OWLOntologyAlreadyExistsException(org.semanticweb.owlapi.model.OWLOntologyAlreadyExistsException) LinkedList(java.util.LinkedList) OWLOntologySetProvider(org.semanticweb.owlapi.model.OWLOntologySetProvider) OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) OWLOntologyChange(org.semanticweb.owlapi.model.OWLOntologyChange) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) OWLOntologyID(org.semanticweb.owlapi.model.OWLOntologyID) OWLOntologyManager(org.semanticweb.owlapi.model.OWLOntologyManager) OWLDataFactory(org.semanticweb.owlapi.model.OWLDataFactory) HashSet(java.util.HashSet)

Example 32 with OWLOntologyID

use of org.semanticweb.owlapi.model.OWLOntologyID in project stanbol by apache.

the class TestClerezzaProvider method testVersionIRISplit.

@Test
public void testVersionIRISplit() throws Exception {
    // Check the first version
    InputStream data = getClass().getResourceAsStream(fn1);
    OWLOntologyID key1 = ontologyProvider.loadInStore(data, RDF_XML, true);
    assertNotNull(key1);
    assertFalse(key1.isAnonymous());
    // Check the second version
    data = getClass().getResourceAsStream(fn2);
    OWLOntologyID key2 = ontologyProvider.loadInStore(data, RDF_XML, true);
    assertNotNull(key2);
    assertFalse(key2.isAnonymous());
    // Must be 2 different graphs
    assertFalse(key1.equals(key2));
    assertEquals(2, ontologyProvider.listPrimaryKeys().size());
    // Ontologies must not be tainting each other
    // Don't use keys any more here. They're not the real public keys!
    Set<OWLOntology> oAll = new HashSet<OWLOntology>();
    for (OWLOntologyID key : ontologyProvider.listPrimaryKeys()) {
        log.info("Found public key {}", key);
        oAll.add(ontologyProvider.getStoredOntology(key, OWLOntology.class, true));
    }
    Iterator<OWLOntology> it = oAll.iterator();
    OWLOntology o1 = it.next();
    OWLOntology o2 = it.next();
    for (OWLNamedIndividual i : o1.getIndividualsInSignature()) {
        Set<OWLClassExpression> tAll = i.getTypes(oAll), t1 = i.getTypes(o1), t2 = i.getTypes(o2);
        // Should be obvious from the OWL API
        assertTrue(tAll.containsAll(t1));
        // Should be obvious from the OWL API
        assertTrue(tAll.containsAll(t2));
        assertFalse(t1.containsAll(t2));
        assertFalse(t2.containsAll(t1));
    }
}
Also used : InputStream(java.io.InputStream) OWLOntologyID(org.semanticweb.owlapi.model.OWLOntologyID) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) OWLNamedIndividual(org.semanticweb.owlapi.model.OWLNamedIndividual) OWLClassExpression(org.semanticweb.owlapi.model.OWLClassExpression) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 33 with OWLOntologyID

use of org.semanticweb.owlapi.model.OWLOntologyID in project stanbol by apache.

the class GraphMultiplexer method getHandles.

@Override
public Set<OntologyCollector> getHandles(OWLOntologyID publicKey) {
    Set<OntologyCollector> handles = new HashSet<OntologyCollector>();
    Set<OWLOntologyID> aliases = listAliases(publicKey);
    aliases.add(publicKey);
    for (OWLOntologyID alias : aliases) {
        IRI ontologyId = buildResource(alias);
        for (Iterator<Triple> it = meta.filter(null, MANAGES_URIREF, ontologyId); it.hasNext(); ) {
            BlankNodeOrIRI sub = it.next().getSubject();
            if (sub instanceof IRI)
                checkHandle((IRI) sub, handles);
            else
                throw new InvalidMetaGraphStateException(sub + " is not a valid ontology collector identifer.");
        }
        for (Iterator<Triple> it = meta.filter(ontologyId, IS_MANAGED_BY_URIREF, null); it.hasNext(); ) {
            RDFTerm obj = it.next().getObject();
            if (obj instanceof IRI)
                checkHandle((IRI) obj, handles);
            else
                throw new InvalidMetaGraphStateException(obj + " is not a valid ontology collector identifer.");
        }
    }
    return handles;
// throw new UnsupportedOperationException("Not implemented yet.");
}
Also used : Triple(org.apache.clerezza.commons.rdf.Triple) IRI(org.apache.clerezza.commons.rdf.IRI) BlankNodeOrIRI(org.apache.clerezza.commons.rdf.BlankNodeOrIRI) OntologyCollector(org.apache.stanbol.ontologymanager.servicesapi.collector.OntologyCollector) OWLOntologyID(org.semanticweb.owlapi.model.OWLOntologyID) BlankNodeOrIRI(org.apache.clerezza.commons.rdf.BlankNodeOrIRI) RDFTerm(org.apache.clerezza.commons.rdf.RDFTerm) HashSet(java.util.HashSet)

Example 34 with OWLOntologyID

use of org.semanticweb.owlapi.model.OWLOntologyID in project stanbol by apache.

the class GraphMultiplexer method clearDependencies.

@Override
public void clearDependencies(OWLOntologyID dependent) {
    if (dependent == null)
        throw new IllegalArgumentException("dependent cannot be null");
    log.debug("Clearing dependencies for {}", dependent);
    Set<Triple> dependencies = new HashSet<Triple>();
    synchronized (meta) {
        Set<OWLOntologyID> aliases = listAliases(dependent);
        aliases.add(dependent);
        for (OWLOntologyID depalias : aliases) {
            IRI dep = buildResource(depalias);
            Iterator<Triple> it = meta.filter(dep, DEPENDS_ON_URIREF, null);
            while (it.hasNext()) {
                Triple t = it.next();
                dependencies.add(t);
                log.debug(" ... Set {} as a dependency to remove.", t.getObject());
            }
            it = meta.filter(null, HAS_DEPENDENT_URIREF, dep);
            while (it.hasNext()) {
                Triple t = it.next();
                dependencies.add(t);
                log.debug(" ... Set {} as a dependency to remove.", t.getSubject());
            }
        }
        meta.removeAll(dependencies);
    }
    log.debug(" ... DONE clearing dependencies.");
}
Also used : Triple(org.apache.clerezza.commons.rdf.Triple) IRI(org.apache.clerezza.commons.rdf.IRI) BlankNodeOrIRI(org.apache.clerezza.commons.rdf.BlankNodeOrIRI) OWLOntologyID(org.semanticweb.owlapi.model.OWLOntologyID) HashSet(java.util.HashSet)

Example 35 with OWLOntologyID

use of org.semanticweb.owlapi.model.OWLOntologyID in project stanbol by apache.

the class GraphMultiplexer method listAliases.

/*
     * XXX see if we can use reasoners, either live or by caching materialisations.
     */
protected Set<OWLOntologyID> listAliases(OWLOntologyID publicKey) {
    if (publicKey == null || publicKey.isAnonymous())
        throw new IllegalArgumentException("Cannot locate aliases for null or anonymous public keys.");
    Set<OWLOntologyID> aliases = new HashSet<OWLOntologyID>();
    IRI ont = buildResource(publicKey);
    // Forwards
    for (Iterator<Triple> it = meta.filter(ont, OWL.sameAs, null); it.hasNext(); ) {
        RDFTerm r = it.next().getObject();
        if (r instanceof IRI)
            aliases.add(buildPublicKey((IRI) r));
    }
    // Backwards
    for (Iterator<Triple> it = meta.filter(null, OWL.sameAs, ont); it.hasNext(); ) {
        RDFTerm r = it.next().getSubject();
        if (r instanceof IRI)
            aliases.add(buildPublicKey((IRI) r));
    }
    return aliases;
}
Also used : Triple(org.apache.clerezza.commons.rdf.Triple) IRI(org.apache.clerezza.commons.rdf.IRI) BlankNodeOrIRI(org.apache.clerezza.commons.rdf.BlankNodeOrIRI) OWLOntologyID(org.semanticweb.owlapi.model.OWLOntologyID) RDFTerm(org.apache.clerezza.commons.rdf.RDFTerm) HashSet(java.util.HashSet)

Aggregations

OWLOntologyID (org.semanticweb.owlapi.model.OWLOntologyID)101 Test (org.junit.Test)32 OWLOntology (org.semanticweb.owlapi.model.OWLOntology)32 OWLOntologyCreationException (org.semanticweb.owlapi.model.OWLOntologyCreationException)23 HashSet (java.util.HashSet)22 OWLOntologyManager (org.semanticweb.owlapi.model.OWLOntologyManager)21 InputStream (java.io.InputStream)20 BlankNodeOrIRI (org.apache.clerezza.commons.rdf.BlankNodeOrIRI)19 Triple (org.apache.clerezza.commons.rdf.Triple)19 IRI (org.semanticweb.owlapi.model.IRI)18 IRI (org.apache.clerezza.commons.rdf.IRI)17 ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)16 RDFTerm (org.apache.clerezza.commons.rdf.RDFTerm)13 IOException (java.io.IOException)12 UnmodifiableOntologyCollectorException (org.apache.stanbol.ontologymanager.servicesapi.collector.UnmodifiableOntologyCollectorException)12 WebApplicationException (javax.ws.rs.WebApplicationException)11 Graph (org.apache.clerezza.commons.rdf.Graph)10 AddImport (org.semanticweb.owlapi.model.AddImport)10 Path (javax.ws.rs.Path)9 OntologySpace (org.apache.stanbol.ontologymanager.servicesapi.scope.OntologySpace)8