Strabon
changeset 1417:a1cf22bfb573
type checking in extension functions in SELECT clause
author | Babis Nikolaou <charnik@di.uoa.gr> |
---|---|
date | Mon Sep 22 16:24:07 2014 +0300 (2014-09-22) |
parents | df3595d612f3 |
children | a232e3307693 |
files | generaldb/src/main/java/org/openrdf/sail/generaldb/evaluation/GeneralDBEvaluation.java |
line diff
1.1 --- a/generaldb/src/main/java/org/openrdf/sail/generaldb/evaluation/GeneralDBEvaluation.java Mon Sep 22 15:13:33 2014 +0300 1.2 +++ b/generaldb/src/main/java/org/openrdf/sail/generaldb/evaluation/GeneralDBEvaluation.java Mon Sep 22 16:24:07 2014 +0300 1.3 @@ -291,37 +291,41 @@ 1.4 throw new UnsupportedExtensionFunctionException("Extension function " + fc.getURI()+ " is not supported."); 1.5 } 1.6 1.7 - // get the first argument of the function call 1.8 - ValueExpr left = fc.getArgs().get(0); 1.9 - 1.10 - // evaluated first argument of function 1.11 - Value leftResult = null; 1.12 + try { 1.13 + if (fc.getArgs().size() == 0) { 1.14 + throw new NoSuchMethodException("too few arguments"); 1.15 + } 1.16 1.17 - // evaluated second argument of function (if any) 1.18 - Value rightResult = null; 1.19 - 1.20 - // evaluated third argument of function (if any) 1.21 - Value thirdResult = null; 1.22 + // get the first argument of the function call 1.23 + ValueExpr left = fc.getArgs().get(0); 1.24 + 1.25 + // evaluated first argument of function 1.26 + Value leftResult = null; 1.27 + 1.28 + // evaluated second argument of function (if any) 1.29 + Value rightResult = null; 1.30 + 1.31 + // evaluated third argument of function (if any) 1.32 + Value thirdResult = null; 1.33 + 1.34 + // evaluate first argument 1.35 + leftResult = evaluate(left, bindings); 1.36 + 1.37 + // function call with 2 or more arguments, evaluate the second one now 1.38 + // see distance function as example 1.39 + if ( fc.getArgs().size() >= 2 ) 1.40 + { 1.41 + ValueExpr right = fc.getArgs().get(1); 1.42 + rightResult = evaluate(right, bindings); 1.43 1.44 - // evaluate first argument 1.45 - leftResult = evaluate(left, bindings); 1.46 + if (fc.getArgs().size() == 3) { 1.47 + ValueExpr third = fc.getArgs().get(2); 1.48 + thirdResult = evaluate(third, bindings); 1.49 + } 1.50 + } 1.51 1.52 - // function call with 2 or more arguments, evaluate the second one now 1.53 - // see distance function as example 1.54 - if ( fc.getArgs().size() >= 2 ) 1.55 - { 1.56 - ValueExpr right = fc.getArgs().get(1); 1.57 - rightResult = evaluate(right, bindings); 1.58 + // having evaluated the arguments, evaluate the function now 1.59 1.60 - if (fc.getArgs().size() == 3) { 1.61 - ValueExpr third = fc.getArgs().get(2); 1.62 - thirdResult = evaluate(third, bindings); 1.63 - } 1.64 - } 1.65 - 1.66 - // having evaluated the arguments of the function, evaluate the function 1.67 - try { 1.68 - 1.69 // 1.70 // SPATIAL METRIC FUNCTIONS 1.71 // 1.72 @@ -336,6 +340,9 @@ 1.73 } 1.74 else if(function instanceof DistanceFunc) 1.75 { 1.76 + // check required number of arguments 1.77 + checkArgs(leftResult, rightResult, thirdResult, 3); 1.78 + 1.79 // get the second geometry 1.80 Geometry rightGeom = getValueAsStrabonPolyhedron(rightResult).getGeometry(); 1.81 1.82 @@ -503,19 +510,56 @@ 1.83 } 1.84 } 1.85 1.86 - public StrabonPolyhedron spatialConstructPicker(Function function, Value left, Value right, Value thirdResult) throws Exception 1.87 + /** 1.88 + * @param leftResult 1.89 + * @param rightResult 1.90 + * @param thirdResult 1.91 + * @param size 1.92 + * @throws NoSuchMethodException 1.93 + */ 1.94 + private void checkArgs(Value leftResult, Value rightResult, Value thirdResult, int size) throws NoSuchMethodException { 1.95 + if (size == 0) { 1.96 + throw new NoSuchMethodException("too few arguments."); 1.97 + 1.98 + } else { 1.99 + if (size >= 1 && leftResult == null) { 1.100 + throw new NoSuchMethodException("expecting an argument."); 1.101 + } 1.102 + 1.103 + if (size >= 2 && rightResult == null) { 1.104 + throw new NoSuchMethodException("expecting a second argument."); 1.105 + } 1.106 + 1.107 + if (size >= 3 && thirdResult == null) { 1.108 + throw new NoSuchMethodException("expecting a third argument."); 1.109 + 1.110 + } 1.111 + 1.112 + if (size > 3) { 1.113 + throw new NoSuchMethodException("too many arguments."); 1.114 + } 1.115 + } 1.116 + } 1.117 + 1.118 + public StrabonPolyhedron spatialConstructPicker(Function function, Value left, Value right, Value third) throws Exception 1.119 { 1.120 StrabonPolyhedron leftArg = getValueAsStrabonPolyhedron(left); 1.121 if(function.getURI().equals(GeoConstants.stSPARQLunion)) 1.122 { 1.123 + // check required number of arguments 1.124 + checkArgs(left, right, third, 2); 1.125 + 1.126 StrabonPolyhedron rightArg = getValueAsStrabonPolyhedron(right); 1.127 return StrabonPolyhedron.union(leftArg, rightArg); 1.128 } 1.129 else if (function instanceof BufferFunc) { 1.130 + // check required number of arguments 1.131 + checkArgs(left, right, third, 3); 1.132 + 1.133 // TODO implement computation of the buffer in meters 1.134 // you'll get the type (degrees/meter) from the thirdResult, which 1.135 // would be a URI. 1.136 - if (OGCConstants.OGCmetre.equals(thirdResult.stringValue())) { 1.137 + if (OGCConstants.OGCmetre.equals(third.stringValue())) { 1.138 logger.info("[GeneraDBEvaluation] Computation of {} will be done in degrees.", function.getURI()); 1.139 } 1.140 1.141 @@ -532,6 +576,9 @@ 1.142 } 1.143 else if(function.getURI().equals(GeoConstants.stSPARQLtransform)) 1.144 { 1.145 + // check required number of arguments 1.146 + checkArgs(left, right, third, 2); 1.147 + 1.148 if(right instanceof URIImpl) 1.149 { 1.150 URIImpl srid = (URIImpl) right; 1.151 @@ -562,16 +609,25 @@ 1.152 } 1.153 else if(function.getURI().equals(GeoConstants.stSPARQLintersection)) 1.154 { 1.155 + // check required number of arguments 1.156 + checkArgs(left, right, third, 2); 1.157 + 1.158 StrabonPolyhedron rightArg = getValueAsStrabonPolyhedron(right); 1.159 return StrabonPolyhedron.intersection(leftArg, rightArg); 1.160 } 1.161 else if(function.getURI().equals(GeoConstants.stSPARQLdifference)) 1.162 { 1.163 + // check required number of arguments 1.164 + checkArgs(left, right, third, 2); 1.165 + 1.166 StrabonPolyhedron rightArg = getValueAsStrabonPolyhedron(right); 1.167 return StrabonPolyhedron.difference(leftArg, rightArg); 1.168 } 1.169 else if(function.getURI().equals(GeoConstants.stSPARQLsymDifference)) 1.170 { 1.171 + // check required number of arguments 1.172 + checkArgs(left, right, third, 2); 1.173 + 1.174 StrabonPolyhedron rightArg = getValueAsStrabonPolyhedron(right); 1.175 return StrabonPolyhedron.symDifference(leftArg, rightArg); 1.176