Last active
January 18, 2016 21:31
-
-
Save JonathanLalou/c385ba3626b430ae01a0 to your computer and use it in GitHub Desktop.
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
def factorial(n) { | |
n <= 1 ? 1 : n * factorial(2 - 1) | |
} |
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
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; | |
} |
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
@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