Java Code Examples for org.eclipse.rdf4j.query.parser.ParsedQuery#getTupleExpr()

The following examples show how to use org.eclipse.rdf4j.query.parser.ParsedQuery#getTupleExpr() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestPropPathMisbehaviour.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * reproduces GH-2343: the obj var of the nested statement pattern should be equal to the objVar of the ALP path
 * that is using the PE
 */
@Test
public void testGH2343() {
	String query1 = "select ?iri ?value where { \n" +
			"    ?iri (<urn:p>+) / <urn:q> ?value .\n" +
			"}";
	ParsedQuery q = parser.parseQuery(query1, "http://base.org/");

	assertNotNull(q);
	assertTrue("expect projection", q.getTupleExpr() instanceof Projection);
	Projection proj = (Projection) q.getTupleExpr();

	assertTrue("expect join", proj.getArg() instanceof Join);
	assertTrue("expect left arg to be ALP", ((Join) proj.getArg()).getLeftArg() instanceof ArbitraryLengthPath);
	ArbitraryLengthPath alp = (ArbitraryLengthPath) ((Join) proj.getArg()).getLeftArg();

	assertTrue("expect single statement pattern in alp PE", alp.getPathExpression() instanceof StatementPattern);
	StatementPattern sp = (StatementPattern) alp.getPathExpression();
	assertNotNull(sp.getSubjectVar());

	assertTrue("expect subj var to be iri", "iri".equals(sp.getSubjectVar().getName()));

	assertTrue("expect obj var of the pattern to be same as the objVar of ALP",
			alp.getObjectVar().equals(sp.getObjectVar()));
}
 
Example 2
Source File: GeneralizedExternalProcessorTest.java    From rya with Apache License 2.0 5 votes vote down vote up
@Test
public void testTwoIndexLargeQuery() throws Exception {

	SPARQLParser parser = new SPARQLParser();

	ParsedQuery pq1 = parser.parseQuery(q15, null);
	ParsedQuery pq2 = parser.parseQuery(q7, null);
	ParsedQuery pq3 = parser.parseQuery(q12, null);

	System.out.println("Query is " + pq1.getTupleExpr());

	SimpleExternalTupleSet extTup1 = new SimpleExternalTupleSet(
			new Projection(pq2.getTupleExpr()));
	SimpleExternalTupleSet extTup2 = new SimpleExternalTupleSet(
			new Projection(pq3.getTupleExpr()));

	List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();

	list.add(extTup2);
	list.add(extTup1);

	IndexedExecutionPlanGenerator iep = new IndexedExecutionPlanGenerator(
			pq1.getTupleExpr(), list);
	List<ExternalTupleSet> indexSet = iep.getNormalizedIndices();
	Assert.assertEquals(4, indexSet.size());

	Set<TupleExpr> processedTups = Sets.newHashSet(iep.getIndexedTuples());
	Assert.assertEquals(5, processedTups.size());

}
 
Example 3
Source File: PCJNodeConsolidatorTest.java    From rya with Apache License 2.0 5 votes vote down vote up
@Test
public void testExactMatchReOrdered() throws Exception {

	String query1 = ""//
			+ "SELECT ?a ?b ?c ?d ?e ?f ?g ?h" //
			+ "{" //
			+ "  ?a <uri:p0> ?b ." //
			+ "  OPTIONAL{?b <uri:p2> ?c. ?c <uri:p1> ?d} . " //
			+ "  OPTIONAL{?b <uri:p3> ?e. ?e <uri:p1> ?f} . "//
			+ "  OPTIONAL{?b <uri:p4> ?g. ?g <uri:p1> ?h} . "//
			+ "}";//

	String query2 = ""//
			+ "SELECT ?a ?b ?c ?d ?e ?f ?g ?h" //
			+ "{" //
			+ "  ?a <uri:p0> ?b ." //
			+ "  OPTIONAL{?b <uri:p2> ?c. ?c <uri:p1> ?d} . " //
			+ "  OPTIONAL{?b <uri:p4> ?g. ?g <uri:p1> ?h} . "//
			+ "  OPTIONAL{?b <uri:p3> ?e. ?e <uri:p1> ?f} . "//
			+ "}";//

	SPARQLParser parser = new SPARQLParser();
	ParsedQuery pq1 = parser.parseQuery(query1, null);
	ParsedQuery pq2 = parser.parseQuery(query2, null);
	TupleExpr te1 = pq1.getTupleExpr();
	TupleExpr te2 = pq2.getTupleExpr();
	LeftJoin join1 = (LeftJoin) ((Projection) te1).getArg();
	LeftJoin join2 = (LeftJoin) ((Projection) te2).getArg();

	QuerySegment<ExternalTupleSet> seg1 = qFactory.getQuerySegment(join1);
    QuerySegment<ExternalTupleSet> seg2 = qFactory.getQuerySegment(join2);

	QueryNodeConsolidator consolidator = new QueryNodeConsolidator(seg1.getOrderedNodes(), seg2.getOrderedNodes());
	List<QueryModelNode> queryNodes = new ArrayList<>(seg2.getOrderedNodes());

	Assert.assertTrue(consolidator.consolidateNodes());
	Assert.assertEquals(consolidator.getQueryNodes(), queryNodes);
}
 
