Search in sources :

Example 11 with Triple

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

the class GraphMultiplexer method checkHandle.

private void checkHandle(IRI candidate, Set<OntologyCollector> handles) {
    /*
         * We have to do it like this because we cannot make this class a Component and reference ONManager
         * and SessionManager, otherwise an activation cycle will occur.
         */
    // FIXME get rid of this.
    ScopeManager scopeManager = ScopeManagerImpl.get();
    SessionManager sessionManager = SessionManagerImpl.get();
    String prefix_scope = _NS_STANBOL_INTERNAL + Scope.shortName + "/", prefix_session = _NS_STANBOL_INTERNAL + Session.shortName + "/";
    // TODO check when not explicitly typed.
    SpaceType spaceType;
    if (meta.contains(new TripleImpl(candidate, RDF.type, SPACE_URIREF))) {
        RDFTerm rScope;
        Iterator<Triple> parentSeeker = meta.filter(candidate, IS_SPACE_CORE_OF_URIREF, null);
        if (parentSeeker.hasNext()) {
            rScope = parentSeeker.next().getObject();
            spaceType = SpaceType.CORE;
        } else {
            parentSeeker = meta.filter(candidate, IS_SPACE_CUSTOM_OF_URIREF, null);
            if (parentSeeker.hasNext()) {
                rScope = parentSeeker.next().getObject();
                spaceType = SpaceType.CUSTOM;
            } else {
                parentSeeker = meta.filter(null, HAS_SPACE_CORE_URIREF, candidate);
                if (parentSeeker.hasNext()) {
                    rScope = parentSeeker.next().getSubject();
                    spaceType = SpaceType.CORE;
                } else {
                    parentSeeker = meta.filter(null, HAS_SPACE_CUSTOM_URIREF, candidate);
                    if (parentSeeker.hasNext()) {
                        rScope = parentSeeker.next().getSubject();
                        spaceType = SpaceType.CUSTOM;
                    } else
                        throw new InvalidMetaGraphStateException("Ontology space " + candidate + " does not declare a parent scope.");
                }
            }
        }
        if (!(rScope instanceof IRI))
            throw new InvalidMetaGraphStateException(rScope + " is not a legal scope identifier.");
        String scopeId = ((IRI) rScope).getUnicodeString().substring(prefix_scope.length());
        Scope scope = scopeManager.getScope(scopeId);
        switch(spaceType) {
            case CORE:
                handles.add(scope.getCoreSpace());
                break;
            case CUSTOM:
                handles.add(scope.getCustomSpace());
                break;
        }
    } else if (meta.contains(new TripleImpl(candidate, RDF.type, SESSION_URIREF))) {
        String sessionId = candidate.getUnicodeString().substring(prefix_session.length());
        handles.add(sessionManager.getSession(sessionId));
    }
}
Also used : Triple(org.apache.clerezza.commons.rdf.Triple) IRI(org.apache.clerezza.commons.rdf.IRI) BlankNodeOrIRI(org.apache.clerezza.commons.rdf.BlankNodeOrIRI) ScopeManager(org.apache.stanbol.ontologymanager.servicesapi.scope.ScopeManager) Scope(org.apache.stanbol.ontologymanager.servicesapi.scope.Scope) SessionManager(org.apache.stanbol.ontologymanager.servicesapi.session.SessionManager) SpaceType(org.apache.stanbol.ontologymanager.servicesapi.scope.OntologySpace.SpaceType) RDFTerm(org.apache.clerezza.commons.rdf.RDFTerm) TripleImpl(org.apache.clerezza.commons.rdf.impl.utils.TripleImpl)

Example 12 with Triple

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

Example 13 with Triple

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

the class GraphMultiplexer method getDependencies.

@Override
public Set<OWLOntologyID> getDependencies(OWLOntologyID dependent) {
    Set<OWLOntologyID> dependencies = new HashSet<OWLOntologyID>();
    log.debug("Getting dependencies for {}", dependent);
    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()) {
                RDFTerm obj = it.next().getObject();
                log.debug(" ... found {} (inverse).", obj);
                if (obj instanceof IRI)
                    dependencies.add(buildPublicKey((IRI) obj));
                else
                    log.warn(" ... Unexpected literal value!");
            }
            it = meta.filter(null, HAS_DEPENDENT_URIREF, dep);
            while (it.hasNext()) {
                RDFTerm sub = it.next().getSubject();
                log.debug(" ... found {} (inverse).", sub);
                if (sub instanceof IRI)
                    dependencies.add(buildPublicKey((IRI) sub));
                else
                    log.warn(" ... Unexpected literal value!");
            }
        }
    }
    return 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) RDFTerm(org.apache.clerezza.commons.rdf.RDFTerm) HashSet(java.util.HashSet)

Example 14 with Triple

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

the class GraphMultiplexer method updateSessionUnregistration.

private void updateSessionUnregistration(Session session) {
    long before = System.currentTimeMillis();
    boolean removable = false, conflict = false;
    final IRI sessionur = getIRIforSession(session);
    Set<Triple> removeUs = new HashSet<Triple>();
    for (Iterator<Triple> it = meta.filter(sessionur, null, null); it.hasNext(); ) {
        Triple t = it.next();
        if (RDF.type.equals(t.getPredicate())) {
            if (SESSION_URIREF.equals(t.getObject()))
                removable = true;
            else
                conflict = true;
        }
        removeUs.add(t);
    }
    if (!removable) {
        log.error("Cannot write session deregistration to persistence:");
        log.error("-- resource {}", sessionur);
        log.error("-- is not typed as a {} in the meta-graph.", SESSION_URIREF);
    } else if (conflict) {
        log.error("Conflict upon session deregistration:");
        log.error("-- resource {}", sessionur);
        log.error("-- has incompatible types in the meta-graph.");
    } else {
        log.debug("Removing all triples for session \"{}\".", session.getID());
        Iterator<Triple> it;
        for (it = meta.filter(null, null, sessionur); it.hasNext(); ) removeUs.add(it.next());
        for (it = meta.filter(sessionur, 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 15 with Triple

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

the class GraphMultiplexer method removeDependency.

@Override
public void removeDependency(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("Removing dependency.");
    log.debug(" ... dependent : {}", dependent);
    log.debug(" ... dependency : {}", dependency);
    IRI depy = buildResource(dependency);
    synchronized (meta) {
        Set<OWLOntologyID> aliases = listAliases(dependent);
        aliases.add(dependent);
        for (OWLOntologyID depalias : aliases) {
            IRI dep = buildResource(depalias);
            Triple t = new TripleImpl(dep, DEPENDS_ON_URIREF, depy);
            boolean found = false;
            if (meta.contains(t)) {
                found = true;
                meta.remove(t);
            }
            t = new TripleImpl(depy, HAS_DEPENDENT_URIREF, dep);
            if (meta.contains(t)) {
                found = true;
                meta.remove(t);
            }
            if (!found)
                log.warn("No such dependency found.");
            else
                log.debug("DONE removing dependency.");
        }
    }
}
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) TripleImpl(org.apache.clerezza.commons.rdf.impl.utils.TripleImpl)

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