Skip to content

Commit

Permalink
test(script-engine): add test for PyTermFactory execution within the …
Browse files Browse the repository at this point in the history
…guest language (Python) using the polyglot api
  • Loading branch information
ashleycaselli committed Jan 5, 2024
1 parent b7b4200 commit bf5fff5
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions src/test/java/org/topbraid/shacl/TestGraalPyScriptEngine.java
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();
}

}

0 comments on commit bf5fff5

Please sign in to comment.