use of org.eclipse.rdf4j.query.parser.sparql.SPARQLParser in project incubator-rya by apache.
the class FluoQueryMetadataDAOIT method constructQueryMetadataTest.
@Test
public void constructQueryMetadataTest() throws MalformedQueryException {
final String query = "select ?x ?y where {?x <uri:p1> ?y. ?y <uri:p2> <uri:o1> }";
final SPARQLParser parser = new SPARQLParser();
final ParsedQuery pq = parser.parseQuery(query, null);
final List<StatementPattern> patterns = StatementPatternCollector.process(pq.getTupleExpr());
final FluoQueryMetadataDAO dao = new FluoQueryMetadataDAO();
// Create the object that will be serialized.
final ConstructQueryMetadata.Builder builder = ConstructQueryMetadata.builder();
builder.setNodeId("nodeId");
builder.setChildNodeId("childNodeId");
builder.setParentNodeId("parentNodeId");
builder.setVarOrder(new VariableOrder("a;b;c"));
builder.setConstructGraph(new ConstructGraph(patterns));
final ConstructQueryMetadata originalMetadata = builder.build();
try (FluoClient fluoClient = FluoFactory.newClient(super.getFluoConfiguration())) {
// Write it to the Fluo table.
try (Transaction tx = fluoClient.newTransaction()) {
dao.write(tx, originalMetadata);
tx.commit();
}
// Read it from the Fluo table.
ConstructQueryMetadata storedMetdata = null;
try (Snapshot sx = fluoClient.newSnapshot()) {
storedMetdata = dao.readConstructQueryMetadata(sx, "nodeId");
}
// Ensure the deserialized object is the same as the serialized one.
assertEquals(originalMetadata, storedMetdata);
}
}
use of org.eclipse.rdf4j.query.parser.sparql.SPARQLParser in project incubator-rya by apache.
the class FluoAndHistoricPcjsDemo method prettyFormatSparql.
private static String[] prettyFormatSparql(final String sparql) throws Exception {
final SPARQLParser parser = new SPARQLParser();
final SPARQLQueryRenderer renderer = new SPARQLQueryRenderer();
final ParsedQuery pq = parser.parseQuery(sparql, null);
final String prettySparql = renderer.render(pq);
return StringUtils.split(prettySparql, '\n');
}
use of org.eclipse.rdf4j.query.parser.sparql.SPARQLParser in project incubator-rya by apache.
the class PeriodicQueryUtilTest method testPeriodicNodePlacement.
@Test
public void testPeriodicNodePlacement() throws MalformedQueryException {
String query = // n
"prefix function: <http://org.apache.rya/function#> " + // n
"prefix time: <http://www.w3.org/2006/time#> " + // n
"prefix fn: <http://www.w3.org/2006/fn#> " + // n
"select ?obs ?time ?lat where {" + // n
"Filter(function:periodic(?time, 12.0, 6.0,time:hours)) " + // n
"Filter(fn:test(?lat, 25)) " + // n
"?obs <uri:hasTime> ?time. " + // n
"?obs <uri:hasLattitude> ?lat }";
SPARQLParser parser = new SPARQLParser();
ParsedQuery pq = parser.parseQuery(query, null);
TupleExpr te = pq.getTupleExpr();
te.visit(new PeriodicQueryNodeVisitor());
PeriodicNodeCollector collector = new PeriodicNodeCollector();
te.visit(collector);
PeriodicQueryNode node2 = new PeriodicQueryNode(12 * 60 * 60 * 1000L, 6 * 3600 * 1000L, TimeUnit.MILLISECONDS, "time", new Join());
Assert.assertEquals(true, periodicNodesEqualIgnoreArg(node2, collector.getPeriodicQueryNode()));
}
use of org.eclipse.rdf4j.query.parser.sparql.SPARQLParser in project incubator-rya by apache.
the class PeriodicQueryUtilTest method testPeriodicNodeLocation.
@Test
public void testPeriodicNodeLocation() throws MalformedQueryException {
String query = // n
"prefix function: <http://org.apache.rya/function#> " + // n
"prefix time: <http://www.w3.org/2006/time#> " + // n
"prefix fn: <http://www.w3.org/2006/fn#> " + // n
"select ?obs ?time ?lat where {" + // n
"Filter(function:periodic(?time, 1,.5,time:hours)) " + // n
"Filter(fn:test(?lat, 25)) " + // n
"?obs <uri:hasTime> ?time. " + // n
"?obs <uri:hasLattitude> ?lat }";
SPARQLParser parser = new SPARQLParser();
ParsedQuery pq = parser.parseQuery(query, null);
TupleExpr te = pq.getTupleExpr();
te.visit(new PeriodicQueryNodeVisitor());
PeriodicNodeCollector collector = new PeriodicNodeCollector();
te.visit(collector);
Assert.assertEquals(2, collector.getPos());
te.visit(new PeriodicQueryNodeRelocator());
collector.resetCount();
te.visit(collector);
Assert.assertEquals(1, collector.getPos());
double window = 1 * 60 * 60 * 1000;
double period = .5 * 3600 * 1000;
PeriodicQueryNode node2 = new PeriodicQueryNode((long) window, (long) period, TimeUnit.MILLISECONDS, "time", new Join());
Assert.assertEquals(true, periodicNodesEqualIgnoreArg(node2, collector.getPeriodicQueryNode()));
}
use of org.eclipse.rdf4j.query.parser.sparql.SPARQLParser in project incubator-rya by apache.
the class FilterSerializerTest method nowTest.
@Test
public void nowTest() throws Exception {
// tests to see if NOW function is correctly serialized and deserialized
// by FilterSerializer
String query = "select * {Filter(NOW())}";
SPARQLParser parser = new SPARQLParser();
ParsedQuery pq = parser.parseQuery(query, null);
Filter filter = (Filter) ((Projection) pq.getTupleExpr()).getArg();
String filterString = FilterSerializer.serialize(filter);
Filter deserializedFilter = FilterSerializer.deserialize(filterString);
assertEquals(filter, deserializedFilter);
}
Aggregations