static private double calcVariance$(double sumSquared, double sum, long N, long N1) { // System.out.printf("sum = %f, sumSq = %f, N=%d\n", sum, sumSquared, N) ; if ( N <= 0 ) throw new ExprEvalException("N= "+N) ; if ( N1 == 0 ) throw new ExprEvalException("Sample size one") ; double x = sumSquared - (sum*sum)/N ; x = x / N1 ; return x ; } }
public NodeValue execute(SHACLARQFunction function, Dataset dataset, Model defaultModel, QuerySolution bindings, Node[] args) { Key key = new Key(function.getSHACLFunction().getURI(), args); Result result = cache.get(key); if(result == null) { result = new Result(); try { result.nodeValue = function.executeBody(dataset, defaultModel, bindings); } catch(ExprEvalException ex) { result.ex = ex; } cache.put(key, result); } if(result.ex != null) { throw new ExprEvalException(result.ex.getMessage()); } else { return result.nodeValue; } }
public static void evalPrint(Expr expr, Binding binding) { try { NodeValue r = eval(expr, binding) ; //System.out.println(r.asQuotedString()) ; Node n = r.asNode() ; String s = FmtUtils.stringForNode(n) ; System.out.println(s) ; } catch (ExprEvalException ex) { System.out.println("Exception: "+ex.getMessage()) ; return ; } catch (ExprBuildException ex) { System.err.println("Build exception: "+ex.getMessage()) ; return ; } }
public NodeValue execute(SHACLARQFunction function, Dataset dataset, Model defaultModel, QuerySolution bindings, Node[] args) { Key key = new Key(function.getSHACLFunction().getURI(), args); Result result = cache.get(key); if(result == null) { result = new Result(); try { result.nodeValue = function.executeBody(dataset, defaultModel, bindings); } catch(ExprEvalException ex) { result.ex = ex; } cache.put(key, result); } if(result.ex != null) { throw new ExprEvalException(result.ex.getMessage()); } else { return result.nodeValue; } }
@Override public NodeValue eval(List<NodeValue> args) { Node arg = NodeFunctions.checkAndGetStringLiteral("REGEX", args.get(0)) ; NodeValue vPattern = args.get(1) ; NodeValue vFlags = ( args.size() == 2 ? null : args.get(2) ) ; RegexEngine regex = regexEngine ; if ( regex == null ) { // Execution time regex compile (not a constant pattern). try { regex = makeRegexEngine(vPattern, vFlags) ; } catch (ExprEvalException ex) { // Avoid multiple logging of the same message (at least if adjacent) String m = ex.getMessage(); if ( m != null && ! m.equals(currentFailMessage) ) Log.warn(this, m); currentFailMessage = m; // This becomes an eval error, the FILTER is false and the current row rejected. throw ex; } } boolean b = regex.match(arg.getLiteralLexicalForm()) ; return b ? NodeValue.TRUE : NodeValue.FALSE ; }
/** * Parse a lexical form of this datatype to a value * @throws DatatypeFormatException if the lexical form is not legal */ @Override public JsonElement parse(String lexicalForm) throws DatatypeFormatException { //Object result = gson.fromJson(lexicalForm, Object.class); JsonElement result; try { result = gson.fromJson(lexicalForm, JsonElement.class); } catch(Exception e) { // TODO This is not the best place for an expr eval exception; it should go to E_StrDatatype throw new ExprEvalException(e); } return result; }
Log.warn( this, ex.getMessage() ); Log.warn( this, ex.getMessage() );
/** * Parse a lexical form of this datatype to a value * @throws DatatypeFormatException if the lexical form is not legal */ @Override public JsonElement parse(String lexicalForm) throws DatatypeFormatException { //Object result = gson.fromJson(lexicalForm, Object.class); JsonElement result; try { result = gson.fromJson(lexicalForm, JsonElement.class); } catch(Exception e) { // TODO This is not the best place for an expr eval exception; it should go to E_StrDatatype throw new ExprEvalException(e); } return result; }
@Test public void fn_error_01() { try { LibTestExpr.eval("fn:error()"); fail("No exception"); } catch (ExprEvalException ex) { assertNull(ex.getMessage()); } }
@Override public NodeValue exec(List<NodeValue> args) { if ( args.size() > 1 ) throw new ExprEvalException("replace: Wrong number of arguments: "+args.size()+" : [wanted 1]") ; NodeValue v1 = args.get(0) ; return convert(v1) ; }
@Test public void fn_error_02() { try { LibTestExpr.eval("fn:error('MESSAGE')"); fail("No exception"); } catch (ExprEvalException ex) { assertEquals("MESSAGE", ex.getMessage()); } } }
public static int makeMask(String modifiers) { if ( modifiers == null ) return 0; int newMask = 0; for ( int i = 0; i < modifiers.length(); i++ ) { switch (modifiers.charAt(i)) { case 'i': newMask |= Pattern.UNICODE_CASE; newMask |= Pattern.CASE_INSENSITIVE; break; case 'm': newMask |= Pattern.MULTILINE; break; case 's': newMask |= Pattern.DOTALL; break; // Not suported by Java regex //case 'x': newMask |= Pattern.; break; case 'q':; break; default: throw new ExprEvalException("Unsupported flag in regex modifiers: "+modifiers.charAt(i)); } } return newMask; } }
private RegularExpression makePattern(String patternStr, String flags) { // flag q supported above. // Input : only m s i x // Check/modify flags. // Always "u", never patternStr // x: Remove whitespace characters (#x9, #xA, #xD and #x20) unless in [] classes try { return new RegularExpression(patternStr, flags) ; } catch (ParseException pEx) { throw new ExprEvalException("Regex: Pattern exception: "+pEx) ; } } }
@Override public NodeValue exec(List<NodeValue> args) { if ( args.size() == 3 ) return XSDFuncOp.strReplace(args.get(0), args.get(1), args.get(2)) ; if ( args.size() == 4 ) return XSDFuncOp.strReplace(args.get(0), args.get(1), args.get(2), args.get(3)) ; throw new ExprEvalException("fn:replace: Wrong number of arguments: Wanted 3 or 4, got " + args.size()) ; } }
@Override public NodeValue exec(List<NodeValue> args) { if ( args.size() < 2 ) throw new ExprEvalException(Lib.className(this)+": Wrong number of arguments: "+ args.size()+" : [wanted at least 2]") ; NodeValue v1 = args.get(0) ; List<NodeValue> allArgs = new ArrayList<>(); for(int i = 1;i < args.size();i++) allArgs.add(args.get(i)); return XSDFuncOp.javaSprintf(v1, allArgs) ; } }
public static Pattern makePattern(String label, String patternStr, String flags) { try { int mask = 0; if ( flags != null ) { mask = makeMask(flags); if ( flags.contains("q") ) patternStr = Pattern.quote(patternStr); } return Pattern.compile(patternStr, mask); } catch (PatternSyntaxException pEx) { throw new ExprEvalException(label+" pattern exception: " + pEx); } }
@Override public NodeValue exec(List<NodeValue> args) { if ( args.size() != 2 && args.size() != 3 ) throw new ExprEvalException("Function '"+Lib.className(this)+"' takes two or three arguments") ; NodeValue value = args.get(0) ; NodeValue picture = args.get(1) ; NodeValue decimalFormatName = null ; if ( args.size() == 3) decimalFormatName = args.get(2) ; return XSDFuncOp.formatNumber(value, picture, decimalFormatName) ; } }
private static String replaceAll(Matcher matcher, String rep) { // Follow Java -- return matcher.replaceAll(rep) ; try { StringBuffer sb = null ; // Delay until needed while(matcher.find()) { if ( sb == null ) sb = new StringBuffer() ; else { // Do one match of zerolength string otherwise filter out. if (matcher.start() == matcher.end() ) continue ; } matcher.appendReplacement(sb, rep); } if ( sb == null ) return null ; matcher.appendTail(sb); return sb.toString(); } catch (IndexOutOfBoundsException ex) { throw new ExprEvalException("IndexOutOfBounds", ex) ; } }
@Override public final NodeValue exec(List<NodeValue> args) { if ( args == null ) // The contract on the function interface is that this should not happen. throw new ARQInternalErrorException("FunctionBase1: Null args list") ; if ( args.size() != 1 ) throw new ExprEvalException("FunctionBase1: Wrong number of arguments: Wanted 1, got "+args.size()) ; NodeValue v1 = args.get(0) ; return exec(v1) ; }
@Override public NodeValue exec(List<NodeValue> args) { if ( args == null ) // The contract on the function interface is that this should not happen. throw new ARQInternalErrorException("FunctionBase1: Null args list") ; switch(args.size()) { case 0: throw new ExprEvalException(); case 1: { NodeValue nv = args.get(0); String str = NodeFunctions.str(nv.asNode()); throw new ExprEvalException(str); } default: throw new ExprEvalException("fn:error: Wrong number of arguments: Wanted 0 or 1, got "+args.size()) ; } } }