DistanceFF extends FunctionBase3 { @Override public NodeValue exec(NodeValue v1, NodeValue v2, NodeValue v3) { try { GeometryWrapper geometry1 = GeometryWrapper.extract(v1, GeometryLiteralIndex.GeometryIndex.PRIMARY); GeometryWrapper geometry2 = GeometryWrapper.extract(v2, GeometryLiteralIndex.GeometryIndex.SECONDARY); if (!(v3.isIRI() || v3.isString())) { throw new ExprEvalException("Not an IRI or String: " + FmtUtils.stringForNode(v3.asNode())); } String unitsURI; if (v3.isIRI()) { unitsURI = v3.asNode().getURI(); } else { unitsURI = v3.asString(); } double distance = geometry1.distance(geometry2, unitsURI); return NodeValue.makeDouble(distance); } catch (DatatypeFormatException | FactoryException | MismatchedDimensionException | TransformException ex) { throw new ExprEvalException(ex.getMessage(), ex); } }  @Override NodeValue exec(NodeValue v1, NodeValue v2, NodeValue v3);  }
@Test public void testExec() { NodeValue v1 = NodeValue.makeNode("<http: NodeValue v2 = NodeValue.makeNode("<http: NodeValue v3 = NodeValue.makeNode(NodeFactory.createURI(Unit_URI.KILOMETER_URL)); DistanceFF instance = new DistanceFF(); double expResult = 109.5057; double result = instance.exec(v1, v2, v3).getDouble(); assertEquals(expResult, result, 0.0001); }
@Test public void testExec2() { NodeValue v1 = NodeValue.makeNode("<http: NodeValue v2 = NodeValue.makeNode("<http: NodeValue v3 = NodeValue.makeString(Unit_URI.KILOMETER_URL); DistanceFF instance = new DistanceFF(); double expResult = 111.1950; double result = instance.exec(v1, v2, v3).getDouble(); assertEquals(expResult, result, 0.0001); }
@Test public void testExec_Paris_London() { NodeValue v1 = SpatialIndexTestData.PARIS_GEOMETRY_WRAPPER.asNodeValue(); NodeValue v2 = SpatialIndexTestData.LONDON_GEOMETRY_WRAPPER.asNodeValue(); NodeValue v3 = NodeValue.makeString(Unit_URI.KILOMETER_URL); DistanceFF instance = new DistanceFF(); double expResult = 343.7713; double result = instance.exec(v1, v2, v3).getDouble(); assertEquals(expResult, result, 0.0001); }
@Test public void testExec_OSGB() { NodeValue v1 = NodeValue.makeNode("<http: NodeValue v2 = NodeValue.makeNode("<http: NodeValue v3 = NodeValue.makeString(Unit_URI.KILOMETER_URL); DistanceFF instance = new DistanceFF(); double expResult = 10; double result = instance.exec(v1, v2, v3).getDouble(); assertEquals(expResult, result, 0.0001); }
@Test(expected = ExprEvalException.class) public void testExec_pos0_fail() { NodeValue v1 = NodeValue.makeString("<http: NodeValue v2 = NodeValue.makeNode("<http: NodeValue v3 = NodeValue.makeNode(NodeFactory.createURI(Unit_URI.KILOMETER_URL)); DistanceFF instance = new DistanceFF(); NodeValue expResult = NodeValue.makeDouble(20); NodeValue result = instance.exec(v1, v2, v3); assertEquals(expResult, result); }
@Test(expected = ExprEvalException.class) public void testExec_pos1_fail() { NodeValue v1 = NodeValue.makeNode("<http: NodeValue v2 = NodeValue.makeString("<http: NodeValue v3 = NodeValue.makeNode(NodeFactory.createURI(Unit_URI.KILOMETER_URL)); DistanceFF instance = new DistanceFF(); NodeValue expResult = NodeValue.makeDouble(20); NodeValue result = instance.exec(v1, v2, v3); assertEquals(expResult, result); }
@Test(expected = ExprEvalException.class) public void testExec_pos2_fail() { NodeValue v1 = NodeValue.makeNode("<http: NodeValue v2 = NodeValue.makeNode("<http: NodeValue v3 = NodeValue.makeInteger(20); DistanceFF instance = new DistanceFF(); NodeValue expResult = NodeValue.makeDouble(20); NodeValue result = instance.exec(v1, v2, v3); assertEquals(expResult, result); }