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

The following examples show how to use org.eclipse.rdf4j.query.algebra.Like. 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: SparqlValueExprRenderer.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * @inheritDoc
 */
@Override
public void meet(Like theOp) throws Exception {
	theOp.getArg().visit(this);
	mBuffer.append(" like \"").append(theOp.getPattern()).append("\"");
	if (!theOp.isCaseSensitive()) {
		mBuffer.append(" ignore case");
	}
}
 
Example #2
Source File: SerqlValueExprRenderer.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * @inheritDoc
 */
@Override
public void meet(Like theOp) throws Exception {
	theOp.getArg().visit(this);
	mBuffer.append(" like \"").append(theOp.getPattern()).append("\"");
	if (!theOp.isCaseSensitive()) {
		mBuffer.append(" ignore case");
	}
}
 
Example #3
Source File: QueryModelBuilder.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public Like visit(ASTLike node, Object data) throws VisitorException {
	ValueExpr expr = (ValueExpr) node.getValueExpr().jjtAccept(this, null);
	String pattern = (String) node.getPattern().jjtAccept(this, null);
	boolean caseSensitive = !node.ignoreCase();

	return new Like(expr, pattern, caseSensitive);
}
 
Example #4
Source File: SPARQLValueExprRenderer.java    From semagrow with Apache License 2.0 5 votes vote down vote up
/**
 * @inheritDoc
 */
@Override
public void meet(Like theOp)
        throws Exception
{
    theOp.getArg().visit(this);
    mBuffer.append(" like \"").append(theOp.getPattern()).append("\"");
    if (!theOp.isCaseSensitive()) {
        mBuffer.append(" ignore case");
    }
}
 
Example #5
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 #6
Source File: StrictEvaluationStrategy.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Determines whether the two operands match according to the <code>like</code> operator. The operator is defined as
 * a string comparison with the possible use of an asterisk (*) at the end and/or the start of the second operand to
 * indicate substring matching.
 *
 * @return <tt>true</tt> if the operands match according to the <tt>like</tt> operator, <tt>false</tt> otherwise.
 */
public Value evaluate(Like node, BindingSet bindings)
		throws QueryEvaluationException {
	Value val = evaluate(node.getArg(), bindings);
	String strVal = null;

	if (val instanceof IRI) {
		strVal = val.toString();
	} else if (val instanceof Literal) {
		strVal = ((Literal) val).getLabel();
	}

	if (strVal == null) {
		throw new ValueExprEvaluationException();
	}

	if (!node.isCaseSensitive()) {
		// Convert strVal to lower case, just like the pattern has been done
		strVal = strVal.toLowerCase();
	}

	int valIndex = 0;
	int prevPatternIndex = -1;
	int patternIndex = node.getOpPattern().indexOf('*');

	if (patternIndex == -1) {
		// No wildcards
		return BooleanLiteral.valueOf(node.getOpPattern().equals(strVal));
	}

	String snippet;

	if (patternIndex > 0) {
		// Pattern does not start with a wildcard, first part must match
		snippet = node.getOpPattern().substring(0, patternIndex);
		if (!strVal.startsWith(snippet)) {
			return BooleanLiteral.FALSE;
		}

		valIndex += snippet.length();
		prevPatternIndex = patternIndex;
		patternIndex = node.getOpPattern().indexOf('*', patternIndex + 1);
	}

	while (patternIndex != -1) {
		// Get snippet between previous wildcard and this wildcard
		snippet = node.getOpPattern().substring(prevPatternIndex + 1, patternIndex);

		// Search for the snippet in the value
		valIndex = strVal.indexOf(snippet, valIndex);
		if (valIndex == -1) {
			return BooleanLiteral.FALSE;
		}

		valIndex += snippet.length();
		prevPatternIndex = patternIndex;
		patternIndex = node.getOpPattern().indexOf('*', patternIndex + 1);
	}

	// Part after last wildcard
	snippet = node.getOpPattern().substring(prevPatternIndex + 1);

	if (snippet.length() > 0) {
		// Pattern does not end with a wildcard.

		// Search last occurence of the snippet.
		valIndex = strVal.indexOf(snippet, valIndex);
		int i;
		while ((i = strVal.indexOf(snippet, valIndex + 1)) != -1) {
			// A later occurence was found.
			valIndex = i;
		}

		if (valIndex == -1) {
			return BooleanLiteral.FALSE;
		}

		valIndex += snippet.length();

		if (valIndex < strVal.length()) {
			// Some characters were not matched
			return BooleanLiteral.FALSE;
		}
	}

	return BooleanLiteral.TRUE;
}
 
Example #7
Source File: AbstractQueryModelVisitor.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public void meet(Like node) throws X {
	meetUnaryValueOperator(node);
}
 
Example #8
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 #9
Source File: HalyardValueExprEvaluation.java    From Halyard with Apache License 2.0 4 votes vote down vote up
/**
 * Determines whether the two operands match according to the <code>like</code> operator. The operator is defined as a string comparison with the possible
 * use of an asterisk (*) at the end and/or the start of the second operand to indicate substring matching.
 *
 * @return <tt>true</tt> if the operands match according to the
 * <tt>like</tt>
 * operator, <tt>false</tt> otherwise.
 */
private Value evaluate(Like node, BindingSet bindings) throws ValueExprEvaluationException, QueryEvaluationException {
    Value val = evaluate(node.getArg(), bindings);
    String strVal = null;
    if (val instanceof URI) {
        strVal = ((URI) val).toString();
    } else if (val instanceof Literal) {
        strVal = ((Literal) val).getLabel();
    }
    if (strVal == null) {
        throw new ValueExprEvaluationException();
    }
    if (!node.isCaseSensitive()) {
        // Convert strVal to lower case, just like the pattern has been done
        strVal = strVal.toLowerCase(Locale.ROOT);
    }
    int valIndex = 0;
    int prevPatternIndex = -1;
    int patternIndex = node.getOpPattern().indexOf('*');
    if (patternIndex == -1) {
        // No wildcards
        return BooleanLiteral.valueOf(node.getOpPattern().equals(strVal));
    }
    String snippet;
    if (patternIndex > 0) {
        // Pattern does not start with a wildcard, first part must match
        snippet = node.getOpPattern().substring(0, patternIndex);
        if (!strVal.startsWith(snippet)) {
            return BooleanLiteral.FALSE;
        }
        valIndex += snippet.length();
        prevPatternIndex = patternIndex;
        patternIndex = node.getOpPattern().indexOf('*', patternIndex + 1);
    }
    while (patternIndex != -1) {
        // Get snippet between previous wildcard and this wildcard
        snippet = node.getOpPattern().substring(prevPatternIndex + 1, patternIndex);
        // Search for the snippet in the value
        valIndex = strVal.indexOf(snippet, valIndex);
        if (valIndex == -1) {
            return BooleanLiteral.FALSE;
        }
        valIndex += snippet.length();
        prevPatternIndex = patternIndex;
        patternIndex = node.getOpPattern().indexOf('*', patternIndex + 1);
    }
    // Part after last wildcard
    snippet = node.getOpPattern().substring(prevPatternIndex + 1);
    if (snippet.length() > 0) {
        // Pattern does not end with a wildcard.
        // Search last occurence of the snippet.
        valIndex = strVal.indexOf(snippet, valIndex);
        int i;
        while ((i = strVal.indexOf(snippet, valIndex + 1)) != -1) {
            // A later occurence was found.
            valIndex = i;
        }
        if (valIndex == -1) {
            return BooleanLiteral.FALSE;
        }
        valIndex += snippet.length();
        if (valIndex < strVal.length()) {
            // Some characters were not matched
            return BooleanLiteral.FALSE;
        }
    }
    return BooleanLiteral.TRUE;
}