use of org.apache.jena.query.Query in project jena by apache.
the class ValuesHandlerTest method testAddNotSquare.
@Test
public void testAddNotSquare() {
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");
Var v3 = Var.alloc("z");
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(v2, Arrays.asList(n3));
handler2.addValueVar(v3, Arrays.asList(nn3));
handler.addAll(handler2);
handler.build();
List<Var> vars = query.getValuesVariables();
assertEquals(3, vars.size());
assertTrue(vars.contains(v));
assertTrue(vars.contains(v2));
assertTrue(vars.contains(v3));
assertNotNull(query.getValuesData());
List<Binding> lb = query.getValuesData();
assertEquals(3, lb.size());
for (Binding b : lb) {
assertTrue(b.contains(v2));
Node node = b.get(v2);
if (node.equals(nn)) {
assertEquals(n, b.get(v));
assertFalse(b.contains(v3));
} else if (node.equals(nn2)) {
assertEquals(n2, b.get(v));
assertFalse(b.contains(v3));
} else if (node.equals(n3)) {
assertFalse(b.contains(v));
assertEquals(nn3, b.get(v3));
} else {
fail("Wrong data in table");
}
}
}
use of org.apache.jena.query.Query in project jena by apache.
the class ExTDB5 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);
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();
}
// Close the dataset.
dataset.close();
}
use of org.apache.jena.query.Query in project jena by apache.
the class TestDatasetWithLuceneStoredLiterals method doTestSearchWithLiteralsMultiple.
protected List<Node> doTestSearchWithLiteralsMultiple(String turtle, String queryString, String expectedEntityURI) {
List<Node> literals = new LinkedList<>();
Model model = dataset.getDefaultModel();
Reader reader = new StringReader(turtle);
dataset.begin(ReadWrite.WRITE);
model.read(reader, "", "TURTLE");
dataset.commit();
Query query = QueryFactory.create(queryString);
dataset.begin(ReadWrite.READ);
try (QueryExecution qexec = QueryExecutionFactory.create(query, dataset)) {
ResultSet results = qexec.execSelect();
int count;
for (count = 0; results.hasNext(); count++) {
QuerySolution soln = results.nextSolution();
String entityURI = soln.getResource("s").getURI();
assertEquals(expectedEntityURI, entityURI);
Literal literal = soln.getLiteral("literal");
assertNotNull(literal);
literals.add(literal.asNode());
}
} finally {
dataset.end();
}
return literals;
}
use of org.apache.jena.query.Query in project jena by apache.
the class AbstractTestTransform method testQuery.
public void testQuery(String input, Transform transform, String... output) {
Query q = QueryFactory.create(input);
Op op = Algebra.compile(q);
test(op, transform, output);
}
use of org.apache.jena.query.Query in project jena by apache.
the class TestOpAsQuery method test_roundTripAlegbra.
// Compare A1 and A2 where
// query[Q1]->algebra[A1]->OpAsQuery->query[Q2]->algebra[A2]
// Sometimes Q1 and Q2 are equivalent but not .equals.
public void test_roundTripAlegbra(String query) {
Query[] r = roundTripQuery(query);
// Even if the strings come out as non-equal because of the translation from algebra to query
// the algebras should be equal
// i.e. the queries should remain semantically equivalent
Op a1 = Algebra.compile(r[0]);
Op a2 = Algebra.compile(r[1]);
Assert.assertEquals(a1, a2);
}
Aggregations