org.eclipse.rdf4j.query.algebra.Compare.CompareOp Java Examples

The following examples show how to use org.eclipse.rdf4j.query.algebra.Compare.CompareOp. 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: QueryAlgebraUtil.java    From CostFed with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Construct the statement string, i.e. "s p ?o_varID FILTER ?o_N=o ". This kind of statement
 * pattern is necessary to later on identify available results.
 * 
 * @param stmt
 * @param varID
 * @param varNames
 * @param bindings
 * @return
 */
protected static TupleExpr constructStatementCheckId(StatementPattern stmt, int varID, Set<String> varNames, BindingSet bindings) {
	
	String _varID = Integer.toString(varID);
	Var subj = appendVarId(stmt.getSubjectVar(), _varID, varNames, bindings);
	Var pred = appendVarId(stmt.getPredicateVar(), _varID, varNames, bindings);
	
	Var obj = new Var("o_" + _varID);
	varNames.add("o_" + _varID);
			
	Value objValue;
	if (stmt.getObjectVar().hasValue()) {
		objValue = stmt.getObjectVar().getValue();
	} else if (bindings.hasBinding(stmt.getObjectVar().getName())){
		objValue = bindings.getBinding(stmt.getObjectVar().getName()).getValue();
	} else {
		// just to make sure that we see an error, will be deleted soon
		throw new RuntimeException("Unexpected.");
	}
	
	Compare cmp = new Compare(obj, new ValueConstant(objValue));
	cmp.setOperator(CompareOp.EQ);
	Filter filter = new Filter( new StatementPattern(subj, pred, obj), cmp);
			
	return filter;
}
 
Example #2
Source File: QueryBlockBuilder.java    From semagrow with Apache License 2.0 6 votes vote down vote up
public Compare.CompareOp getOperator() {
    Compare.CompareOp compareOp = null;

    if (op instanceof CompareAll)
        compareOp = ((CompareAll)op).getOperator();
    else if (op instanceof CompareAny)
        compareOp = ((CompareAny)op).getOperator();
    else if (op instanceof In)
        compareOp = Compare.CompareOp.EQ;

    if (compareOp != null) {
        if (negated)
            return negateOp(compareOp);
        else
            return compareOp;
    }
    else
        throw new RuntimeException();
}
 
Example #3
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 #4
Source File: QueryEvaluationUtil.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public static boolean compare(Value leftVal, Value rightVal, CompareOp operator, boolean strict)
		throws ValueExprEvaluationException {
	if (leftVal instanceof Literal && rightVal instanceof Literal) {
		// Both left and right argument is a Literal
		return compareLiterals((Literal) leftVal, (Literal) rightVal, operator, strict);
	} else {
		// All other value combinations
		switch (operator) {
		case EQ:
			return valuesEqual(leftVal, rightVal);
		case NE:
			return !valuesEqual(leftVal, rightVal);
		default:
			throw new ValueExprEvaluationException(
					"Only literals with compatible, ordered datatypes can be compared using <, <=, > and >= operators");
		}
	}
}
 
Example #5
Source File: DistanceQuerySpecBuilder.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private static Object[] getFilterAndDistance(QueryModelNode node, String compareArgVarName) {
	Object[] rv = null;
	if (node instanceof Filter) {
		Filter f = (Filter) node;
		ValueExpr condition = f.getCondition();
		if (condition instanceof Compare) {
			Compare compare = (Compare) condition;
			CompareOp op = compare.getOperator();
			ValueExpr dist = null;
			if (op == CompareOp.LT
					&& compareArgVarName.equals(DistanceQuerySpec.getVarName(compare.getLeftArg()))) {
				dist = compare.getRightArg();
			} else if (op == CompareOp.GT
					&& compareArgVarName.equals(DistanceQuerySpec.getVarName(compare.getRightArg()))) {
				dist = compare.getLeftArg();
			}
			rv = new Object[] { f, dist };
		}
	} else if (node != null) {
		rv = getFilterAndDistance(node.getParentNode(), compareArgVarName);
	}
	return rv;
}
 