Example 4
Source File: PrecompJoinOptimizerTest2.java    From rya with Apache License 2.0 5 votes vote down vote up
@Test
public void testSingleIndexLargeQuery() throws Exception {

    final SPARQLParser parser1 = new SPARQLParser();
    final SPARQLParser parser2 = new SPARQLParser();

    final ParsedQuery pq1 = parser1.parseQuery(q8, null);
    final ParsedQuery pq2 = parser2.parseQuery(q7, null);

    final SimpleExternalTupleSet extTup = new SimpleExternalTupleSet(
            new Projection(pq2.getTupleExpr()));

    final List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();
    list.add(extTup);

    final TupleExpr tup = pq1.getTupleExpr().clone();
    provider.setIndices(list);
    final PCJOptimizer pcj = new PCJOptimizer(list, false, provider);
    pcj.optimize(tup, null, null);

    final Set<StatementPattern> qSet = Sets.newHashSet(StatementPatternCollector
            .process(pq1.getTupleExpr()));
    final Set<QueryModelNode> eTupSet = PcjIntegrationTestingUtil
            .getTupleSets(tup);

    final Set<StatementPattern> set = Sets.newHashSet();
    for (final QueryModelNode s : eTupSet) {
        set.addAll(StatementPatternCollector.process(((ExternalTupleSet) s)
                .getTupleExpr()));
    }

    Assert.assertTrue(set.equals(qSet));

}
 
Example 5
Source File: PCJNodeConsolidatorTest.java    From rya with Apache License 2.0 5 votes vote down vote up
@Test
public void testSwitchTwoBoundVars() throws Exception {

	String query1 = ""//
			+ "SELECT ?a ?b ?c " //
			+ "{" //
			+ "  ?a <uri:p0> ?c ." //
			+ "  ?b<uri:p1> ?c ." //
			+ " OPTIONAL{ ?a <uri:p1> ?b } ." //
			+ " ?a <uri:p2> <uri:o2>. " //
			+ " ?b <uri:p3> <uri:o3> " //
			+ "}";//

	String query2 = ""//
			+ "SELECT ?a ?b ?c" //
			+ "{" //
			+ " ?a <uri:p2> <uri:o2>. " //
			+ " ?b <uri:p3> <uri:o3>. " //
			+ " OPTIONAL{ ?a <uri:p1> ?b } ." //
			+ "  ?a <uri:p0> ?c ." //
			+ "  ?b<uri:p1> ?c " //
			+ "}";//

	SPARQLParser parser = new SPARQLParser();
	ParsedQuery pq1 = parser.parseQuery(query1, null);
	ParsedQuery pq2 = parser.parseQuery(query2, null);
	TupleExpr te1 = pq1.getTupleExpr();
	TupleExpr te2 = pq2.getTupleExpr();
	Join join1 = (Join) ((Projection) te1).getArg();
	Join join2 = (Join) ((Projection) te2).getArg();

	QuerySegment<ExternalTupleSet> seg1 = qFactory.getQuerySegment(join1);
    QuerySegment<ExternalTupleSet> seg2 = qFactory.getQuerySegment(join2);

	QueryNodeConsolidator consolidator = new QueryNodeConsolidator(seg1.getOrderedNodes(), seg2.getOrderedNodes());
	List<QueryModelNode> queryNodes = new ArrayList<>(seg2.getOrderedNodes());

	Assert.assertTrue(consolidator.consolidateNodes());
	Assert.assertEquals(consolidator.getQueryNodes(), queryNodes);
}
 
