Search in sources :

Example 96 with Triple

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

the class GraphMultiplexer method getDependents.

@Override
public Set<OWLOntologyID> getDependents(OWLOntologyID dependency) {
    Set<OWLOntologyID> dependents = new HashSet<OWLOntologyID>();
    IRI dep = buildResource(dependency);
    log.debug("Getting depents for {}", dependency);
    synchronized (meta) {
        Iterator<Triple> it = meta.filter(null, DEPENDS_ON_URIREF, dep);
        while (it.hasNext()) {
            RDFTerm sub = it.next().getSubject();
            log.debug(" ... found {} (inverse).", sub);
            if (sub instanceof IRI)
                dependents.add(buildPublicKey((IRI) sub));
            else
                log.warn(" ... Unexpected literal value!");
        }
        it = meta.filter(dep, HAS_DEPENDENT_URIREF, null);
        while (it.hasNext()) {
            RDFTerm obj = it.next().getObject();
            log.debug(" ... found {} (inverse).", obj);
            if (obj instanceof IRI)
                dependents.add(buildPublicKey((IRI) obj));
            else
                log.warn(" ... Unexpected literal value!");
        }
    }
    return dependents;
}
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)

Example 97 with Triple

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

the class GraphMultiplexer method onOntologyAdded.

@Override
public void onOntologyAdded(OntologyCollector collector, OWLOntologyID addedOntology) {
    // When the ontology provider hears an ontology has been added to a collector, it has to register this
    // into the metadata graph.
    // log.info("Heard addition of ontology {} to collector {}", addedOntology, collector.getID());
    // log.info("This ontology is stored as {}", getKey(addedOntology));
    String colltype = "";
    if (// Cannot be
    collector instanceof Scope)
        // Cannot be
        colltype = Scope.shortName + "/";
    else if (collector instanceof OntologySpace)
        colltype = OntologySpace.shortName + "/";
    else if (collector instanceof Session)
        colltype = Session.shortName + "/";
    IRI c = new IRI(_NS_STANBOL_INTERNAL + colltype + collector.getID());
    IRI u = // keymap.getMapping(addedOntology);
    buildResource(addedOntology);
    // TODO OntologyProvider should not be aware of scopes, spaces or sessions. Move elsewhere.
    boolean hasValues = false;
    log.debug("Ontology {}", addedOntology);
    log.debug("-- is already managed by the following collectors :");
    for (Iterator<Triple> it = meta.filter(u, IS_MANAGED_BY_URIREF, null); it.hasNext(); ) {
        hasValues = true;
        log.debug("-- {}", it.next().getObject());
    }
    for (Iterator<Triple> it = meta.filter(null, MANAGES_URIREF, u); it.hasNext(); ) {
        hasValues = true;
        log.debug("-- {} (inverse)", it.next().getSubject());
    }
    if (!hasValues)
        log.debug("-- <none>");
    // Add both inverse triples. This graph has to be traversed efficiently, no need for reasoners.
    IRI predicate1 = null, predicate2 = null;
    if (collector instanceof OntologySpace) {
        predicate1 = MANAGES_URIREF;
        predicate2 = IS_MANAGED_BY_URIREF;
    } else if (collector instanceof Session) {
        // TODO implement model for sessions.
        predicate1 = MANAGES_URIREF;
        predicate2 = IS_MANAGED_BY_URIREF;
    } else {
        log.error("Unrecognized ontology collector type {} for \"{}\". Aborting.", collector.getClass(), collector.getID());
        return;
    }
    if (u != null)
        synchronized (meta) {
            Triple t;
            if (predicate1 != null) {
                t = new TripleImpl(c, predicate1, u);
                boolean b = meta.add(t);
                log.debug((b ? "Successful" : "Redundant") + " addition of meta triple");
                log.debug("-- {} ", t);
            }
            if (predicate2 != null) {
                t = new TripleImpl(u, predicate2, c);
                boolean b = meta.add(t);
                log.debug((b ? "Successful" : "Redundant") + " addition of meta triple");
                log.debug("-- {} ", t);
            }
        }
}
Also used : Triple(org.apache.clerezza.commons.rdf.Triple) IRI(org.apache.clerezza.commons.rdf.IRI) BlankNodeOrIRI(org.apache.clerezza.commons.rdf.BlankNodeOrIRI) Scope(org.apache.stanbol.ontologymanager.servicesapi.scope.Scope) OntologySpace(org.apache.stanbol.ontologymanager.servicesapi.scope.OntologySpace) TripleImpl(org.apache.clerezza.commons.rdf.impl.utils.TripleImpl) Session(org.apache.stanbol.ontologymanager.servicesapi.session.Session)

