org.eclipse.rdf4j.query.algebra.And Java Examples
The following examples show how to use
org.eclipse.rdf4j.query.algebra.And.
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: BasicGroup.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 6 votes |
private ValueExpr filtersAsAnd() { ValueExpr aExpr = null; for (ValueExpr aValEx : mFilters) { if (aExpr == null) { aExpr = aValEx; } else { And aAnd = new And(); aAnd.setLeftArg(aValEx); aAnd.setRightArg(aExpr); aExpr = aAnd; } } return aExpr; }
Example #2
Source File: CopyRule.java From rya with Apache License 2.0 | 6 votes |
/** * If we can't evaluate one half of an AND by looking at this statement alone, it might * turn out to be true in the full graph, so the statement is relevant if the half we * can evaluate is true. If we can't evaluate either half, then the AND is useless and * we must assume the statement is relevant. Otherwise, keep both sides. */ @Override public void meet(final And expr) { final ValueExpr left = expr.getLeftArg(); final ValueExpr right = expr.getRightArg(); left.visit(this); right.visit(this); final QueryModelNode parent = expr.getParentNode(); if (trivialCondition(left)) { if (trivialCondition(right)) { // Both sides are trivial; replace whole node parent.replaceChildNode(expr, null); } else { // Left side trivial, right side good; replace node with right arg parent.replaceChildNode(expr, right); } } else if (trivialCondition(right)) { // Left side good, right side trivial; replace node with left arg parent.replaceChildNode(expr, left); } // Otherwise, both sides are useful }
Example #3
Source File: FilterUtils.java From CostFed with GNU Affero General Public License v3.0 | 6 votes |
public static ValueExpr toFilter(ConjunctiveFilterExpr filterExpr) throws FilterConversionException { List<FilterExpr> expressions = filterExpr.getExpressions(); if (expressions.size()==2) { return new And(expressions.get(0).getExpression(), expressions.get(0).getExpression()); } And and = new And(); and.setLeftArg( expressions.get(0).getExpression() ); And tmp = and; int idx; for (idx=1; idx<expressions.size()-1; idx++) { And _a = new And(); _a.setLeftArg( expressions.get(idx).getExpression() ); tmp.setRightArg(_a); tmp = _a; } tmp.setRightArg( expressions.get(idx).getExpression()); return and; }
Example #4
Source File: FilterUtils.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 6 votes |
public static ValueExpr toFilter(ConjunctiveFilterExpr filterExpr) throws FilterConversionException { List<FilterExpr> expressions = filterExpr.getExpressions(); if (expressions.size() == 2) { return new And(expressions.get(0).getExpression(), expressions.get(0).getExpression()); } And and = new And(); and.setLeftArg(expressions.get(0).getExpression()); And tmp = and; int idx; for (idx = 1; idx < expressions.size() - 1; idx++) { And _a = new And(); _a.setLeftArg(expressions.get(idx).getExpression()); tmp.setRightArg(_a); tmp = _a; } tmp.setRightArg(expressions.get(idx).getExpression()); return and; }
Example #5
Source File: CopyRule.java From rya with Apache License 2.0 | 6 votes |
/** * Constrain a rule with a filter condition. If there are already conditions on the rule, they * will be ANDed together. If this rule doesn't define all the variables used in the condition * (because the rule only matches one statement pattern), it will assume those portions match, * so that we are guaranteed to include all relevant statements. * @param condition A boolean filter expression */ public void addCondition(final ValueExpr condition) { final ValueExpr newCondition = condition.clone(); if (this.condition == null) { setCondition(newCondition); } else { setCondition(new And(this.condition, newCondition)); } this.condition.visit(visitor); // If, after rewriting, the condition still contains undefined variables, we can't // meaningfully apply it to reject statements. if (trivialCondition(this.condition)) { this.condition = null; } }
Example #6
Source File: ConjunctiveConstraintSplitter.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 5 votes |
protected void getConjunctiveConstraints(ValueExpr valueExpr, List<ValueExpr> conjunctiveConstraints) { if (valueExpr instanceof And) { And and = (And) valueExpr; getConjunctiveConstraints(and.getLeftArg(), conjunctiveConstraints); getConjunctiveConstraints(and.getRightArg(), conjunctiveConstraints); } else { conjunctiveConstraints.add(valueExpr); } }
Example #7
Source File: QueryModelNormalizer.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 5 votes |
@Override public void meet(And and) { super.meet(and); if (and.getLeftArg().equals(and.getRightArg())) { and.replaceWith(and.getLeftArg()); } }
Example #8
Source File: FilterFunctionOptimizer.java From rya with Apache License 2.0 | 5 votes |
@Override public void meet(final FunctionCall call) { final IRI fnUri = VF.createIRI(call.getURI()); final Var resultVar = IndexingFunctionRegistry.getResultVarFromFunctionCall(fnUri, call.getArgs()); if (resultVar != null && resultVar.getName().equals(matchVar)) { addFilter(VF.createIRI(call.getURI()), extractArguments(matchVar, call)); if (call.getParentNode() instanceof Filter || call.getParentNode() instanceof And || call.getParentNode() instanceof LeftJoin) { call.replaceWith(new ValueConstant(VF.createLiteral(true))); } else { throw new IllegalArgumentException("Query error: Found " + call + " as part of an expression that is too complex"); } } }
Example #9
Source File: SpinRenderer.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 5 votes |
@Override public void meet(And node) throws RDFHandlerException { Resource currentSubj = subject; flushPendingStatement(); handler.handleStatement(valueFactory.createStatement(subject, RDF.TYPE, SP.AND)); predicate = SP.ARG1_PROPERTY; node.getLeftArg().visit(this); predicate = SP.ARG2_PROPERTY; node.getRightArg().visit(this); subject = currentSubj; predicate = null; }
Example #10
Source File: FilterOptimizer.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 5 votes |
/** * returns true if this filter can be used for optimization. Currently no conjunctive or disjunctive expressions are * supported. * * @param e * @return whether the expression is compatible */ protected boolean isCompatibleExpr(ValueExpr e) { if (e instanceof And || e instanceof Or) { return false; } if (e instanceof Not) { return isCompatibleExpr(((Not) e).getArg()); } return true; }
Example #11
Source File: FilterOptimizer.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 5 votes |
/** * add the conjunctive expressions to specified list, has recursive step. * * @param expr the expr, in the best case in CNF * @param conjExpr the list to which expressions will be added */ protected void getConjunctiveExpressions(ValueExpr expr, List<ValueExpr> conjExpr) { if (expr instanceof And) { And and = (And) expr; getConjunctiveExpressions(and.getLeftArg(), conjExpr); getConjunctiveExpressions(and.getRightArg(), conjExpr); } else { conjExpr.add(expr); } }
Example #12
Source File: GeoEnabledFilterFunctionOptimizer.java From rya with Apache License 2.0 | 5 votes |
@Override public void meet(final FunctionCall call) { final IRI fnIri = VF.createIRI(call.getURI()); final Var resultVar = IndexingFunctionRegistry.getResultVarFromFunctionCall(fnIri, call.getArgs()); if (resultVar != null && resultVar.getName().equals(matchVar)) { addFilter(VF.createIRI(call.getURI()), GeoParseUtils.extractArguments(matchVar, call)); if (call.getParentNode() instanceof Filter || call.getParentNode() instanceof And || call.getParentNode() instanceof LeftJoin) { call.replaceWith(new ValueConstant(VF.createLiteral(true))); } else { throw new IllegalArgumentException("Query error: Found " + call + " as part of an expression that is too complex"); } } }
Example #13
Source File: FilterOptimizer.java From CostFed with GNU Affero General Public License v3.0 | 5 votes |
/** * returns true if this filter can be used for optimization. Currently no * conjunctive or disjunctive expressions are supported. * * @param e * @return */ protected boolean isCompatibleExpr(ValueExpr e) { if (e instanceof And || e instanceof Or) { return false; } if (e instanceof Not) { return isCompatibleExpr( ((Not)e).getArg() ); } return true; }
Example #14
Source File: FilterOptimizer.java From CostFed with GNU Affero General Public License v3.0 | 5 votes |
/** * add the conjunctive expressions to specified list, has recursive step. * * @param expr * the expr, in the best case in CNF * @param conjExpr * the list to which expressions will be added */ protected void getConjunctiveExpressions(ValueExpr expr, List<ValueExpr> conjExpr) { if (expr instanceof And) { And and = (And)expr; getConjunctiveExpressions(and.getLeftArg(), conjExpr); getConjunctiveExpressions(and.getRightArg(), conjExpr); } else conjExpr.add(expr); }
Example #15
Source File: SPARQLValueExprRenderer.java From semagrow with Apache License 2.0 | 5 votes |
/** * @inheritDoc */ @Override public void meet(And theAnd) throws Exception { binaryMeet("&&", theAnd); }
Example #16
Source File: AbstractQueryModelVisitor.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 4 votes |
@Override public void meet(And node) throws X { meetBinaryValueOperator(node); }
Example #17
Source File: HalyardValueExprEvaluation.java From Halyard with Apache License 2.0 | 4 votes |
/** * 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 #18
Source File: TupleExprBuilder.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 4 votes |
@Override public Object visit(ASTAnd 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 And(leftArg, rightArg); }
Example #19
Source File: ValueExprFactory.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 4 votes |
public static And and(ValueExpr theLeft, ValueExpr theRight) { return new And(theLeft, theRight); }
Example #20
Source File: FilterBuilder.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 4 votes |
public GroupBuilder<T, E> and(ValueExpr theLeft, ValueExpr theRight) { return filter(new And(theLeft, theRight)); }
Example #21
Source File: SerqlValueExprRenderer.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 4 votes |
/** * @inheritDoc */ @Override public void meet(And theAnd) throws Exception { binaryMeet("and", theAnd); }
Example #22
Source File: SparqlValueExprRenderer.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 4 votes |
/** * @inheritDoc */ @Override public void meet(And theAnd) throws Exception { binaryMeet("&&", theAnd); }
Example #23
Source File: ConstantOptimizerTest.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 4 votes |
@Override public void meet(And and) { logicalAndfound = true; super.meet(and); }
Example #24
Source File: StrictEvaluationStrategy.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 4 votes |
@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 #25
Source File: FilterOptimizer.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 4 votes |
@Override public void meet(Filter filter) { if (filter.getArg() instanceof EmptyResult) { log.debug( "Argument of filter expression does not yield results at the provided sources, replacing Filter node."); filter.replaceWith(filter.getArg()); return; } /* * TODO idea: if we have a FILTER such as ?s='a' OR ?s='b' OR ?s='c' handle this appropriately */ ValueExpr valueExpr = filter.getCondition(); /* * TODO transform condition into some normal form, e.g. CNF */ // determine conjunctive expressions List<ValueExpr> conjunctiveExpressions = new ArrayList<>(); getConjunctiveExpressions(valueExpr, conjunctiveExpressions); FilterExprInsertVisitor filterExprVst = new FilterExprInsertVisitor(); List<ValueExpr> remainingExpr = new ArrayList<>(conjunctiveExpressions.size()); for (ValueExpr cond : conjunctiveExpressions) { /* * Determine if this filter is applicable for optimization. Currently only leaf expressions are applicable, * i.e. not combined expressions. */ if (isCompatibleExpr(cond)) { HashSet<String> exprVars = new VarFinder().findVars(cond); FilterExpr filterExpr = new FilterExpr(cond, exprVars); filterExprVst.initialize(filterExpr); filter.getArg().visit(filterExprVst); // if the filter expr. is handled in the stmt we do not have to keep it if (filterExprVst.canRemove()) { continue; } remainingExpr.add(filterExpr.getExpression()); } else { remainingExpr.add(cond); } } if (remainingExpr.isEmpty()) { filter.replaceWith(filter.getArg()); // remove the filter } else if (remainingExpr.size() == 1) { filter.setCondition(remainingExpr.get(0)); // just apply the remaining condition } else { // construct conjunctive value expr And root = new And(); root.setLeftArg(remainingExpr.get(0)); And tmp = root; for (int i = 1; i < remainingExpr.size() - 1; i++) { And _a = new And(); _a.setLeftArg(remainingExpr.get(i)); tmp.setRightArg(_a); tmp = _a; } tmp.setRightArg(remainingExpr.get(remainingExpr.size() - 1)); filter.setCondition(root); } }
Example #26
Source File: FilterOptimizer.java From CostFed with GNU Affero General Public License v3.0 | 4 votes |
@Override public void meet(Filter filter) { if (filter.getArg() instanceof EmptyResult) { log.debug("Argument of filter expression does not yield results at the provided sources, replacing Filter node."); filter.replaceWith(filter.getArg()); return; } /* * TODO idea: * if we have a FILTER such as ?s='a' OR ?s='b' OR ?s='c' handle this appropriately */ ValueExpr valueExpr = filter.getCondition(); /* * TODO transform condition into some normal form, e.g. CNF */ // determine conjunctive expressions List<ValueExpr> conjunctiveExpressions = new ArrayList<ValueExpr>(); getConjunctiveExpressions(valueExpr, conjunctiveExpressions); FilterExprInsertVisitor filterExprVst = new FilterExprInsertVisitor(); List<ValueExpr> remainingExpr = new ArrayList<ValueExpr>(conjunctiveExpressions.size()); for (ValueExpr cond : conjunctiveExpressions) { /* * Determine if this filter is applicable for optimization. * Currently only leaf expressions are applicable, i.e. * not combined expressions. */ if (isCompatibleExpr(cond)) { HashSet<String> exprVars = new VarFinder().findVars(cond); FilterExpr filterExpr = new FilterExpr(cond, exprVars); filterExprVst.initialize(filterExpr); filter.getArg().visit(filterExprVst); // if the filter expr. is handled in the stmt we do not have to keep it if (filterExprVst.canRemove()) continue; remainingExpr.add(filterExpr.getExpression()); } else { remainingExpr.add(cond); } } if (remainingExpr.size()==0) { filter.replaceWith(filter.getArg()); // remove the filter } else if (remainingExpr.size()==1) { filter.setCondition(remainingExpr.get(0)); // just apply the remaining condition } else { // construct conjunctive value expr And root = new And(); root.setLeftArg(remainingExpr.get(0)); And tmp = root; for (int i=1; i<remainingExpr.size()-1; i++) { And _a = new And(); _a.setLeftArg(remainingExpr.get(i)); tmp.setRightArg(_a); tmp = _a; } tmp.setRightArg(remainingExpr.get(remainingExpr.size()-1)); filter.setCondition(root); } }