Search in sources :

Example 1 with JavaScriptOperator

use of org.apache.apex.malhar.lib.script.JavaScriptOperator in project apex-malhar by apache.

the class ApplicationWithScript method populateDAG.

@Override
public void populateDAG(DAG dag, Configuration conf) {
    int maxValue = 30000;
    RandomEventGenerator rand = dag.addOperator("rand", new RandomEventGenerator());
    rand.setMinvalue(0);
    rand.setMaxvalue(maxValue);
    RoundRobinHashMap<String, Object> rrhm = dag.addOperator("rrhm", new RoundRobinHashMap<String, Object>());
    rrhm.setKeys(new String[] { "x", "y" });
    JavaScriptOperator calc = dag.addOperator("picalc", new JavaScriptOperator());
    calc.setPassThru(false);
    calc.put("i", 0);
    calc.put("count", 0);
    calc.addSetupScript("function pi() { if (x*x+y*y <= " + maxValue * maxValue + ") { i++; } count++; return i / count * 4; }");
    calc.setInvoke("pi");
    dag.addStream("rand_rrhm", rand.integer_data, rrhm.data);
    dag.addStream("rrhm_calc", rrhm.map, calc.inBindings);
    ConsoleOutputOperator console = dag.addOperator("console", new ConsoleOutputOperator());
    dag.addStream("rand_console", calc.result, console.input);
}
Also used : ConsoleOutputOperator(org.apache.apex.malhar.lib.io.ConsoleOutputOperator) RandomEventGenerator(org.apache.apex.malhar.lib.testbench.RandomEventGenerator) JavaScriptOperator(org.apache.apex.malhar.lib.script.JavaScriptOperator)

Aggregations

ConsoleOutputOperator (org.apache.apex.malhar.lib.io.ConsoleOutputOperator)1 JavaScriptOperator (org.apache.apex.malhar.lib.script.JavaScriptOperator)1 RandomEventGenerator (org.apache.apex.malhar.lib.testbench.RandomEventGenerator)1