Search in sources :

Example 56 with Triple

use of org.apache.clerezza.commons.rdf.Triple in project stanbol by apache.

the class AbstractOntologyCollectorImpl method exportToGraph.

/**
 * This method has no conversion calls, to it can be invoked by subclasses that wish to modify it
 * afterwards.
 *
 * @param merge
 * @return
 */
protected Graph exportToGraph(boolean merge, org.semanticweb.owlapi.model.IRI prefix) {
    // if (merge) throw new UnsupportedOperationException(
    // "Merge not implemented yet for Clerezza triple collections.");
    long before = System.currentTimeMillis();
    // No need to store, give it a name, or anything.
    Graph root = new SimpleGraph();
    IRI iri = new IRI(prefix + _id);
    // Add the import declarations for directly managed ontologies.
    if (root != null) {
        // Set the ontology ID
        root.add(new TripleImpl(iri, RDF.type, OWL.Ontology));
        if (merge) {
            log.warn("Merging of Clerezza triple collections is only implemented one level down. Import statements will be preserved for further levels.");
            Iterator<Triple> it;
            Set<RDFTerm> importTargets = new HashSet<RDFTerm>();
            for (OWLOntologyID ontologyId : managedOntologies) {
                ImmutableGraph g = getOntology(ontologyId, ImmutableGraph.class, false);
                root.addAll(g);
                it = g.filter(null, OWL.imports, null);
                while (it.hasNext()) {
                    org.semanticweb.owlapi.model.IRI tgt;
                    RDFTerm r = it.next().getObject();
                    try {
                        if (r instanceof IRI)
                            tgt = org.semanticweb.owlapi.model.IRI.create(((IRI) r).getUnicodeString());
                        else if (r instanceof Literal)
                            tgt = org.semanticweb.owlapi.model.IRI.create(((Literal) r).getLexicalForm());
                        else
                            tgt = org.semanticweb.owlapi.model.IRI.create(r.toString());
                        tgt = URIUtils.sanitize(tgt);
                        importTargets.add(new IRI(tgt.toString()));
                    } catch (Exception ex) {
                        log.error("FAILED to obtain import target from resource {}", r);
                        continue;
                    }
                }
                it = g.filter(null, RDF.type, OWL.Ontology);
                while (it.hasNext()) {
                    BlankNodeOrIRI ontology = it.next().getSubject();
                    log.debug("Removing all triples related to {} from {}", ontology, iri);
                    Iterator<Triple> it2 = g.filter(ontology, null, null);
                    while (it2.hasNext()) root.remove(it2.next());
                }
                /*
                     * Reinstate import statements, though. If imported ontologies were not merged earlier, we
                     * are not doing it now anyway.
                     */
                for (RDFTerm target : importTargets) root.add(new TripleImpl(iri, OWL.imports, target));
            }
        } else {
            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) {
                org.semanticweb.owlapi.model.IRI physIRI = // .create(base + ontologyId.getVersionIRI()));
                org.semanticweb.owlapi.model.IRI.create(base + OntologyUtils.encode(ontologyId));
                root.add(new TripleImpl(iri, OWL.imports, new IRI(physIRI.toString())));
            }
        }
        log.debug("Clerezza export of {} completed in {} ms.", getID(), System.currentTimeMillis() - before);
    }
    return root;
}
Also used : IRI(org.apache.clerezza.commons.rdf.IRI) BlankNodeOrIRI(org.apache.clerezza.commons.rdf.BlankNodeOrIRI) BlankNodeOrIRI(org.apache.clerezza.commons.rdf.BlankNodeOrIRI) RDFTerm(org.apache.clerezza.commons.rdf.RDFTerm) OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) MissingOntologyException(org.apache.stanbol.ontologymanager.servicesapi.collector.MissingOntologyException) OWLOntologyAlreadyExistsException(org.semanticweb.owlapi.model.OWLOntologyAlreadyExistsException) UnmodifiableOntologyCollectorException(org.apache.stanbol.ontologymanager.servicesapi.collector.UnmodifiableOntologyCollectorException) OntologyCollectorModificationException(org.apache.stanbol.ontologymanager.servicesapi.collector.OntologyCollectorModificationException) Triple(org.apache.clerezza.commons.rdf.Triple) ImmutableGraph(org.apache.clerezza.commons.rdf.ImmutableGraph) IndexedGraph(org.apache.stanbol.commons.indexedgraph.IndexedGraph) SimpleGraph(org.apache.clerezza.commons.rdf.impl.utils.simple.SimpleGraph) Graph(org.apache.clerezza.commons.rdf.Graph) OWLOntologyID(org.semanticweb.owlapi.model.OWLOntologyID) Literal(org.apache.clerezza.commons.rdf.Literal) SimpleGraph(org.apache.clerezza.commons.rdf.impl.utils.simple.SimpleGraph) TripleImpl(org.apache.clerezza.commons.rdf.impl.utils.TripleImpl) ImmutableGraph(org.apache.clerezza.commons.rdf.ImmutableGraph) HashSet(java.util.HashSet)

