Search in sources :

Example 6 with Value

use of org.openrdf.model.Value in project backstage by zepheira.

the class DomLensNode method generateContentWithInnerTemplates.

protected void generateContentWithInnerTemplates(Value value, Scriptable result, Database database, SailRepositoryConnection connection) {
    ScriptableArrayBuilder arrayBuilder = new ScriptableArrayBuilder();
    try {
        ExpressionQueryResult eqr = _contentExpression.computeOutputOnValue(value, database, connection);
        if (eqr != null) {
            TupleQueryResult queryResult = eqr.tupleQuery.evaluate();
            try {
                while (queryResult.hasNext()) {
                    BindingSet bindingSet = queryResult.next();
                    Value value2 = bindingSet.getValue(eqr.resultVar.getName());
                    arrayBuilder.add(generateInnerContentWithInnerTemplates(value2, database, connection));
                }
            } finally {
                queryResult.close();
            }
        } else {
            arrayBuilder.add(generateInnerContentWithInnerTemplates(value, database, connection));
        }
    } catch (Exception e) {
        _logger.error("", e);
    }
    result.put("content", result, arrayBuilder.toArray());
}
Also used : ScriptableArrayBuilder(edu.mit.simile.backstage.util.ScriptableArrayBuilder) BindingSet(org.openrdf.query.BindingSet) ExpressionQueryResult(edu.mit.simile.backstage.model.data.ExpressionQueryResult) Value(org.openrdf.model.Value) TupleQueryResult(org.openrdf.query.TupleQueryResult)

Example 7 with Value

use of org.openrdf.model.Value in project stanbol by apache.

the class AbstractSesameBackend method listObjectsInternal.

protected Collection<Value> listObjectsInternal(RepositoryConnection connection, Resource subject, org.openrdf.model.URI property, boolean includeInferred, Resource... context) throws RepositoryException {
    ValueFactory valueFactory = connection.getValueFactory();
    Set<Value> result = new HashSet<Value>();
    RepositoryResult<Statement> qResult = connection.getStatements(merge(subject, connection.getValueFactory()), merge(property, connection.getValueFactory()), null, includeInferred, context);
    try {
        while (qResult.hasNext()) {
            result.add(qResult.next().getObject());
        }
    } finally {
        qResult.close();
    }
    return result;
}
Also used : Statement(org.openrdf.model.Statement) Value(org.openrdf.model.Value) ValueFactory(org.openrdf.model.ValueFactory) HashSet(java.util.HashSet)

Example 8 with Value

use of org.openrdf.model.Value in project stanbol by apache.

the class RdfIndexingSource method extractRepresentation.

/**
     * Extracts all {@link Statement}s part of the Representation. If
     * {@link #followBNodeState} this is called recursively for {@link Statement}s
     * where the value is an {@link BNode}.
     */
protected void extractRepresentation(RepositoryConnection con, Model model, Resource node, Set<BNode> visited) throws RepositoryException {
    //we need all the outgoing relations and also want to follow bNodes until
    //the next UriRef. However we are not interested in incoming relations!
    RepositoryResult<Statement> outgoing = con.getStatements(node, null, null, includeInferred, contexts);
    Statement statement;
    Set<BNode> bnodes = followBNodeState ? new HashSet<BNode>() : null;
    while (outgoing.hasNext()) {
        statement = outgoing.next();
        model.add(statement);
        if (followBNodeState) {
            Value object = statement.getObject();
            if (object instanceof BNode && !visited.contains(object)) {
                bnodes.add((BNode) object);
            }
        }
    //else do not follow values beeing BNodes
    }
    outgoing.close();
    if (followBNodeState) {
        for (BNode bnode : bnodes) {
            visited.add(bnode);
            //TODO: recursive calls could cause stackoverflows with wired graphs
            extractRepresentation(con, model, bnode, visited);
        }
    }
}
Also used : BNode(org.openrdf.model.BNode) Statement(org.openrdf.model.Statement) Value(org.openrdf.model.Value)

Example 9 with Value

use of org.openrdf.model.Value in project stanbol by apache.

the class SesameYard method findReferences.

