Search in sources :

Example 11 with SPARQLParser

use of org.openrdf.query.parser.sparql.SPARQLParser in project incubator-rya by apache.

the class ParsedQueryUtilTest method findProjection_projectionIsTopNode.

@Test
public void findProjection_projectionIsTopNode() throws MalformedQueryException {
    // A SPARQL query that will result in the Projection node being the top node.
    String sparql = "SELECT ?a ?b " + "WHERE {" + "?a <http://talksTo> ?b" + "}";
    // Run the test.
    SPARQLParser parser = new SPARQLParser();
    ParsedQuery query = parser.parseQuery(sparql, null);
    Optional<Projection> projection = new ParsedQueryUtil().findProjection(query);
    assertTrue(projection.isPresent());
}
Also used : SPARQLParser(org.openrdf.query.parser.sparql.SPARQLParser) ParsedQuery(org.openrdf.query.parser.ParsedQuery) Projection(org.openrdf.query.algebra.Projection) Test(org.junit.Test)

Example 12 with SPARQLParser

use of org.openrdf.query.parser.sparql.SPARQLParser in project incubator-rya by apache.

the class SimpleExternalTupleSetTest method getSupportedVariableOrderMap.

@Test
public void getSupportedVariableOrderMap() throws MalformedQueryException {
    // Create the PCJ expression.
    final String sparql = "SELECT ?f ?m ?d { " + "?f <urn:talksTo> ?m . " + "?m <uri:associatesWith> ?d . " + "}";
    final ParsedQuery query = new SPARQLParser().parseQuery(sparql, null);
    final Projection pcjExpression = (Projection) query.getTupleExpr();
    // Create the object that is being tested.
    final SimpleExternalTupleSet testSet = new SimpleExternalTupleSet(pcjExpression);
    // Verify the correct Supported Variable Order Map is created.
    final Map<String, Set<String>> expected = new HashMap<>();
    String varOrder = "f";
    Set<String> vars = new HashSet<>();
    vars.add("f");
    expected.put(varOrder, vars);
    varOrder = "f;m";
    vars = new HashSet<>();
    vars.add("f");
    vars.add("m");
    expected.put(varOrder, vars);
    varOrder = "f;m;d";
    vars = new HashSet<>();
    vars.add("f");
    vars.add("m");
    vars.add("d");
    expected.put(varOrder, vars);
    assertEquals(expected, testSet.getSupportedVariableOrderMap());
}
Also used : SPARQLParser(org.openrdf.query.parser.sparql.SPARQLParser) HashSet(java.util.HashSet) Set(java.util.Set) ParsedQuery(org.openrdf.query.parser.ParsedQuery) HashMap(java.util.HashMap) Projection(org.openrdf.query.algebra.Projection) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 13 with SPARQLParser

use of org.openrdf.query.parser.sparql.SPARQLParser in project incubator-rya by apache.

the class SimpleExternalTupleSetTest method hashCode_notSame.

public void hashCode_notSame() throws MalformedQueryException {
    // Create the first SimpleExternalTupleSet object.
    final String sparql1 = "SELECT ?f ?m ?d { " + "?f <urn:talksTo> ?m . " + "?m <uri:associatesWith> ?d . " + "}";
    final ParsedQuery query1 = new SPARQLParser().parseQuery(sparql1, null);
    final Projection pcjExpression1 = (Projection) query1.getTupleExpr();
    final SimpleExternalTupleSet set1 = new SimpleExternalTupleSet(pcjExpression1);
    // Create another one using a different expression.
    final String sparql2 = "SELECT ?f ?m ?d { " + "?f <urn:talksTo> ?m . " + "?m <uri:friendsWith> ?d . " + "}";
    final ParsedQuery query2 = new SPARQLParser().parseQuery(sparql2, null);
    final Projection pcjExpression2 = (Projection) query2.getTupleExpr();
    final SimpleExternalTupleSet set2 = new SimpleExternalTupleSet(pcjExpression2);
    // Show they are not equal.
    assertNotEquals(set1.hashCode(), set2.hashCode());
}
Also used : SPARQLParser(org.openrdf.query.parser.sparql.SPARQLParser) ParsedQuery(org.openrdf.query.parser.ParsedQuery) Projection(org.openrdf.query.algebra.Projection)

Example 14 with SPARQLParser

use of org.openrdf.query.parser.sparql.SPARQLParser in project incubator-rya by apache.

the class MongoDbSmartUriIT method testStorage.

