use of org.apache.jena.query.Query in project jena by apache.
the class ValuesHandlerTest method testAddSquare.
@Test
public void testAddSquare() {
Node n = NodeFactory.createLiteral("hello");
Node nn = NodeFactory.createLiteral("hola");
Node n2 = NodeFactory.createLiteral("there");
Node nn2 = NodeFactory.createLiteral("aqui");
Var v = Var.alloc("x");
Var v2 = Var.alloc("y");
handler.addValueVar(v, Arrays.asList(n, n2));
handler.addValueVar(v2, Arrays.asList(nn, nn2));
ValuesHandler handler2 = new ValuesHandler(new Query());
Node n3 = NodeFactory.createLiteral("why");
Node nn3 = NodeFactory.createLiteral("quando");
handler2.addValueVar(v, Arrays.asList(n3));
handler2.addValueVar(v2, Arrays.asList(nn3));
handler.addAll(handler2);
handler.build();
List<Var> vars = query.getValuesVariables();
assertEquals(2, vars.size());
assertTrue(vars.contains(v));
assertTrue(vars.contains(v2));
assertNotNull(query.getValuesData());
List<Binding> lb = query.getValuesData();
assertEquals(3, lb.size());
List<Node> ln = new ArrayList<Node>();
ln.add(n);
ln.add(n2);
ln.add(n3);
List<Node> ln2 = new ArrayList<Node>();
ln2.add(nn);
ln2.add(nn2);
ln2.add(nn3);
for (Binding b : lb) {
assertTrue(b.contains(v));
assertTrue(ln.contains(b.get(v)));
ln.remove(b.get(v));
assertTrue(b.contains(v2));
assertTrue(ln2.contains(b.get(v2)));
ln2.remove(b.get(v2));
}
}
use of org.apache.jena.query.Query in project jena by apache.
the class WhereHandlerTest method testAddAllPopulatedEmpty.
@Test
public void testAddAllPopulatedEmpty() {
handler.addWhere(new TriplePath(Triple.ANY));
Query query2 = new Query();
WhereHandler handler2 = new WhereHandler(query2);
handler2.addWhere(new TriplePath(new Triple(NodeFactory.createURI("one"), NodeFactory.createURI("two"), NodeFactory.createLiteral("three"))));
handler.addAll(handler2);
assertContainsRegex(WHERE + OPEN_CURLY + "ANY" + SPACE + "ANY" + SPACE + "ANY" + DOT + SPACE + uri("one") + SPACE + uri("two") + SPACE + quote("three") + OPT_SPACE + CLOSE_CURLY, query.toString());
}
use of org.apache.jena.query.Query in project jena by apache.
the class WhereHandlerTest method testAddAllOnEmpty.
@Test
public void testAddAllOnEmpty() {
Query query2 = new Query();
WhereHandler handler2 = new WhereHandler(query2);
handler2.addWhere(new TriplePath(new Triple(NodeFactory.createURI("one"), NodeFactory.createURI("two"), NodeFactory.createLiteral("three"))));
handler.addAll(handler2);
assertContainsRegex(WHERE + OPEN_CURLY + uri("one") + SPACE + uri("two") + SPACE + quote("three") + OPT_SPACE + CLOSE_CURLY, query.toString());
}
use of org.apache.jena.query.Query in project jena by apache.
the class ExTDB4 method main.
public static void main(String... argv) {
// Direct way: Make a TDB-back Jena model in the named directory.
String directory = "MyDatabases/DB1";
Dataset dataset = TDBFactory.createDataset(directory);
// Potentially expensive query.
String sparqlQueryString = "SELECT (count(*) AS ?count) { ?s ?p ?o }";
// See http://incubator.apache.org/jena/documentation/query/app_api.html
Query query = QueryFactory.create(sparqlQueryString);
QueryExecution qexec = QueryExecutionFactory.create(query, dataset);
ResultSet results = qexec.execSelect();
ResultSetFormatter.out(results);
qexec.close();
dataset.close();
}
use of org.apache.jena.query.Query in project jena by apache.
the class ExTDB_Txn1 method execQuery.
public static void execQuery(String sparqlQueryString, Dataset dataset) {
Query query = QueryFactory.create(sparqlQueryString);
QueryExecution qexec = QueryExecutionFactory.create(query, dataset);
try {
ResultSet results = qexec.execSelect();
for (; results.hasNext(); ) {
QuerySolution soln = results.nextSolution();
int count = soln.getLiteral("count").getInt();
System.out.println("count = " + count);
}
} finally {
qexec.close();
}
}
Aggregations