Java Code Examples for org.eclipse.rdf4j.query.algebra.BindingSetAssignment#setBindingSets()

The following examples show how to use org.eclipse.rdf4j.query.algebra.BindingSetAssignment#setBindingSets() . 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: QuerySpecTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testReplaceQueryPatternsWithNonEmptyResults() {
	final String expectedQueryPlan = "Join\n" +
			"   Join\n" +
			"      SingletonSet\n" +
			"      SingletonSet\n" +
			"   BindingSetAssignment ([[searchR=urn:1]])\n";
	final ParsedQuery query = parseQuery(QUERY);
	final List<SearchQueryEvaluator> queries = new ArrayList<>();
	new QuerySpecBuilder(true)
			.process(query.getTupleExpr(), EmptyBindingSet.getInstance(), queries);
	assertEquals(1, queries.size());
	QuerySpec querySpec = (QuerySpec) queries.get(0);
	BindingSetAssignment bsa = new BindingSetAssignment();
	bsa.setBindingSets(createBindingSet("searchR", "urn:1"));
	querySpec.replaceQueryPatternsWithResults(bsa);
	String result = querySpec.getParentQueryModelNode().getParentNode().toString().replaceAll("\r\n|\r", "\n");
	assertEquals(expectedQueryPlan, result);
}
 
Example 2
Source File: HashJoinIterationTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testCartesianJoin() throws QueryEvaluationException {
	BindingSetAssignment left = new BindingSetAssignment();
	{
		QueryBindingSet leftb = new QueryBindingSet();
		leftb.addBinding("a", vf.createLiteral("1"));
		left.setBindingSets(Arrays.<BindingSet>asList(leftb));
	}

	BindingSetAssignment right = new BindingSetAssignment();
	{
		QueryBindingSet rightb = new QueryBindingSet();
		rightb.addBinding("b", vf.createLiteral("2"));
		right.setBindingSets(Arrays.<BindingSet>asList(rightb));
	}

	HashJoinIteration iter = new HashJoinIteration(evaluator, left, right, EmptyBindingSet.getInstance(), false);
	BindingSet actual = iter.next();

	assertEquals("1", actual.getValue("a").stringValue());
	assertEquals("2", actual.getValue("b").stringValue());
}
 
Example 3
Source File: HashJoinIterationTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testInnerJoin() throws QueryEvaluationException {
	BindingSetAssignment left = new BindingSetAssignment();
	{
		QueryBindingSet leftb = new QueryBindingSet();
		leftb.addBinding("a", vf.createLiteral("1"));
		leftb.addBinding("i", vf.createLiteral("x"));
		left.setBindingSets(Arrays.<BindingSet>asList(leftb));
	}

	BindingSetAssignment right = new BindingSetAssignment();
	{
		QueryBindingSet rightb = new QueryBindingSet();
		rightb.addBinding("b", vf.createLiteral("2"));
		rightb.addBinding("i", vf.createLiteral("x"));
		right.setBindingSets(Arrays.<BindingSet>asList(rightb));
	}

	HashJoinIteration iter = new HashJoinIteration(evaluator, left, right, EmptyBindingSet.getInstance(), false);
	BindingSet actual = iter.next();

	assertEquals("1", actual.getValue("a").stringValue());
	assertEquals("2", actual.getValue("b").stringValue());
	assertEquals("x", actual.getValue("i").stringValue());
}
 
Example 4
Source File: HashJoinIterationTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testLeftJoin() throws QueryEvaluationException {
	BindingSetAssignment left = new BindingSetAssignment();
	{
		QueryBindingSet leftb = new QueryBindingSet();
		leftb.addBinding("a", vf.createLiteral("1"));
		leftb.addBinding("i", vf.createLiteral("x"));
		left.setBindingSets(Arrays.<BindingSet>asList(leftb));
	}

	BindingSetAssignment right = new BindingSetAssignment();
	{
		QueryBindingSet rightb = new QueryBindingSet();
		rightb.addBinding("b", vf.createLiteral("2"));
		rightb.addBinding("i", vf.createLiteral("y"));
		right.setBindingSets(Arrays.<BindingSet>asList(rightb));
	}

	HashJoinIteration iter = new HashJoinIteration(evaluator, left, right, EmptyBindingSet.getInstance(), true);
	BindingSet actual = iter.next();

	assertEquals("1", actual.getValue("a").stringValue());
	assertEquals("x", actual.getValue("i").stringValue());
	assertFalse(actual.hasBinding("b"));
}
 
