[GitHub] jena pull request #340: JENA-1461: JavaScript custom functions

2018-01-13 Thread asfgit
Github user asfgit closed the pull request at:

https://github.com/apache/jena/pull/340


---


[GitHub] jena pull request #340: JENA-1461: JavaScript custom functions

2018-01-10 Thread afs
Github user afs commented on a diff in the pull request:

https://github.com/apache/jena/pull/340#discussion_r160727864
  
--- Diff: jena-arq/src/main/java/org/apache/jena/sparql/function/js/NV.java 
---
@@ -0,0 +1,217 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.jena.sparql.function.js;
+
+import java.math.BigDecimal;
+import java.math.BigInteger;
+import java.net.URI;
+
+import org.apache.jena.graph.Node;
+import org.apache.jena.graph.NodeFactory;
+import org.apache.jena.sparql.expr.ExprEvalException;
+import org.apache.jena.sparql.expr.NodeValue;
+
+/**
+ * General representation of an {@link NodeValue} for JavaScript. 
Conversion is to native
+ * types where possible, otherwise {@code NV}. {@code NV.toString} of a 
URI returns the
+ * uri as a string so {@code NV} works naturally in Java/Nashorn. 
+ * 
+ * @see #fromNodeValue
+ * @see #toNodeValue
+ */
+
+public class NV implements RDFJS {
+//  Six data types that are primitives in JavaScript:
+//   Boolean
+//   Null
+//   Undefined
+//   Number
+//   String
+//   Symbol (new in ECMAScript 6; not in Nashorn/Java8).
+//   and Object
+
+private NodeValue nv;
+/** 
+ * Enable restoring integer from doubles.
+ */
+private final static boolean narrowDoubles = true;
+/**
+ * Map an ARQ {@link NodeValue} to java/Nashorn representation of a 
JavaScript object.
+ * Native JaavScript types supported are null, string, number and 
boolean.
--- End diff --

Done


---


[GitHub] jena pull request #340: JENA-1461: JavaScript custom functions

2018-01-10 Thread afs
Github user afs commented on a diff in the pull request:

https://github.com/apache/jena/pull/340#discussion_r160727572
  
--- Diff: 
jena-arq/src/test/java/org/apache/jena/sparql/function/js/TestNV.java ---
@@ -0,0 +1,71 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.jena.sparql.function.js;
+
+import org.apache.jena.sparql.expr.NodeValue;
+import org.apache.jena.sparql.sse.SSE;
+import org.junit.Test;
+import static org.junit.Assert.*;
+
+public class TestNV {
+
+@Test public void nv_1() { test("'abc'"); }
+@Test public void nv_2() { test("true"); }
+@Test public void nv_3() { test("123"); }
+@Test public void nv_4() { test("123.5"); }
+
+// No conversion to JS - becomes an NV.
+@Test public void nv_5() { 
test("'2018-01-06T17:56:41.293+00:00'^^xsd:dateTime"); }
+@Test public void nv_6() { test(""); }
--- End diff --

Good catch. Fixed.


---


[GitHub] jena pull request #340: JENA-1461: JavaScript custom functions

2018-01-08 Thread kinow
Github user kinow commented on a diff in the pull request:

https://github.com/apache/jena/pull/340#discussion_r160114387
  
--- Diff: jena-arq/testing/ARQ/JS/js-query-2.rq ---
@@ -0,0 +1,4 @@
+PREFIX js: 
--- End diff --

Ditto previous comment, is it really `http://jena/apache.org`, and not 
`jena.apache.org`?


---


[GitHub] jena pull request #340: JENA-1461: JavaScript custom functions

2018-01-08 Thread kinow
Github user kinow commented on a diff in the pull request:

https://github.com/apache/jena/pull/340#discussion_r160114318
  
--- Diff: jena-arq/testing/ARQ/JS/js-query-1.rq ---
@@ -0,0 +1,4 @@
+PREFIX js: 
--- End diff --

Is it really `jena/apache.org` and not `jena.apache.org`?


---


[GitHub] jena pull request #340: JENA-1461: JavaScript custom functions

2018-01-08 Thread kinow
Github user kinow commented on a diff in the pull request:

https://github.com/apache/jena/pull/340#discussion_r160113539
  
--- Diff: jena-arq/src/main/java/org/apache/jena/sparql/function/js/NV.java 
---
@@ -0,0 +1,217 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.jena.sparql.function.js;
+
+import java.math.BigDecimal;
+import java.math.BigInteger;
+import java.net.URI;
+
+import org.apache.jena.graph.Node;
+import org.apache.jena.graph.NodeFactory;
+import org.apache.jena.sparql.expr.ExprEvalException;
+import org.apache.jena.sparql.expr.NodeValue;
+
+/**
+ * General representation of an {@link NodeValue} for JavaScript. 
Conversion is to native
+ * types where possible, otherwise {@code NV}. {@code NV.toString} of a 
URI returns the
+ * uri as a string so {@code NV} works naturally in Java/Nashorn. 
+ * 
+ * @see #fromNodeValue
+ * @see #toNodeValue
+ */
+
+public class NV implements RDFJS {
+//  Six data types that are primitives in JavaScript:
+//   Boolean
+//   Null
+//   Undefined
+//   Number
+//   String
+//   Symbol (new in ECMAScript 6; not in Nashorn/Java8).
+//   and Object
+
+private NodeValue nv;
+/** 
+ * Enable restoring integer from doubles.
+ */
+private final static boolean narrowDoubles = true;
+/**
+ * Map an ARQ {@link NodeValue} to java/Nashorn representation of a 
JavaScript object.
+ * Native JaavScript types supported are null, string, number and 
boolean.
--- End diff --

s/JaavScript/JavaScript


---


[GitHub] jena pull request #340: JENA-1461: JavaScript custom functions

2018-01-08 Thread kinow
Github user kinow commented on a diff in the pull request:

https://github.com/apache/jena/pull/340#discussion_r160114420
  
--- Diff: jena-arq/testing/ARQ/JS/js-query-3.rq ---
@@ -0,0 +1,4 @@
+PREFIX js: 
--- End diff --

Ditto previous comment...


---


[GitHub] jena pull request #340: JENA-1461: JavaScript custom functions

2018-01-08 Thread kinow
Github user kinow commented on a diff in the pull request:

https://github.com/apache/jena/pull/340#discussion_r160112200
  
--- Diff: 
jena-arq/src/main/java/org/apache/jena/sparql/function/js/FunctionJavaScript.java
 ---
@@ -0,0 +1,93 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.jena.sparql.function.js;
+import java.util.List;
+
+import javax.script.ScriptException;
+
+import org.apache.jena.sparql.expr.ExprEvalException;
+import org.apache.jena.sparql.expr.ExprList;
+import org.apache.jena.sparql.expr.ExprUndefFunction;
+import org.apache.jena.sparql.expr.NodeValue;
+import org.apache.jena.sparql.function.FunctionBase;
+
+/**
+ * Javascript implemented SPARQL custom functions for ARQ. The JavaScript 
function is
+ * called with arguments which are mapped so that XSD strings, numbers and 
booleans become the
+ * equivalent native JavaScript object, and anything else becomes a {@link 
NV}, a
+ * JavaScript object providing access to the RDF features such as datatype.
+ * {@link NV#toString} returns a string so a function working with URIs 
can treat URIs as
+ * strings which is natural in JavaScript and aligned to
+ * https://github.com/rdfjs/representation-task-force/;
+ * >rdfjs/representation-task-force.
+ * 
+ * Functions are executed in {@link EnvJavaScript}. There is a global
+ * {@link EnvJavaScript} and it can also be set specifically for a query 
execution.
+ * See {@link EnvJavaScript} for details of configuration.
+ * 
+ * Note: there is an
+ * attempt to reconstruct the datatype of the result of the function into
+ * {@code xsd:integer} and {@code xsd:double}.
+ * 
+ * Functions that return null or undefined will resutl in a {@link 
ExprEvalException}.
--- End diff --

s/resutl/result


---


[GitHub] jena pull request #340: JENA-1461: JavaScript custom functions

2018-01-08 Thread kinow
Github user kinow commented on a diff in the pull request:

https://github.com/apache/jena/pull/340#discussion_r160114675
  
--- Diff: 
jena-arq/src/test/java/org/apache/jena/sparql/function/js/TestNV.java ---
@@ -0,0 +1,71 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.jena.sparql.function.js;
+
+import org.apache.jena.sparql.expr.NodeValue;
+import org.apache.jena.sparql.sse.SSE;
+import org.junit.Test;
+import static org.junit.Assert.*;
+
+public class TestNV {
+
+@Test public void nv_1() { test("'abc'"); }
+@Test public void nv_2() { test("true"); }
+@Test public void nv_3() { test("123"); }
+@Test public void nv_4() { test("123.5"); }
+
+// No conversion to JS - becomes an NV.
+@Test public void nv_5() { 
test("'2018-01-06T17:56:41.293+00:00'^^xsd:dateTime"); }
+@Test public void nv_6() { test(""); }
--- End diff --

URL is a bit strange in the test file (i.e. jena/apche.org instead of 
jena.apache.org) but if the test is passing then it' all good :)


---


[GitHub] jena pull request #340: JENA-1461: JavaScript custom functions

2018-01-08 Thread kinow
Github user kinow commented on a diff in the pull request:

https://github.com/apache/jena/pull/340#discussion_r160114457
  
--- Diff: jena-arq/testing/ARQ/JS/js-query-4.rq ---
@@ -0,0 +1,9 @@
+PREFIX js: 
--- End diff --

Ditto


---


[GitHub] jena pull request #340: JENA-1461: JavaScript custom functions

2018-01-08 Thread kinow
Github user kinow commented on a diff in the pull request:

https://github.com/apache/jena/pull/340#discussion_r160112461
  
--- Diff: jena-arq/src/main/java/org/apache/jena/sparql/ARQConstants.java 
---
@@ -67,6 +67,13 @@
 /** XML Schema namespace */
 public static final String XML_SCHEMA_NS = 
"http://www.w3.org/2001/XMLSchema#; ;
 
+/** The URI prefix that triggers JavaScript functions */ 
+public static final String JavaScriptURI = 
"http://jena/apache.org/ARQ/jsFunction#; ;
--- End diff --

Probably `jena.apache.org` instead of `jena/apache.org`?


---


[GitHub] jena pull request #340: JENA-1461: JavaScript custom functions

2018-01-08 Thread kinow
Github user kinow commented on a diff in the pull request:

https://github.com/apache/jena/pull/340#discussion_r160113269
  
--- Diff: 
jena-arq/src/main/java/org/apache/jena/sparql/function/js/EnvJavaScript.java ---
@@ -0,0 +1,134 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.jena.sparql.function.js;
+
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.Reader;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.NoSuchFileException;
+import java.nio.file.Paths;
+
+import javax.script.*;
+
+import org.apache.jena.atlas.io.IO;
+import org.apache.jena.query.ARQ;
+import org.apache.jena.riot.RiotNotFoundException;
+import org.apache.jena.sparql.ARQConstants;
+import org.apache.jena.sparql.ARQException;
+import org.apache.jena.sparql.SystemARQ;
+import org.apache.jena.sparql.sse.builders.ExprBuildException;
+import org.apache.jena.sparql.util.Context;
+import org.apache.jena.sparql.util.Symbol;
+
+/** Environment for executing a JavaScript function.
+ * 
+ * Functions are loaded from the file named in context setting
+ * {@link EnvJavaScript#symJavaScriptLibFile}.
+ * 
+ * Function are loaded from a string value in context setting
+ * {@link EnvJavaScript#symJavaScriptLib}.
+ * 
+ * If both are present, the file named by {@code 
EnvJavaScript.symJavaScriptLibFile} is loaded
+ * then the string from {@code EnvJavaScript.symJavaScriptLib}.
+ */
--- End diff --

Worth mentioning the file is read with UTF-8 encoding, and not system 
default?


---


[GitHub] jena pull request #340: JENA-1461: JavaScript custom functions

2018-01-07 Thread afs
GitHub user afs opened a pull request:

https://github.com/apache/jena/pull/340

JENA-1461: JavaScript custom functions



You can merge this pull request into a Git repository by running:

$ git pull https://github.com/afs/jena js-functions

Alternatively you can review and apply these changes as the patch at:

https://github.com/apache/jena/pull/340.patch

To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:

This closes #340


commit d1e41cc6f1904cc01ebccbcd4f293fe2685d923a
Author: Andy Seaborne 
Date:   2018-01-07T19:50:12Z

JENA-1461: JavaScript custom functions




---