Search in sources :

Example 81 with TripleImpl

use of org.apache.clerezza.commons.rdf.impl.utils.TripleImpl in project stanbol by apache.

the class GraphMultiplexer method updateScopeRegistration.

/**
     * Write registration info for a new ontology scope and its spaces.
     * 
     * @param scope
     *            the scope whose information needs to be updated.
     */
private void updateScopeRegistration(Scope scope) {
    final IRI scopeur = getIRIforScope(scope.getID());
    final IRI coreur = getIRIforSpace(scope.getCoreSpace());
    final IRI custur = getIRIforSpace(scope.getCustomSpace());
    // If this method was called after a scope rebuild, the following will have little to no effect.
    synchronized (meta) {
        // Spaces are created along with the scope, so it is safe to add their triples.
        meta.add(new TripleImpl(scopeur, RDF.type, SCOPE_URIREF));
        meta.add(new TripleImpl(coreur, RDF.type, SPACE_URIREF));
        meta.add(new TripleImpl(custur, RDF.type, SPACE_URIREF));
        meta.add(new TripleImpl(scopeur, HAS_SPACE_CORE_URIREF, coreur));
        meta.add(new TripleImpl(scopeur, HAS_SPACE_CUSTOM_URIREF, custur));
        // Add inverse predicates so we can traverse the graph in both directions.
        meta.add(new TripleImpl(coreur, IS_SPACE_CORE_OF_URIREF, scopeur));
        meta.add(new TripleImpl(custur, IS_SPACE_CUSTOM_OF_URIREF, scopeur));
    }
    log.debug("Ontology collector information triples added for scope \"{}\".", scope);
}
Also used : IRI(org.apache.clerezza.commons.rdf.IRI) BlankNodeOrIRI(org.apache.clerezza.commons.rdf.BlankNodeOrIRI) TripleImpl(org.apache.clerezza.commons.rdf.impl.utils.TripleImpl)

Example 82 with TripleImpl

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

Example 83 with TripleImpl

use of org.apache.clerezza.commons.rdf.impl.utils.TripleImpl in project stanbol by apache.

the class GraphMultiplexer method setDependency.

@Override
public void setDependency(OWLOntologyID dependent, OWLOntologyID dependency) {
    if (dependent == null)
        throw new IllegalArgumentException("dependent cannot be null");
    if (dependency == null)
        throw new IllegalArgumentException("dependency cannot be null");
    log.debug("Setting dependency.");
    log.debug(" ... dependent : {}", dependent);
    log.debug(" ... dependency : {}", dependency);
    IRI dep = buildResource(dependent), depy = buildResource(dependency);
    // TODO check for the actual resource!
    synchronized (meta) {
        meta.add(new TripleImpl(dep, DEPENDS_ON_URIREF, depy));
    }
    log.debug("DONE setting dependency.");
}
Also used : IRI(org.apache.clerezza.commons.rdf.IRI) BlankNodeOrIRI(org.apache.clerezza.commons.rdf.BlankNodeOrIRI) TripleImpl(org.apache.clerezza.commons.rdf.impl.utils.TripleImpl)

Example 84 with TripleImpl

use of org.apache.clerezza.commons.rdf.impl.utils.TripleImpl in project stanbol by apache.

the class GraphMultiplexer method onOntologyRemoved.

@Override
public void onOntologyRemoved(OntologyCollector collector, OWLOntologyID removedOntology) {
    log.info("Heard removal of ontology {} from collector {}", removedOntology, collector.getID());
    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());
    Set<OWLOntologyID> aliases = listAliases(removedOntology);
    aliases.add(removedOntology);
    boolean badState = true;
    for (OWLOntologyID alias : aliases) {
        IRI u = buildResource(alias);
        // XXX condense the following code
        log.debug("Checking ({},{}) pattern", c, u);
        for (Iterator<Triple> it = meta.filter(c, null, u); it.hasNext(); ) {
            IRI property = it.next().getPredicate();
            if (collector instanceof OntologySpace || collector instanceof Session) {
                if (property.equals(MANAGES_URIREF))
                    badState = false;
            }
        }
        log.debug("Checking ({},{}) pattern", u, c);
        for (Iterator<Triple> it = meta.filter(u, null, c); it.hasNext(); ) {
            IRI property = it.next().getPredicate();
            if (collector instanceof OntologySpace || collector instanceof Session) {
                if (property.equals(IS_MANAGED_BY_URIREF))
                    badState = false;
            }
        }
        synchronized (meta) {
            if (collector instanceof OntologySpace || collector instanceof Session) {
                meta.remove(new TripleImpl(c, MANAGES_URIREF, u));
                meta.remove(new TripleImpl(u, IS_MANAGED_BY_URIREF, c));
            }
        }
    }
    if (badState)
        throw new InvalidMetaGraphStateException("No relationship found between ontology collector " + c + " and stored ontology " + removedOntology + " (or its aliases).");
}
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) OWLOntologyID(org.semanticweb.owlapi.model.OWLOntologyID) TripleImpl(org.apache.clerezza.commons.rdf.impl.utils.TripleImpl) Session(org.apache.stanbol.ontologymanager.servicesapi.session.Session)

Example 85 with TripleImpl

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

Aggregations

TripleImpl (org.apache.clerezza.commons.rdf.impl.utils.TripleImpl)143 IRI (org.apache.clerezza.commons.rdf.IRI)104 PlainLiteralImpl (org.apache.clerezza.commons.rdf.impl.utils.PlainLiteralImpl)69 Graph (org.apache.clerezza.commons.rdf.Graph)66 BlankNodeOrIRI (org.apache.clerezza.commons.rdf.BlankNodeOrIRI)49 Triple (org.apache.clerezza.commons.rdf.Triple)41 RDFTerm (org.apache.clerezza.commons.rdf.RDFTerm)26 EngineException (org.apache.stanbol.enhancer.servicesapi.EngineException)23 HashMap (java.util.HashMap)20 Language (org.apache.clerezza.commons.rdf.Language)20 Literal (org.apache.clerezza.commons.rdf.Literal)20 LiteralFactory (org.apache.clerezza.rdf.core.LiteralFactory)20 IOException (java.io.IOException)18 SimpleGraph (org.apache.clerezza.commons.rdf.impl.utils.simple.SimpleGraph)17 Test (org.junit.Test)16 ContentItem (org.apache.stanbol.enhancer.servicesapi.ContentItem)15 IndexedGraph (org.apache.stanbol.commons.indexedgraph.IndexedGraph)14 HashSet (java.util.HashSet)13 StringSource (org.apache.stanbol.enhancer.servicesapi.impl.StringSource)13 BlankNode (org.apache.clerezza.commons.rdf.BlankNode)11