Search in sources :

Example 31 with Value

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

the class DomLensNode method generateContent.

protected void generateContent(Value value, Scriptable result, Database database, SailRepositoryConnection connection) {
    ScriptableArrayBuilder arrayBuilder = new ScriptableArrayBuilder();
    String valueType = "text";
    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(renderInnerValue(value2, database, connection));
                }
            } finally {
                queryResult.close();
            }
        }
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    DefaultScriptableObject o = new DefaultScriptableObject();
    o.put("valueType", o, valueType);
    o.put("values", o, arrayBuilder.toArray());
    result.put("content", result, o);
}
Also used : ScriptableArrayBuilder(edu.mit.simile.backstage.util.ScriptableArrayBuilder) BindingSet(org.openrdf.query.BindingSet) DefaultScriptableObject(edu.mit.simile.backstage.util.DefaultScriptableObject) ExpressionQueryResult(edu.mit.simile.backstage.model.data.ExpressionQueryResult) Value(org.openrdf.model.Value) TupleQueryResult(org.openrdf.query.TupleQueryResult)

Example 32 with Value

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

the class DomLensNode method testCondition.

protected boolean testCondition(Value value, Scriptable result, Database database, SailRepositoryConnection connection) {
    if (_conditionTest == null) {
        return true;
    }
    boolean r = false;
    try {
        ExpressionQueryResult eqr = _conditionExpression.computeOutputOnValue(value, database, connection);
        if (eqr != null) {
            TupleQueryResult queryResult = eqr.tupleQuery.evaluate();
            try {
                if ("if-exists".equals(_conditionTest)) {
                    r = queryResult.hasNext();
                } else if ("if".equals(_conditionTest)) {
                    if (queryResult.hasNext()) {
                        BindingSet bindingSet = queryResult.next();
                        Value value2 = bindingSet.getValue(eqr.resultVar.getName());
                        if (value2 instanceof Literal) {
                            r = ((Literal) value2).booleanValue();
                        }
                    }
                }
            } finally {
                queryResult.close();
            }
        } else {
            if (value instanceof Literal) {
                r = ((Literal) value).booleanValue();
            }
        }
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    result.put("condition", result, r);
    return r;
}
Also used : BindingSet(org.openrdf.query.BindingSet) ExpressionQueryResult(edu.mit.simile.backstage.model.data.ExpressionQueryResult) Literal(org.openrdf.model.Literal) Value(org.openrdf.model.Value) TupleQueryResult(org.openrdf.query.TupleQueryResult)

Example 33 with Value

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

the class Database method createTypeRecord.

private TypeRecord createTypeRecord(URI type, SailConnection sc) {
    String id = getTypeId(type, sc);
    String label = SailUtilities.getStringObject(sc, type, RDFS.LABEL, id);
    Properties properties = new Properties();
    CloseableIteration<? extends Statement, SailException> i = null;
    try {
        i = sc.getStatements(type, null, null, true);
    } catch (SailException e) {
        _logger.error("Failed to get all statements in order to get type 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)) {
                    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 TypeRecord(type, id, label, 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 34 with Value

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

the class RdfRepresentation method add.

@Override
public void add(String field, Object value) throws IllegalArgumentException {
    if (field == null) {
        throw new IllegalArgumentException("The parsed field MUST NOT be NULL");
    } else if (field.isEmpty()) {
        throw new IllegalArgumentException("The parsed field MUST NOT be Empty");
    }
    if (value == null) {
        throw new IllegalArgumentException("NULL values are not supported by Representations");
    }
    URI property = sesameFactory.createURI(field);
    Collection<Object> values = new ArrayList<Object>();
    //process the parsed value with the Utility Method ->
    // this converts Objects as defined in the specification
    ModelUtils.checkValues(factory, value, values);
    //We still need to implement support for specific types supported by this implementation
    for (Object current : values) {
        if (current instanceof Value) {
            //native support for Sesame types!
            addValue(property, (Value) current);
        } else if (current instanceof RdfWrapper) {
            //for Sesame RDF wrapper we can directly use the Value
            addValue(property, ((RdfWrapper) current).getValue());
        } else if (current instanceof Reference) {
            addValue(property, sesameFactory.createURI(((Reference) current).getReference()));
        } else if (current instanceof Text) {
            addValue(property, sesameFactory.createLiteral(((Text) current).getText(), ((Text) current).getLanguage()));
        } else {
            //else add an typed Literal!
            addValue(property, createTypedLiteral(current));
        }
    }
}
Also used : Reference(org.apache.stanbol.entityhub.servicesapi.model.Reference) ArrayList(java.util.ArrayList) Value(org.openrdf.model.Value) Text(org.apache.stanbol.entityhub.servicesapi.model.Text) URI(org.openrdf.model.URI)

Example 35 with Value

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

the class SesameYard method find.

@Override
public final QueryResultList<Representation> find(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, true);
        //parse the results and generate the Representations
        //create an own valueFactors so that all the data of the query results
        //are added to the same Sesame Model
        Model model = new TreeModel();
        RdfValueFactory valueFactory = new RdfValueFactory(model, sesameFactory);
        List<Representation> representations = limit > 0 ? new ArrayList<Representation>(limit) : new ArrayList<Representation>();
        Map<String, URI> bindings = new HashMap<String, URI>(query.getFieldVariableMappings().size());
        for (Entry<String, String> mapping : query.getFieldVariableMappings().entrySet()) {
            bindings.put(mapping.getValue(), sesameFactory.createURI(mapping.getKey()));
        }
        while (results.hasNext()) {
            BindingSet result = results.next();
            Value value = result.getValue(query.getRootVariableName());
            if (value instanceof URI) {
                URI subject = (URI) value;
                //link the result with the query result
                model.add(queryRoot, queryResult, subject);
                //now copy over the other selected data
                for (String binding : result.getBindingNames()) {
                    URI property = bindings.get(binding);
                    if (property != null) {
                        model.add(subject, property, result.getValue(binding));
                    }
                //else no mapping for the query.getRootVariableName()
                }
                //create a representation and add it to the results
                representations.add(valueFactory.createRdfRepresentation(subject));
            }
        //ignore non URI results
        }
        con.commit();
        return new SesameQueryResultList(model, query, representations);
    } 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) HashMap(java.util.HashMap) RdfRepresentation(org.apache.stanbol.entityhub.model.sesame.RdfRepresentation) Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) RepositoryException(org.openrdf.repository.RepositoryException) URI(org.openrdf.model.URI) TreeModel(org.openrdf.model.impl.TreeModel) YardException(org.apache.stanbol.entityhub.servicesapi.yard.YardException) QueryEvaluationException(org.openrdf.query.QueryEvaluationException) SparqlFieldQuery(org.apache.stanbol.entityhub.query.sparql.SparqlFieldQuery) Model(org.openrdf.model.Model) TreeModel(org.openrdf.model.impl.TreeModel) Value(org.openrdf.model.Value) TupleQueryResult(org.openrdf.query.TupleQueryResult) RdfValueFactory(org.apache.stanbol.entityhub.model.sesame.RdfValueFactory)

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