Example #6
Source File: SpinParser.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private CompareOp toCompareOp(IRI func) {
	if (SP.EQ.equals(func)) {
		return CompareOp.EQ;
	} else if (SP.NE.equals(func)) {
		return CompareOp.NE;
	} else if (SP.LT.equals(func)) {
		return CompareOp.LT;
	} else if (SP.LE.equals(func)) {
		return CompareOp.LE;
	} else if (SP.GE.equals(func)) {
		return CompareOp.GE;
	} else if (SP.GT.equals(func)) {
		return CompareOp.GT;
	} else {
		return null;
	}
}
 
Example #7
Source File: SpinRenderer.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private Value toValue(CompareOp op) {
	switch (op) {
	case EQ:
		return SP.EQ;
	case NE:
		return SP.NE;
	case LT:
		return SP.LT;
	case LE:
		return SP.LE;
	case GE:
		return SP.GE;
	case GT:
		return SP.GT;
	}
	throw new AssertionError("Unrecognised CompareOp: " + op);
}
 
Example #8
Source File: QueryBlockBuilder.java    From semagrow with Apache License 2.0 5 votes vote down vote up
private CompareOp negateOp(CompareOp op) {
    switch (op) {
        case EQ : return CompareOp.NE;
        case NE : return CompareOp.EQ;
        case LE : return CompareOp.GT;
        case GE : return CompareOp.LT;
        case GT : return CompareOp.LE;
        case LT : return CompareOp.GE;
        default : throw new RuntimeException();
    }
}
 
Example #9
Source File: QueryModelBuilder.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public CompareAll visit(ASTCompareAll node, Object data) throws VisitorException {
	ValueExpr valueExpr = (ValueExpr) node.getLeftOperand().jjtAccept(this, null);
	TupleExpr tupleExpr = (TupleExpr) node.getRightOperand().jjtAccept(this, null);
	CompareOp op = node.getOperator().getValue();

	return new CompareAll(valueExpr, tupleExpr, op);
}
 
Example #10
Source File: QueryModelBuilder.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public CompareAny visit(ASTCompareAny node, Object data) throws VisitorException {
	ValueExpr valueExpr = (ValueExpr) node.getLeftOperand().jjtAccept(this, null);
	TupleExpr tupleExpr = (TupleExpr) node.getRightOperand().jjtAccept(this, null);
	CompareOp op = node.getOperator().getValue();

	return new CompareAny(valueExpr, tupleExpr, op);
}
 
Example #11
Source File: QueryModelBuilder.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public Compare visit(ASTCompare node, Object data) throws VisitorException {
	ValueExpr leftArg = (ValueExpr) node.getLeftOperand().jjtAccept(this, null);
	ValueExpr rightArg = (ValueExpr) node.getRightOperand().jjtAccept(this, null);
	CompareOp operator = node.getOperator().getValue();

	return new Compare(leftArg, rightArg, operator);
}
 
Example #12
Source File: QueryEvaluationUtilTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Assert that there is an exception as a result of comparing the two literals with the given operator.
 *
 * @param lit1 The left literal
 * @param lit2 The right literal
 * @param op   The operator for the comparison
 */
private void assertCompareException(Literal lit1, Literal lit2, CompareOp op, boolean strict) throws Exception {
	try {
		boolean returnValue = QueryEvaluationUtil.compareLiterals(lit1, lit2, op, strict);
		fail("Did not receive expected ValueExprEvaluationException (return value was " + returnValue + ") for "
				+ lit1.toString() + op.getSymbol() + lit2.toString());
	} catch (ValueExprEvaluationException e) {
		// Expected exception
	}
}
 
Example #13
Source File: StrictEvaluationStrategy.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public Value evaluate(ListMemberOperator node, BindingSet bindings)
		throws QueryEvaluationException {
	List<ValueExpr> args = node.getArguments();
	Value leftValue = evaluate(args.get(0), bindings);

	boolean result = false;
	ValueExprEvaluationException typeError = null;
	for (int i = 1; i < args.size(); i++) {
		ValueExpr arg = args.get(i);
		try {
			Value rightValue = evaluate(arg, bindings);
			result = leftValue == null && rightValue == null;
			if (!result) {
				result = QueryEvaluationUtil.compare(leftValue, rightValue, CompareOp.EQ);
			}
			if (result) {
				break;
			}
		} catch (ValueExprEvaluationException caught) {
			typeError = caught;
		}
	}

	if (typeError != null && !result) {
		// cf. SPARQL spec a type error is thrown if the value is not in the
		// list and one of the list members caused a type error in the
		// comparison.
		throw typeError;
	}

	return BooleanLiteral.valueOf(result);
}
 