Example 6
Source File: IndexedExecutionPlanGeneratorTest.java    From rya with Apache License 2.0 5 votes vote down vote up
@Test
public void testThrowsException1() throws Exception {

	SPARQLParser parser = new SPARQLParser();

	ParsedQuery pq1 = parser.parseQuery(q16, null);
	ParsedQuery pq2 = parser.parseQuery(q17, null);
	ParsedQuery pq3 = parser.parseQuery(q18, null);

	SimpleExternalTupleSet extTup1 = new SimpleExternalTupleSet(
			(Projection) pq2.getTupleExpr());
	SimpleExternalTupleSet extTup2 = new SimpleExternalTupleSet(
			(Projection) pq3.getTupleExpr());

	List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();

	list.add(extTup2);
	list.add(extTup1);

	IndexedExecutionPlanGenerator iep = new IndexedExecutionPlanGenerator(
			pq1.getTupleExpr(), list);
	List<ExternalTupleSet> indexSet = iep.getNormalizedIndices();
	Assert.assertEquals(6, indexSet.size());

	Iterator<TupleExpr> processedTups = iep.getIndexedTuples();

	boolean exceptionThrown = false;

	try {
		processedTups.remove();
	} catch (UnsupportedOperationException e) {
		exceptionThrown = true;
	}

	Assert.assertTrue(exceptionThrown);

}
 
Example 7
Source File: IndexedExecutionPlanGeneratorTest.java    From rya with Apache License 2.0 5 votes vote down vote up
@Test
public void testThrowsException2() throws Exception {

	SPARQLParser parser = new SPARQLParser();

	ParsedQuery pq1 = parser.parseQuery(q19, null);
	ParsedQuery pq2 = parser.parseQuery(q20, null);

	SimpleExternalTupleSet extTup1 = new SimpleExternalTupleSet(
			(Projection) pq2.getTupleExpr());

	List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();

	list.add(extTup1);

	IndexedExecutionPlanGenerator iep = new IndexedExecutionPlanGenerator(
			pq1.getTupleExpr(), list);
	List<ExternalTupleSet> indexSet = iep.getNormalizedIndices();
	Assert.assertEquals(3, indexSet.size());

	Iterator<TupleExpr> processedTups = iep.getIndexedTuples();

	processedTups.next();
	processedTups.next();
	processedTups.next();

	boolean exceptionThrown = false;
	try {
		processedTups.next();
	} catch (NoSuchElementException e) {
		exceptionThrown = true;
	}

	Assert.assertTrue(exceptionThrown);

}
 
Example 8
Source File: RDFQueryLogParser.java    From semagrow with Apache License 2.0 5 votes vote down vote up
private TupleExpr parseQuery(Value literal, Model model) {
    if (literal instanceof Literal) {
        String queryString = ((Literal)literal).stringValue();
        try {
            ParsedQuery query = QueryParserUtil.parseQuery(QueryLanguage.SPARQL, queryString, "http://www.iamabsoluteuri.com");
            return query.getTupleExpr();
        } catch (MalformedQueryException e) {
            return null;
        }
    }

    return null;
}
 
Example 9
Source File: PeriodicQueryUtilTest.java    From rya with Apache License 2.0 5 votes vote down vote up
@Test
public void testPeriodicNodeLocation() throws MalformedQueryException {
     String query = "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 }"; //n
     
     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()));
     
}
 
Example 10
Source File: PrecompJoinOptimizerTest2.java    From rya with Apache License 2.0 5 votes vote down vote up
@Test
public void testThreeIndexGeoFreeCompareFilterMix() throws Exception {

    final SPARQLParser parser1 = new SPARQLParser();
    final SPARQLParser parser2 = new SPARQLParser();
    final SPARQLParser parser3 = new SPARQLParser();

    final ParsedQuery pq1 = parser1.parseQuery(q25, null);
    final ParsedQuery pq2 = parser2.parseQuery(q24, null);
    final ParsedQuery pq3 = parser3.parseQuery(q26, null);

    final SimpleExternalTupleSet extTup1 = new SimpleExternalTupleSet(
            new Projection(pq2.getTupleExpr()));
    final SimpleExternalTupleSet extTup2 = new SimpleExternalTupleSet(
            new Projection(pq3.getTupleExpr()));

    final List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();
    list.add(extTup1);
    list.add(extTup2);

    final TupleExpr tup = pq1.getTupleExpr().clone();
    provider.setIndices(list);
    final PCJOptimizer pcj = new PCJOptimizer(list, false, provider);
    pcj.optimize(tup, null, null);

    final Set<StatementPattern> qSet = Sets.newHashSet(StatementPatternCollector
            .process(pq1.getTupleExpr()));
    final Set<QueryModelNode> eTupSet = PcjIntegrationTestingUtil
            .getTupleSets(tup);

    final Set<StatementPattern> set = Sets.newHashSet();
    for (final QueryModelNode s : eTupSet) {
        set.addAll(StatementPatternCollector.process(((ExternalTupleSet) s)
                .getTupleExpr()));
    }

    Assert.assertTrue(set.equals(qSet) && eTupSet.size() == 2);

}
 
