org.eclipse.rdf4j.query.algebra.SameTerm Java Examples

The following examples show how to use org.eclipse.rdf4j.query.algebra.SameTerm. 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: QueryModelBuilder.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public List<Var> visit(ASTNode node, Object data) throws VisitorException {
	List<Var> nodeVars = new ArrayList<>();

	for (ASTNodeElem nodeElem : node.getNodeElemList()) {
		Var nodeVar = (Var) nodeElem.jjtAccept(this, null);
		nodeVars.add(nodeVar);
	}

	// Create any implicit unequalities
	for (int i = 0; i < nodeVars.size() - 1; i++) {
		Var var1 = nodeVars.get(i);

		for (int j = i + 1; j < nodeVars.size(); j++) {
			Var var2 = nodeVars.get(j);

			// At least one of the variables should be non-constant
			// for the unequality to make any sense:
			if (!var1.hasValue() || !var2.hasValue()) {
				graphPattern.addConstraint(new Not(new SameTerm(var1, var2)));
			}
		}
	}

	return nodeVars;
}
 
Example #2
Source File: CompareOptimizer.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public void meet(Compare compare) {
	super.meet(compare);

	if (compare.getOperator() == CompareOp.EQ) {
		ValueExpr leftArg = compare.getLeftArg();
		ValueExpr rightArg = compare.getRightArg();

		boolean leftIsVar = isVar(leftArg);
		boolean rightIsVar = isVar(rightArg);
		boolean leftIsResource = isResource(leftArg);
		boolean rightIsResource = isResource(rightArg);

		if (leftIsVar && rightIsResource || leftIsResource && rightIsVar || leftIsResource && rightIsResource) {
			SameTerm sameTerm = new SameTerm(leftArg, rightArg);
			compare.replaceWith(sameTerm);
		}
	}
}
 
Example #3
Source File: TupleExprBuilder.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private List<StatementPattern> replaceSameTermVars(List<StatementPattern> statementPatterns,
		Set<SameTerm> sameTermConstraints) {
	if (sameTermConstraints != null) {
		for (SameTerm st : sameTermConstraints) {
			Var left = (Var) st.getLeftArg();
			Var right = (Var) st.getRightArg();
			for (StatementPattern sp : statementPatterns) {
				Var subj = sp.getSubjectVar();
				Var obj = sp.getObjectVar();

				if (subj.equals(left) || subj.equals(right)) {
					if (obj.equals(left) || obj.equals(right)) {
						sp.setObjectVar(subj);
					}
				}
			}
		}
	}
	return statementPatterns;
}
 
Example #4
Source File: SerqlValueExprRenderer.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * @inheritDoc
 */
@Override
public void meet(SameTerm theOp) throws Exception {
	if (SeRQLQueryRenderer.SERQL_ONE_X_COMPATIBILITY_MODE) {
		mBuffer.append("(");
		theOp.getLeftArg().visit(this);
		mBuffer.append(" = ");
		theOp.getRightArg().visit(this);
		mBuffer.append(")");
	} else {
		mBuffer.append(" sameTerm(");
		theOp.getLeftArg().visit(this);
		mBuffer.append(", ");
		theOp.getRightArg().visit(this);
		mBuffer.append(")");
	}
}
 
Example #5
Source File: StrictEvaluationStrategy.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public Value evaluate(SameTerm node, BindingSet bindings)
		throws QueryEvaluationException {
	Value leftVal = evaluate(node.getLeftArg(), bindings);
	Value rightVal = evaluate(node.getRightArg(), bindings);

	return BooleanLiteral.valueOf(leftVal != null && leftVal.equals(rightVal));
}
 
Example #6
Source File: SPARQLValueExprRenderer.java    From semagrow with Apache License 2.0 5 votes vote down vote up
/**
 * @inheritDoc
 */
@Override
public void meet(SameTerm theOp)
        throws Exception
{
    mBuffer.append(" sameTerm(");
    theOp.getLeftArg().visit(this);
    mBuffer.append(", ");
    theOp.getRightArg().visit(this);
    mBuffer.append(")");
}
 
Example #7
Source File: SparqlValueExprRenderer.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * @inheritDoc
 */
@Override
public void meet(SameTerm theOp) throws Exception {
	mBuffer.append(" sameTerm(");
	theOp.getLeftArg().visit(this);
	mBuffer.append(", ");
	theOp.getRightArg().visit(this);
	mBuffer.append(")");
}
 
Example #8
Source File: FilterBuilder.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public GroupBuilder<T, E> sameTerm(ValueExpr theLeft, ValueExpr theRight) {
	return filter(new SameTerm(theLeft, theRight));
}
 
Example #9
Source File: AbstractQueryModelVisitor.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public void meet(SameTerm node) throws X {
	meetBinaryValueOperator(node);
}
 