Example #14
Source File: QueryAlgebraUtil.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Construct the statement string, i.e. "s p ?o_varID FILTER ?o_N=o ". This kind of statement pattern is necessary
 * to later on identify available results.
 *
 * @param stmt
 * @param varID
 * @param varNames
 * @param bindings
 * @return the expression
 */
protected static TupleExpr constructStatementCheckId(StatementPattern stmt, int varID, Set<String> varNames,
		BindingSet bindings) {

	String _varID = Integer.toString(varID);
	Var subj = appendVarId(stmt.getSubjectVar(), _varID, varNames, bindings);
	Var pred = appendVarId(stmt.getPredicateVar(), _varID, varNames, bindings);

	Var obj = new Var("o_" + _varID);
	varNames.add("o_" + _varID);

	Value objValue;
	if (stmt.getObjectVar().hasValue()) {
		objValue = stmt.getObjectVar().getValue();
	} else if (bindings.hasBinding(stmt.getObjectVar().getName())) {
		objValue = bindings.getBinding(stmt.getObjectVar().getName()).getValue();
	} else {
		// just to make sure that we see an error, will be deleted soon
		throw new RuntimeException("Unexpected.");
	}

	Compare cmp = new Compare(obj, new ValueConstant(objValue));
	cmp.setOperator(CompareOp.EQ);
	Filter filter = new Filter(new StatementPattern(subj, pred, obj), cmp);

	return filter;
}
 
Example #15
Source File: FilterUtilTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testConjunctiveFilterExpr() throws Exception {

	FilterExpr left = createFilterExpr("age", 15, CompareOp.GT);
	FilterExpr right = createFilterExpr("age", 25, CompareOp.LT);
	ConjunctiveFilterExpr expr = new ConjunctiveFilterExpr(left, right);

	Assertions.assertEquals(
			"( ( ?age > '15'^^<http://www.w3.org/2001/XMLSchema#int> ) && ( ?age < '25'^^<http://www.w3.org/2001/XMLSchema#int> ) )",
			FilterUtils.toSparqlString(expr));
}
 
Example #16
Source File: CompareAll.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public CompareOp getOperator() {
	return operator;
}
 
Example #17
Source File: FilterExpr.java    From CostFed with GNU Affero General Public License v3.0 4 votes vote down vote up
public boolean isCompareEq() {
	return expr instanceof Compare && ((Compare)expr).getOperator()==CompareOp.EQ;
}
 
Example #18
Source File: FilterExpr.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public boolean isCompareEq() {
	return expr instanceof Compare && ((Compare) expr).getOperator() == CompareOp.EQ;
}
 
Example #19
Source File: NullProcessor.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public Object visit(ASTCompare compareNode, Object data) throws VisitorException {
	boolean leftIsNull = compareNode.getLeftOperand() instanceof ASTNull;
	boolean rightIsNull = compareNode.getRightOperand() instanceof ASTNull;
	CompareOp operator = compareNode.getOperator().getValue();

	if (leftIsNull && rightIsNull) {
		switch (operator) {
		case EQ:
			logger.warn("Use of NULL values in SeRQL queries has been deprecated, use BOUND(...) instead");
			compareNode.jjtReplaceWith(new ASTBooleanConstant(true));
			break;
		case NE:
			logger.warn("Use of NULL values in SeRQL queries has been deprecated, use BOUND(...) instead");
			compareNode.jjtReplaceWith(new ASTBooleanConstant(false));
			break;
		default:
			throw new VisitorException(
					"Use of NULL values in SeRQL queries has been deprecated, use BOUND(...) instead");
		}
	} else if (leftIsNull || rightIsNull) {
		ASTValueExpr valueOperand;
		if (leftIsNull) {
			valueOperand = compareNode.getRightOperand();
		} else {
			valueOperand = compareNode.getLeftOperand();
		}

		if (valueOperand instanceof ASTVar && operator == EQ || operator == NE) {
			ASTBooleanExpr replacementNode = new ASTBound(valueOperand);

			if (operator == EQ) {
				replacementNode = new ASTNot(replacementNode);
			}

			compareNode.jjtReplaceWith(replacementNode);

			return null;
		}

		throw new VisitorException(
				"Use of NULL values in SeRQL queries has been deprecated, use BOUND(...) instead");
	}

	return null;
}
 
