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());
}
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());
}
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());
}
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);
}
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();
}
}
Aggregations