Search in sources :

Example 26 with Statement

use of org.openrdf.model.Statement in project vcell by virtualcell.

the class RDFEvaluator method getProperties.

public <T> RDFValueSets getProperties(T subject, RDFObjectPool<T> pool, URI property) {
    Set<Value> objects = new HashSet<Value>();
    Set<Resource> subjectResources = pool.getResources(subject);
    for (Graph graph : graphs) {
        for (Resource subjectResource : subjectResources) {
            Iterator<Statement> stmtIter = graph.match(subjectResource, property, null);
            while (stmtIter.hasNext()) {
                objects.add(stmtIter.next().getObject());
            }
        }
    }
    return new RDFValueSets(objects);
}
Also used : Graph(org.openrdf.model.Graph) Statement(org.openrdf.model.Statement) RDFValueSets(org.vcell.sybil.rdf.nodestore.RDFValueSets) Value(org.openrdf.model.Value) Resource(org.openrdf.model.Resource) HashSet(java.util.HashSet)

Example 27 with Statement

use of org.openrdf.model.Statement in project vcell by virtualcell.

the class RDFEvaluator method createAllObjectsForPool.

public <T> Set<T> createAllObjectsForPool(RDFObjectPool<T> pool) {
    Set<T> objects = new HashSet<T>();
    Set<Resource> supportedTypes = pool.getSupportedTypes();
    for (Graph graph : graphs) {
        for (Resource type : supportedTypes) {
            Iterator<Statement> stmtIter = graph.match(null, RDF.TYPE, type);
            while (stmtIter.hasNext()) {
                Statement statement = stmtIter.next();
                try {
                    objects.add(pool.getOrCreateObject(statement.getSubject(), Collections.singleton(type)));
                } catch (UnsupportedRDFTypeException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    return objects;
}
Also used : Graph(org.openrdf.model.Graph) Statement(org.openrdf.model.Statement) UnsupportedRDFTypeException(org.vcell.sybil.rdf.pool.UnsupportedRDFTypeException) Resource(org.openrdf.model.Resource) HashSet(java.util.HashSet)

Example 28 with Statement

use of org.openrdf.model.Statement in project vcell by virtualcell.

the class ModelComparer method extractSubModels.

private void extractSubModels(Graph diff, Set<Graph> subModels) {
    MultiMap<Resource, Resource> bNodesMap = new MultiHashMap<Resource, Resource>();
    MultiMap<Resource, Statement> triples = new MultiHashMap<Resource, Statement>();
    for (Statement statement : diff) {
        Set<Resource> bNodesInTriple = new HashSet<Resource>();
        recordIfBlankNode(statement.getSubject(), statement, bNodesInTriple, triples);
        recordIfBlankNode(statement.getPredicate(), statement, bNodesInTriple, triples);
        recordIfBlankNode(statement.getObject(), statement, bNodesInTriple, triples);
        Set<Resource> bNodesUnion = new HashSet<Resource>();
        bNodesUnion.addAll(bNodesInTriple);
        for (Resource bNode : bNodesInTriple) {
            Set<Resource> bNodes = bNodesMap.get(bNode);
            if (bNodes != null) {
                bNodesUnion.addAll(bNodes);
            }
        }
        for (Resource bNode : bNodesUnion) {
            bNodesMap.put(bNode, bNodesUnion);
        }
    }
    for (Set<Resource> bNodes : bNodesMap.values()) {
        Graph model = new HashGraph();
        for (Resource bNode : bNodes) {
            for (Statement statement : triples.get(bNode)) {
                model.add(statement);
            }
        }
        subModels.add(model);
    }
}
Also used : HashGraph(org.sbpax.impl.HashGraph) Graph(org.openrdf.model.Graph) HashGraph(org.sbpax.impl.HashGraph) Statement(org.openrdf.model.Statement) Resource(org.openrdf.model.Resource) MultiHashMap(org.sbpax.util.MultiHashMap) HashSet(java.util.HashSet)

Example 29 with Statement

use of org.openrdf.model.Statement in project vcell by virtualcell.

the class SameAsCrystalizer method smelt.

public Graph smelt(Graph rdf) {
    Map<Resource, Set<Resource>> sameAsSetsMap = new HashMap<Resource, Set<Resource>>();
    Iterator<Statement> sameAsStmtIter = rdf.match(null, OWL.SAMEAS, null);
    while (sameAsStmtIter.hasNext()) {
        Statement sameAsStatement = sameAsStmtIter.next();
        Value objectNode = sameAsStatement.getObject();
        if (objectNode instanceof Resource) {
            Resource subject = sameAsStatement.getSubject();
            Resource object = (Resource) objectNode;
            Set<Resource> subjectSameAsSet = sameAsSetsMap.get(subject);
            Set<Resource> objectSameAsSet = sameAsSetsMap.get(object);
            if (subjectSameAsSet != null && objectSameAsSet != null) {
                if (!subjectSameAsSet.equals(objectSameAsSet)) {
                    Set<Resource> sameAsUnionSet = new HashSet<Resource>();
                    sameAsUnionSet.addAll(subjectSameAsSet);
                    sameAsUnionSet.addAll(objectSameAsSet);
                    sameAsSetsMap.put(subject, sameAsUnionSet);
                    sameAsSetsMap.put(object, sameAsUnionSet);
                }
            } else if (subjectSameAsSet != null) {
                subjectSameAsSet.add(object);
                sameAsSetsMap.put(object, subjectSameAsSet);
            } else if (objectSameAsSet != null) {
                objectSameAsSet.add(subject);
                sameAsSetsMap.put(subject, objectSameAsSet);
            } else {
                Set<Resource> sameAsUnionSet = new HashSet<Resource>();
                sameAsUnionSet.add(subject);
                sameAsUnionSet.add(object);
                sameAsSetsMap.put(subject, sameAsUnionSet);
                sameAsSetsMap.put(object, sameAsUnionSet);
            }
        }
    }
    Set<Set<Resource>> sameAsSets = new HashSet<Set<Resource>>();
    for (Map.Entry<Resource, Set<Resource>> sameAsSetsEntry : sameAsSetsMap.entrySet()) {
        sameAsSets.add(sameAsSetsEntry.getValue());
    }
    Map<Resource, Resource> projectionMap = new HashMap<Resource, Resource>();
    for (Set<Resource> sameAsSet : sameAsSets) {
        Resource preferredResource = null;
        for (Resource resource : sameAsSet) {
            if (preferredResource == null || comparator.compare(resource, preferredResource) > 0) {
                preferredResource = resource;
            }
        }
        for (Resource resource : sameAsSet) {
            if (resource != preferredResource) {
                projectionMap.put(resource, preferredResource);
            }
        }
    }
    RDFResourceProjection rdfResourceProjection = new RDFResourceProjection(projectionMap);
    Graph rdfSmelted = rdfResourceProjection.smelt(rdf);
    Iterator<Statement> iter = rdfSmelted.match(null, OWL.SAMEAS, null);
    while (iter.hasNext()) {
        iter.next();
        iter.remove();
    }
    for (Map.Entry<Resource, Resource> projectionEntry : projectionMap.entrySet()) {
        Resource subject = projectionEntry.getValue();
        Resource object = projectionEntry.getKey();
        if (!subject.equals(object)) {
            rdfSmelted.add(subject, OWL.SAMEAS, object);
        }
    }
    return rdfSmelted;
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) Statement(org.openrdf.model.Statement) Resource(org.openrdf.model.Resource) Graph(org.openrdf.model.Graph) Value(org.openrdf.model.Value) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 30 with Statement

use of org.openrdf.model.Statement in project vcell by virtualcell.

the class NamespaceAssimilator method smelt.

public Graph smelt(Graph rdf) {
    Graph rdfNew = new HashGraph();
    for (Statement statement : rdf) {
        Resource subjectNew = smelt(rdf, statement.getSubject());
        URI predicateNew = smelt(rdf, statement.getPredicate());
        Value objectNew = statement.getObject() instanceof Resource ? smelt(rdf, (Resource) statement.getObject()) : statement.getObject();
        rdfNew.add(subjectNew, predicateNew, objectNew);
    }
    for (Map.Entry<Resource, Resource> entry : resourceMap.entrySet()) {
        rdfNew.add(entry.getKey(), OWL.SAMEAS, entry.getValue());
    }
    return rdfNew;
}
Also used : HashGraph(org.sbpax.impl.HashGraph) Graph(org.openrdf.model.Graph) HashGraph(org.sbpax.impl.HashGraph) Statement(org.openrdf.model.Statement) Resource(org.openrdf.model.Resource) Value(org.openrdf.model.Value) URI(org.openrdf.model.URI) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

Statement (org.openrdf.model.Statement)67 Resource (org.openrdf.model.Resource)22 Value (org.openrdf.model.Value)22 URI (org.openrdf.model.URI)21 HashSet (java.util.HashSet)16 SailException (org.openrdf.sail.SailException)10 Collection (java.util.Collection)9 Literal (org.openrdf.model.Literal)9 Graph (org.openrdf.model.Graph)7 Edge (com.tinkerpop.blueprints.Edge)6 Test (org.junit.Test)6 Vertex (com.tinkerpop.blueprints.Vertex)5 HashMap (java.util.HashMap)5 Map (java.util.Map)4 PrintWriter (java.io.PrintWriter)3 Set (java.util.Set)3 BNode (org.openrdf.model.BNode)3 ValueFactory (org.openrdf.model.ValueFactory)3 RDFHandlerException (org.openrdf.rio.RDFHandlerException)3 RdfXmlSerializer (org.qi4j.library.rdf.serializer.RdfXmlSerializer)3