@Test
public void testStorage() throws SmartUriException, MalformedQueryException, RuntimeException, QueryEvaluationException {
    smartUriConverter.storeEntity(BOB_ENTITY);
    final String sparql = "SELECT * WHERE { " + "<" + BOB.getData() + "> <" + RDF.TYPE + "> <" + PERSON_TYPE.getId().getData() + "> . " + "<" + BOB.getData() + "> <" + HAS_SSN.getData() + "> ?ssn . " + "<" + BOB.getData() + "> <" + HAS_AGE.getData() + "> ?age . " + "<" + BOB.getData() + "> <" + HAS_WEIGHT.getData() + "> ?weight . " + "<" + BOB.getData() + "> <" + HAS_ADDRESS.getData() + "> ?address . " + "}";
    final StatementPatternCollector spCollector = new StatementPatternCollector();
    new SPARQLParser().parseQuery(sparql, null).getTupleExpr().visit(spCollector);
    final List<StatementPattern> patterns = spCollector.getStatementPatterns();
    final EntityQueryNode entityQueryNode = new EntityQueryNode(PERSON_TYPE, patterns, smartUriConverter.getEntityStorage());
    final QueryBindingSet queryBindingSet = new QueryBindingSet();
    final Property ssnProperty = BOB_ENTITY.lookupTypeProperty(PERSON_TYPE, HAS_SSN).get();
    queryBindingSet.addBinding(HAS_SSN.getData(), RyaToRdfConversions.convertValue(ssnProperty.getValue()));
    final CloseableIteration<BindingSet, QueryEvaluationException> iter = entityQueryNode.evaluate(queryBindingSet);
    int count = 0;
    // These should match what was used in the SPARQL query.
    final List<String> queryParamNames = Lists.newArrayList("ssn", "age", "weight", "address");
    while (iter.hasNext()) {
        final BindingSet bs = iter.next();
        assertTrue(bs.getBindingNames().containsAll(queryParamNames));
        count++;
    }
    assertEquals(count, 1);
}
Also used : StatementPatternCollector(org.openrdf.query.algebra.helpers.StatementPatternCollector) QueryBindingSet(org.openrdf.query.algebra.evaluation.QueryBindingSet) BindingSet(org.openrdf.query.BindingSet) SPARQLParser(org.openrdf.query.parser.sparql.SPARQLParser) EntityQueryNode(org.apache.rya.indexing.entity.query.EntityQueryNode) QueryBindingSet(org.openrdf.query.algebra.evaluation.QueryBindingSet) StatementPattern(org.openrdf.query.algebra.StatementPattern) QueryEvaluationException(org.openrdf.query.QueryEvaluationException) Property(org.apache.rya.indexing.entity.model.Property) Test(org.junit.Test)

Example 15 with SPARQLParser

use of org.openrdf.query.parser.sparql.SPARQLParser in project incubator-rya by apache.

the class MongoPcjIntegrationTest method testEvaluateTwoIndexValidate.

