public static boolean isValid(List<Var> paramVars, Expr expr) { Set<Var> exprVars = expr.getVarsMentioned(); boolean result = exprVars.containsAll(paramVars); return result; }
public static Set<Var> getVarsMentioned(Iterable<? extends Expr> clause) { Set<Var> result = new HashSet<Var>(); for(Expr expr : clause) { result.addAll(expr.getVarsMentioned()); } return result; }
public ClauseBase(Set<Expr> exprs) { this.exprs = exprs; varToExpr = HashMultimap.create(); Set<Var> vars = new HashSet<Var>(); for(Expr expr : exprs) { Set<Var> evs = expr.getVarsMentioned(); vars.addAll(evs); for(Var ev : evs) { varToExpr.put(ev, expr); } } this.varsMentioned = vars; }
@Override public void visit(OpProcedure opProc) { for ( Expr expr : opProc.getArgs() ) { Set<Var> vars = expr.getVarsMentioned() ; defines.addAll(vars) ; } }
public Set<Var> getReferencedVars() { Set<Var> result = new HashSet<Var>(); for(Entry<Var, RestrictedExpr> entry : varToExprs.entries()) { Set<Var> tmp = entry.getValue().getExpr().getVarsMentioned(); result.addAll(tmp); } return result; }
public List<String> getReferencedColumnNames() { Set<Var> vars = expr.getVarsMentioned(); List<String> result = new ArrayList<String>(vars.size()); for(Var var : vars) { result.add(var.getVarName()); } return result; }
private static void checkExprListAssignment(Collection<Var> vars, VarExprList exprList) { Set<Var> vars2 = new LinkedHashSet<>(vars) ; exprList.forEachExpr((v,e) -> { Set<Var> varInExpr = e.getVarsMentioned() ; // Include mentioned variables // These may be unused in the query (in vars) but still contribute. vars2.addAll(varInExpr) ; checkAssignment( vars2, e, v ); vars2.add( v ); }) ; }
/** For any expression now in scope, wrap the op with a filter */ private static Op insertAnyFilter(ExprList exprs, Set<Var> patternVarsScope, Op op) { for (Iterator<Expr> iter = exprs.iterator(); iter.hasNext();) { Expr expr = iter.next(); // Cache Set<Var> exprVars = expr.getVarsMentioned(); if (patternVarsScope.containsAll(exprVars)) { if (op == null) op = OpTable.unit(); op = OpFilter.filter(expr, op); iter.remove(); } } return op; }
/** For any expression now in scope, wrap the op with a filter. */ private static Op insertAnyFilter$(ExprList unplacedExprs, Collection<Var> patternVarsScope, Op op) { for (Iterator<Expr> iter = unplacedExprs.iterator(); iter.hasNext();) { Expr expr = iter.next() ; // Cache Set<Var> exprVars = expr.getVarsMentioned() ; if ( patternVarsScope.containsAll(exprVars) ) { if ( op == null ) op = OpTable.unit() ; op = OpFilter.filter(expr, op) ; iter.remove() ; } } return op ; }
private Placement placeProject(ExprList exprs, OpProject input) { Collection<Var> varsProject = input.getVars() ; ExprList pushed = new ExprList() ; ExprList unpushed = new ExprList() ; for ( Expr expr : exprs ) { Set<Var> exprVars = expr.getVarsMentioned() ; if ( varsProject.containsAll(exprVars) ) pushed.add(expr); else unpushed.add(expr) ; } if ( pushed.isEmpty() ) return resultNoChange(input) ; // (filter (project ...)) ===> (project (filter ...)) return processSubOp1(pushed, unpushed, input) ; }
@Test public void test_function_expansion_01() { Expr e = new ExprVar("x"); UserDefinedFunctionFactory.getFactory().add("http://example/simple", e, new ArrayList<>(e.getVarsMentioned())); UserDefinedFunction f = (UserDefinedFunction) UserDefinedFunctionFactory.getFactory().create("http://example/simple"); f.build("http://example/simple", new ExprList(new NodeValueBoolean(true))); Expr actual = f.getActualExpr(); Assert.assertFalse(e.equals(actual)); Assert.assertEquals(0, actual.getVarsMentioned().size()); Assert.assertEquals(new NodeValueBoolean(true), actual); }
@Test(expected=ExprBuildException.class) public void test_function_expansion_bad_02() { Expr single = new ExprVar("x"); UserDefinedFunctionFactory.getFactory().add("http://example/single", single, new ArrayList<>(single.getVarsMentioned())); //It's an error to use a variable which is not mentioned in the argument list, even in a call to a dependent function Expr test = new E_Function("http://example/single", new ExprList(new ExprVar("x"))); UserDefinedFunctionFactory.getFactory().add("http://example/test", test, new ArrayList<Var>()); } }
@BeforeClass public static void setup() { UserDefinedFunctionFactory.getFactory().clear(); //Define a square function Expr square = new E_Multiply(new ExprVar("x"), new ExprVar("x")); UserDefinedFunctionFactory.getFactory().add("http://example/square", square, new ArrayList<>(square.getVarsMentioned())); }
@Test public void test_function_expansion_02() { Expr e = new E_Multiply(new ExprVar("x"), new ExprVar("x")); UserDefinedFunctionFactory.getFactory().add("http://example/square", e, new ArrayList<>(e.getVarsMentioned())); UserDefinedFunction f = (UserDefinedFunction) UserDefinedFunctionFactory.getFactory().create("http://example/square"); f.build("http://example/square", new ExprList(new NodeValueInteger(3))); Expr actual = f.getActualExpr(); Assert.assertFalse(e.equals(actual)); Assert.assertEquals(0, actual.getVarsMentioned().size()); Assert.assertEquals(new E_Multiply(new NodeValueInteger(3), new NodeValueInteger(3)), actual); }
@Test public void test_function_expansion_04() { Expr square = new E_Multiply(new ExprVar("x"), new ExprVar("x")); UserDefinedFunctionFactory.getFactory().add("http://example/square", square, new ArrayList<>(square.getVarsMentioned())); //Test that with preserveDependencies set to false (the default) that the definition of cube is actually //expanded to include the definition of square Expr cube = new E_Multiply(new E_Function("http://example/square", new ExprList(new ExprVar("x"))), new ExprVar("x")); UserDefinedFunctionFactory.getFactory().add("http://example/cube", cube, new ArrayList<>(cube.getVarsMentioned())); UserDefinedFunctionDefinition def = UserDefinedFunctionFactory.getFactory().get("http://example/cube"); Expr base = def.getBaseExpr(); Assert.assertTrue(base instanceof E_Multiply); E_Multiply m = (E_Multiply)base; Assert.assertTrue(m.getArg1() instanceof E_Multiply); Assert.assertTrue(m.getArg2() instanceof ExprVar); Assert.assertEquals(1, base.getVarsMentioned().size()); }
@Test public void test_function_expansion_05() { Expr square = new E_Multiply(new ExprVar("x"), new ExprVar("x")); UserDefinedFunctionFactory.getFactory().add("http://example/square", square, new ArrayList<>(square.getVarsMentioned())); //Test that with preserveDependencies set to false (the default) that the definition of cube is actually //expanded to include the definition of square Expr cube = new E_Multiply(new E_Function("http://example/square", new ExprList(new ExprVar("y"))), new ExprVar("y")); UserDefinedFunctionFactory.getFactory().add("http://example/cube", cube, new ArrayList<>(cube.getVarsMentioned())); UserDefinedFunctionDefinition def = UserDefinedFunctionFactory.getFactory().get("http://example/cube"); Expr base = def.getBaseExpr(); Assert.assertTrue(base instanceof E_Multiply); E_Multiply m = (E_Multiply)base; Assert.assertTrue(m.getArg1() instanceof E_Multiply); Assert.assertTrue(m.getArg2() instanceof ExprVar); Assert.assertEquals(1, base.getVarsMentioned().size()); }
@Test public void test_function_non_expansion_01() { Expr square = new E_Multiply(new ExprVar("x"), new ExprVar("x")); UserDefinedFunctionFactory.getFactory().add("http://example/square", square, new ArrayList<>(square.getVarsMentioned())); //Test that with preserveDependencies set to true that the definition of cube is not expanded Expr cube = new E_Multiply(new E_Function("http://example/square", new ExprList(new ExprVar("x"))), new ExprVar("x")); UserDefinedFunctionFactory.getFactory().add("http://example/cube", cube, new ArrayList<>(cube.getVarsMentioned())); UserDefinedFunctionDefinition def = UserDefinedFunctionFactory.getFactory().get("http://example/cube"); Expr base = def.getBaseExpr(); Assert.assertTrue(base instanceof E_Multiply); E_Multiply multiply = (E_Multiply)base; Assert.assertTrue(multiply.getArg1() instanceof E_Function); Assert.assertTrue(multiply.getArg2() instanceof ExprVar); E_Function lhs = (E_Function)multiply.getArg1(); Assert.assertEquals("http://example/square", lhs.getFunctionIRI()); Assert.assertEquals(1, base.getVarsMentioned().size()); }
@Test public void test_user_defined_function_factory_add_02() { Expr e1 = new E_Multiply(new ExprVar("x"), new ExprVar("x")); Expr e2 = new E_Multiply(new ExprVar("y"), new ExprVar("y")); UserDefinedFunctionFactory.getFactory().add("http://example/square", e1, new ArrayList<>(e1.getVarsMentioned())); Assert.assertTrue(UserDefinedFunctionFactory.getFactory().isRegistered("http://example/square")); Assert.assertEquals(e1, UserDefinedFunctionFactory.getFactory().get("http://example/square").getBaseExpr()); UserDefinedFunctionFactory.getFactory().add("http://example/square", e2, new ArrayList<>(e2.getVarsMentioned())); Assert.assertTrue(UserDefinedFunctionFactory.getFactory().isRegistered("http://example/square")); Assert.assertEquals(e2, UserDefinedFunctionFactory.getFactory().get("http://example/square").getBaseExpr()); }
@Test public void test_user_defined_function_factory_add_01() { Expr e = new E_Multiply(new ExprVar("x"), new ExprVar("x")); UserDefinedFunctionFactory.getFactory().add("http://example/square", e, new ArrayList<>(e.getVarsMentioned())); Assert.assertTrue(UserDefinedFunctionFactory.getFactory().isRegistered("http://example/square")); Assert.assertEquals(e, UserDefinedFunctionFactory.getFactory().get("http://example/square").getBaseExpr()); }
@Test public void test_user_defined_function_factory_add_03() throws ParseException { Expr e = new E_Multiply(new ExprVar("x"), new ExprVar("x")); //Instead of registering the pre-built expression register using a string for the expression UserDefinedFunctionFactory.getFactory().add("http://example/square", "?x * ?x", new ArrayList<>(e.getVarsMentioned())); Assert.assertTrue(UserDefinedFunctionFactory.getFactory().isRegistered("http://example/square")); Assert.assertEquals(e, UserDefinedFunctionFactory.getFactory().get("http://example/square").getBaseExpr()); } }