Add more Leviathan functions (JENA-507) - cartesian() - 2D and 3D cartesian distance - sqrt() - Square root - ten() - Raise 10 to a power i.e. 10^arg
Project: http://git-wip-us.apache.org/repos/asf/jena/repo Commit: http://git-wip-us.apache.org/repos/asf/jena/commit/7cd9e4d7 Tree: http://git-wip-us.apache.org/repos/asf/jena/tree/7cd9e4d7 Diff: http://git-wip-us.apache.org/repos/asf/jena/diff/7cd9e4d7 Branch: refs/heads/JENA-507 Commit: 7cd9e4d757bbfd709b62f599e698a98e7549ab90 Parents: 94f8b89 Author: Rob Vesse <[email protected]> Authored: Thu Oct 9 14:33:38 2014 +0100 Committer: Rob Vesse <[email protected]> Committed: Thu Oct 9 14:33:38 2014 +0100 ---------------------------------------------------------------------- .../function/library/leviathan/cartesian.java | 44 ++++++++++++++++++++ .../sparql/function/library/leviathan/sqrt.java | 13 ++++++ .../sparql/function/library/leviathan/ten.java | 13 ++++++ .../sparql/expr/TestLeviathanFunctions.java | 35 ++++++++++++++++ 4 files changed, 105 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/jena/blob/7cd9e4d7/jena-arq/src/main/java/com/hp/hpl/jena/sparql/function/library/leviathan/cartesian.java ---------------------------------------------------------------------- diff --git a/jena-arq/src/main/java/com/hp/hpl/jena/sparql/function/library/leviathan/cartesian.java b/jena-arq/src/main/java/com/hp/hpl/jena/sparql/function/library/leviathan/cartesian.java new file mode 100644 index 0000000..65f8188 --- /dev/null +++ b/jena-arq/src/main/java/com/hp/hpl/jena/sparql/function/library/leviathan/cartesian.java @@ -0,0 +1,44 @@ +package com.hp.hpl.jena.sparql.function.library.leviathan; + +import java.util.List; + +import com.hp.hpl.jena.query.QueryBuildException; +import com.hp.hpl.jena.sparql.expr.ExprEvalException; +import com.hp.hpl.jena.sparql.expr.ExprList; +import com.hp.hpl.jena.sparql.expr.NodeValue; +import com.hp.hpl.jena.sparql.function.FunctionBase; +import com.hp.hpl.jena.sparql.util.Utils; + +public class cartesian extends FunctionBase { + + @Override + public NodeValue exec(List<NodeValue> args) { + if (args.size() != 4 && args.size() != 6) + throw new ExprEvalException("Incorrect number of arguments"); + + switch (args.size()) { + case 4: { + double dX = args.get(0).getDouble() - args.get(2).getDouble(); + double dY = args.get(1).getDouble() - args.get(3).getDouble(); + + return NodeValue.makeDouble(Math.sqrt(Math.pow(dX, 2) + Math.pow(dY, 2))); + } + case 6: { + double dX = args.get(0).getDouble() - args.get(3).getDouble(); + double dY = args.get(1).getDouble() - args.get(4).getDouble(); + double dZ = args.get(2).getDouble() - args.get(5).getDouble(); + + return NodeValue.makeDouble(Math.sqrt(Math.pow(dX, 2) + Math.pow(dY, 2) + Math.pow(dZ, 2))); + } + default: + throw new ExprEvalException("Incorrect number of arguments"); + } + } + + @Override + public void checkBuild(String uri, ExprList args) { + if (args.size() != 4 && args.size() != 6) + throw new QueryBuildException("Function '" + Utils.className(this) + "' takes 4 or 6 argument(s)"); + } + +} http://git-wip-us.apache.org/repos/asf/jena/blob/7cd9e4d7/jena-arq/src/main/java/com/hp/hpl/jena/sparql/function/library/leviathan/sqrt.java ---------------------------------------------------------------------- diff --git a/jena-arq/src/main/java/com/hp/hpl/jena/sparql/function/library/leviathan/sqrt.java b/jena-arq/src/main/java/com/hp/hpl/jena/sparql/function/library/leviathan/sqrt.java new file mode 100644 index 0000000..c2a80ec --- /dev/null +++ b/jena-arq/src/main/java/com/hp/hpl/jena/sparql/function/library/leviathan/sqrt.java @@ -0,0 +1,13 @@ +package com.hp.hpl.jena.sparql.function.library.leviathan; + +import com.hp.hpl.jena.sparql.expr.NodeValue; +import com.hp.hpl.jena.sparql.function.FunctionBase1; + +public class sqrt extends FunctionBase1 { + + @Override + public NodeValue exec(NodeValue v) { + return NodeValue.makeDouble(Math.sqrt(v.getDouble())); + } + +} http://git-wip-us.apache.org/repos/asf/jena/blob/7cd9e4d7/jena-arq/src/main/java/com/hp/hpl/jena/sparql/function/library/leviathan/ten.java ---------------------------------------------------------------------- diff --git a/jena-arq/src/main/java/com/hp/hpl/jena/sparql/function/library/leviathan/ten.java b/jena-arq/src/main/java/com/hp/hpl/jena/sparql/function/library/leviathan/ten.java new file mode 100644 index 0000000..51ab00d --- /dev/null +++ b/jena-arq/src/main/java/com/hp/hpl/jena/sparql/function/library/leviathan/ten.java @@ -0,0 +1,13 @@ +package com.hp.hpl.jena.sparql.function.library.leviathan; + +import com.hp.hpl.jena.sparql.expr.NodeValue; +import com.hp.hpl.jena.sparql.function.FunctionBase1; + +public class ten extends FunctionBase1 { + + @Override + public NodeValue exec(NodeValue v) { + return NodeValue.makeDouble(Math.pow(10d, v.getDouble())); + } + +} http://git-wip-us.apache.org/repos/asf/jena/blob/7cd9e4d7/jena-arq/src/test/java/com/hp/hpl/jena/sparql/expr/TestLeviathanFunctions.java ---------------------------------------------------------------------- diff --git a/jena-arq/src/test/java/com/hp/hpl/jena/sparql/expr/TestLeviathanFunctions.java b/jena-arq/src/test/java/com/hp/hpl/jena/sparql/expr/TestLeviathanFunctions.java index 427e08c..346cc48 100644 --- a/jena-arq/src/test/java/com/hp/hpl/jena/sparql/expr/TestLeviathanFunctions.java +++ b/jena-arq/src/test/java/com/hp/hpl/jena/sparql/expr/TestLeviathanFunctions.java @@ -193,6 +193,41 @@ public class TestLeviathanFunctions extends BaseTest { test("lfn:root(64,3)", "4"); } + @Test + public void sqrt_01() { + test("lfn:sqrt(4)", "2"); + } + + @Test + public void sqrt_02() { + test("lfn:sqrt(144)", "12"); + } + + @Test + public void cartesian_01() { + test("lfn:cartesian(0, 0, 0, 0)", "0"); + } + + @Test + public void cartesian_02() { + test("lfn:cartesian(0, 0, 3, 4)", "5"); + } + + @Test + public void cartesian_03() { + test("lfn:cartesian(0, 0, 0, 3, 4, 0)", "5"); + } + + @Test + public void cartesian_04() { + test("lfn:cartesian(0, 0, 0, 0, 3, 4)", "5"); + } + + @Test + public void cartesian_05() { + test("lfn:cartesian(0, 0, 0, 3, 0, 4)", "5"); + } + private static void test(String exprString, String result) { Node r = NodeFactoryExtra.parseNode(result); test(exprString, r);