@Test
public void testEvaluateTwoIndexValidate() throws Exception {
    final Sail nonPcjSail = RyaSailFactory.getInstance(conf);
    final MongoDBRdfConfiguration pcjConf = conf.clone();
    pcjConf.setBoolean(ConfigUtils.USE_PCJ, true);
    final Sail pcjSail = RyaSailFactory.getInstance(pcjConf);
    final SailRepositoryConnection conn = new SailRepository(nonPcjSail).getConnection();
    final SailRepositoryConnection pcjConn = new SailRepository(pcjSail).getConnection();
    addPCJS(pcjConn);
    try {
        final URI superclass = new URIImpl("uri:superclass");
        final URI superclass2 = new URIImpl("uri:superclass2");
        conn.add(subclass, RDF.TYPE, superclass);
        conn.add(subclass2, RDF.TYPE, superclass2);
        conn.add(obj, RDFS.LABEL, new LiteralImpl("label"));
        conn.add(obj2, RDFS.LABEL, new LiteralImpl("label2"));
        final String indexSparqlString = // 
        "" + // 
        "SELECT ?dog ?pig ?duck  " + // 
        "{" + // 
        "  ?pig a ?dog . " + // 
        "  ?pig <http://www.w3.org/2000/01/rdf-schema#label> ?duck " + // 
        "}";
        final String indexSparqlString2 = // 
        "" + // 
        "SELECT ?o ?f ?e ?c ?l  " + // 
        "{" + // 
        "  ?e <uri:talksTo> ?o . " + // 
        "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l. " + // 
        "  ?c a ?f . " + // 
        "}";
        final String queryString = // 
        "" + // 
        "SELECT ?e ?c ?l ?f ?o " + // 
        "{" + // 
        "  ?e a ?c . " + // 
        "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l. " + // 
        "  ?e <uri:talksTo> ?o . " + // 
        "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l. " + // 
        "  ?c a ?f . " + // 
        "}";
        PcjIntegrationTestingUtil.createAndPopulatePcj(conn, getMongoClient(), conf.getMongoDBName() + 1, conf.getRyaInstanceName(), indexSparqlString);
        final MongoPcjQueryNode ais1 = new MongoPcjQueryNode(conf, conf.getMongoDBName() + 1);
        PcjIntegrationTestingUtil.createAndPopulatePcj(conn, getMongoClient(), conf.getMongoDBName() + 2, conf.getRyaInstanceName(), indexSparqlString2);
        final MongoPcjQueryNode ais2 = new MongoPcjQueryNode(conf, conf.getMongoDBName() + 2);
        final List<ExternalTupleSet> index = new ArrayList<>();
        index.add(ais1);
        index.add(ais2);
        ParsedQuery pq = null;
        final SPARQLParser sp = new SPARQLParser();
        pq = sp.parseQuery(queryString, null);
        final List<TupleExpr> teList = Lists.newArrayList();
        final TupleExpr te = pq.getTupleExpr();
        final PCJOptimizer pcj = new PCJOptimizer(index, false, new MongoPcjIndexSetProvider(new StatefulMongoDBRdfConfiguration(conf, getMongoClient())));
        pcj.optimize(te, null, null);
        teList.add(te);
        final IndexPlanValidator ipv = new IndexPlanValidator(false);
        assertTrue(ipv.isValid(te));
    } finally {
        conn.close();
        pcjConn.close();
        nonPcjSail.shutDown();
        pcjSail.shutDown();
    }
}
Also used : SPARQLParser(org.openrdf.query.parser.sparql.SPARQLParser) StatefulMongoDBRdfConfiguration(org.apache.rya.mongodb.StatefulMongoDBRdfConfiguration) MongoPcjQueryNode(org.apache.rya.indexing.mongodb.pcj.MongoPcjQueryNode) SailRepository(org.openrdf.repository.sail.SailRepository) ParsedQuery(org.openrdf.query.parser.ParsedQuery) ArrayList(java.util.ArrayList) IndexPlanValidator(org.apache.rya.indexing.IndexPlanValidator.IndexPlanValidator) URIImpl(org.openrdf.model.impl.URIImpl) SailRepositoryConnection(org.openrdf.repository.sail.SailRepositoryConnection) URI(org.openrdf.model.URI) TupleExpr(org.openrdf.query.algebra.TupleExpr) ExternalTupleSet(org.apache.rya.indexing.external.tupleSet.ExternalTupleSet) LiteralImpl(org.openrdf.model.impl.LiteralImpl) PCJOptimizer(org.apache.rya.indexing.pcj.matching.PCJOptimizer) Sail(org.openrdf.sail.Sail) MongoPcjIndexSetProvider(org.apache.rya.indexing.mongodb.pcj.MongoPcjIndexSetProvider) MongoDBRdfConfiguration(org.apache.rya.mongodb.MongoDBRdfConfiguration) StatefulMongoDBRdfConfiguration(org.apache.rya.mongodb.StatefulMongoDBRdfConfiguration) Test(org.junit.Test)

Aggregations

SPARQLParser (org.openrdf.query.parser.sparql.SPARQLParser)265 ParsedQuery (org.openrdf.query.parser.ParsedQuery)249 Test (org.junit.Test)212 TupleExpr (org.openrdf.query.algebra.TupleExpr)162 ArrayList (java.util.ArrayList)100 ExternalTupleSet (org.apache.rya.indexing.external.tupleSet.ExternalTupleSet)99 SimpleExternalTupleSet (org.apache.rya.indexing.external.tupleSet.SimpleExternalTupleSet)86 StatementPattern (org.openrdf.query.algebra.StatementPattern)82 QueryModelNode (org.openrdf.query.algebra.QueryModelNode)62 PCJOptimizer (org.apache.rya.indexing.pcj.matching.PCJOptimizer)46 QueryEvaluationException (org.openrdf.query.QueryEvaluationException)45 QueryBindingSet (org.openrdf.query.algebra.evaluation.QueryBindingSet)43 RyaURI (org.apache.rya.api.domain.RyaURI)42 BindingSet (org.openrdf.query.BindingSet)41 RyaStatement (org.apache.rya.api.domain.RyaStatement)39 Projection (org.openrdf.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 LiteralImpl (org.openrdf.model.impl.LiteralImpl)19