Example 11
Source File: OptionalJoinSegmentTest.java    From rya with Apache License 2.0 4 votes vote down vote up
@Test
public void testBasicOptionalWithFilter() throws Exception {

	String query1 = ""//
			+ "SELECT ?e ?c ?l" //
			+ "{" //
			+ " Filter(?e = <uri:Bob>)" //
			+ "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l . "//
			+ "  ?e a ?c . "//
			+ "  OPTIONAL{?e <uri:talksTo> ?l } . "//
			+ "}";//

	String query2 = ""//
			+ "SELECT ?e ?c ?l" //
			+ "{" //
			+ " Filter(?e = <uri:Bob>)" //
			+ "  ?e a ?c . "//
			+ "  OPTIONAL{?e <uri:talksTo> ?l } . "//
			+ "}";//

	SPARQLParser parser = new SPARQLParser();
	ParsedQuery pq1 = parser.parseQuery(query1, null);
	ParsedQuery pq2 = parser.parseQuery(query2, null);
	TupleExpr te1 = pq1.getTupleExpr();
	TupleExpr te2 = pq2.getTupleExpr();
	TopOfQueryFilterRelocator.moveFiltersToTop(te1);
	Filter filter1 = (Filter) ((Projection) te1).getArg();
	Filter filter2 = (Filter) ((Projection) te2).getArg();

	QuerySegment<ExternalTupleSet> seg1 = qFactory.getQuerySegment(filter1);
       QuerySegment<ExternalTupleSet> seg2 = qFactory.getQuerySegment(filter2);

	Assert.assertEquals(filter1, seg1.getQuery().getTupleExpr());
	Assert.assertEquals(true, seg1.containsQuerySegment(seg2));

	SimpleExternalTupleSet pcj = new SimpleExternalTupleSet((Projection)te2);
	List<QueryModelNode> nodes = seg1.getOrderedNodes();
	QueryModelNode node = nodes.get(3);
	seg1.replaceWithExternalSet(seg2, pcj);

	Set<QueryModelNode> nodeSet = new HashSet<>();
	nodeSet.add(node);
	nodeSet.add(pcj);

	Assert.assertEquals(nodeSet, seg1.getUnOrderedNodes());

}
 
Example 12
Source File: OptionalJoinSegmentTest.java    From rya with Apache License 2.0 4 votes vote down vote up
@Test
public void testBasicOptional() throws MalformedQueryException {

	String query1 = ""//
			+ "SELECT ?e ?c ?l" //
			+ "{" //
			+ "  ?e a ?c . "//
			+ "  OPTIONAL{?e <uri:talksTo> ?l } . "//
			+ "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l "//
			+ "}";//

	String query2 = ""//
			+ "SELECT ?e ?c ?l" //
			+ "{" //
			+ "  ?e a ?c . "//
			+ "  OPTIONAL{?e <uri:talksTo> ?l } . "//
			+ "}";//

	SPARQLParser parser = new SPARQLParser();
	ParsedQuery pq1 = parser.parseQuery(query1, null);
	ParsedQuery pq2 = parser.parseQuery(query2, null);
	TupleExpr te1 = pq1.getTupleExpr();
	TupleExpr te2 = pq2.getTupleExpr();
	Join join = (Join) ((Projection) te1).getArg();
	LeftJoin lj = (LeftJoin) ((Projection) te2).getArg();

	QuerySegment<ExternalTupleSet> seg1 = qFactory.getQuerySegment(join);
       QuerySegment<ExternalTupleSet> seg2 = qFactory.getQuerySegment(lj);


	Assert.assertEquals(true, seg1.containsQuerySegment(seg2));
	Assert.assertEquals(join, seg1.getQuery().getTupleExpr());

	SimpleExternalTupleSet pcj = new SimpleExternalTupleSet((Projection)te2);
	List<QueryModelNode> nodes = seg1.getOrderedNodes();
	QueryModelNode node = nodes.get(0);
	seg1.replaceWithExternalSet(seg2, pcj);

	Set<QueryModelNode> nodeSet = new HashSet<>();
	nodeSet.add(node);
	nodeSet.add(pcj);

	Assert.assertEquals(nodeSet, seg1.getUnOrderedNodes());

}
 