@Override
public QueryResultList<String> findReferences(FieldQuery parsedQuery) throws YardException, IllegalArgumentException {
    if (parsedQuery == null) {
        throw new IllegalArgumentException("The parsed query MUST NOT be NULL!");
    }
    final SparqlFieldQuery query = SparqlFieldQueryFactory.getSparqlFieldQuery(parsedQuery);
    RepositoryConnection con = null;
    TupleQueryResult results = null;
    try {
        con = repository.getConnection();
        con.begin();
        //execute the query
        int limit = QueryUtils.getLimit(query, getConfig().getDefaultQueryResultNumber(), getConfig().getMaxQueryResultNumber());
        results = executeSparqlFieldQuery(con, query, limit, false);
        //parse the results
        List<String> ids = limit > 0 ? new ArrayList<String>(limit) : new ArrayList<String>();
        while (results.hasNext()) {
            BindingSet result = results.next();
            Value value = result.getValue(query.getRootVariableName());
            if (value instanceof Resource) {
                ids.add(value.stringValue());
            }
        }
        con.commit();
        return new QueryResultListImpl<String>(query, ids, String.class);
    } catch (RepositoryException e) {
        throw new YardException("Unable to execute findReferences query", e);
    } catch (QueryEvaluationException e) {
        throw new YardException("Unable to execute findReferences query", e);
    } finally {
        if (results != null) {
            //close the result if present
            try {
                results.close();
            } catch (QueryEvaluationException ignore) {
            /* ignore */
            }
        }
        if (con != null) {
            try {
                con.close();
            } catch (RepositoryException ignore) {
            /* ignore */
            }
        }
    }
}
Also used : RepositoryConnection(org.openrdf.repository.RepositoryConnection) BindingSet(org.openrdf.query.BindingSet) Resource(org.openrdf.model.Resource) RepositoryException(org.openrdf.repository.RepositoryException) YardException(org.apache.stanbol.entityhub.servicesapi.yard.YardException) QueryEvaluationException(org.openrdf.query.QueryEvaluationException) SparqlFieldQuery(org.apache.stanbol.entityhub.query.sparql.SparqlFieldQuery) Value(org.openrdf.model.Value) QueryResultListImpl(org.apache.stanbol.entityhub.core.query.QueryResultListImpl) TupleQueryResult(org.openrdf.query.TupleQueryResult)

Example 10 with Value

use of org.openrdf.model.Value in project stanbol by apache.

the class SesameYard method extractRepresentation.

/**
     * Recursive Method internally doing all the work for 
     * {@link #createRepresentationGraph(UriRef, TripleCollection)}
     * @param con the repository connection to read the data from
     * @param model The model to add the statements retrieved
     * @param node the current node. Changes in recursive calls as it follows
     * @param visited holding all the visited BNodes to avoid cycles. Other nodes 
     * need not be added because this implementation would not follow it anyway
     * outgoing relations if the object is a {@link BNode} instance.
     * @throws RepositoryException 
     */
private void extractRepresentation(RepositoryConnection con, Model model, Resource node, Set<BNode> visited) throws RepositoryException {
    //we need all the outgoing relations and also want to follow bNodes until
    //the next UriRef. However we are not interested in incoming relations!
    RepositoryResult<Statement> outgoing = con.getStatements(node, null, null, includeInferred, contexts);
    Statement statement;
    Set<BNode> bnodes = new HashSet<BNode>();
    while (outgoing.hasNext()) {
        statement = outgoing.next();
        model.add(statement);
        Value object = statement.getObject();
        if (object instanceof BNode && !visited.contains(object)) {
            bnodes.add((BNode) object);
        }
    }
    outgoing.close();
    for (BNode bnode : bnodes) {
        visited.add(bnode);
        //TODO: recursive calls could cause stackoverflows with wired graphs
        extractRepresentation(con, model, bnode, visited);
    }
}
Also used : BNode(org.openrdf.model.BNode) Statement(org.openrdf.model.Statement) Value(org.openrdf.model.Value) HashSet(java.util.HashSet)

Aggregations

Value (org.openrdf.model.Value)46 Statement (org.openrdf.model.Statement)22 URI (org.openrdf.model.URI)22 Resource (org.openrdf.model.Resource)17 HashSet (java.util.HashSet)11 BindingSet (org.openrdf.query.BindingSet)10 TupleQueryResult (org.openrdf.query.TupleQueryResult)8 BNode (org.openrdf.model.BNode)6 Literal (org.openrdf.model.Literal)6 ExpressionQueryResult (edu.mit.simile.backstage.model.data.ExpressionQueryResult)5 ScriptableArrayBuilder (edu.mit.simile.backstage.util.ScriptableArrayBuilder)5 HashMap (java.util.HashMap)5 QueryEvaluationException (org.openrdf.query.QueryEvaluationException)5 DefaultScriptableObject (edu.mit.simile.backstage.util.DefaultScriptableObject)4 ArrayList (java.util.ArrayList)4 Graph (org.openrdf.model.Graph)4 SailException (org.openrdf.sail.SailException)4 Map (java.util.Map)3 Set (java.util.Set)3 SparqlFieldQuery (org.apache.stanbol.entityhub.query.sparql.SparqlFieldQuery)3