Search in sources :

Example 1 with GroovyScriptEngine

use of com.hortonworks.streamline.streams.runtime.script.engine.GroovyScriptEngine in project streamline by hortonworks.

the class GroovyScriptTest method testBindingsAreBoundOnlyWhenEvaluation.

@Test
public void testBindingsAreBoundOnlyWhenEvaluation() {
    GroovyScriptEngine groovyScriptEngine = new GroovyScriptEngine();
    String groovyExpression = "temperature > 10 && humidity < 30";
    GroovyScript<Boolean> groovyScript = new GroovyScript<Boolean>(groovyExpression, groovyScriptEngine);
    HashMap<String, Object> fieldsAndValue = new HashMap<>();
    fieldsAndValue.put("temperature", 20);
    fieldsAndValue.put("humidity", 10);
    try {
        assertTrue(groovyScript.evaluate(StreamlineEventImpl.builder().fieldsAndValues(fieldsAndValue).dataSourceId("1").build()));
    } catch (ScriptException e) {
        e.printStackTrace();
        Assert.fail("It shouldn't throw ScriptException");
    }
    fieldsAndValue.clear();
    fieldsAndValue.put("no_related_field", 3);
    try {
        groovyScript.evaluate(StreamlineEventImpl.builder().fieldsAndValues(fieldsAndValue).dataSourceId("1").build());
        // it means that previous bound variables are used now
        Assert.fail("It should not evaluate correctly");
    } catch (ScriptException e) {
    // no-op, that's what we want
    }
}
Also used : ScriptException(javax.script.ScriptException) GroovyScriptEngine(com.hortonworks.streamline.streams.runtime.script.engine.GroovyScriptEngine) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 2 with GroovyScriptEngine

use of com.hortonworks.streamline.streams.runtime.script.engine.GroovyScriptEngine in project streamline by hortonworks.

the class GroovyScriptNestedExprTest method testEvaluateNestedMapList.

@Test
public void testEvaluateNestedMapList() throws Exception {
    groovyScript = new GroovyScript<>("x < y['a'][0]", new GroovyScriptEngine());
    List<Integer> nestedList = new ArrayList<>();
    nestedList.add(5);
    nestedList.add(1);
    Map<String, Object> nestedMap = new HashMap<>();
    nestedMap.put("a", nestedList);
    Map<String, Object> kv = new HashMap<>();
    kv.put("x", 2);
    kv.put("y", nestedMap);
    StreamlineEvent event = StreamlineEventImpl.builder().fieldsAndValues(kv).dataSourceId("1").build();
    Boolean result = groovyScript.evaluate(event);
    System.out.println(result);
}
Also used : GroovyScriptEngine(com.hortonworks.streamline.streams.runtime.script.engine.GroovyScriptEngine) HashMap(java.util.HashMap) StreamlineEvent(com.hortonworks.streamline.streams.StreamlineEvent) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 3 with GroovyScriptEngine

use of com.hortonworks.streamline.streams.runtime.script.engine.GroovyScriptEngine in project streamline by hortonworks.

the class GroovyScriptNestedExprTest method testEvaluateNestedMap.

@Test
public void testEvaluateNestedMap() throws Exception {
    groovyScript = new GroovyScript<>("x < y['b']", new GroovyScriptEngine());
    Map<String, Object> nested = new HashMap<>();
    nested.put("a", 5);
    nested.put("b", 10);
    Map<String, Object> kv = new HashMap<>();
    kv.put("x", 2);
    kv.put("y", nested);
    StreamlineEvent event = StreamlineEventImpl.builder().fieldsAndValues(kv).dataSourceId("1").build();
    Boolean result = groovyScript.evaluate(event);
    System.out.println(result);
}
Also used : GroovyScriptEngine(com.hortonworks.streamline.streams.runtime.script.engine.GroovyScriptEngine) HashMap(java.util.HashMap) StreamlineEvent(com.hortonworks.streamline.streams.StreamlineEvent) Test(org.junit.Test)

Example 4 with GroovyScriptEngine

use of com.hortonworks.streamline.streams.runtime.script.engine.GroovyScriptEngine in project streamline by hortonworks.

the class GroovyScriptNestedExprTest method testEvaluateNestedList.

@Test
public void testEvaluateNestedList() throws Exception {
    groovyScript = new GroovyScript<>("x < y[0]", new GroovyScriptEngine());
    List<Integer> nested = new ArrayList<>();
    nested.add(5);
    nested.add(1);
    Map<String, Object> kv = new HashMap<>();
    kv.put("x", 2);
    kv.put("y", nested);
    StreamlineEvent event = StreamlineEventImpl.builder().fieldsAndValues(kv).dataSourceId("1").build();
    Boolean result = groovyScript.evaluate(event);
    System.out.println(result);
}
Also used : GroovyScriptEngine(com.hortonworks.streamline.streams.runtime.script.engine.GroovyScriptEngine) HashMap(java.util.HashMap) StreamlineEvent(com.hortonworks.streamline.streams.StreamlineEvent) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 5 with GroovyScriptEngine

use of com.hortonworks.streamline.streams.runtime.script.engine.GroovyScriptEngine in project streamline by hortonworks.

the class GroovyScriptTest method testGroovyScriptEnsuresThreadSafe.

@Test
public void testGroovyScriptEnsuresThreadSafe() throws InterruptedException {
    GroovyScriptEngine groovyScriptEngine = new GroovyScriptEngine();
    String groovyExpression = "a % 2 == 0";
    final GroovyScript<Boolean> groovyScript = new GroovyScript<>(groovyExpression, groovyScriptEngine);
    final AtomicInteger index = new AtomicInteger(0);
    List<Thread> threads = new ArrayList<>();
    final AtomicReference<Throwable> anyException = new AtomicReference<>();
    for (int i = 0; i < 500; i++) {
        Thread t = new Thread(new Runnable() {

            @Override
            public void run() {
                for (int j = 0; j < 10; j++) {
                    try {
                        Thread.sleep(new Random().nextInt(10));
                    } catch (InterruptedException e) {
                    // no-op
                    }
                    int aVal = index.getAndIncrement();
                    HashMap<String, Object> fieldsAndValue = new HashMap<>();
                    fieldsAndValue.put("a", aVal);
                    try {
                        assertEquals(aVal % 2 == 0, groovyScript.evaluate(StreamlineEventImpl.builder().fieldsAndValues(fieldsAndValue).dataSourceId("1").build()));
                    } catch (Throwable e) {
                        e.printStackTrace();
                        anyException.set(e);
                    }
                }
            }
        });
        threads.add(t);
        t.start();
    }
    for (Thread t : threads) {
        t.join();
    }
    if (anyException.get() != null) {
        Assert.fail("Exception occurred within thread, first one is " + anyException.get().getMessage());
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) AtomicReference(java.util.concurrent.atomic.AtomicReference) GroovyScriptEngine(com.hortonworks.streamline.streams.runtime.script.engine.GroovyScriptEngine) Random(java.util.Random) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.Test)

Aggregations

GroovyScriptEngine (com.hortonworks.streamline.streams.runtime.script.engine.GroovyScriptEngine)6 HashMap (java.util.HashMap)5 Test (org.junit.Test)5 StreamlineEvent (com.hortonworks.streamline.streams.StreamlineEvent)4 ArrayList (java.util.ArrayList)3 GroovyExpression (com.hortonworks.streamline.streams.runtime.rule.condition.expression.GroovyExpression)1 GroovyScript (com.hortonworks.streamline.streams.runtime.script.GroovyScript)1 Collection (java.util.Collection)1 Random (java.util.Random)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 ScriptException (javax.script.ScriptException)1