Search in sources :

Example 6 with SPARQLParser

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);
    }
}
Also used : SPARQLParser(org.eclipse.rdf4j.query.parser.sparql.SPARQLParser) FluoClient(org.apache.fluo.api.client.FluoClient) ParsedQuery(org.eclipse.rdf4j.query.parser.ParsedQuery) VariableOrder(org.apache.rya.indexing.pcj.storage.accumulo.VariableOrder) ConstructGraph(org.apache.rya.indexing.pcj.fluo.app.ConstructGraph) StatementPattern(org.eclipse.rdf4j.query.algebra.StatementPattern) Snapshot(org.apache.fluo.api.client.Snapshot) Transaction(org.apache.fluo.api.client.Transaction) Test(org.junit.Test)

Example 7 with SPARQLParser

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');
}
Also used : SPARQLParser(org.eclipse.rdf4j.query.parser.sparql.SPARQLParser) ParsedQuery(org.eclipse.rdf4j.query.parser.ParsedQuery) SPARQLQueryRenderer(org.eclipse.rdf4j.queryrender.sparql.SPARQLQueryRenderer)

Example 8 with SPARQLParser

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()));
}
Also used : SPARQLParser(org.eclipse.rdf4j.query.parser.sparql.SPARQLParser) PeriodicQueryNodeVisitor(org.apache.rya.indexing.pcj.fluo.app.util.PeriodicQueryUtil.PeriodicQueryNodeVisitor) ParsedQuery(org.eclipse.rdf4j.query.parser.ParsedQuery) Join(org.eclipse.rdf4j.query.algebra.Join) TupleExpr(org.eclipse.rdf4j.query.algebra.TupleExpr) Test(org.junit.Test)

Example 9 with SPARQLParser

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()));
}
Also used : SPARQLParser(org.eclipse.rdf4j.query.parser.sparql.SPARQLParser) PeriodicQueryNodeVisitor(org.apache.rya.indexing.pcj.fluo.app.util.PeriodicQueryUtil.PeriodicQueryNodeVisitor) ParsedQuery(org.eclipse.rdf4j.query.parser.ParsedQuery) PeriodicQueryNodeRelocator(org.apache.rya.indexing.pcj.fluo.app.util.PeriodicQueryUtil.PeriodicQueryNodeRelocator) Join(org.eclipse.rdf4j.query.algebra.Join) TupleExpr(org.eclipse.rdf4j.query.algebra.TupleExpr) Test(org.junit.Test)

Example 10 with SPARQLParser

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);
}
Also used : SPARQLParser(org.eclipse.rdf4j.query.parser.sparql.SPARQLParser) ParsedQuery(org.eclipse.rdf4j.query.parser.ParsedQuery) Filter(org.eclipse.rdf4j.query.algebra.Filter) Test(org.junit.Test)

Aggregations

SPARQLParser (org.eclipse.rdf4j.query.parser.sparql.SPARQLParser)260 ParsedQuery (org.eclipse.rdf4j.query.parser.ParsedQuery)244 Test (org.junit.Test)208 TupleExpr (org.eclipse.rdf4j.query.algebra.TupleExpr)162 ArrayList (java.util.ArrayList)99 ExternalTupleSet (org.apache.rya.indexing.external.tupleSet.ExternalTupleSet)99 SimpleExternalTupleSet (org.apache.rya.indexing.external.tupleSet.SimpleExternalTupleSet)86 StatementPattern (org.eclipse.rdf4j.query.algebra.StatementPattern)82 QueryModelNode (org.eclipse.rdf4j.query.algebra.QueryModelNode)62 PCJOptimizer (org.apache.rya.indexing.pcj.matching.PCJOptimizer)46 QueryBindingSet (org.eclipse.rdf4j.query.algebra.evaluation.QueryBindingSet)43 RyaIRI (org.apache.rya.api.domain.RyaIRI)42 QueryEvaluationException (org.eclipse.rdf4j.query.QueryEvaluationException)40 RyaStatement (org.apache.rya.api.domain.RyaStatement)39 BindingSet (org.eclipse.rdf4j.query.BindingSet)37 Projection (org.eclipse.rdf4j.query.algebra.Projection)37 RyaType (org.apache.rya.api.domain.RyaType)35 HashSet (java.util.HashSet)27 StatementMetadata (org.apache.rya.api.domain.StatementMetadata)20 StatementMetadataNode (org.apache.rya.indexing.statement.metadata.matching.StatementMetadataNode)18