PlanoutDSLCompiler { @SuppressWarnings("unchecked") public static Map<String, ?> dsl_to_json(final String dsl) throws ValidationException { checkArgument(StringUtils.isNotEmpty(dsl), "dsl text is null or empty"); final ScriptEngine engine = new ScriptEngineManager().getEngineByName("JavaScript"); engine.put("input", dsl); try { engine.eval(script); final Object json = engine.get("output"); checkState(json instanceof Map, "Expected compiled object to be an instance of Map, but it is %s", Helper.getClassName(json)); return Helper.deepCopy((Map<String, ?>)json, JSCollectionDetector.get()); } catch (Exception e) { throw new ValidationException("Failed to compile DSL:\n" + dsl, e); } }  @SuppressWarnings("unchecked") static Map<String, ?> dsl_to_json(final String dsl);  }
@Test public void test_dsl_to_json() throws Exception { Map<String, ?> tests = ImmutableMap.of( "x = 2;", ImmutableMap.of("op", "set", "var", "x", "value", 2.0), "return bernoulliTrial(0.1)", ImmutableMap.of("op", "return", "value", ImmutableMap.of("value", 0.1, "op", "bernoulliTrial")) ); for (String input : tests.keySet()) { Map output = PlanoutDSLCompiler.dsl_to_json(input); assertEquals("seq", output.get("op")); List seq = Helper.cast(output.get("seq")); assertEquals(1, seq.size()); assertEquals(tests.get(input), seq.get(0)); } }