Example #10
Source File: TupleExprBuilder.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private Set<SameTerm> getSameTermConstraints(Filter filter) throws VisitorException {
	final SameTermCollector collector = new SameTermCollector();
	filter.visit(collector);

	return collector.getCollectedSameTerms();
}
 
Example #11
Source File: TupleExprBuilder.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public void meet(SameTerm st) {
	collectedSameTerms.add(st);
}
 
Example #12
Source File: TupleExprBuilder.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * @return Returns the collected SameTerms.
 */
public Set<SameTerm> getCollectedSameTerms() {
	return collectedSameTerms;
}
 
Example #13
Source File: TupleExprBuilder.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public SameTerm visit(ASTSameTerm node, Object data) throws VisitorException {
	ValueExpr leftArg = (ValueExpr) node.jjtGetChild(0).jjtAccept(this, null);
	ValueExpr rightArg = (ValueExpr) node.jjtGetChild(1).jjtAccept(this, null);
	return new SameTerm(leftArg, rightArg);
}
 
Example #14
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 #15
Source File: QueryModelBuilder.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public SameTerm visit(ASTSameTerm node, Object data) throws VisitorException {
	ValueExpr leftArg = (ValueExpr) node.getLeftOperand().jjtAccept(this, null);
	ValueExpr rightArg = (ValueExpr) node.getRightOperand().jjtAccept(this, null);
	return new SameTerm(leftArg, rightArg);
}
 
Example #16
Source File: HalyardValueExprEvaluation.java    From Halyard with Apache License 2.0 4 votes vote down vote up
/**
 * Determines which evaluate method to call based on the type of {@link ValueExpr}
 * @param expr the expression to evaluate
 * @param bindings the set of named value bindings the set of named value bindings
 * @return the {@link Value} resulting from the evaluation
 * @throws ValueExprEvaluationException
 * @throws QueryEvaluationException
 */
Value evaluate(ValueExpr expr, BindingSet bindings) throws ValueExprEvaluationException, QueryEvaluationException {
    if (expr instanceof Var) {
        return evaluate((Var) expr, bindings);
    } else if (expr instanceof ValueConstant) {
        return evaluate((ValueConstant) expr, bindings);
    } else if (expr instanceof BNodeGenerator) {
        return evaluate((BNodeGenerator) expr, bindings);
    } else if (expr instanceof Bound) {
        return evaluate((Bound) expr, bindings);
    } else if (expr instanceof Str) {
        return evaluate((Str) expr, bindings);
    } else if (expr instanceof Label) {
        return evaluate((Label) expr, bindings);
    } else if (expr instanceof Lang) {
        return evaluate((Lang) expr, bindings);
    } else if (expr instanceof LangMatches) {
        return evaluate((LangMatches) expr, bindings);
    } else if (expr instanceof Datatype) {
        return evaluate((Datatype) expr, bindings);
    } else if (expr instanceof Namespace) {
        return evaluate((Namespace) expr, bindings);
    } else if (expr instanceof LocalName) {
        return evaluate((LocalName) expr, bindings);
    } else if (expr instanceof IsResource) {
        return evaluate((IsResource) expr, bindings);
    } else if (expr instanceof IsURI) {
        return evaluate((IsURI) expr, bindings);
    } else if (expr instanceof IsBNode) {
        return evaluate((IsBNode) expr, bindings);
    } else if (expr instanceof IsLiteral) {
        return evaluate((IsLiteral) expr, bindings);
    } else if (expr instanceof IsNumeric) {
        return evaluate((IsNumeric) expr, bindings);
    } else if (expr instanceof IRIFunction) {
        return evaluate((IRIFunction) expr, bindings);
    } else if (expr instanceof Regex) {
        return evaluate((Regex) expr, bindings);
    } else if (expr instanceof Coalesce) {
        return evaluate((Coalesce) expr, bindings);
    } else if (expr instanceof Like) {
        return evaluate((Like) expr, bindings);
    } else if (expr instanceof FunctionCall) {
        return evaluate((FunctionCall) expr, bindings);
    } else if (expr instanceof And) {
        return evaluate((And) expr, bindings);
    } else if (expr instanceof Or) {
        return evaluate((Or) expr, bindings);
    } else if (expr instanceof Not) {
        return evaluate((Not) expr, bindings);
    } else if (expr instanceof SameTerm) {
        return evaluate((SameTerm) expr, bindings);
    } else if (expr instanceof Compare) {
        return evaluate((Compare) expr, bindings);
    } else if (expr instanceof MathExpr) {
        return evaluate((MathExpr) expr, bindings);
    } else if (expr instanceof In) {
        return evaluate((In) expr, bindings);
    } else if (expr instanceof CompareAny) {
        return evaluate((CompareAny) expr, bindings);
    } else if (expr instanceof CompareAll) {
        return evaluate((CompareAll) expr, bindings);
    } else if (expr instanceof Exists) {
        return evaluate((Exists) expr, bindings);
    } else if (expr instanceof If) {
        return evaluate((If) expr, bindings);
    } else if (expr instanceof ListMemberOperator) {
        return evaluate((ListMemberOperator) expr, bindings);
    } else if (expr == null) {
        throw new IllegalArgumentException("expr must not be null");
    } else {
        throw new QueryEvaluationException("Unsupported value expr type: " + expr.getClass());
    }
}
 