Example 98 with Triple

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

the class GraphMultiplexer method updateScopeUnregistration.

/**
 * Remove all information on a deregistered ontology scope and its spaces.
 *
 * @param scope
 *            the scope whose information needs to be updated.
 */
private void updateScopeUnregistration(Scope scope) {
    long before = System.currentTimeMillis();
    boolean removable = false, conflict = false;
    final IRI scopeur = getIRIforScope(scope.getID());
    final IRI coreur = getIRIforSpace(scope.getCoreSpace());
    final IRI custur = getIRIforSpace(scope.getCustomSpace());
    Set<Triple> removeUs = new HashSet<Triple>();
    for (Iterator<Triple> it = meta.filter(scopeur, null, null); it.hasNext(); ) {
        Triple t = it.next();
        if (RDF.type.equals(t.getPredicate())) {
            if (SCOPE_URIREF.equals(t.getObject()))
                removable = true;
            else
                conflict = true;
        }
        removeUs.add(t);
    }
    if (!removable) {
        log.error("Cannot write scope deregistration to persistence:");
        log.error("-- resource {}", scopeur);
        log.error("-- is not typed as a {} in the meta-graph.", SCOPE_URIREF);
    } else if (conflict) {
        log.error("Conflict upon scope deregistration:");
        log.error("-- resource {}", scopeur);
        log.error("-- has incompatible types in the meta-graph.");
    } else {
        log.debug("Removing all triples for scope \"{}\".", scope.getID());
        Iterator<Triple> it;
        for (it = meta.filter(null, null, scopeur); it.hasNext(); ) removeUs.add(it.next());
        for (it = meta.filter(null, null, coreur); it.hasNext(); ) removeUs.add(it.next());
        for (it = meta.filter(coreur, null, null); it.hasNext(); ) removeUs.add(it.next());
        for (it = meta.filter(null, null, custur); it.hasNext(); ) removeUs.add(it.next());
        for (it = meta.filter(custur, null, null); it.hasNext(); ) removeUs.add(it.next());
        meta.removeAll(removeUs);
        log.debug("Done; removed {} triples in {} ms.", removeUs.size(), System.currentTimeMillis() - before);
    }
}
Also used : Triple(org.apache.clerezza.commons.rdf.Triple) IRI(org.apache.clerezza.commons.rdf.IRI) BlankNodeOrIRI(org.apache.clerezza.commons.rdf.BlankNodeOrIRI) Iterator(java.util.Iterator) HashSet(java.util.HashSet)

Example 99 with Triple

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

the class GraphMultiplexer method getSize.

@Override
public int getSize(OWLOntologyID publicKey) {
    IRI subj = buildResource(publicKey);
    Iterator<Triple> it = meta.filter(subj, SIZE_IN_TRIPLES_URIREF, null);
    if (it.hasNext()) {
        RDFTerm obj = it.next().getObject();
        if (obj instanceof Literal) {
            String s = ((Literal) obj).getLexicalForm();
            try {
                return Integer.parseInt(s);
            } catch (Exception ex) {
                log.warn("Not a valid integer value {} for size of {}", s, publicKey);
                return -1;
            }
        }
    }
    return 0;
}
Also used : Triple(org.apache.clerezza.commons.rdf.Triple) IRI(org.apache.clerezza.commons.rdf.IRI) BlankNodeOrIRI(org.apache.clerezza.commons.rdf.BlankNodeOrIRI) Literal(org.apache.clerezza.commons.rdf.Literal) RDFTerm(org.apache.clerezza.commons.rdf.RDFTerm)

