use of org.openrdf.query.BindingSet in project stanbol by apache.
the class SesameYard method findRepresentation.
@Override
public QueryResultList<Representation> findRepresentation(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 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>();
while (results.hasNext()) {
BindingSet result = results.next();
Value value = result.getValue(query.getRootVariableName());
if (value instanceof URI) {
//copy all data to the model and create the representation
RdfRepresentation rep = createRepresentationGraph(con, valueFactory, (URI) value);
//link the result with the query result
model.add(queryRoot, queryResult, value);
representations.add(rep);
}
//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 */
}
}
}
}
use of org.openrdf.query.BindingSet in project blueprints by tinkerpop.
the class SailTest method testJoins.
@Test
public void testJoins() throws Exception {
SPARQLParser parser = new SPARQLParser();
BindingSet bindings = new EmptyBindingSet();
String baseURI = "http://example.org/bogus/";
SailConnection sc = sail.getConnection();
try {
CloseableIteration<? extends BindingSet, QueryEvaluationException> results;
int count;
String queryStr = "PREFIX : <urn:com.tinkerpop.blueprints.pgm.oupls.sail.test/>\n" + "PREFIX foaf: <http://xmlns.com/foaf/0.1/>\n" + "SELECT ?foaf WHERE {\n" + " :ford foaf:knows ?friend .\n" + " ?friend foaf:knows ?foaf .\n" + "}";
ParsedQuery query = parser.parseQuery(queryStr, baseURI);
results = sc.evaluate(query.getTupleExpr(), query.getDataset(), bindings, false);
count = 0;
while (results.hasNext()) {
count++;
BindingSet set = results.next();
URI foaf = (URI) set.getValue("foaf");
assertTrue(foaf.stringValue().startsWith("urn:com.tinkerpop.blueprints.pgm.oupls.sail.test/"));
}
results.close();
assertEquals(4, count);
} finally {
sc.rollback();
sc.close();
}
}
use of org.openrdf.query.BindingSet in project blueprints by tinkerpop.
the class SailTest method testEvaluate.
// tuple queries ///////////////////////////////////////////////////////////
@Test
public void testEvaluate() throws Exception {
Set<String> languages;
String prefix = "urn:com.tinkerpop.blueprints.pgm.oupls.sail.test/";
URI thorUri = sail.getValueFactory().createURI(prefix + "thor");
SailConnection sc = sail.getConnection();
try {
sc.begin();
URI uriA = sail.getValueFactory().createURI("http://example.org/uriA");
URI uriB = sail.getValueFactory().createURI("http://example.org/uriB");
URI uriC = sail.getValueFactory().createURI("http://example.org/uriC");
sc.addStatement(uriA, uriB, uriC);
sc.commit();
sc.begin();
SPARQLParser parser = new SPARQLParser();
BindingSet bindings = new EmptyBindingSet();
String baseURI = "http://example.org/bogus/";
String queryStr;
ParsedQuery query;
CloseableIteration<? extends BindingSet, QueryEvaluationException> results;
int count;
// s ?p ?o SELECT
queryStr = "SELECT ?y ?z WHERE { <http://example.org/uriA> ?y ?z }";
query = parser.parseQuery(queryStr, baseURI);
results = sc.evaluate(query.getTupleExpr(), query.getDataset(), bindings, false);
count = 0;
while (results.hasNext()) {
count++;
BindingSet set = results.next();
URI y = (URI) set.getValue("y");
Value z = (Value) set.getValue("z");
assertNotNull(y);
assertNotNull(z);
// System.out.println("y = " + y + ", z = " + z);
}
results.close();
assertTrue(count > 0);
// s p ?o SELECT using a namespace prefix
queryStr = "PREFIX foaf: <http://xmlns.com/foaf/0.1/>\n" + "SELECT ?z WHERE { <" + prefix + "thor> foaf:name ?z }";
query = parser.parseQuery(queryStr, baseURI);
results = sc.evaluate(query.getTupleExpr(), query.getDataset(), bindings, false);
count = 0;
languages = new HashSet<String>();
while (results.hasNext()) {
count++;
BindingSet set = results.next();
Literal z = (Literal) set.getValue("z");
assertNotNull(z);
languages.add(z.getLanguage());
}
results.close();
assertTrue(count > 0);
assertEquals(2, languages.size());
assertTrue(languages.contains("en"));
assertTrue(languages.contains("is"));
// ?s p o SELECT using a plain literal value with no language tag
queryStr = "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n" + "SELECT ?s WHERE { ?s rdfs:comment \"he really knows where his towel is\" }";
URI fordUri = sail.getValueFactory().createURI(prefix + "ford");
query = parser.parseQuery(queryStr, baseURI);
results = sc.evaluate(query.getTupleExpr(), query.getDataset(), bindings, false);
count = 0;
while (results.hasNext()) {
count++;
BindingSet set = results.next();
URI s = (URI) set.getValue("s");
assertNotNull(s);
assertEquals(s, fordUri);
}
results.close();
assertTrue(count > 0);
// ?s p o SELECT using a language-specific literal value
queryStr = "PREFIX foaf: <http://xmlns.com/foaf/0.1/>\n" + "SELECT ?s WHERE { ?s foaf:name \"Thor\"@en }";
query = parser.parseQuery(queryStr, baseURI);
results = sc.evaluate(query.getTupleExpr(), query.getDataset(), bindings, false);
count = 0;
while (results.hasNext()) {
count++;
BindingSet set = results.next();
URI s = (URI) set.getValue("s");
assertNotNull(s);
assertEquals(s, thorUri);
}
results.close();
assertTrue(count > 0);
// The language tag is necessary
queryStr = "PREFIX foaf: <http://xmlns.com/foaf/0.1/>\n" + "SELECT ?s WHERE { ?s foaf:name \"Thor\" }";
query = parser.parseQuery(queryStr, baseURI);
results = sc.evaluate(query.getTupleExpr(), query.getDataset(), bindings, false);
count = 0;
while (results.hasNext()) {
count++;
results.next();
}
results.close();
assertEquals(0, count);
// ?s p o SELECT using a typed literal value
queryStr = "PREFIX foaf: <http://xmlns.com/foaf/0.1/>\n" + "PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>\n" + "SELECT ?s WHERE { ?s foaf:msnChatID \"Thorster123\"^^xsd:string }";
query = parser.parseQuery(queryStr, baseURI);
results = sc.evaluate(query.getTupleExpr(), query.getDataset(), bindings, false);
count = 0;
while (results.hasNext()) {
count++;
BindingSet set = results.next();
URI s = (URI) set.getValue("s");
assertNotNull(s);
assertEquals(s, thorUri);
}
results.close();
assertTrue(count > 0);
// The data type is necessary
queryStr = "PREFIX foaf: <http://xmlns.com/foaf/0.1/>\n" + "PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>\n" + "SELECT ?s WHERE { ?s foaf:msnChatID \"Thorster123\" }";
query = parser.parseQuery(queryStr, baseURI);
results = sc.evaluate(query.getTupleExpr(), query.getDataset(), bindings, false);
count = 0;
while (results.hasNext()) {
count++;
results.next();
}
results.close();
assertEquals(0, count);
// s ?p o SELECT
// TODO: commented out languages for now
queryStr = "PREFIX foaf: <http://xmlns.com/foaf/0.1/>\n" + "PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>\n" + "SELECT ?p WHERE { <" + prefix + "thor> ?p \"Thor\"@en }";
query = parser.parseQuery(queryStr, baseURI);
URI foafNameUri = sail.getValueFactory().createURI("http://xmlns.com/foaf/0.1/name");
results = sc.evaluate(query.getTupleExpr(), query.getDataset(), bindings, false);
count = 0;
while (results.hasNext()) {
count++;
BindingSet set = results.next();
URI p = (URI) set.getValue("p");
assertNotNull(p);
assertEquals(p, foafNameUri);
}
results.close();
assertTrue(count > 0);
// context-specific SELECT
queryStr = "PREFIX foaf: <http://xmlns.com/foaf/0.1/>\n" + "SELECT ?z\n" + "FROM <" + prefix + "ctx1>\n" + "WHERE { <" + prefix + "thor> foaf:name ?z }";
query = parser.parseQuery(queryStr, baseURI);
results = sc.evaluate(query.getTupleExpr(), query.getDataset(), bindings, false);
count = 0;
languages = new HashSet<String>();
while (results.hasNext()) {
count++;
BindingSet set = results.next();
Literal z = (Literal) set.getValue("z");
assertNotNull(z);
languages.add(z.getLanguage());
}
results.close();
assertTrue(count > 0);
assertEquals(2, languages.size());
assertTrue(languages.contains("en"));
assertTrue(languages.contains("is"));
queryStr = "PREFIX foaf: <http://xmlns.com/foaf/0.1/>\n" + "SELECT ?z\n" + "FROM <http://example.org/emptycontext>\n" + "WHERE { <" + prefix + "thor> foaf:name ?z }";
query = parser.parseQuery(queryStr, baseURI);
results = sc.evaluate(query.getTupleExpr(), query.getDataset(), bindings, false);
count = 0;
while (results.hasNext()) {
count++;
results.next();
}
results.close();
assertEquals(0, count);
// s p o? select without and with inferencing
// TODO commented out waiting for inferencing
// queryStr =
// "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n"
// + "SELECT ?o\n"
// + "WHERE { <" + prefix + "instance1> rdf:type ?o }";
// query = parser.parseQuery(queryStr, baseURI);
// results = sc.evaluate(query.getTupleExpr(), query.getDataset(),
// bindings, false);
// count = 0;
// while (results.hasNext()) {
// count++;
// BindingSet set = results.next();
// URI o = (URI) set.getValue("o");
// assertEquals(prefix + "classB", o.toString());
// }
// results.close();
// assertEquals(1, count);
// results = sc.evaluate(query.getTupleExpr(), query.getDataset(),
// bindings, true);
// count = 0;
// boolean foundA = false, foundB = false;
// while (results.hasNext()) {
// count++;
// BindingSet set = results.next();
// URI o = (URI) set.getValue("o");
// String s = o.toString();
// if (s.equals(prefix + "classA")) {
// foundA = true;
// } else if (s.equals(prefix + "classB")) {
// foundB = true;
// }
// }
// results.close();
// assertEquals(2, count);
// assertTrue(foundA);
// assertTrue(foundB);
} finally {
sc.rollback();
sc.close();
}
}
use of org.openrdf.query.BindingSet in project backstage by zepheira.
the class ListFacet method createComponentState.
protected List<FacetChoice> createComponentState(TupleQueryResult queryResult) throws QueryEvaluationException {
List<FacetChoice> facetChoices = new ArrayList<FacetChoice>();
Database database = _context.getDatabase();
while (queryResult.hasNext()) {
BindingSet bindingSet = queryResult.next();
Value value = bindingSet.getValue(_valueVar.getName());
Value count = bindingSet.getValue(_countVar.getName());
String s = valueToString(value);
int c = Integer.parseInt(count.stringValue());
FacetChoice fc = new FacetChoice();
fc._value = value;
fc._valueString = s;
fc._count = c;
fc._label = database.valueToLabel(value);
facetChoices.add(fc);
}
return facetChoices;
}
use of org.openrdf.query.BindingSet 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());
}
Aggregations