Example #17
Source File: StrictEvaluationStrategy.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public Value evaluate(ValueExpr expr, BindingSet bindings)
		throws QueryEvaluationException {
	if (expr instanceof Var) {
		return evaluate((Var) expr, bindings);
	} else if (expr instanceof ValueConstant) {
		return evaluate((ValueConstant) expr, bindings);
	} else if (expr instanceof BNodeGenerator) {
		return evaluate((BNodeGenerator) expr, bindings);
	} else if (expr instanceof Bound) {
		return evaluate((Bound) expr, bindings);
	} else if (expr instanceof Str) {
		return evaluate((Str) expr, bindings);
	} else if (expr instanceof Label) {
		return evaluate((Label) expr, bindings);
	} else if (expr instanceof Lang) {
		return evaluate((Lang) expr, bindings);
	} else if (expr instanceof LangMatches) {
		return evaluate((LangMatches) expr, bindings);
	} else if (expr instanceof Datatype) {
		return evaluate((Datatype) expr, bindings);
	} else if (expr instanceof Namespace) {
		return evaluate((Namespace) expr, bindings);
	} else if (expr instanceof LocalName) {
		return evaluate((LocalName) expr, bindings);
	} else if (expr instanceof IsResource) {
		return evaluate((IsResource) expr, bindings);
	} else if (expr instanceof IsURI) {
		return evaluate((IsURI) expr, bindings);
	} else if (expr instanceof IsBNode) {
		return evaluate((IsBNode) expr, bindings);
	} else if (expr instanceof IsLiteral) {
		return evaluate((IsLiteral) expr, bindings);
	} else if (expr instanceof IsNumeric) {
		return evaluate((IsNumeric) expr, bindings);
	} else if (expr instanceof IRIFunction) {
		return evaluate((IRIFunction) expr, bindings);
	} else if (expr instanceof Regex) {
		return evaluate((Regex) expr, bindings);
	} else if (expr instanceof Coalesce) {
		return evaluate((Coalesce) expr, bindings);
	} else if (expr instanceof Like) {
		return evaluate((Like) expr, bindings);
	} else if (expr instanceof FunctionCall) {
		return evaluate((FunctionCall) expr, bindings);
	} else if (expr instanceof And) {
		return evaluate((And) expr, bindings);
	} else if (expr instanceof Or) {
		return evaluate((Or) expr, bindings);
	} else if (expr instanceof Not) {
		return evaluate((Not) expr, bindings);
	} else if (expr instanceof SameTerm) {
		return evaluate((SameTerm) expr, bindings);
	} else if (expr instanceof Compare) {
		return evaluate((Compare) expr, bindings);
	} else if (expr instanceof MathExpr) {
		return evaluate((MathExpr) expr, bindings);
	} else if (expr instanceof In) {
		return evaluate((In) expr, bindings);
	} else if (expr instanceof CompareAny) {
		return evaluate((CompareAny) expr, bindings);
	} else if (expr instanceof CompareAll) {
		return evaluate((CompareAll) expr, bindings);
	} else if (expr instanceof Exists) {
		return evaluate((Exists) expr, bindings);
	} else if (expr instanceof If) {
		return evaluate((If) expr, bindings);
	} else if (expr instanceof ListMemberOperator) {
		return evaluate((ListMemberOperator) expr, bindings);
	} else if (expr instanceof ValueExprTripleRef) {
		return evaluate((ValueExprTripleRef) expr, bindings);
	} else if (expr == null) {
		throw new IllegalArgumentException("expr must not be null");
	} else {
		throw new QueryEvaluationException("Unsupported value expr type: " + expr.getClass());
	}
}
 
Example #18
Source File: HalyardValueExprEvaluation.java    From Halyard with Apache License 2.0 2 votes vote down vote up
/**
 * Evaluate if the left and right arguments of the {@link SameTerm} node are equal
 * @param node the node to evaluate
 * @param bindings the set of named value bindings
 * @return
 * @throws ValueExprEvaluationException
 * @throws QueryEvaluationException
 */
private Value evaluate(SameTerm node, BindingSet bindings) throws ValueExprEvaluationException, QueryEvaluationException {
    Value leftVal = evaluate(node.getLeftArg(), bindings);
    Value rightVal = evaluate(node.getRightArg(), bindings);
    return BooleanLiteral.valueOf(leftVal != null && leftVal.equals(rightVal));
}