Search in sources :

Example 6 with SailException

use of org.openrdf.sail.SailException in project backstage by zepheira.

the class Database method internalBuildPropertyRecords.

private void internalBuildPropertyRecords(SailConnection sc) {
    _propertyRecords = new LinkedList<PropertyRecord>();
    _propertyIDToRecord = new HashMap<String, PropertyRecord>();
    CloseableIteration<? extends Statement, SailException> i;
    try {
        i = sc.getStatements(null, null, null, true);
    } catch (SailException e) {
        _logger.error("Failed to get all statements in order to get property records", e);
        return;
    }
    Set<URI> predicates = new HashSet<URI>();
    try {
        while (i.hasNext()) {
            Statement s = i.next();
            predicates.add(s.getPredicate());
        }
    } catch (SailException e) {
        _logger.warn("Failed to iterate through statements", e);
    } finally {
        try {
            i.close();
        } catch (SailException e) {
            _logger.warn("Failed to close statement iterator", e);
        }
    }
    for (URI predicate : predicates) {
        PropertyRecord r = createPropertyRecord(predicate, sc);
        if (r != null) {
            _propertyRecords.add(r);
            _propertyIDToRecord.put(r.id, r);
        }
    }
}
Also used : Statement(org.openrdf.model.Statement) SailException(org.openrdf.sail.SailException) URI(org.openrdf.model.URI) HashSet(java.util.HashSet)

Example 7 with SailException

use of org.openrdf.sail.SailException in project backstage by zepheira.

the class Database method createPropertyRecord.

private PropertyRecord createPropertyRecord(URI predicate, SailConnection sc) {
    String id = getPropertyId(predicate, sc);
    String label = SailUtilities.getStringObject(sc, predicate, RDFS.LABEL, id);
    String valueType = SailUtilities.getStringObject(sc, predicate, ExhibitOntology.VALUE_TYPE, "text");
    Properties properties = new Properties();
    CloseableIteration<? extends Statement, SailException> i = null;
    try {
        i = sc.getStatements(predicate, null, null, true);
    } catch (SailException e) {
        _logger.error("Failed to get all statements in order to get property record", e);
        return null;
    }
    if (i != null) {
        try {
            while (i.hasNext()) {
                Statement s = i.next();
                URI p = s.getPredicate();
                Value o = s.getObject();
                if (!p.equals(RDFS.LABEL) && !p.equals(ExhibitOntology.ID) && !p.equals(ExhibitOntology.VALUE_TYPE)) {
                    properties.put(p.getLocalName(), SailUtilities.valueToString(o));
                }
            }
        } catch (SailException e) {
            _logger.warn("Failed to iterate through statements", e);
        } finally {
            try {
                i.close();
            } catch (SailException e) {
                _logger.warn("Failed to close statement iterator", e);
            }
        }
    }
    return new PropertyRecord(predicate, id, label, valueType, properties);
}
Also used : Statement(org.openrdf.model.Statement) Value(org.openrdf.model.Value) SailException(org.openrdf.sail.SailException) Properties(java.util.Properties) URI(org.openrdf.model.URI)

Example 8 with SailException

use of org.openrdf.sail.SailException in project backstage by zepheira.

the class Database method getItemLabel.

public String getItemLabel(String itemID) {
    String label = _itemIdToLabel.get(itemID);
    if (label == null) {
        getRepository();
        URI itemURI = _itemIdToUri.get(itemID);
        if (itemURI != null) {
            try {
                SailConnection sc = _sail.getConnection();
                try {
                    CloseableIteration<? extends Statement, SailException> i = sc.getStatements(itemURI, RDFS.LABEL, null, true);
                    try {
                        if (i.hasNext()) {
                            label = Utilities.valueToString(i.next().getObject());
                        }
                    } finally {
                        i.close();
                    }
                } finally {
                    sc.close();
                }
            } catch (SailException e) {
            }
        }
        if (label == null) {
            label = itemID;
        }
        _itemIdToLabel.put(itemID, label);
    }
    return label;
}
Also used : SailConnection(org.openrdf.sail.SailConnection) SailException(org.openrdf.sail.SailException) URI(org.openrdf.model.URI)

Example 9 with SailException

use of org.openrdf.sail.SailException in project blueprints by tinkerpop.

the class PropertyGraphSailConnection method evaluateInternal.

protected CloseableIteration<? extends BindingSet, QueryEvaluationException> evaluateInternal(final TupleExpr query, final Dataset dataset, final BindingSet bindings, final boolean includeInferred) throws SailException {
    try {
        TripleSource tripleSource = new SailConnectionTripleSource(this, context.valueFactory, includeInferred);
        EvaluationStrategyImpl strategy = new EvaluationStrategyImpl(tripleSource, dataset);
        return strategy.evaluate(query, bindings);
    } catch (QueryEvaluationException e) {
        throw new SailException(e);
    }
}
Also used : SailConnectionTripleSource(net.fortytwo.sesametools.SailConnectionTripleSource) TripleSource(org.openrdf.query.algebra.evaluation.TripleSource) QueryEvaluationException(org.openrdf.query.QueryEvaluationException) SailConnectionTripleSource(net.fortytwo.sesametools.SailConnectionTripleSource) SailException(org.openrdf.sail.SailException) EvaluationStrategyImpl(org.openrdf.query.algebra.evaluation.impl.EvaluationStrategyImpl)

Example 10 with SailException

use of org.openrdf.sail.SailException 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

SailException (org.openrdf.sail.SailException)23 SailConnection (org.openrdf.sail.SailConnection)10 Statement (org.openrdf.model.Statement)8 URI (org.openrdf.model.URI)8 HashSet (java.util.HashSet)4 Test (org.junit.Test)4 Namespace (org.openrdf.model.Namespace)4 QueryEvaluationException (org.openrdf.query.QueryEvaluationException)4 SailConnectionTripleSource (net.fortytwo.sesametools.SailConnectionTripleSource)3 Resource (org.openrdf.model.Resource)3 TripleSource (org.openrdf.query.algebra.evaluation.TripleSource)3 NotifyingSailConnection (org.openrdf.sail.NotifyingSailConnection)3 Edge (com.tinkerpop.blueprints.Edge)2 Vertex (com.tinkerpop.blueprints.Vertex)2 Properties (java.util.Properties)2 Value (org.openrdf.model.Value)2 ValueFactory (org.openrdf.model.ValueFactory)2 EvaluationStrategyImpl (org.openrdf.query.algebra.evaluation.impl.EvaluationStrategyImpl)2 Graph (com.tinkerpop.blueprints.Graph)1 KeyIndexableGraph (com.tinkerpop.blueprints.KeyIndexableGraph)1