GreatCircleFF extends FunctionBase5 { @Override public NodeValue exec(NodeValue v1, NodeValue v2, NodeValue v3, NodeValue v4, NodeValue v5) { try { if (!v1.isNumber()) { throw new ExprEvalException("Not a Number: " + FmtUtils.stringForNode(v1.asNode())); } if (!v2.isNumber()) { throw new ExprEvalException("Not a Number: " + FmtUtils.stringForNode(v2.asNode())); } if (!v3.isNumber()) { throw new ExprEvalException("Not a Number: " + FmtUtils.stringForNode(v3.asNode())); } if (!v4.isNumber()) { throw new ExprEvalException("Not a Number: " + FmtUtils.stringForNode(v4.asNode())); } if (!(v5.isIRI() || v5.isString())) { throw new ExprEvalException("Not an IRI or String: " + FmtUtils.stringForNode(v5.asNode())); } double lat1 = v1.getDouble(); double lon1 = v2.getDouble(); double lat2 = v3.getDouble(); double lon2 = v4.getDouble(); String unitsURI; if (v5.isIRI()) { unitsURI = v5.asNode().getURI(); } else { unitsURI = v5.asString(); } double distanceMetres = GreatCircleDistance.haversineFormula(lat1, lon1, lat2, lon2); Boolean isUnitsLinear = UnitsRegistry.isLinearUnits(unitsURI); double distance = UnitsOfMeasure.convertBetween(distanceMetres, Unit_URI.METRE_URL, unitsURI, isUnitsLinear, lat1); return NodeValue.makeDouble(distance); } catch (DatatypeFormatException ex) { throw new ExprEvalException(ex.getMessage(), ex); } }  @Override NodeValue exec(NodeValue v1, NodeValue v2, NodeValue v3, NodeValue v4, NodeValue v5);  }
@Test public void testExec() { NodeValue v1 = NodeValue.makeDouble(10.0); NodeValue v2 = NodeValue.makeDouble(20.0); NodeValue v3 = NodeValue.makeDouble(10.0); NodeValue v4 = NodeValue.makeDouble(21.0); NodeValue v5 = NodeValue.makeNode(NodeFactory.createURI(Unit_URI.KILOMETER_URL)); GreatCircleFF instance = new GreatCircleFF(); double expResult = 109.5057; double result = instance.exec(v1, v2, v3, v4, v5).getDouble(); assertEquals(expResult, result, 0.0001); }
@Test public void testExec2() { NodeValue v1 = NodeValue.makeDouble(10.0); NodeValue v2 = NodeValue.makeDouble(20.0); NodeValue v3 = NodeValue.makeDouble(11.0); NodeValue v4 = NodeValue.makeDouble(20.0); NodeValue v5 = NodeValue.makeString(Unit_URI.KILOMETER_URL); GreatCircleFF instance = new GreatCircleFF(); double expResult = 111.1950; double result = instance.exec(v1, v2, v3, v4, v5).getDouble(); assertEquals(expResult, result, 0.0001); }
@Test public void testExec_Paris_London() { NodeValue v1 = NodeValue.makeDouble(48.85341); NodeValue v2 = NodeValue.makeDouble(2.34880); NodeValue v3 = NodeValue.makeDouble(51.50853); NodeValue v4 = NodeValue.makeDouble(-0.12574); NodeValue v5 = NodeValue.makeString(Unit_URI.KILOMETER_URL); GreatCircleFF instance = new GreatCircleFF(); double expResult = 343.7713; double result = instance.exec(v1, v2, v3, v4, v5).getDouble(); assertEquals(expResult, result, 0.0001); }
@Test(expected = ExprEvalException.class) public void testExec_pos0_fail() { NodeValue v1 = NodeValue.makeString("10.0"); NodeValue v2 = NodeValue.makeDouble(20.0); NodeValue v3 = NodeValue.makeDouble(10.0); NodeValue v4 = NodeValue.makeDouble(20.0001); NodeValue v5 = NodeValue.makeNode(NodeFactory.createURI(Unit_URI.KILOMETER_URL)); GreatCircleFF instance = new GreatCircleFF(); NodeValue expResult = NodeValue.makeDouble(20); NodeValue result = instance.exec(v1, v2, v3, v4, v5); assertEquals(expResult, result); }
@Test(expected = ExprEvalException.class) public void testExec_pos1_fail() { NodeValue v1 = NodeValue.makeDouble(10.0); NodeValue v2 = NodeValue.makeString("20.0"); NodeValue v3 = NodeValue.makeDouble(10.0); NodeValue v4 = NodeValue.makeDouble(20.0001); NodeValue v5 = NodeValue.makeNode(NodeFactory.createURI(Unit_URI.KILOMETER_URL)); GreatCircleFF instance = new GreatCircleFF(); NodeValue expResult = NodeValue.makeDouble(20); NodeValue result = instance.exec(v1, v2, v3, v4, v5); assertEquals(expResult, result); }
@Test(expected = ExprEvalException.class) public void testExec_pos2_fail() { NodeValue v1 = NodeValue.makeDouble(10.0); NodeValue v2 = NodeValue.makeDouble(20.0); NodeValue v3 = NodeValue.makeString("10.0"); NodeValue v4 = NodeValue.makeDouble(20.0001); NodeValue v5 = NodeValue.makeNode(NodeFactory.createURI(Unit_URI.KILOMETER_URL)); GreatCircleFF instance = new GreatCircleFF(); NodeValue expResult = NodeValue.makeDouble(20); NodeValue result = instance.exec(v1, v2, v3, v4, v5); assertEquals(expResult, result); }
@Test(expected = ExprEvalException.class) public void testExec_pos3_fail() { NodeValue v1 = NodeValue.makeDouble(10.0); NodeValue v2 = NodeValue.makeDouble(20.0); NodeValue v3 = NodeValue.makeDouble(10.0); NodeValue v4 = NodeValue.makeString("20.0001"); NodeValue v5 = NodeValue.makeNode(NodeFactory.createURI(Unit_URI.KILOMETER_URL)); GreatCircleFF instance = new GreatCircleFF(); NodeValue expResult = NodeValue.makeDouble(20); NodeValue result = instance.exec(v1, v2, v3, v4, v5); assertEquals(expResult, result); }
@Test(expected = ExprEvalException.class) public void testExec_pos4_fail() { NodeValue v1 = NodeValue.makeDouble(10.0); NodeValue v2 = NodeValue.makeDouble(20.0); NodeValue v3 = NodeValue.makeDouble(10.0); NodeValue v4 = NodeValue.makeDouble(20.0001); NodeValue v5 = NodeValue.makeInteger(20); GreatCircleFF instance = new GreatCircleFF(); NodeValue expResult = NodeValue.makeDouble(20); NodeValue result = instance.exec(v1, v2, v3, v4, v5); assertEquals(expResult, result); }