/** * 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); }
@Override public void meet(And node) throws RuntimeException { builder.append("("); node.getLeftArg().visit(this); builder.append(" AND "); node.getRightArg().visit(this); builder.append(")"); }
@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); } }