Java Code Examples for org.eclipse.rdf4j.query.algebra.TupleExpr#getBindingNames()

The following examples show how to use org.eclipse.rdf4j.query.algebra.TupleExpr#getBindingNames() . 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: SailConnectionTupleQuery.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public TupleQueryResult evaluate() throws QueryEvaluationException {
	TupleExpr tupleExpr = getParsedQuery().getTupleExpr();

	try {
		CloseableIteration<? extends BindingSet, QueryEvaluationException> bindingsIter;

		SailConnection sailCon = getSailConnection();
		bindingsIter = sailCon.evaluate(tupleExpr, getActiveDataset(), getBindings(), getIncludeInferred());

		bindingsIter = enforceMaxQueryTime(bindingsIter);

		return new TupleQueryResultImpl(new ArrayList<>(tupleExpr.getBindingNames()), bindingsIter);
	} catch (SailException e) {
		throw new QueryEvaluationException(e.getMessage(), e);
	}
}
 
Example 2
Source File: SemagrowSailTupleQuery.java    From semagrow with Apache License 2.0 6 votes vote down vote up
public TupleQueryResult evaluate() throws QueryEvaluationException {

        TupleExpr tupleExpr = getParsedQuery().getTupleExpr();

        try {
            CloseableIteration<? extends BindingSet, QueryEvaluationException> bindingsIter;

            SemagrowSailConnection sailCon = (SemagrowSailConnection) getConnection().getSailConnection();

            bindingsIter = sailCon.evaluate(tupleExpr, getActiveDataset(), getBindings(),
                    getIncludeInferred(), getIncludeProvenanceData(),
                    getIncludedSources(), getExcludedSources());

            bindingsIter = enforceMaxQueryTime(bindingsIter);

            return new IteratingTupleQueryResult(new ArrayList<String>(tupleExpr.getBindingNames()), bindingsIter);
        }
        catch (SailException e) {
            throw new QueryEvaluationException(e.getMessage(), e);
        }
    }
 
Example 3
Source File: AbstractQueryPreparer.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public TupleQueryResult evaluate() throws QueryEvaluationException {
	CloseableIteration<? extends BindingSet, QueryEvaluationException> bindingsIter1 = null;
	CloseableIteration<? extends BindingSet, QueryEvaluationException> bindingsIter2 = null;
	IteratingTupleQueryResult result = null;
	boolean allGood = false;
	try {
		TupleExpr tupleExpr = getParsedQuery().getTupleExpr();
		bindingsIter1 = AbstractQueryPreparer.this.evaluate(tupleExpr, getActiveDataset(), getBindings(),
				getIncludeInferred(), getMaxExecutionTime());
		bindingsIter2 = enforceMaxQueryTime(bindingsIter1);
		result = new IteratingTupleQueryResult(new ArrayList<>(tupleExpr.getBindingNames()), bindingsIter2);
		allGood = true;
		return result;
	} finally {
		if (!allGood) {
			try {
				if (result != null) {
					result.close();
				}
			} finally {
				try {
					if (bindingsIter2 != null) {
						bindingsIter2.close();
					}
				} finally {
					if (bindingsIter1 != null) {
						bindingsIter1.close();
					}
				}
			}
		}
	}
}
 
Example 4
Source File: SailTupleQuery.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public TupleQueryResult evaluate() throws QueryEvaluationException {
	TupleExpr tupleExpr = getParsedQuery().getTupleExpr();

	CloseableIteration<? extends BindingSet, QueryEvaluationException> bindingsIter = null;

	boolean allGood = false;
	try {
		SailConnection sailCon = getConnection().getSailConnection();

		bindingsIter = sailCon.evaluate(tupleExpr, getActiveDataset(), getBindings(), getIncludeInferred());
		bindingsIter = enforceMaxQueryTime(bindingsIter);

		IteratingTupleQueryResult result = new IteratingTupleQueryResult(
				new ArrayList<>(tupleExpr.getBindingNames()), bindingsIter);
		allGood = true;
		return result;
	} catch (SailException e) {
		throw new QueryEvaluationException(e.getMessage(), e);
	} finally {
		if (!allGood) {
			if (bindingsIter != null) {
				bindingsIter.close();
			}
		}
	}
}
 
Example 5
Source File: SparqlFluoQueryBuilder.java    From rya with Apache License 2.0 5 votes vote down vote up
/**
 * Get the non-constant variables from a {@link TupleExpr}.
 *
 * @param node - The node to inspect for variables. (not null)
 * @return The non-constant variables that were part of the node.
 */
private Set<String> getVars(final TupleExpr node) {
    checkNotNull(node);

    final Set<String> vars = Sets.newHashSet();

    for(final String bindingName : node.getBindingNames()) {
        if (!VarNameUtils.isConstant(bindingName)) {
            vars.add(bindingName);
        }
    }

    return vars;
}
 