Example 13
Source File: JoinSegmentPCJMatcherTest.java    From rya with Apache License 2.0 4 votes vote down vote up
@Test
public void testBasicMatchWithFilter() throws Exception {

	String query1 = ""//
			+ "SELECT ?e ?c ?l" //
			+ "{" //
			+ " Filter(?e = <uri:Bob>)" //
			+ " Filter(?c = <uri:Lawyer>)" //
			+ "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l . "//
			+ "  ?e a ?c . "//
			+ "  ?e <uri:talksTo> ?l  . "//
			+ "}";//

	String query2 = ""//
			+ "SELECT ?e ?c ?l" //
			+ "{" //
			+ " Filter(?e = <uri:Bob>)" //
			+ " ?e a ?c . "//
			+ " ?e <uri:talksTo> ?l . "//
			+ "}";//

	SPARQLParser parser = new SPARQLParser();
	ParsedQuery pq1 = parser.parseQuery(query1, null);
	ParsedQuery pq2 = parser.parseQuery(query2, null);
	TupleExpr te1 = pq1.getTupleExpr();
	TupleExpr te2 = pq2.getTupleExpr();
	Projection proj = (Projection) te1;
	Filter filter = (Filter) proj.getArg();

	ExternalSetMatcher<ExternalTupleSet> jsm = pcjFactory.getMatcher(qFactory.getQuerySegment(filter));
	SimpleExternalTupleSet pcj = new SimpleExternalTupleSet((Projection)te2);
	Assert.assertEquals(true, jsm.match(pcj));
	TupleExpr te = jsm.getQuery();
	Assert.assertEquals(new HashSet<QueryModelNode>(), jsm.getUnmatchedArgNodes());

	Set<QueryModelNode> qNodes = QueryNodeGatherer.getNodes(te);
	List<QueryModelNode> nodes = jsm.getOrderedNodes();
	Set<QueryModelNode> nodeSet = new HashSet<>();
	nodeSet.add(nodes.get(0));
	nodeSet.add(pcj);
	nodeSet.add(nodes.get(1));

	Assert.assertEquals(nodeSet, new HashSet<QueryModelNode>(nodes));
	Assert.assertEquals(nodeSet, qNodes);

}
 
Example 14
Source File: PCJOptimizerTest.java    From rya with Apache License 2.0 4 votes vote down vote up
@Test
public void testSegmentWithLeftJoinsAndFilters() throws Exception {

    final String query1 = ""//
            + "SELECT ?e ?c ?l" //
            + "{" //
            + " Filter(?e = <uri:s1>) " //
            + " Filter(?c = <uri:s2>) " //
            + " ?e <uri:p1> <uri:o1>. " + " OPTIONAL {?e <uri:p2> ?l}. " + " ?c <uri:p3> <uri:o3>  . "//
            + "  ?c <uri:p4> ?e  . "//
            + " OPTIONAL {?e <uri:p2> ?c } . "//
            + "}";//

    final String query2 = ""//
            + "SELECT ?e ?c ?l" //
            + "{" //
            + " Filter(?c = <uri:s2>) " //
            + " ?e <uri:p1> <uri:o1>. " + " OPTIONAL {?e <uri:p2> ?l}. " + " ?c <uri:p3> <uri:o3>  . "//
            + "}";//

    final String query3 = ""//
            + "SELECT ?e ?c" //
            + "{" //
            + " Filter(?e = <uri:s1>) " //
            + "  ?c <uri:p4> ?e  . "//
            + " OPTIONAL {?e <uri:p2> ?c } . "//
            + "}";//

    final SPARQLParser parser = new SPARQLParser();
    final ParsedQuery pq1 = parser.parseQuery(query1, null);
    final ParsedQuery pq2 = parser.parseQuery(query2, null);
    final ParsedQuery pq3 = parser.parseQuery(query3, null);
    final TupleExpr te1 = pq1.getTupleExpr();
    final TupleExpr te2 = pq2.getTupleExpr();
    final TupleExpr te3 = pq3.getTupleExpr();

    final TupleExpr unOpt = te1.clone();

    final SimpleExternalTupleSet pcj1 = new SimpleExternalTupleSet((Projection) te2);
    final SimpleExternalTupleSet pcj2 = new SimpleExternalTupleSet((Projection) te3);
    final List<ExternalTupleSet> externalList = new ArrayList<>();
    externalList.add(pcj1);
    externalList.add(pcj2);

    provider.setIndices(externalList);
    final PCJOptimizer optimizer = new PCJOptimizer(externalList, false, provider);
    optimizer.optimize(te1, null, null);

    Assert.assertEquals(true, validatePcj(te1, unOpt, externalList, new HashSet<QueryModelNode>()));
}
 
