use of org.openrdf.query.TupleQueryResult in project gocd by gocd.
the class SesameGraph method selectFirst.
public BoundVariables selectFirst(String sparqlSelect) {
BoundVariables boundVariables;
TupleQueryResult tupleQueryResult = getTupleQueryResult(sparqlSelect);
try {
if (!tupleQueryResult.hasNext()) {
return null;
}
boundVariables = new SesameBoundVariables(tupleQueryResult.getBindingNames(), tupleQueryResult.next());
if (tupleQueryResult.hasNext()) {
tupleQueryResult.close();
throw new MoreThanOneResultFoundException(sparqlSelect);
}
} catch (QueryEvaluationException e) {
throw new ShineRuntimeException("Could not parse query: <<" + sparqlSelect + ">>", e);
}
return boundVariables;
}
use of org.openrdf.query.TupleQueryResult in project backstage by zepheira.
the class DomLensNode method generateContentAttributes.
protected void generateContentAttributes(Value value, Scriptable result, Database database, SailRepositoryConnection connection) {
ScriptableArrayBuilder arrayBuilder = new ScriptableArrayBuilder();
for (ContentAttribute a : _contentAttributes) {
DefaultScriptableObject o = new DefaultScriptableObject();
o.put("name", o, a.name);
try {
boolean first = true;
StringBuffer sb = new StringBuffer();
ExpressionQueryResult eqr = a.expression.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());
if (first) {
first = false;
} else {
sb.append(";");
}
sb.append(renderInnerValueToText(value2, database, connection));
}
} finally {
queryResult.close();
}
}
o.put("value", o, sb.toString());
} catch (Exception e) {
_logger.error("", e);
}
arrayBuilder.add(o);
}
result.put("contentAttributes", result, arrayBuilder.toArray());
}
use of org.openrdf.query.TupleQueryResult 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);
}
use of org.openrdf.query.TupleQueryResult 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;
}
use of org.openrdf.query.TupleQueryResult 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 */
}
}
}
}
Aggregations