Example 5
Source File: LuceneSailConnection.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Evaluate the given Lucene queries, generate bindings from the query result, add the bindings to the query tree,
 * and remove the Lucene queries from the given query tree.
 *
 * @param queries
 * @throws SailException
 */
private void evaluateLuceneQueries(Collection<SearchQueryEvaluator> queries) throws SailException {
	// TODO: optimize lucene queries here
	// - if they refer to the same subject, merge them into one lucene query
	// - multiple different property constraints can be put into the lucene
	// query string (escape colons here)

	if (closed.get()) {
		throw new SailException("Sail has been closed already");
	}

	// evaluate queries, generate binding sets, and remove queries
	for (SearchQueryEvaluator query : queries) {
		// evaluate the Lucene query and generate bindings
		final Collection<BindingSet> bindingSets = luceneIndex.evaluate(query);

		final BindingSetAssignment bsa = new BindingSetAssignment();

		// found something?
		if (bindingSets != null && !bindingSets.isEmpty()) {
			bsa.setBindingSets(bindingSets);
			if (bindingSets instanceof BindingSetCollection) {
				bsa.setBindingNames(((BindingSetCollection) bindingSets).getBindingNames());
			}
		}

		query.replaceQueryPatternsWithResults(bsa);
	}
}
 
Example 6
Source File: DistanceQuerySpecTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testReplaceQueryPatternsWithNonEmptyResults() {
	final String expectedQueryPlan = "Projection\n" +
			"   ProjectionElemList\n" +
			"      ProjectionElem \"toUri\"\n" +
			"      ProjectionElem \"to\"\n" +
			"   BindingSetAssignment ([[toUri=urn:subject1;to=\"POINT (2.2945 48.8582)\"^^<http://www.opengis.net/ont/geosparql#wktLiteral>]])\n";
	final ParsedQuery query = parseQuery(QUERY);

	final List<SearchQueryEvaluator> queries = new ArrayList<>();

	new DistanceQuerySpecBuilder(new SearchIndexImpl())
			.process(query.getTupleExpr(), EmptyBindingSet.getInstance(), queries);
	assertEquals(1, queries.size());

	final DistanceQuerySpec querySpec = (DistanceQuerySpec) queries.get(0);

	final MapBindingSet bindingSet = new MapBindingSet();
	bindingSet.addBinding("toUri", VF.createIRI("urn:subject1"));
	bindingSet.addBinding("to", VF.createLiteral("POINT (2.2945 48.8582)", GEO.WKT_LITERAL));

	BindingSetAssignment bsa = new BindingSetAssignment();
	bsa.setBindingSets(Collections.singletonList(bindingSet));

	querySpec.replaceQueryPatternsWithResults(bsa);
	String result = querySpec.getParentQueryModelNode().getParentNode().toString().replaceAll("\r\n|\r", "\n");
	assertEquals(expectedQueryPlan, result);
}
 
Example 7
Source File: GeoRelationQuerySpecTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testReplaceQueryPatternsWithNonEmptyResults() {
	final String expectedQueryPlan = "Projection\n" +
			"   ProjectionElemList\n" +
			"      ProjectionElem \"matchUri\"\n" +
			"      ProjectionElem \"match\"\n" +
			"   BindingSetAssignment ([[toUri=urn:subject4;to=\"POLYGON ((2.3294 48.8726, 2.2719 48.8643, 2.3370 48.8398, 2.3294 48.8726))\"^^<http://www.opengis.net/ont/geosparql#wktLiteral>]])\n";
	final ParsedQuery query = parseQuery(QUERY);

	final List<SearchQueryEvaluator> queries = new ArrayList<>();

	new GeoRelationQuerySpecBuilder(new SearchIndexImpl())
			.process(query.getTupleExpr(), EmptyBindingSet.getInstance(), queries);
	assertEquals(1, queries.size());

	final GeoRelationQuerySpec querySpec = (GeoRelationQuerySpec) queries.get(0);

	final MapBindingSet bindingSet = new MapBindingSet();
	bindingSet.addBinding("toUri", VF.createIRI("urn:subject4"));
	bindingSet.addBinding("to", VF.createLiteral(
			"POLYGON ((2.3294 48.8726, 2.2719 48.8643, 2.3370 48.8398, 2.3294 48.8726))", GEO.WKT_LITERAL));

	BindingSetAssignment bsa = new BindingSetAssignment();
	bsa.setBindingSets(Collections.singletonList(bindingSet));

	querySpec.replaceQueryPatternsWithResults(bsa);
	String result = querySpec.getParentQueryModelNode().getParentNode().toString().replaceAll("\r\n|\r", "\n");
	assertEquals(expectedQueryPlan, result);
}
 