Example 15
Source File: MongoPcjIntegrationTest.java    From rya with Apache License 2.0 4 votes vote down vote up
@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 IRI superclass = VF.createIRI("uri:superclass");
        final IRI superclass2 = VF.createIRI("uri:superclass2");

        conn.add(subclass, RDF.TYPE, superclass);
        conn.add(subclass2, RDF.TYPE, superclass2);
        conn.add(obj, RDFS.LABEL, VF.createLiteral("label"));
        conn.add(obj2, RDFS.LABEL, VF.createLiteral("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();
    }
}
 
Example 16
Source File: TestSparqlStarParser.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Test
public void testUseInValues() throws Exception {
	String simpleSparqlQuery = "SELECT ?ref WHERE { values ?ref {<<<urn:A> <urn:B> 1>>} }";

	ParsedQuery q = parser.parseQuery(simpleSparqlQuery, null);

	assertNotNull(q);
	assertTrue("expect projection", q.getTupleExpr() instanceof Projection);
	Projection proj = (Projection) q.getTupleExpr();

	assertTrue("expect extension", proj.getArg() instanceof Extension);
	Extension ext = (Extension) proj.getArg();

	assertTrue("single extention elemrnt", ext.getElements().size() == 1);
	ExtensionElem elem = ext.getElements().get(0);

	assertEquals("name should match", elem.getName(), "ref");
	assertTrue("expect Var in extention element", elem.getExpr() instanceof Var);
	assertEquals("names should match", elem.getName(), ((Var) elem.getExpr()).getName());

	assertTrue("expect BindingSetAssignment as arg", ext.getArg() instanceof BindingSetAssignment);
	BindingSetAssignment values = (BindingSetAssignment) ext.getArg();
	boolean oneValue[] = new boolean[] { false };
	values.getBindingSets().forEach(bs -> {
		Value v = bs.getValue("ref");
		assertTrue("expect binding for ref", v != null);
		assertTrue("expect Triple ", v instanceof Triple);
		Triple triple = (Triple) v;
		assertTrue("subject should be IRI", triple.getSubject() instanceof IRI);
		assertEquals("subject should match", "urn:A", triple.getSubject().toString());

		assertTrue("predicate should be IRI", triple.getPredicate() instanceof IRI);
		assertEquals("predicate should match", "urn:B", triple.getPredicate().toString());

		assertTrue("object should be Literal", triple.getObject() instanceof Literal);
		assertEquals("object should match", 1, ((Literal) triple.getObject()).intValue());

		assertTrue("expect one value", oneValue[0] == false);
		oneValue[0] = true;
	});
	assertTrue("expect one value", oneValue[0]);
}
 
Example 17
Source File: IndexPlanValidatorTest.java    From rya with Apache License 2.0 4 votes vote down vote up
@Test
public void testEvaluateTwoIndexTwoVarOrder2()
        throws Exception {


    final String indexSparqlString = ""//
            + "SELECT ?e ?l ?c  " //
            + "{" //
            + "  ?e a ?c . "//
            + "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l "//
            + "}";//

    final String indexSparqlString2 = ""//
            + "SELECT ?e ?o ?l " //
            + "{" //
            + "  ?e <uri:talksTo> ?o . "//
            + "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l "//
            + "}";//

    final String queryString = ""//
            + "SELECT ?e ?c ?l ?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 "//
            + "}";//

    final SPARQLParser sp = new SPARQLParser();
    final ParsedQuery index1 = sp.parseQuery(indexSparqlString, null);
    final ParsedQuery index2 = sp.parseQuery(indexSparqlString2, null);

    final List<ExternalTupleSet> index = Lists.newArrayList();

    final SimpleExternalTupleSet ais1 = new SimpleExternalTupleSet(
            (Projection) index1.getTupleExpr());
    final SimpleExternalTupleSet ais2 = new SimpleExternalTupleSet(
            (Projection) index2.getTupleExpr());

    index.add(ais1);
    index.add(ais2);

    final ParsedQuery pq = sp.parseQuery(queryString, null);
    final TupleExpr tup = pq.getTupleExpr().clone();
    provider.setIndices(index);
    final PCJOptimizer pcj = new PCJOptimizer(index, false, provider);
    pcj.optimize(tup, null, null);


    final IndexPlanValidator ipv = new IndexPlanValidator(false);
    Assert.assertEquals(true, ipv.isValid(tup));

}
 
