Search in sources :

Example 56 with URI

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

the class TestJPAX method main.

public static void main(String[] args) {
    Graph graph = new HashGraph();
    String ns = DefaultNameSpaces.EX.uri;
    URI r1 = graph.getValueFactory().createURI(ns + "r1");
    URI pe1 = graph.getValueFactory().createURI(ns + "pe1");
    URI pe2 = graph.getValueFactory().createURI(ns + "pe2");
    graph.add(r1, RDF.TYPE, BioPAX3.BiochemicalReaction);
    graph.add(pe1, RDF.TYPE, BioPAX3.Protein);
    graph.add(pe2, RDF.TYPE, BioPAX3.Protein);
    graph.add(r1, BioPAX3.left, pe1);
    graph.add(r1, BioPAX3.right, pe2);
    try {
        Map<String, String> nsMap = DefaultNameSpaces.defaultMap.convertToMap();
        SesameRioUtil.writeRDFToStream(System.out, graph, nsMap, RDFFormat.RDFXML);
    } catch (RDFHandlerException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
Also used : HashGraph(org.sbpax.impl.HashGraph) Graph(org.openrdf.model.Graph) HashGraph(org.sbpax.impl.HashGraph) RDFHandlerException(org.openrdf.rio.RDFHandlerException) URI(org.openrdf.model.URI)

Example 57 with URI

use of org.openrdf.model.URI in project blueprints by tinkerpop.

the class SailEdge method setProperty.

public void setProperty(final String key, final Object value) {
    if (key.equals(SailTokens.NAMED_GRAPH)) {
        try {
            URI namedGraph = new URIImpl(this.graph.expandPrefix(value.toString()));
            SailHelper.removeStatement(this.rawEdge, this.graph.getSailConnection().get());
            this.rawEdge = new ContextStatementImpl(this.rawEdge.getSubject(), this.rawEdge.getPredicate(), this.rawEdge.getObject(), namedGraph);
            SailHelper.addStatement(this.rawEdge, this.graph.getSailConnection().get());
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage(), e);
        }
    } else {
        throw new IllegalArgumentException(NAMED_GRAPH_PROPERTY);
    }
}
Also used : ContextStatementImpl(org.openrdf.model.impl.ContextStatementImpl) URIImpl(org.openrdf.model.impl.URIImpl) URI(org.openrdf.model.URI)

Example 58 with URI

use of org.openrdf.model.URI in project blueprints by tinkerpop.

the class SailGraph method addEdge.

public Edge addEdge(final Object id, final Vertex outVertex, final Vertex inVertex, final String label) {
    if (label == null)
        throw ExceptionFactory.edgeLabelCanNotBeNull();
    Value outVertexValue = ((SailVertex) outVertex).getRawVertex();
    Value inVertexValue = ((SailVertex) inVertex).getRawVertex();
    if (!(outVertexValue instanceof Resource)) {
        throw new IllegalArgumentException(outVertex.toString() + " is not a legal URI or blank node");
    }
    try {
        URI labelURI = new URIImpl(this.expandPrefix(label));
        Statement statement = new StatementImpl((Resource) outVertexValue, labelURI, inVertexValue);
        SailHelper.addStatement(statement, this.sailConnection.get());
        return new SailEdge(statement, this);
    } catch (Exception e) {
        throw new RuntimeException(e.getMessage(), e);
    }
}
Also used : Statement(org.openrdf.model.Statement) StatementImpl(org.openrdf.model.impl.StatementImpl) Value(org.openrdf.model.Value) Resource(org.openrdf.model.Resource) URIImpl(org.openrdf.model.impl.URIImpl) URI(org.openrdf.model.URI) SailException(org.openrdf.sail.SailException) QueryEvaluationException(org.openrdf.query.QueryEvaluationException) RDFHandlerException(org.openrdf.rio.RDFHandlerException)

Example 59 with URI

use of org.openrdf.model.URI in project blueprints by tinkerpop.

the class PropertyGraphSailConnection method vertexPropertiesWithValue.

private StatementGenerator<Vertex> vertexPropertiesWithValue(final Object value, final Literal object) {
    return new StatementGenerator<Vertex>() {

        public void generate(Vertex source, Collection<Statement> results) {
            for (String key : source.getPropertyKeys()) {
                Object v = source.getProperty(key);
                if (null != v && v.equals(value)) {
                    URI predicate = predicateForPropertyKey(key);
                    Statement s = context.valueFactory.createStatement(uriForVertex(source), predicate, object);
                    results.add(s);
                }
            }
        }
    };
}
Also used : Vertex(com.tinkerpop.blueprints.Vertex) Statement(org.openrdf.model.Statement) Collection(java.util.Collection) URI(org.openrdf.model.URI)

Example 60 with URI

use of org.openrdf.model.URI in project blueprints by tinkerpop.

the class PropertyGraphSailConnection method getStatements_xPO.

private CloseableIteration<Statement, SailException> getStatements_xPO(final URI predicate, final Value object) throws SailException {
    if (predicate.equals(RDF.TYPE)) {
        if (object.equals(PropertyGraphSail.VERTEX)) {
            Source<Vertex> s = new Source<Vertex>(context.graph.getVertices().iterator(), vertexTypes);
            return new StatementIteration(s);
        } else if (object.equals(PropertyGraphSail.EDGE) && firstClassEdges) {
            Source<Edge> s = new Source<Edge>(context.graph.getEdges().iterator(), edgeTypes);
            return new StatementIteration(s);
        } else {
            return new StatementIteration();
        }
    } else if (predicate.equals(PropertyGraphSail.ID)) {
        Object id = literalToObject(object);
        if (null == id) {
            return new StatementIteration();
        } else {
            Vertex v = context.graph.getVertex(id);
            Edge e = firstClassEdges ? context.graph.getEdge(id) : null;
            if (null == v && null == e) {
                return new StatementIteration();
            } else {
                Collection<Statement> s = new LinkedList<Statement>();
                if (null != v) {
                    vertexIds.generate(v, s);
                }
                if (null != e) {
                    edgeIds.generate(e, s);
                }
                return new SimpleCloseableIteration<Statement, SailException>(s.iterator());
            }
        }
    } else if (predicate.equals(PropertyGraphSail.LABEL)) {
        if (!firstClassEdges) {
            return new StatementIteration();
        }
        // TODO: find edges faster using indices
        Object label = literalToObject(object);
        if (null == label || !(label instanceof String)) {
            return new StatementIteration();
        } else {
            Source<Edge> edges = new Source<Edge>(context.graph.getEdges().iterator(), matchingLabels((String) label, object));
            return new StatementIteration(edges);
        }
    } else if (predicate.equals(PropertyGraphSail.HEAD)) {
        if (!firstClassEdges) {
            return new StatementIteration();
        }
        Vertex v = object instanceof URI ? vertexForURI((URI) object) : null;
        if (null == v) {
            return new StatementIteration();
        } else {
            Iterator<Edge> edgeIterator = v.getEdges(Direction.IN).iterator();
            Source<Edge> edges = new Source<Edge>(edgeIterator, heads);
            return new StatementIteration(edges);
        }
    } else if (predicate.equals(PropertyGraphSail.TAIL)) {
        if (!firstClassEdges) {
            return new StatementIteration();
        }
        Vertex v = object instanceof URI ? vertexForURI((URI) object) : null;
        if (null == v) {
            return new StatementIteration();
        } else {
            Iterator<Edge> edgeIterator = v.getEdges(Direction.OUT).iterator();
            Source<Edge> edges = new Source<Edge>(edgeIterator, tails);
            return new StatementIteration(edges);
        }
    } else if (isPropertyPredicate(predicate)) {
        Object value = literalToObject(object);
        if (null == value) {
            return new StatementIteration();
        } else {
            // TODO: lookup matching vertices and edges faster using indices
            String key = keyFromPredicate(predicate);
            Source<Vertex> vertices = new Source<Vertex>(context.graph.getVertices().iterator(), vertexPropertiesWithKeyAndValue(key, predicate, value, (Literal) object));
            if (firstClassEdges) {
                Source<Edge> edges = new Source<Edge>(context.graph.getEdges().iterator(), edgePropertiesWithKeyAndValue(key, predicate, value, (Literal) object));
                return new StatementIteration(vertices, edges);
            } else {
                return new StatementIteration(vertices);
            }
        }
    } else if (isRelationPredicate(predicate)) {
        if (!(object instanceof URI)) {
            return new StatementIteration();
        } else {
            String label = labelForRelationPredicate(predicate);
            Vertex vObj = vertexForURI((URI) object);
            if (null != vObj) {
                Source<Vertex> s = new Source<Vertex>(new SingleItemIterator<Vertex>(vObj), new RelationGenerator(label, true));
                return new StatementIteration(s);
            } else {
                return new StatementIteration();
            }
        }
    } else {
        return new StatementIteration();
    }
}
Also used : Vertex(com.tinkerpop.blueprints.Vertex) Statement(org.openrdf.model.Statement) SailException(org.openrdf.sail.SailException) URI(org.openrdf.model.URI) SailConnectionTripleSource(net.fortytwo.sesametools.SailConnectionTripleSource) TripleSource(org.openrdf.query.algebra.evaluation.TripleSource) Iterator(java.util.Iterator) Collection(java.util.Collection) Edge(com.tinkerpop.blueprints.Edge)

Aggregations

URI (org.openrdf.model.URI)111 Test (org.junit.Test)28 SailConnection (org.openrdf.sail.SailConnection)25 Value (org.openrdf.model.Value)22 Statement (org.openrdf.model.Statement)21 NotifyingSailConnection (org.openrdf.sail.NotifyingSailConnection)21 Resource (org.openrdf.model.Resource)20 Literal (org.openrdf.model.Literal)14 ValueFactory (org.openrdf.model.ValueFactory)12 RepositoryException (org.openrdf.repository.RepositoryException)11 SailException (org.openrdf.sail.SailException)10 ShineRuntimeException (com.thoughtworks.studios.shine.ShineRuntimeException)8 Vertex (com.tinkerpop.blueprints.Vertex)7 HashSet (java.util.HashSet)7 Representation (org.apache.stanbol.entityhub.servicesapi.model.Representation)7 Model (org.openrdf.model.Model)7 URIImpl (org.openrdf.model.impl.URIImpl)7 Edge (com.tinkerpop.blueprints.Edge)6 BNode (org.openrdf.model.BNode)6 QueryEvaluationException (org.openrdf.query.QueryEvaluationException)6