Example 57 with Triple

use of org.apache.clerezza.commons.rdf.Triple in project stanbol by apache.

the class ScopeImpl method exportToGraph.

/**
 * Get a Clerezza {@link Graph} representation of the scope.
 *
 * @param merge
 *            if true the core and custom spaces will be recursively merged with the scope graph,
 *            otherwise owl:imports statements will be added.
 * @return the RDF representation of the scope as a modifiable graph.
 */
protected Graph exportToGraph(boolean merge, org.semanticweb.owlapi.model.IRI universalPrefix) {
    // No need to store, give it a name, or anything.
    Graph root = new SimpleGraph();
    IRI iri = new IRI(universalPrefix + getID());
    if (root != null) {
        // Set the ontology ID
        root.add(new TripleImpl(iri, RDF.type, OWL.Ontology));
        if (merge) {
            ImmutableGraph custom, core;
            // Get the subjects of "bad" triples (those with subjects of type owl:Ontology).
            Iterator<Triple> it;
            Set<BlankNodeOrIRI> ontologies = new HashSet<BlankNodeOrIRI>();
            Set<RDFTerm> importTargets = new HashSet<RDFTerm>();
            custom = this.getCustomSpace().export(ImmutableGraph.class, merge);
            // root.addAll(space);
            it = custom.filter(null, RDF.type, OWL.Ontology);
            while (it.hasNext()) ontologies.add(it.next().getSubject());
            it = custom.filter(null, OWL.imports, null);
            while (it.hasNext()) importTargets.add(it.next().getObject());
            core = this.getCoreSpace().export(ImmutableGraph.class, merge);
            // root.addAll(space);
            it = core.filter(null, RDF.type, OWL.Ontology);
            while (it.hasNext()) ontologies.add(it.next().getSubject());
            it = core.filter(null, OWL.imports, null);
            while (it.hasNext()) importTargets.add(it.next().getObject());
            // Make sure the scope itself is not in the "bad" subjects.
            ontologies.remove(iri);
            for (BlankNodeOrIRI nl : ontologies) log.debug("{} -related triples will not be added to {}", nl, iri);
            // Merge the two spaces, skipping the "bad" triples.
            log.debug("Merging custom space of {}.", getID());
            for (Triple t : custom) if (!ontologies.contains(t.getSubject()))
                root.add(t);
            log.debug("Merging core space of {}.", getID());
            for (Triple t : core) if (!ontologies.contains(t.getSubject()))
                root.add(t);
            /*
                 * Reinstate import statements, though. If imported ontologies were not merged earlier, we are
                 * not doing it now anyway.
                 */
            for (RDFTerm target : importTargets) root.add(new TripleImpl(iri, OWL.imports, target));
        } else {
            IRI physIRI = new IRI(universalPrefix.toString() + this.getID() + "/" + SpaceType.CUSTOM.getIRISuffix());
            root.add(new TripleImpl(iri, OWL.imports, physIRI));
            physIRI = new IRI(universalPrefix.toString() + this.getID() + "/" + SpaceType.CORE.getIRISuffix());
            root.add(new TripleImpl(iri, OWL.imports, physIRI));
        }
    }
    return root;
}
Also used : Triple(org.apache.clerezza.commons.rdf.Triple) IRI(org.apache.clerezza.commons.rdf.IRI) BlankNodeOrIRI(org.apache.clerezza.commons.rdf.BlankNodeOrIRI) ImmutableGraph(org.apache.clerezza.commons.rdf.ImmutableGraph) SimpleGraph(org.apache.clerezza.commons.rdf.impl.utils.simple.SimpleGraph) Graph(org.apache.clerezza.commons.rdf.Graph) SimpleGraph(org.apache.clerezza.commons.rdf.impl.utils.simple.SimpleGraph) BlankNodeOrIRI(org.apache.clerezza.commons.rdf.BlankNodeOrIRI) RDFTerm(org.apache.clerezza.commons.rdf.RDFTerm) TripleImpl(org.apache.clerezza.commons.rdf.impl.utils.TripleImpl) ImmutableGraph(org.apache.clerezza.commons.rdf.ImmutableGraph) HashSet(java.util.HashSet)

