public static Pair<Var, NodeValue> extractValueConstraint(Expr expr) { if (!(expr instanceof ExprFunction2)) { return null; } ExprFunction func = (ExprFunction) expr; Expr a = func.getArg(1); Expr b = func.getArg(2); if (a.isVariable() == b.isVariable()) { return null; } if (b.isVariable()) { Expr tmp = b; b = a; a = tmp; } if (!b.isConstant()) { return null; } return Pair.create(a.asVar(), b.getConstant()); }
/* @see com.hp.hpl.jena.sparql.syntax.ElementVisitorBase#visit(com.hp.hpl.jena.sparql.syntax.ElementFilter) */ @Override public void visit(final ElementFilter elementfilter) { final Expr expr = elementfilter.getExpr(); final BuiltinAtom atom; try { if (expr instanceof Expression) // RDQL { final Expression e = (Expression) expr; final SWRLDataObject arg1 = makeDataObject(e.getArg(0)); final SWRLDataObject arg2 = makeDataObject(e.getArg(1)); if (e instanceof Q_Equal) atom = swrlFactory.createEqual(arg1, arg2); else if (e instanceof Q_NotEqual) atom = swrlFactory.createNotEqual(arg1, arg2); else if (e instanceof Q_GreaterThan) atom = swrlFactory.createGreaterThan(arg1, arg2); else if (e instanceof Q_GreaterThanOrEqual) atom = swrlFactory.createGreaterThanOrEqual(arg1, arg2); else if (e instanceof Q_LessThan) atom = swrlFactory.createLessThan(arg1, arg2); else if (e instanceof Q_LessThanOrEqual) atom = swrlFactory.createLessThanOrEqual(arg1, arg2); else throw new IllegalArgumentException( "Unsupported constraint expression " + e + " used in RDQL query."); } else if (expr.isFunction()) // SPARQL { final ExprFunction f = (ExprFunction) expr; final SWRLDataObject arg1 = makeDataObject(f.getArg(0)); final SWRLDataObject arg2 = makeDataObject(f.getArg(1)); if (f instanceof E_Equals) atom = swrlFactory.createEqual(arg1, arg2); else if (f instanceof E_NotEquals) atom = swrlFactory.createNotEqual(arg1, arg2); else if (f instanceof E_GreaterThan) atom = swrlFactory.createGreaterThan(arg1, arg2); else if (f instanceof E_GreaterThanOrEqual) atom = swrlFactory.createGreaterThanOrEqual(arg1, arg2); else if (f instanceof E_LessThan) atom = swrlFactory.createLessThan(arg1, arg2); else if (f instanceof E_LessThanOrEqual) atom = swrlFactory.createLessThanOrEqual(arg1, arg2); else throw new IllegalArgumentException( "Unsupported constraint (filter) " + f + " used in SPARQL query."); } else return; atoms = atoms.cons(atom); } catch (final URISyntaxException e) { throw new IllegalArgumentException(e.getInput() + " is not a valid URI!"); } }
public static boolean containsDirectFuncChild(Expr expr, Class<?> clazz) { if (!(expr instanceof ExprFunction)) { return false; } ExprFunction func = (ExprFunction) expr; for (Expr arg : func.getArgs()) { if (arg == null) { continue; } if (clazz.isAssignableFrom(arg.getClass())) { return true; } } return false; }
private void visitExprFunction(ExprFunction function) { logger.debug("visit ExprFunction " + function); if (!convertable) { expression.push(Expression.FALSE); // prevent stack empty exceptions when conversion return; // fails in the middle of a multi-arg operator conversion } if (!extensionSupports(function)) { conversionFailed(function); return; } for (int i = 0; i < function.numArgs(); i++) function.getArg(i + 1).visit(this); List<Expression> args = new ArrayList<Expression>(function.numArgs()); for (int i = 0; i < function.numArgs(); i++) args.add(expression.pop()); Collections.reverse(args); extensionConvert(function, args); }
public static Expr handle(ExprFunction expr) { // System.out.println("Converting to DNF: [" + expr.getClass() + "]: " + expr); // not(and(A, B)) -> or(not A, not B) // not(or(A, B)) -> or(not A, not B) if (expr instanceof E_LogicalNot) { Expr tmp = ((E_LogicalNot) expr).getArg(); if (!(tmp instanceof ExprFunction)) { return expr; } ExprFunction child = (ExprFunction) tmp; Expr newExpr = expr; if (child instanceof E_LogicalAnd) { newExpr = new E_LogicalOr( eval(new E_LogicalNot(child.getArg(1))), eval(new E_LogicalNot(child.getArg(2)))); } else if (child instanceof E_LogicalOr) { newExpr = new E_LogicalAnd( eval(new E_LogicalNot(child.getArg(1))), eval(new E_LogicalNot(child.getArg(2)))); } else if (child instanceof E_LogicalNot) { // Remove double negation newExpr = eval(child.getArg(1)); } else { return expr; } return eval(newExpr); } else if (expr instanceof E_LogicalOr) { // return expr; // return eval(expr); return new E_LogicalOr(eval(expr.getArg(1)), eval(expr.getArg(2))); } /* Given: * (A or B) AND (C x D) becomes: * (A and (C x D)) OR (B and (c x D)) * * * (A or B) AND (C or D) * * Goal: * (A and C) OR (A and D) OR (B and C) OR (B and D) * * This method transforms any "or" children of an AND node. * other nodes are left untouched: * (A or B) AND (c x D) becomes: * (A and (c x D)) OR (B and (c x D)) */ else if (expr instanceof E_LogicalAnd) { Expr aa = eval(expr.getArg(1)); Expr bb = eval(expr.getArg(2)); E_LogicalOr a = null; Expr b = null; if (aa instanceof E_LogicalOr) { a = (E_LogicalOr) aa; b = bb; } else if (bb instanceof E_LogicalOr) { a = (E_LogicalOr) bb; b = aa; } if (a == null) { return new E_LogicalAnd(aa, bb); } else { return new E_LogicalOr( eval(new E_LogicalAnd(a.getArg(1), b)), eval(new E_LogicalAnd(a.getArg(2), b))); } } else if (expr instanceof E_NotEquals) { // Normalize (a != b) to !(a = b) --- this makes it easier to find "a and !a" // cases return new E_LogicalNot(eval(new E_Equals(expr.getArg(1), expr.getArg(2)))); } return expr; /* if(expr instanceof E_LogicalNot) { Expr tmp = ((E_LogicalNot)expr).getArg(); if (!(tmp instanceof ExprFunction)) return expr; ExprFunction child = (ExprFunction) tmp; String newFuncName = ""; if (child.getName().equals("and")) newFuncName = "or"; else if (child.getName().equals("or")) newFuncName = "and"; else if (child.getName().equals("not")) // Remove double negation return eval(child.getChildren().get(0)); else return expr; FuncExpr result = new FuncExpr(newFuncName, child.getArity()); for (int i = 0; i < child.getArity(); ++i) result.setChild(i, new FuncExpr("not", child.getChildren().get(i))); return eval(result); } if (expr.getName().equals("or")) { List<IExpr> children = new ArrayList<IExpr>(); for (IExpr child : expr.getChildren()) children.add(eval(child)); if (!ExprUtil.containsDirectFuncChild(children, "or")) return new FuncExpr("or", children); // flatten or expressions // or(or(A, B), C) becomes or(A, B, C) List<IExpr> resultChildren = new ArrayList<IExpr>(); for (IExpr child : children) if (ExprUtil.isFunc(child, "or")) resultChildren.addAll(child.getChildren()); else resultChildren.add(child); return new FuncExpr("or", resultChildren); } if (expr.getName().equals("and")) { List<IExpr> children = new ArrayList<IExpr>(); for (IExpr child : expr.getChildren()) children.add(eval(child)); // FIXME an and-node must have at least 2 children // but maybe validation should be done somewhere else // On the other hand it might be convenient to assume that // whenever a binary expression only contains a single child // it should be treated as if there was no operation at all. // No 'or'-expression => nothing todo if (!ExprUtil.containsDirectFuncChild(children, "or")) return new FuncExpr("and", children); // Collect all expressions List<List<IExpr>> tables = new ArrayList<List<IExpr>>(); for (IExpr child : children) { if (ExprUtil.isFunc(child, "or")) tables.add(child.getChildren()); else tables.add(Collections.singletonList(child)); } Collection<List<IExpr>> joinedTable = new JoinIterable<IExpr>( tables); FuncExpr result = new FuncExpr("or", joinedTable.size()); // List<IExpr> resultChildren = new ArrayList<IExpr>(); int i = 0; for (List<IExpr> row : joinedTable) { IExpr newChild = new FuncExpr("and", row); result.setChild(i, newChild); ++i; } return result; } // TODO Auto-generated method stub return expr; */ }
/** * Processes (at the time of writing some) known functions available in the Sparqlify-ML, * variables and constant strings. Since arguments of the considered function can be functions, * variables or constant strings as well, this method is called recursively to get to the most * inner expression and build up the rest based on that. * * @param expr a restriction expression (com.hp.hpl.jena.sparql.expr.Expr) like a function * (concat( ... ), uri( ... ), ...) or a variable * @return a String containing the R2RML counterpart of these Sparqlify-ML expressions */ private String processRestrExpr(Expr expr) { String exprStr = ""; /* * functions */ if (expr.isFunction()) { ExprFunction func = expr.getFunction(); // concat( ... ) if (func.getFunctionSymbol().equals(concatLabel)) { List<Expr> args = func.getArgs(); for (Expr arg : args) { // explicitely use string representation of IRIs get strings // without leading and trailing angle brackets if (arg.isConstant() && arg.toString().startsWith("<")) { exprStr += arg.getConstant().asString(); continue; } exprStr += processRestrExpr(arg); } // uri( ... ) } else if (func.getFunctionIRI().equals(uriFunctionIRI)) { // there should be just one argument here Expr subExpr = func.getArgs().get(0); exprStr += processRestrExpr(subExpr); // plainLiteral( ... ) } else if (func.getFunctionIRI().equals(plainLiteralFunctionIRI)) { // I am only interested in the first argument here, since a // possible second argument containing a language tag is only // of interest, if the plainLiteral is the most outer function // which is handled in a different place. Expr subExpr = func.getArgs().get(0); exprStr += processRestrExpr(subExpr); // typedLiteral } else if (func.getFunctionIRI().equals(typedLiteralFunctionIRI)) { // As above I am only interested in the first argument here // since the second argument stating which type the first // argument has, should be processed in a different place. Expr subExpr = func.getArgs().get(0); exprStr += processRestrExpr(subExpr); } else { // URL encode // FIXME: no URL encoding is done here!! Expr subExpr = func.getArgs().get(0); exprStr += processRestrExpr(subExpr); } /* * variables and strings */ } else { // strings if (expr.isConstant()) { String constStr = expr.toString(); if (constStr.startsWith("\"")) { // strip off the leading and trailing quote constStr = constStr.substring(1); if (constStr.endsWith("\"")) { int strLength = constStr.length(); constStr = constStr.substring(0, strLength - 1); } } exprStr += constStr; // variables } else if (expr.isVariable()) { String varStr = expr.toString(); // strip off the leading question mark... varStr = varStr.substring(1); // ...and put the value in curly braces varStr = "{" + varStr + "}"; exprStr += varStr; } } return exprStr; }