-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(script-engine): add test for PyTermFactory execution within the …
…guest language (Python) using the polyglot api
- Loading branch information
1 parent
b7b4200
commit bf5fff5
Showing
1 changed file
with
19 additions
and
6 deletions.
There are no files selected for viewing
25 changes: 19 additions & 6 deletions
25
src/test/java/org/topbraid/shacl/TestGraalPyScriptEngine.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,35 @@ | ||
package org.topbraid.shacl; | ||
|
||
import org.graalvm.polyglot.Context; | ||
import org.graalvm.polyglot.Value; | ||
import org.junit.Test; | ||
import org.topbraid.shacl.py.GraalPyScriptEngine; | ||
import org.topbraid.shacl.py.PyScriptEngine; | ||
import org.topbraid.shacl.py.PyScriptEngineFactory; | ||
import org.topbraid.shacl.py.model.PyLiteral; | ||
import org.topbraid.shacl.py.model.PyTermFactory; | ||
|
||
import javax.script.ScriptException; | ||
import java.util.Objects; | ||
|
||
public class TestGraalPyScriptEngine { | ||
|
||
private static final String VALUE = "testEngine"; | ||
|
||
private static final String LANG = "en"; | ||
|
||
@Test | ||
public void testEngine() throws ScriptException { | ||
public void testEngine() { | ||
PyScriptEngine engine = PyScriptEngineFactory.get().createScriptEngine("Graal"); | ||
Value value = (Value) engine.eval("[1,2,42,4]"); | ||
int element = value.getArrayElement(2).asInt(); | ||
assert Objects.equals(element, 42); | ||
// TODO add test with TermFactory | ||
Context context = ((GraalPyScriptEngine) engine).getContext(); | ||
context.getPolyglotBindings().putMember("PyTermFactory", new PyTermFactory()); | ||
Value value = context.eval("python", | ||
"import polyglot \n" + | ||
"py_tf = polyglot.import_value('PyTermFactory')\n" + | ||
"py_tf.literal(\"" + VALUE + "\", \"" + LANG + "\")"); | ||
System.out.println(value.toString()); | ||
PyLiteral tf = new PyTermFactory().literal(VALUE, LANG); | ||
assert Objects.equals(value.toString(), tf.toString()); | ||
context.close(); | ||
} | ||
|
||
} |