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 ; } }
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) ; } }
@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) ; } }
@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) ; }
public static NodeValue strLang(NodeValue v1, NodeValue v2) { if ( !v1.isString() ) throw new ExprEvalException("Not a string (arg 1): " + v1) ; if ( !v2.isString() ) throw new ExprEvalException("Not a string (arg 2): " + v2) ; String lex = v1.asString() ; String lang = v2.asString() ; if ( lang.isEmpty() ) throw new ExprEvalException("Empty lang tag") ; return NodeValue.makeLangString(lex, lang) ; }
public static NodeValue get(Symbol symbol, FunctionEnv env) { Object obj = env.getContext().get(symbol); if ( obj == null ) throw new ExprEvalException("null for symbol: " + symbol); if ( !(obj instanceof Node) ) throw new ExprEvalException("Not a Node: " + Lib.className(obj)); Node n = (Node)obj; NodeValue nv = NodeValue.makeNode(n); return nv; } }
private static void checkFlags(String flags) { if ( flags == null ) return; // F&O spec defines regex: Can only contain s, m, i, x, q // Not all are supported by all regex engines. if ( ! StringUtils.containsOnly(flags, "smixq") ) throw new ExprEvalException("REGEX: Only 'smixq' are legal as pattern flags: got \""+FmtUtils.stringEsc(flags)+"\""); }
@Override public NodeValue evalSpecial(Binding binding, FunctionEnv env) { for ( Expr expr : super.getArgs() ) { try { NodeValue nv = expr.eval(binding, env) ; return nv ; } catch (ExprEvalException ex) {} } throw new ExprEvalException("COALESCE: no value") ; }
private void forceToNode() { if ( node == null ) node = asNode() ; if ( node == null ) raise(new ExprEvalException("Not a node: "+this)) ; }
@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("Function '"+Lib.className(this)+" Null args list") ; if ( args.size() != 0 ) throw new ExprEvalException("Function '"+Lib.className(this)+" Wanted 0, got "+args.size()) ; return exec() ; }
public static NodeValue javaSubstring(NodeValue nvString, NodeValue nvStart, NodeValue nvFinish) { try { String string = nvString.getString() ; int start = nvStart.getInteger().intValue() ; if ( nvFinish == null ) return NodeValue.makeString(string.substring(start)) ; int finish = nvFinish.getInteger().intValue() ; return NodeValue.makeString(string.substring(start, string.offsetByCodePoints(start, finish - start))) ; } catch (IndexOutOfBoundsException ex) { throw new ExprEvalException("IndexOutOfBounds", ex) ; } }
@Override public void build(String uri, ExprList args) { if ( args.size() != 3 && args.size() != 2 ) throw new ExprEvalException("matches: Wrong number of arguments: Wanted 2 or 3, got "+args.size()) ; myArgs = args ; }
@Override public NodeValue eval(NodeValue x, NodeValue y) { if ( ! x.isString() ) throw new ExprEvalException("cast: arg 2 is not a string: "+x) ; if ( ! y.isIRI() ) throw new ExprEvalException("cast: arg 2 is not a URI: "+y) ; String lex = x.getString() ; y.asNode().getURI() ; throw new ARQNotImplemented() ; }
public static NodeValue strEncodeForURI(NodeValue v) { Node n = v.asNode() ; if ( !n.isLiteral() ) throw new ExprEvalException("Not a literal") ; if ( ! Util.isSimpleString(n) && ! Util.isLangString(n) ) throw new ExprEvalException("Not a string literal") ; String str = n.getLiteralLexicalForm() ; String encStr = IRILib.encodeUriComponent(str) ; encStr = IRILib.encodeNonASCII(encStr) ; return NodeValue.makeString(encStr) ; }
@Override public NodeValue exec(NodeValue v) { Node n = v.asNode() ; if ( ! n.isURI() ) throw new ExprEvalException("Not a URI: "+FmtUtils.stringForNode(n)) ; String str = n.getNameSpace() ; return NodeValue.makeString(str) ; } }
@Override public NodeValue exec(NodeValue v) { Node n = v.asNode() ; if ( ! n.isBlank() ) throw new ExprEvalException("bnode: not a blank node") ; NodeValue nv = NodeValue.makeString(n.getBlankNodeId().getLabelString()) ; return nv ; } }
private static DateTimeStruct parseTime(NodeValue nv) { String lex = nv.asNode().getLiteralLexicalForm() ; if ( nv.isDateTime() ) return DateTimeStruct.parseDateTime(lex) ; else if ( nv.isTime() ) return DateTimeStruct.parseTime(lex) ; else throw new ExprEvalException("Not a datatype for time") ; }
public static String str(Node node) { if ( node.isLiteral() ) return node.getLiteral().getLexicalForm() ; if ( node.isURI() ) return node.getURI() ; // if ( node.isBlank() ) return node.getBlankNodeId().getLabelString() ; // if ( node.isBlank() ) return "" ; if ( node.isBlank() ) NodeValue.raise(new ExprTypeException("Blank node: " + node)) ; NodeValue.raise(new ExprEvalException("Not a string: " + node)) ; return "[undef]" ; }