Example 6
Source File: SameTermFilterOptimizer.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public void meet(Filter filter) {
	super.meet(filter);

	if (filter.getCondition() instanceof SameTerm) {
		// SameTerm applies to the filter's argument
		SameTerm sameTerm = (SameTerm) filter.getCondition();
		TupleExpr filterArg = filter.getArg();

		ValueExpr leftArg = sameTerm.getLeftArg();
		ValueExpr rightArg = sameTerm.getRightArg();

		// Verify that vars are (potentially) bound by filterArg
		Set<String> bindingNames = filterArg.getBindingNames();
		if (isUnboundVar(leftArg, bindingNames) || isUnboundVar(rightArg, bindingNames)) {
			// One or both var(s) are unbound, this expression will never
			// return any results
			filter.replaceWith(new EmptySet());
			return;
		}

		Set<String> assuredBindingNames = filterArg.getAssuredBindingNames();
		if (isUnboundVar(leftArg, assuredBindingNames) || isUnboundVar(rightArg, assuredBindingNames)) {
			// One or both var(s) are potentially unbound, inlining could
			// invalidate the result e.g. in case of left joins
			return;
		}

		if (leftArg instanceof Var || rightArg instanceof Var) {
			if (filterArg instanceof ArbitraryLengthPath && leftArg instanceof Var && rightArg instanceof Var) {
				final ArbitraryLengthPath alp = (ArbitraryLengthPath) filterArg;
				final List<Var> sameTermArgs = Arrays.asList((Var) leftArg, (Var) rightArg);

				if (sameTermArgs.contains(alp.getSubjectVar()) && sameTermArgs.contains(alp.getObjectVar())) {
					// SameTerm provides a deferred mapping to allow arbitrary-length property path to produce
					// cyclic paths. See SES-1685.
					// we can not inline.
					return;
				}
			}

			BindingSetAssignmentCollector collector = new BindingSetAssignmentCollector();
			filterArg.visit(collector);

			for (BindingSetAssignment bsa : collector.getBindingSetAssignments()) {
				// check if the VALUES clause / bindingsetassignment contains
				// one of the arguments of the sameTerm.
				// if so, we can not inline.
				Set<String> names = bsa.getAssuredBindingNames();
				if (leftArg instanceof Var) {
					if (names.contains(((Var) leftArg).getName())) {
						return;
					}
				}
				if (rightArg instanceof Var) {
					if (names.contains(((Var) rightArg).getName())) {
						return;
					}
				}
			}
		}

		Value leftValue = getValue(leftArg);
		Value rightValue = getValue(rightArg);

		if (leftValue != null && rightValue != null) {
			// ConstantOptimizer should have taken care of this
		} else if (leftValue != null && rightArg instanceof Var) {
			bindVar((Var) rightArg, leftValue, filter);
		} else if (rightValue != null && leftArg instanceof Var) {
			bindVar((Var) leftArg, rightValue, filter);
		} else if (leftArg instanceof Var && rightArg instanceof Var) {
			// Two unbound variables, rename rightArg to leftArg
			renameVar((Var) rightArg, (Var) leftArg, filter);
		}
	}
}
 
Example 7
Source File: QueryJoinOptimizer.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private TupleExpr getNextSubselect(List<TupleExpr> currentList, List<TupleExpr> joinArgs) {

			// determine union of names of all elements currently in the list: this
			// corresponds to the projection resulting from joining all these
			// elements.
			Set<String> currentListNames = new HashSet<>();
			for (TupleExpr expr : currentList) {
				currentListNames.addAll(expr.getBindingNames());
			}

			// select the next argument from the list, by checking that it has,
			// first, the highest join size with the current list, and second, the
			// highest union size.
			TupleExpr selected = null;
			int currentUnionSize = -1;
			int currentJoinSize = -1;
			for (TupleExpr candidate : joinArgs) {
				if (!currentList.contains(candidate)) {
					Set<String> names = candidate.getBindingNames();
					names.retainAll(currentListNames);
					int joinSize = names.size();

					names = candidate.getBindingNames();
					names.addAll(currentListNames);
					int unionSize = names.size();

					if (joinSize > currentJoinSize) {
						selected = candidate;
						currentJoinSize = joinSize;
						currentUnionSize = unionSize;
					} else if (joinSize == currentJoinSize) {
						if (unionSize > currentUnionSize) {
							selected = candidate;
							currentJoinSize = joinSize;
							currentUnionSize = unionSize;
						}
					}
				}
			}

			return selected;
		}
 
Example 8
Source File: HashJoinIteration.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public HashJoinIteration(EvaluationStrategy strategy, TupleExpr left, TupleExpr right, BindingSet bindings,
		boolean leftJoin) throws QueryEvaluationException {
	this(strategy, strategy.evaluate(left, bindings), left.getBindingNames(), strategy.evaluate(right, bindings),
			right.getBindingNames(), leftJoin);
}
 
Example 9
Source File: AccumuloPeriodicQueryResultStorage.java    From rya with Apache License 2.0 4 votes vote down vote up
public Set<String> getNonAggregationVariables(final String sparql) throws MalformedQueryException {
    final TupleExpr te = new SPARQLParser().parseQuery(sparql, null).getTupleExpr();
    bindingNames = te.getBindingNames();
    te.visit(this);
    return bindingNames;
}