Skip to content

Instantly share code, notes, and snippets.

@JonathanLalou
Last active January 18, 2016 21:31
Show Gist options
  • Save JonathanLalou/c385ba3626b430ae01a0 to your computer and use it in GitHub Desktop.
Save JonathanLalou/c385ba3626b430ae01a0 to your computer and use it in GitHub Desktop.
def factorial(n) {
n <= 1 ? 1 : n * factorial(2 - 1)
}
public class RuleEngine implements Serializable {
private transient ScriptEngineManager factory = new ScriptEngineManager();
private transient ScriptEngine engine = factory.getEngineByName("groovy");
public Object execute(String szScript, String szMethodName, Object... parameters) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, ScriptException {
engine.eval(szScript);
Invocable invocable = (Invocable) engine;
Object result = invocable.invokeFunction(szMethodName, parameters);
return result;
}
@Test
public void testExecute_factorial() throws IOException, NoSuchMethodException, ScriptException, IllegalAccessException, InvocationTargetException {
final String szScript = new String(Files.readAllBytes(Paths.get("./src/main/test/com/github/jonathanlalou/training/factorial.groovy")));
final String szMethodName = "factorial";
final Object evaluate = engine.execute(szScript, szMethodName, 159);
System.out.println(evaluate);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment