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

The following examples show how to use org.eclipse.rdf4j.query.algebra.Regex. 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: RegexAsStringFunctionOptimizer.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public void meet(Regex node) {
	final ValueExpr flagsArg = node.getFlagsArg();
	if (flagsArg == null || flagsArg.toString().isEmpty()) {
		// if we have no flags then we can not be in case insensitive mode
		if (node.getPatternArg() instanceof ValueConstant) {
			ValueConstant vc = (ValueConstant) node.getPatternArg();
			String regex = vc.getValue().stringValue();
			final boolean anchoredAtStart = regex.startsWith("^");
			final boolean anchoredAtEnd = regex.endsWith("$");

			if (anchoredAtStart && anchoredAtEnd) {
				equalsCandidate(node, regex);
			} else if (anchoredAtStart) {
				strstartsCandidate(node, regex);
			} else if (anchoredAtEnd) {
				strendsCandidate(node, regex);
			} else {
				containsCandidate(node, regex);
			}
		}
	}
	super.meet(node);
}
 
Example #2
Source File: RegexAsStringFunctionOptimizer.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void strstartsCandidate(Regex node, String regex) {
	final String potential = regex.substring(1, regex.length());
	if (plain(potential)) {
		ValueConstant vc = new ValueConstant(vf.createLiteral(potential));
		node.replaceWith(new FunctionCall(FN.STARTS_WITH.stringValue(), node.getArg(), vc));
	}
}
 
Example #3
Source File: SPARQLValueExprRenderer.java    From semagrow with Apache License 2.0 5 votes vote down vote up
/**
 * @inheritDoc
 */
@Override
public void meet(Regex theOp)
        throws Exception
{
    mBuffer.append(" regex(");
    theOp.getArg().visit(this);
    mBuffer.append(", ");
    theOp.getPatternArg().visit(this);
    if (theOp.getFlagsArg() != null) {
        mBuffer.append(",");
        theOp.getFlagsArg().visit(this);
    }
    mBuffer.append(")");
}
 
Example #4
Source File: FilterBuilder.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public GroupBuilder<T, E> regex(ValueExpr theExpr, String thePattern, String theFlags) {
	Regex aRegex = new Regex();
	aRegex.setArg(theExpr);
	aRegex.setPatternArg(new ValueConstant(SimpleValueFactory.getInstance().createLiteral(thePattern)));
	if (theFlags != null) {
		aRegex.setFlagsArg(new ValueConstant(SimpleValueFactory.getInstance().createLiteral(theFlags)));
	}

	return filter(aRegex);
}
 
Example #5
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(Regex theOp) throws Exception {
	mBuffer.append(" regex(");
	theOp.getArg().visit(this);
	mBuffer.append(", ");
	theOp.getPatternArg().visit(this);
	if (theOp.getFlagsArg() != null) {
		mBuffer.append(",");
		theOp.getFlagsArg().visit(this);
	}
	mBuffer.append(")");
}
 
Example #6
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(Regex theOp) throws Exception {
	mBuffer.append(" regex(");
	theOp.getArg().visit(this);
	mBuffer.append(", ");
	theOp.getPatternArg().visit(this);
	if (theOp.getFlagsArg() != null) {
		mBuffer.append(",");
		theOp.getFlagsArg().visit(this);
	}
	mBuffer.append(")");
}
 
Example #7
Source File: RegexAsStringFunctionOptimizer.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void equalsCandidate(Regex node, String regex) {
	final String potential = regex.substring(1, regex.length() - 1);
	if (plain(potential)) {
		ValueConstant vc = new ValueConstant(vf.createLiteral(potential));
		node.replaceWith(new Compare(node.getArg(), vc, Compare.CompareOp.EQ));
	}
}
 
Example #8
Source File: FilterUtils.java    From CostFed with GNU Affero General Public License v3.0 5 votes vote down vote up
protected static void append(ValueExpr expr, StringBuilder sb) throws FilterConversionException {
	if (expr instanceof Compare) {
		append((Compare)expr, sb);
	} else if (expr instanceof Var) {
		append((Var)expr, sb);
	} else if (expr instanceof ValueConstant) {
		append((ValueConstant)expr, sb);
	} else if (expr instanceof Regex) {
	    append((Regex)expr, sb);
	} else {
		// TODO add more!
		throw new FilterConversionException("Expression type not supported, fallback to sesame evaluation: " + expr.getClass().getCanonicalName());
	}
}
 
Example #9
Source File: RegexAsStringFunctionOptimizer.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void strendsCandidate(Regex node, String regex) {
	final String potential = regex.substring(0, regex.length() - 1);
	if (plain(potential)) {
		ValueConstant vc = new ValueConstant(vf.createLiteral(potential));
		node.replaceWith(new FunctionCall(FN.ENDS_WITH.stringValue(), node.getArg(), vc));
	}
}
 
Example #10
Source File: SpinRenderer.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void meet(Regex node) throws RDFHandlerException {
	Resource currentSubj = subject;
	flushPendingStatement();
	handler.handleStatement(valueFactory.createStatement(subject, RDF.TYPE, SP.REGEX));
	predicate = SP.ARG1_PROPERTY;
	node.getLeftArg().visit(this);
	predicate = SP.ARG2_PROPERTY;
	node.getRightArg().visit(this);
	subject = currentSubj;
	predicate = null;
}
 
Example #11
Source File: FilterUtils.java    From CostFed with GNU Affero General Public License v3.0 5 votes vote down vote up
protected static void append(Regex re, StringBuilder sb) throws FilterConversionException {
    sb.append("REGEX (");
    append(re.getLeftArg(), sb);
    sb.append(",");
    append(re.getRightArg(), sb);
    sb.append(" )");
}
 
Example #12
Source File: RegexAsStringFunctionOptimizer.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private void containsCandidate(Regex node, String potential) {
	if (plain(potential)) {
		node.replaceWith(new FunctionCall(FN.CONTAINS.stringValue(), node.getArg(), node.getPatternArg()));
	}
}
 
Example #13
Source File: AbstractQueryModelVisitor.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public void meet(Regex node) throws X {
	meetBinaryValueOperator(node);
}
 
Example #14
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>regex</code> operator.
 *
 * @return <tt>true</tt> if the operands match according to the <tt>regex</tt> operator, <tt>false</tt> otherwise.
 */
public Value evaluate(Regex node, BindingSet bindings)
		throws QueryEvaluationException {
	Value arg = evaluate(node.getArg(), bindings);
	Value parg = evaluate(node.getPatternArg(), bindings);
	Value farg = null;
	ValueExpr flagsArg = node.getFlagsArg();
	if (flagsArg != null) {
		farg = evaluate(flagsArg, bindings);
	}

	if (QueryEvaluationUtil.isStringLiteral(arg) && QueryEvaluationUtil.isSimpleLiteral(parg)
			&& (farg == null || QueryEvaluationUtil.isSimpleLiteral(farg))) {
		String text = ((Literal) arg).getLabel();
		String ptn = ((Literal) parg).getLabel();
		String flags = "";
		if (farg != null) {
			flags = ((Literal) farg).getLabel();
		}
		// TODO should this Pattern be cached?
		int f = 0;
		for (char c : flags.toCharArray()) {
			switch (c) {
			case 's':
				f |= Pattern.DOTALL;
				break;
			case 'm':
				f |= Pattern.MULTILINE;
				break;
			case 'i':
				f |= Pattern.CASE_INSENSITIVE;
				f |= Pattern.UNICODE_CASE;
				break;
			case 'x':
				f |= Pattern.COMMENTS;
				break;
			case 'd':
				f |= Pattern.UNIX_LINES;
				break;
			case 'u':
				f |= Pattern.UNICODE_CASE;
				break;
			case 'q':
				f |= Pattern.LITERAL;
				break;
			default:
				throw new ValueExprEvaluationException(flags);
			}
		}
		Pattern pattern = Pattern.compile(ptn, f);
		boolean result = pattern.matcher(text).find();
		return BooleanLiteral.valueOf(result);
	}

	throw new ValueExprEvaluationException();
}
 
Example #15
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 #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: 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>regex</code> operator.
 *
 * @return <tt>true</tt> if the operands match according to the
 * <tt>regex</tt> operator, <tt>false</tt> otherwise.
 */
private Value evaluate(Regex node, BindingSet bindings) throws ValueExprEvaluationException, QueryEvaluationException {
    Value arg = evaluate(node.getArg(), bindings);
    Value parg = evaluate(node.getPatternArg(), bindings);
    Value farg = null;
    ValueExpr flagsArg = node.getFlagsArg();
    if (flagsArg != null) {
        farg = evaluate(flagsArg, bindings);
    }
    if (QueryEvaluationUtil.isStringLiteral(arg) && QueryEvaluationUtil.isSimpleLiteral(parg)
            && (farg == null || QueryEvaluationUtil.isSimpleLiteral(farg))) {
        String text = ((Literal) arg).getLabel();
        String ptn = ((Literal) parg).getLabel();
        String flags = "";
        if (farg != null) {
            flags = ((Literal) farg).getLabel();
        }
        // TODO should this Pattern be cached?
        int f = 0;
        for (char c : flags.toCharArray()) {
            switch (c) {
                case 's':
                    f |= Pattern.DOTALL;
                    break;
                case 'm':
                    f |= Pattern.MULTILINE;
                    break;
                case 'i':
                    f |= Pattern.CASE_INSENSITIVE;
                    f |= Pattern.UNICODE_CASE;
                    break;
                case 'x':
                    f |= Pattern.COMMENTS;
                    break;
                case 'd':
                    f |= Pattern.UNIX_LINES;
                    break;
                case 'u':
                    f |= Pattern.UNICODE_CASE;
                    break;
                default:
                    throw new ValueExprEvaluationException(flags);
            }
        }
        Pattern pattern = Pattern.compile(ptn, f);
        boolean result = pattern.matcher(text).find();
        return BooleanLiteral.valueOf(result);
    }
    throw new ValueExprEvaluationException();
}