Example 8
Source File: TupleExprBuilder.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public BindingSetAssignment visit(ASTInlineData node, Object data) throws VisitorException {

	BindingSetAssignment bsa = new BindingSetAssignment();

	List<ASTVar> varNodes = node.jjtGetChildren(ASTVar.class);
	List<Var> vars = new ArrayList<>(varNodes.size());

	// preserve order in query
	Set<String> bindingNames = new LinkedHashSet<>(varNodes.size());
	for (ASTVar varNode : varNodes) {
		Var var = (Var) varNode.jjtAccept(this, data);
		vars.add(var);
		bindingNames.add(var.getName());
	}

	bsa.setBindingNames(bindingNames);

	List<ASTBindingSet> bindingNodes = node.jjtGetChildren(ASTBindingSet.class);
	List<BindingSet> bindingSets = new ArrayList<>(bindingNodes.size());

	for (ASTBindingSet bindingNode : bindingNodes) {
		BindingSet bindingSet = (BindingSet) bindingNode.jjtAccept(this, vars);
		bindingSets.add(bindingSet);
	}

	bsa.setBindingSets(bindingSets);

	graphPattern.addRequiredTE(bsa);
	return bsa;
}
 
Example 9
Source File: TupleExprBuilder.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public BindingSetAssignment visit(ASTBindingsClause node, Object data) throws VisitorException {
	BindingSetAssignment bsa = new BindingSetAssignment();

	List<ASTVar> varNodes = node.jjtGetChildren(ASTVar.class);
	List<Var> vars = new ArrayList<>(varNodes.size());

	// preserve order in query
	Set<String> bindingNames = new LinkedHashSet<>(varNodes.size());
	for (ASTVar varNode : varNodes) {
		Var var = (Var) varNode.jjtAccept(this, data);
		vars.add(var);
		bindingNames.add(var.getName());
	}

	bsa.setBindingNames(bindingNames);

	List<ASTBindingSet> bindingNodes = node.jjtGetChildren(ASTBindingSet.class);
	List<BindingSet> bindingSets = new ArrayList<>(bindingNodes.size());

	for (ASTBindingSet bindingNode : bindingNodes) {
		BindingSet bindingSet = (BindingSet) bindingNode.jjtAccept(this, vars);
		bindingSets.add(bindingSet);
	}

	bsa.setBindingSets(bindingSets);

	return bsa;
}
 
Example 10
Source File: OneOfVisitor.java    From rya with Apache License 2.0 5 votes vote down vote up
@Override
protected void meetSP(final StatementPattern node) throws Exception {
    final Var subVar = node.getSubjectVar();
    final Var predVar = node.getPredicateVar();
    final Var objVar = node.getObjectVar();
    final Var conVar = node.getContextVar();
    if (predVar != null && objVar != null && objVar.getValue() != null && objVar.getValue() instanceof Resource && RDF.TYPE.equals(predVar.getValue()) && !EXPANDED.equals(conVar)) {
        final Resource object = (Resource) objVar.getValue();
        if (inferenceEngine.isEnumeratedType(object)) {
            final Set<BindingSet> solutions = new LinkedHashSet<>();
            final Set<Resource> enumeration = inferenceEngine.getEnumeration(object);
            for (final Resource enumType : enumeration) {
                final QueryBindingSet qbs = new QueryBindingSet();
                qbs.addBinding(subVar.getName(), enumType);
                solutions.add(qbs);
            }

            if (!solutions.isEmpty()) {
                final BindingSetAssignment enumNode = new BindingSetAssignment();
                enumNode.setBindingSets(solutions);

                node.replaceWith(enumNode);
                log.trace("Replacing node with inferred one of enumeration: " + enumNode);
            }
        }
    }
}
 
Example 11
Source File: BindingSetAssignmentBlock.java    From semagrow with Apache License 2.0 4 votes vote down vote up
public Collection<Plan> getPlans(CompilerContext context) {
    BindingSetAssignment expr = new BindingSetAssignment();
    expr.setBindingNames(bindingNames);
    expr.setBindingSets(bindingSets);
    return Collections.singleton(context.asPlan(expr));
}