Example #20
Source File: FilterUtilTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private FilterExpr createFilterExpr(String leftVarName, int rightConstant, CompareOp operator) {
	Compare compare = new Compare(new Var(leftVarName), valueConstant(rightConstant), operator);
	return new FilterExpr(compare, new HashSet<>());

}
 
Example #21
Source File: ASTCompOperator.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public void setValue(CompareOp operator) {
	this.value = operator;
}
 
Example #22
Source File: ASTCompOperator.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public CompareOp getValue() {
	return value;
}
 
Example #23
Source File: SyntaxTreeBuilder.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
final public void CompOperator() throws ParseException {
	/* @bgen(jjtree) CompOperator */
	ASTCompOperator jjtn000 = new ASTCompOperator(JJTCOMPOPERATOR);
	boolean jjtc000 = true;
	jjtree.openNodeScope(jjtn000);
	try {
		switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
		case EQ:
			jj_consume_token(EQ);
			jjtree.closeNodeScope(jjtn000, true);
			jjtc000 = false;
			jjtn000.setValue(CompareOp.EQ);
			break;
		case NE:
			jj_consume_token(NE);
			jjtree.closeNodeScope(jjtn000, true);
			jjtc000 = false;
			jjtn000.setValue(CompareOp.NE);
			break;
		case LT:
			jj_consume_token(LT);
			jjtree.closeNodeScope(jjtn000, true);
			jjtc000 = false;
			jjtn000.setValue(CompareOp.LT);
			break;
		case LE:
			jj_consume_token(LE);
			jjtree.closeNodeScope(jjtn000, true);
			jjtc000 = false;
			jjtn000.setValue(CompareOp.LE);
			break;
		case GE:
			jj_consume_token(GE);
			jjtree.closeNodeScope(jjtn000, true);
			jjtc000 = false;
			jjtn000.setValue(CompareOp.GE);
			break;
		case GT:
			jj_consume_token(GT);
			jjtree.closeNodeScope(jjtn000, true);
			jjtc000 = false;
			jjtn000.setValue(CompareOp.GT);
			break;
		default:
			jj_la1[59] = jj_gen;
			jj_consume_token(-1);
			throw new ParseException();
		}
	} finally {
		if (jjtc000) {
			jjtree.closeNodeScope(jjtn000, true);
		}
	}
}
 
Example #24
Source File: ASTCompare.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public void setOperator(CompareOp operator) {
	this.operator = operator;
}
 
Example #25
Source File: ASTCompare.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public CompareOp getOperator() {
	return operator;
}
 
Example #26
Source File: CompareAny.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public void setOperator(CompareOp operator) {
	assert operator != null : "operator must not be null";
	this.operator = operator;
}
 
Example #27
Source File: CompareAny.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public CompareOp getOperator() {
	return operator;
}
 
Example #28
Source File: CompareAny.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public CompareAny(ValueExpr valueExpr, TupleExpr subQuery, CompareOp operator) {
	super(valueExpr, subQuery);
	setOperator(operator);
}
 
Example #29
Source File: CompareAll.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public void setOperator(CompareOp operator) {
	assert operator != null : "operator must not be null";
	this.operator = operator;
}
 
Example #30
Source File: GreaterThanOrEqualTo.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
protected Value evaluate(ValueFactory valueFactory, Value arg1, Value arg2) throws ValueExprEvaluationException {
	return BooleanLiteral.valueOf(QueryEvaluationUtil.compare(arg1, arg2, CompareOp.GE));
}