Example 18
Source File: IndexPlanValidatorTest.java    From rya with Apache License 2.0 4 votes vote down vote up
@Test
public void testEvaluateTwoIndexDiffVars2() throws Exception {


    final String indexSparqlString = ""//
            + "SELECT ?dog ?pig ?chicken  " //
            + "{" //
            + "  ?dog a ?chicken . "//
            + "  ?dog <http://www.w3.org/2000/01/rdf-schema#label> ?pig "//
            + "}";//

    final String indexSparqlString2 = ""//
            + "SELECT ?fish ?ant ?turkey " //
            + "{" //
            + "  ?fish <uri:talksTo> ?turkey . "//
            + "  ?turkey <http://www.w3.org/2000/01/rdf-schema#label> ?ant "//
            + "}";//

    final String queryString = ""//
            + "SELECT ?e ?c ?l ?o ?f ?g " //
            + "{" //
            + "  ?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 . "//
            + "  ?f <uri:talksTo> ?g . " //
            + "}";//

    final SPARQLParser sp = new SPARQLParser();
    final ParsedQuery index1 = sp.parseQuery(indexSparqlString, null);
    final ParsedQuery index2 = sp.parseQuery(indexSparqlString2, null);

    final List<ExternalTupleSet> index = Lists.newArrayList();

    final SimpleExternalTupleSet ais1 = new SimpleExternalTupleSet(
            (Projection) index1.getTupleExpr());
    final SimpleExternalTupleSet ais2 = new SimpleExternalTupleSet(
            (Projection) index2.getTupleExpr());

    index.add(ais1);
    index.add(ais2);

    final ParsedQuery pq = sp.parseQuery(queryString, null);
    final TupleExpr tup = pq.getTupleExpr().clone();
    provider.setIndices(index);
    final PCJOptimizer pcj = new PCJOptimizer(index, false, provider);
    pcj.optimize(tup, null, null);

    final IndexPlanValidator ipv = new IndexPlanValidator(false);
    Assert.assertEquals(true, ipv.isValid(tup));

}
 
Example 19
Source File: PrecompJoinOptimizerTest.java    From rya with Apache License 2.0 4 votes vote down vote up
@Test
public void testThreeIndex() throws Exception {

    final String q7 = ""//
            + "SELECT ?s ?t ?u " //
            + "{" //
            + "  ?s a ?t ."//
            + "  ?t <http://www.w3.org/2000/01/rdf-schema#label> ?u ."//
            + "  ?u <uri:talksTo> ?s . "//
            + "}";//

    final String q8 = ""//
            + "SELECT ?e ?l ?c " //
            + "{" //
            + "  ?e a ?l ."//
            + "  ?l <http://www.w3.org/2000/01/rdf-schema#label> ?c ."//
            + "  ?c <uri:talksTo> ?e . "//
            + "}";//

    final String q9 = ""//
            + "SELECT ?f ?m ?d " //
            + "{" //
            + "  ?f a ?m ."//
            + "  ?m <http://www.w3.org/2000/01/rdf-schema#label> ?d ."//
            + "  ?d <uri:talksTo> ?f . "//
            + "}";//

    final String q15 = ""//
            + "SELECT ?f ?m ?d ?e ?l ?c " //
            + "{" //
            + "  ?f a ?m ."//
            + "  ?e a ?l ."//
            + "  ?d <uri:talksTo> ?f . "//
            + "  ?c <uri:talksTo> ?e . "//
            + "  ?m <http://www.w3.org/2000/01/rdf-schema#label> ?d ."//
            + "  ?l <http://www.w3.org/2000/01/rdf-schema#label> ?c ."//
            + "}";//

    final SPARQLParser parser = new SPARQLParser();

    final ParsedQuery pq1 = parser.parseQuery(q15, null);
    final ParsedQuery pq2 = parser.parseQuery(q7, null);
    final ParsedQuery pq3 = parser.parseQuery(q8, null);
    final ParsedQuery pq4 = parser.parseQuery(q9, null);

    final SimpleExternalTupleSet extTup1 = new SimpleExternalTupleSet(
            (Projection) pq2.getTupleExpr());
    final SimpleExternalTupleSet extTup2 = new SimpleExternalTupleSet(
            (Projection) pq3.getTupleExpr());
    final SimpleExternalTupleSet extTup3 = new SimpleExternalTupleSet(
            (Projection) pq4.getTupleExpr());

    final List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();

    list.add(extTup1);

    final List<QueryModelNode> optTupNodes = Lists.newArrayList();
    optTupNodes.add(extTup2);
    optTupNodes.add(extTup3);

    provider.setIndices(list);
    final PCJOptimizer pcj = new PCJOptimizer(list, true, provider);
    final TupleExpr te = pq1.getTupleExpr();
    pcj.optimize(te, null, null);

    final NodeCollector nc = new NodeCollector();
    te.visit(nc);

    Assert.assertEquals(nc.qNodes.size(), optTupNodes.size());
    for (final QueryModelNode node : nc.qNodes) {
        Assert.assertTrue(optTupNodes.contains(node));
    }

}
 