Example 100 with Triple

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

the class GraphMultiplexer method buildResource.

/**
 * Creates an {@link IRI} out of an {@link OWLOntologyID}, so it can be used as an identifier. This
 * does NOT necessarily correspond to the IRI that identifies the stored graph. In order to obtain
 * that, check the objects of any MAPS_TO_GRAPH assertions.
 *
 * @param publicKey
 * @return
 */
protected IRI buildResource(final OWLOntologyID publicKey) {
    if (publicKey == null)
        throw new IllegalArgumentException("Cannot build a IRI resource on a null public key!");
    // The IRI is of the form ontologyIRI[:::versionIRI] (TODO use something less conventional?)
    // XXX should versionIRI also include the version IRI set by owners? Currently not
    // Remember not to sanitize logical identifiers.
    org.semanticweb.owlapi.model.IRI ontologyIri = publicKey.getOntologyIRI(), versionIri = publicKey.getVersionIRI();
    if (ontologyIri == null)
        throw new IllegalArgumentException("Cannot build a IRI resource on an anonymous public key!");
    log.debug("Searching for a meta graph entry for public key:");
    log.debug(" -- {}", publicKey);
    IRI match = null;
    LiteralFactory lf = LiteralFactory.getInstance();
    Literal oiri = lf.createTypedLiteral(new IRI(ontologyIri.toString()));
    Literal viri = versionIri == null ? null : lf.createTypedLiteral(new IRI(versionIri.toString()));
    for (Iterator<Triple> it = meta.filter(null, HAS_ONTOLOGY_IRI_URIREF, oiri); it.hasNext(); ) {
        RDFTerm subj = it.next().getSubject();
        log.debug(" -- Ontology IRI match found. Scanning");
        log.debug(" -- RDFTerm : {}", subj);
        if (!(subj instanceof IRI)) {
            log.debug(" ---- (uncomparable: skipping...)");
            continue;
        }
        if (viri != null) {
            // Must find matching versionIRI
            if (meta.contains(new TripleImpl((IRI) subj, HAS_VERSION_IRI_URIREF, viri))) {
                log.debug(" ---- Version IRI match!");
                match = (IRI) subj;
                // Found
                break;
            } else {
                log.debug(" ---- Expected version IRI match not found.");
                // There could be another with the right versionIRI.
                continue;
            }
        } else {
            // Must find unversioned resource
            if (meta.filter((IRI) subj, HAS_VERSION_IRI_URIREF, null).hasNext()) {
                log.debug(" ---- Unexpected version IRI found. Skipping.");
                continue;
            } else {
                log.debug(" ---- Unversioned match!");
                match = (IRI) subj;
                // Found
                break;
            }
        }
    }
    log.debug("Matching IRI in graph : {}", match);
    if (match == null)
        return new IRI(OntologyUtils.encode(publicKey));
    else
        return match;
}
Also used : Triple(org.apache.clerezza.commons.rdf.Triple) IRI(org.apache.clerezza.commons.rdf.IRI) BlankNodeOrIRI(org.apache.clerezza.commons.rdf.BlankNodeOrIRI) Literal(org.apache.clerezza.commons.rdf.Literal) RDFTerm(org.apache.clerezza.commons.rdf.RDFTerm) TripleImpl(org.apache.clerezza.commons.rdf.impl.utils.TripleImpl) LiteralFactory(org.apache.clerezza.rdf.core.LiteralFactory)

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