/** * A convenience method to make an expression from a string. Evaluates the * expression with respect to the current query. * * @param expression The expression to parse. * @return the Expr object. * @throws QueryParseException on error. */ public Expr makeExpr(String expression) throws QueryParseException { return ExprUtils.parse(query, expression, true); }
protected Expr asExpr(Node n) { return ExprUtils.nodeToExpr(n) ; }
static void testDouble(String exprString, Node result, double delta) { Expr expr = ExprUtils.parse(exprString, pmap); NodeValue actual = expr.eval(null, new FunctionEnvBase()); NodeValue expected = NodeValue.makeNode(result); // Note that we don't test lexical form because we can get mismatches // between how things like doubles are expressed if (NodeValue.sameAs(expected, actual)) return; testDouble(exprString, expected.getDouble(), delta); ; }
if(expr != null && (!expr.isVariable() || binding.contains(expr.asVar()))) { NodeValue x = expr.eval(binding, env); if(x != null) { String paramName; paramName = "arg" + (i + 1); bindings.add(paramName, model.asRDFNode(x.asNode())); if(cachable) { paramsForCache[i] = x.asNode(); ByteArrayOutputStream bos = new ByteArrayOutputStream(); IndentedWriter iOut = new IndentedWriter(bos); ExprUtils.fmtSPARQL(iOut, expr, new SerializationContext(model)); iOut.flush(); sb.append(bos.toString()); sb.append(FmtUtils.stringForNode(result.asNode(), model));
private static void test_exprSprintf_tz_possibilites(String nodeStr, String... possible) { String exprStr = "afn:sprintf('%1$tm %1$te,%1$tY', "+NodeValue.makeDateTime(nodeStr).toString()+")" ; Expr expr = ExprUtils.parse(exprStr) ; NodeValue r = expr.eval(null, LibTestExpr.createTest()) ; assertTrue(r.isString()) ; String s = r.getString() ; // Timezones! The locale data can be -1, 0, +1 from the Z day. boolean b = false ; for (String poss : possible ) { if ( poss.equals(s) ) b = true ; } assertTrue(b) ; }
public static Pair<Expr, Expr> optimizeFunctionOpConstant(Expr a, Expr b) { NodeValue constant = null; ExprFunction function = null; boolean swapArgs = false; UnaryOperator<Expr> factory; if(a.isConstant() && b.isFunction()) { constant = a.getConstant(); function = b.getFunction(); } else if(a.isFunction() && b.isConstant()) { function = a.getFunction(); constant = b.getConstant(); swapArgs = true; } else { return Pair.create(a, b); } factory = inverseFunctionManager.getInverse(b.getFunction().getFunctionIRI()); if(factory == null) { return Pair.create(a, b); } Expr invConst = factory.apply(constant); Expr resultConst = org.apache.jena.sparql.util.ExprUtils.eval(invConst); Expr resultFunc = function.getArg(1); return createPair(resultConst, resultFunc, swapArgs); }
if(a.equals(SparqlifyConstants.nvTypeError)) { newExpr = SparqlifyConstants.nvTypeError; } else { if(evaledArg.equals(SparqlifyConstants.nvTypeError)) { return SparqlifyConstants.nvTypeError; if(exprTransformer != null && newExpr.isFunction()) { tmp = exprTransformer.transform(newExpr.getFunction()); result = ExprUtils.eval(tmp); } catch(ExprNotComparableException e) { return SparqlifyConstants.nvTypeError;
Set<Var> exprVars = expr.getVarsMentioned(); for(Var var : exprVars) { if(!binding.contains(var)) { if(usedVars.containsAll(expr.getVarsMentioned())) { continue; } else if(usedVars.equals(expr.getVarsMentioned())) { throw new RuntimeException("Multiple expressions binding the variable (ambiguity) " + bindingVar + ": " + entry.getValue()); } else if(!expr.getVarsMentioned().containsAll(usedVars)) { value = ExprUtils.eval(expr, binding); Node resultValue = value == null ? null : value.asNode();
static void testDouble(String exprString, double expected, double delta) { Expr expr = ExprUtils.parse(exprString, pmap); NodeValue actual = expr.eval(null, new FunctionEnvBase()); assertTrue("Not a double: "+actual, actual.isDouble() ) ; double result = actual.getDouble() ; // Because Java floating point calculations are woefully imprecise we // are in many cases simply testing that the differences between the // values are within a given delta if ( Double.isInfinite(expected) ) { assertTrue("Expected INF: Got "+result, Double.isInfinite(result)) ; return ; } if ( Double.isNaN(expected) ) { assertTrue("Expected NaN: Got "+result, Double.isNaN(result)) ; return ; } double difference = Math.abs(result - expected); assertTrue("Values not within given delta " + delta + ": Expected = " + expected + " : Actual = " + actual, difference <= delta); }
public Boolean determineSatisfiability(Expr expr) { /* BindingMap bindingMap = new BindingMap(); for(Entry<Var, Node> entry : binding.entrySet()) { bindingMap.add(entry.getKey(), entry.getValue()); }*/ if(binding.keySet().containsAll(expr.getVarsMentioned())) { try { NodeValue value = ExprUtils.eval(expr, bindingMap); return value.getBoolean(); } catch(Exception e) { // Evaluation of the expression failed despite all variables were bound // Satisfiability unknown System.err.println(e); return null; } } else if(expr instanceof E_LogicalNot) { Boolean tmp = determineSatisfiability(((E_LogicalNot)expr).getArg()); return tmp == null ? null : !tmp; } else if(expr instanceof E_Equals) { E_Equals e = (E_Equals)expr; RestrictionSetImpl a = getRestriction(e.getArg1()); RestrictionSetImpl b = getRestriction(e.getArg2()); return determineSatisfiabilityEquals(a, b); } else { return null; } }
Entry<Var, NodeValue> c = ExprUtils.extractConstantConstraint(expr); if(c != null && c.getKey().equals(s)) { Node n = c.getValue().asNode(); concretePredicates.add(n); } else { ExprList exprs = new ExprList(); for(Node node : concretePredicates) { Expr expr = org.apache.jena.sparql.util.ExprUtils.nodeToExpr(node); exprs.add(expr);
Expr target = ExprUtils.nodeToExpr(entry.getKey()); Node n = values.get(entry.getKey()); Var v = entry.getKey(); if (n.isVariable()) { v = Var.alloc(n); if (target.equals(e)) { e = ExprUtils.nodeToExpr(n);
and = and && ExprUtils.eval(expr, binding).getBoolean(); if(and == false) { break;
public static void fmtSPARQL(IndentedWriter iOut, ExprList exprs) { fmtSPARQL(iOut, exprs, FmtUtils.sCxt()) ; }