Example 20
Source File: StarQueryTest.java    From rya with Apache License 2.0 2 votes vote down vote up
@Test
public void testBasicFunctionality() throws MalformedQueryException {
  
    String q1 = "" //
            + "SELECT ?X ?Y1 ?Y2 " //
            + "{"//
            + "GRAPH <http://joe> { " //
            +  "?X <uri:cf1> ?Y1 ."//
            +  "?X <uri:cf2> ?Y2 ."//
            +  "?X <uri:cf3> ?Y3 ."//
            +  "}" //
            +  "}";
    
    
    SPARQLParser parser = new SPARQLParser();
    
    ParsedQuery pq1 = null;
    pq1 = parser.parseQuery(q1, null);

    TupleExpr te1 = pq1.getTupleExpr();
    
    System.out.println(te1);
    List<StatementPattern> spList1 = StatementPatternCollector.process(te1);
    
    Assert.assertTrue(StarQuery.isValidStarQuery(spList1));
    
   
    StarQuery sq1 = new StarQuery(spList1);
    
    Var v = sq1.getCommonVar();
    
    Assert.assertEquals("X", v.getName());
    Assert.assertEquals(null, v.getValue());
    Assert.assertEquals(v.getValue(), sq1.getCommonVarValue());
    Assert.assertTrue(!sq1.commonVarHasValue());
    Assert.assertEquals("X", sq1.getCommonVarName());
    Assert.assertTrue(sq1.isCommonVarURI());
    
    Assert.assertTrue(sq1.hasContext());
    Assert.assertEquals("http://joe", sq1.getContextURI());
    
    TextColumn[] cond = sq1.getColumnCond(); 
    
    for(int i = 0; i < cond.length; i++ ) {
    
        Assert.assertEquals(cond[i].getColumnFamily().toString(), "uri:cf" + (i+1));
        Assert.assertEquals(cond[i].getColumnQualifier().toString(), "object");
    
    }
    
    Set<String> unCommonVars = Sets.newHashSet();
    unCommonVars.add("Y1");
    unCommonVars.add("Y2");
    unCommonVars.add("Y3");
    Assert.assertEquals(unCommonVars, sq1.getUnCommonVars());
    
    Map<String, Integer> varPos = sq1.getVarPos();
    
    Assert.assertEquals(0, varPos.get("Y1").intValue());
    Assert.assertEquals(1, varPos.get("Y2").intValue());
    Assert.assertEquals(2, varPos.get("Y3").intValue());
    
    QueryBindingSet bs1 = new QueryBindingSet();
    QueryBindingSet bs2 = new QueryBindingSet();
    
    Value v1 = VF.createIRI("uri:hank");
    Value v2 = VF.createIRI("uri:bob");
    
    bs1.addBinding("X",v1);
    bs2.addBinding("X", v1);
    bs2.addBinding("Y3", v2);
    
    Set<String> s1 = StarQuery.getCommonVars(sq1, bs1);
    Set<String> s2 = StarQuery.getCommonVars(sq1, bs2);
    
    Set<String> s3 = Sets.newHashSet();
    Set<String> s4 = Sets.newHashSet();
    s3.add("X");
    s4.add("X");
    s4.add("Y3");
    
    
    Assert.assertEquals(s1, s3);
    Assert.assertEquals(s2, s4);
    
    
    
}