Example 58 with Triple

use of org.apache.clerezza.commons.rdf.Triple in project stanbol by apache.

the class TestStorage method storageOnScopeCreation.

@Test
public void storageOnScopeCreation() throws Exception {
    assertEquals(1, ontologyProvider.getStore().listGraphs().size());
    // This one has an import that we want to hijack locally, so we use the ParentPathInputSource.
    OntologyInputSource<?> ois = new ParentPathInputSource(new File(getClass().getResource("/ontologies/minorcharacters.owl").toURI()));
    Scope sc = onManager.createOntologyScope(scopeId, ois);
    Set<Triple> triples = new HashSet<Triple>();
    for (IRI iri : ontologyProvider.getStore().listGraphs()) {
        log.info("{}", iri.toString());
        IRI entity = new IRI(Constants.PEANUTS_MINOR_BASE + "#" + Constants.truffles);
        ImmutableGraph ctx = new GraphNode(entity, ontologyProvider.getStore().getGraph(iri)).getNodeContext();
        Iterator<Triple> it = ctx.iterator();
        while (it.hasNext()) triples.add(it.next());
    }
    assertFalse(ontologyProvider.getStore().listGraphs().isEmpty());
    assertEquals(3, triples.size());
}
Also used : Triple(org.apache.clerezza.commons.rdf.Triple) IRI(org.apache.clerezza.commons.rdf.IRI) Scope(org.apache.stanbol.ontologymanager.servicesapi.scope.Scope) ParentPathInputSource(org.apache.stanbol.ontologymanager.sources.owlapi.ParentPathInputSource) GraphNode(org.apache.clerezza.rdf.utils.GraphNode) File(java.io.File) ImmutableGraph(org.apache.clerezza.commons.rdf.ImmutableGraph) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 59 with Triple

use of org.apache.clerezza.commons.rdf.Triple 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 60 with Triple

use of org.apache.clerezza.commons.rdf.Triple 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)

Aggregations

Triple (org.apache.clerezza.commons.rdf.Triple)151 IRI (org.apache.clerezza.commons.rdf.IRI)88 BlankNodeOrIRI (org.apache.clerezza.commons.rdf.BlankNodeOrIRI)84 RDFTerm (org.apache.clerezza.commons.rdf.RDFTerm)70 Graph (org.apache.clerezza.commons.rdf.Graph)45 TripleImpl (org.apache.clerezza.commons.rdf.impl.utils.TripleImpl)41 HashSet (java.util.HashSet)34 Literal (org.apache.clerezza.commons.rdf.Literal)30 ArrayList (java.util.ArrayList)27 Lock (java.util.concurrent.locks.Lock)21 HashMap (java.util.HashMap)20 SimpleGraph (org.apache.clerezza.commons.rdf.impl.utils.simple.SimpleGraph)19 OWLOntologyID (org.semanticweb.owlapi.model.OWLOntologyID)19 IndexedGraph (org.apache.stanbol.commons.indexedgraph.IndexedGraph)15 PlainLiteralImpl (org.apache.clerezza.commons.rdf.impl.utils.PlainLiteralImpl)12 Test (org.junit.Test)12 EngineException (org.apache.stanbol.enhancer.servicesapi.EngineException)10 ImmutableGraph (org.apache.clerezza.commons.rdf.ImmutableGraph)9 GraphNode (org.apache.clerezza.rdf.utils.GraphNode)8 IOException (java.io.IOException)7