@Test
  public void testProcess() throws Exception {
    logger.info("HexFunctionExtension TestCase");

    siddhiManager = new SiddhiManager();
    String inValueStream = "@config(async = 'true')define stream InValueStream (inValue string);";

    String eventFuseExecutionPlan =
        ("@info(name = 'query1') from InValueStream "
            + "select str:hex(inValue) as hexString "
            + "insert into OutMediationStream;");
    ExecutionPlanRuntime executionPlanRuntime =
        siddhiManager.createExecutionPlanRuntime(inValueStream + eventFuseExecutionPlan);

    executionPlanRuntime.addCallback(
        "query1",
        new QueryCallback() {
          @Override
          public void receive(long timeStamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timeStamp, inEvents, removeEvents);
            String result;
            for (Event event : inEvents) {
              count.incrementAndGet();
              result = (String) event.getData(0);
              Assert.assertEquals("4d7953514c", result);
            }
          }
        });
    InputHandler inputHandler = executionPlanRuntime.getInputHandler("InValueStream");
    executionPlanRuntime.start();
    inputHandler.send(new Object[] {"MySQL"});
    SiddhiTestHelper.waitForEvents(1000, 1, count, 60000);
    executionPlanRuntime.shutdown();
  }
  @Test
  public void testProcess() throws Exception {
    logger.info("AsinFunctionExtension TestCase");

    siddhiManager = new SiddhiManager();
    String inValueStream = "define stream InValueStream (inValue double);";

    String eventFuseExecutionPlan =
        ("@info(name = 'query1') from InValueStream "
            + "select math:asin(inValue) as asinValue "
            + "insert into OutMediationStream;");
    ExecutionPlanRuntime executionPlanRuntime =
        siddhiManager.createExecutionPlanRuntime(inValueStream + eventFuseExecutionPlan);

    executionPlanRuntime.addCallback(
        "query1",
        new QueryCallback() {
          @Override
          public void receive(long timeStamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timeStamp, inEvents, removeEvents);
            Double results;
            for (Event event : inEvents) {
              results = (Double) event.getData(0);
              Assert.assertEquals((Double) 0.6435011087932844, results);
            }
          }
        });
    InputHandler inputHandler = executionPlanRuntime.getInputHandler("InValueStream");
    executionPlanRuntime.start();
    inputHandler.send(new Object[] {0.6d});
    Thread.sleep(100);
    executionPlanRuntime.shutdown();
  }
  @Test
  public void testProcess() throws Exception {
    logger.info("OctalFunctionExtension TestCase");

    siddhiManager = new SiddhiManager();
    String inValueStream = "@config(async = 'true')define stream InValueStream (inValue long);";

    String eventFuseExecutionPlan =
        ("@info(name = 'query1') from InValueStream "
            + "select math:oct(inValue) as octValue "
            + "insert into OutMediationStream;");
    ExecutionPlanRuntime executionPlanRuntime =
        siddhiManager.createExecutionPlanRuntime(inValueStream + eventFuseExecutionPlan);

    executionPlanRuntime.addCallback(
        "query1",
        new QueryCallback() {
          @Override
          public void receive(long timeStamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timeStamp, inEvents, removeEvents);
            String result;
            for (Event event : inEvents) {
              result = (String) event.getData(0);
              Assert.assertEquals("143", result);
            }
          }
        });
    InputHandler inputHandler = executionPlanRuntime.getInputHandler("InValueStream");
    executionPlanRuntime.start();
    inputHandler.send(new Object[] {99l});
    Thread.sleep(100);
    executionPlanRuntime.shutdown();
  }
Beispiel #4
0
  @Test(expected = ExecutionPlanCreationException.class)
  public void testJavaScriptCompilationFailure() throws InterruptedException {

    log.info("testJavaScriptCompilationFailure");

    SiddhiManager siddhiManager = new SiddhiManager();
    siddhiManager.setExtension(
        "evalscript:javascript", org.wso2.siddhi.extension.evalscript.EvalJavaScript.class);
    siddhiManager.setExtension(
        "evalscript:scala", org.wso2.siddhi.extension.evalscript.EvalScala.class);

    String concatFunc =
        "define function concatJ[JavaScript] return string {\n"
            + "  var str1 = data[0;\n"
            + "  var str2 = data[1];\n"
            + "  var str3 = data[2];\n"
            + "  var res = str1.concat(str2,str3);\n"
            + "  return res;\n"
            + "};";

    ExecutionPlanRuntime executionPlanRuntime =
        siddhiManager.createExecutionPlanRuntime(concatFunc);

    executionPlanRuntime.shutdown();
  }
Beispiel #5
0
  @Test(expected = DuplicateDefinitionException.class)
  public void testDefineFunctionsWithSameFunctionID() throws InterruptedException {

    log.info("testDefineFunctionsWithSameFunctionID");

    SiddhiManager siddhiManager = new SiddhiManager();
    siddhiManager.setExtension(
        "evalscript:javascript", org.wso2.siddhi.extension.evalscript.EvalJavaScript.class);
    siddhiManager.setExtension(
        "evalscript:scala", org.wso2.siddhi.extension.evalscript.EvalScala.class);

    String concatFunc1 =
        "define function concat[Scala] return string {\n"
            + "  var concatenatedString = \"\"\n"
            + "  for(i <- 0 until data.length) {\n"
            + "     concatenatedString += data(i).toString\n"
            + "  }\n"
            + "  concatenatedString\n"
            + "};";

    String concatFunc2 =
        "define function concat[JavaScript] return string {\n"
            + "  var str1 = data[0];\n"
            + "  var str2 = data[1];\n"
            + "  var str3 = data[2];\n"
            + "  var res = str1.concat(str2,str3);\n"
            + "  return res;\n"
            + "};\n";
    ExecutionPlanRuntime executionPlanRuntime =
        siddhiManager.createExecutionPlanRuntime(concatFunc1 + concatFunc2);
    executionPlanRuntime.shutdown();
  }
Beispiel #6
0
  @Test
  public void testReturnType() throws InterruptedException {

    log.info("testReturnType");

    SiddhiManager siddhiManager = new SiddhiManager();
    siddhiManager.setExtension(
        "evalscript:javascript", org.wso2.siddhi.extension.evalscript.EvalJavaScript.class);
    siddhiManager.setExtension(
        "evalscript:scala", org.wso2.siddhi.extension.evalscript.EvalScala.class);

    String toFloatSFunc =
        "define function toFloatS[Scala] return float {\n"
            + "   data(0).asInstanceOf[String].toFloat\n"
            + "};\n";

    String cseEventStream =
        "@config(async = 'true')define stream cseEventStream (symbol string, price string, volume long);\n";

    String query1 =
        ("@info(name = 'query1') from cseEventStream select price , toFloatS(price) as priceF insert into mailto1;\n");
    String query2 =
        ("@info(name = 'query2') from mailto1 select priceF/2 as newPrice insert into mailto2;\n");

    ExecutionPlanRuntime executionPlanRuntime =
        siddhiManager.createExecutionPlanRuntime(toFloatSFunc + cseEventStream + query1 + query2);

    executionPlanRuntime.addCallback(
        "query2",
        new QueryCallback() {
          @Override
          public void receive(long timeStamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timeStamp, inEvents, removeEvents);
            isReceived[0] = true;
            value[0] = inEvents[inEvents.length - 1].getData(0);
          }
        });

    isReceived[0] = false;
    value[0] = null;

    InputHandler inputHandler = executionPlanRuntime.getInputHandler("cseEventStream");
    executionPlanRuntime.start();
    inputHandler.send(new Object[] {"WSO2", "50.0", 60f, 60l, 6});

    Thread.sleep(100);

    if (isReceived[0]) {
      Assert.assertEquals(25.0f, value[0]);
    } else {
      throw new RuntimeException("The event has not been received");
    }

    executionPlanRuntime.shutdown();
  }
Beispiel #7
0
  @Test
  public void querySyncTest3() throws InterruptedException {
    log.info("querySync test3");

    SiddhiManager siddhiManager = new SiddhiManager();
    String streams =
        ""
            + "define stream cseEventStream (symbol string, price float, volume int); "
            + "define stream twitterStream (user string, tweet string, company string); ";
    String query =
        ""
            + "@info(name = 'query1') "
            + "@synchronized('true') "
            + "from cseEventStream#window.time(1 sec) as a join twitterStream#window.time(1 sec) as b "
            + "on a.symbol== b.company "
            + "select a.symbol as symbol, b.tweet, a.price "
            + "insert all events into outputStream ;";

    ExecutionPlanRuntime executionPlanRuntime =
        siddhiManager.createExecutionPlanRuntime(streams + query);
    try {
      executionPlanRuntime.addCallback(
          "query1",
          new QueryCallback() {
            @Override
            public void receive(long timeStamp, Event[] inEvents, Event[] removeEvents) {
              EventPrinter.print(timeStamp, inEvents, removeEvents);
              if (inEvents != null) {
                inEventCount.addAndGet(inEvents.length);
              }
              if (removeEvents != null) {
                removeEventCount.addAndGet(removeEvents.length);
              }
              eventArrived = true;
            }
          });

      InputHandler cseEventStreamHandler = executionPlanRuntime.getInputHandler("cseEventStream");
      InputHandler twitterStreamHandler = executionPlanRuntime.getInputHandler("twitterStream");
      executionPlanRuntime.start();
      cseEventStreamHandler.send(new Object[] {"WSO2", 55.6f, 100});
      twitterStreamHandler.send(new Object[] {"User1", "Hello World", "WSO2"});
      cseEventStreamHandler.send(new Object[] {"IBM", 75.6f, 100});
      Thread.sleep(500);
      cseEventStreamHandler.send(new Object[] {"WSO2", 57.6f, 100});

      SiddhiTestHelper.waitForEvents(100, 2, inEventCount, 60000);
      SiddhiTestHelper.waitForEvents(100, 2, removeEventCount, 60000);
      Assert.assertEquals(2, inEventCount.get());
      Assert.assertEquals(2, removeEventCount.get());
      Assert.assertTrue(eventArrived);
    } finally {
      executionPlanRuntime.shutdown();
    }
  }
Beispiel #8
0
  @Test
  public void testEvalJavaScriptConcat() throws InterruptedException {

    log.info("testEvalJavaScriptConcat");

    SiddhiManager siddhiManager = new SiddhiManager();
    siddhiManager.setExtension(
        "evalscript:javascript", org.wso2.siddhi.extension.evalscript.EvalJavaScript.class);
    siddhiManager.setExtension(
        "evalscript:scala", org.wso2.siddhi.extension.evalscript.EvalScala.class);

    String concatFunc =
        "define function concatJ[JavaScript] return string {\n"
            + "  var str1 = data[0];\n"
            + "  var str2 = data[1];\n"
            + "  var str3 = data[2];\n"
            + "  var res = str1.concat(str2,str3);\n"
            + "  return res;\n"
            + "};";

    String cseEventStream =
        "@config(async = 'true')define stream cseEventStream (symbol string, price float, volume long);";
    String query =
        ("@info(name = 'query1') from cseEventStream select price , concatJ(symbol,' ',price) as concatStr "
            + "group by volume insert into mailOutput;");
    ExecutionPlanRuntime executionPlanRuntime =
        siddhiManager.createExecutionPlanRuntime(concatFunc + cseEventStream + query);

    executionPlanRuntime.addCallback(
        "query1",
        new QueryCallback() {
          @Override
          public void receive(long timeStamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timeStamp, inEvents, removeEvents);
            isReceived[0] = true;
            value[0] = inEvents[inEvents.length - 1].getData(1);
          }
        });

    isReceived[0] = false;
    value[0] = null;

    InputHandler inputHandler = executionPlanRuntime.getInputHandler("cseEventStream");
    executionPlanRuntime.start();

    inputHandler.send(new Object[] {"WSO2", 50f, 60f, 60l, 6});
    Thread.sleep(100);

    if (isReceived[0]) {
      Assert.assertEquals("WSO2 50", value[0]);
    } else {
      throw new RuntimeException("The event has not been received");
    }
    executionPlanRuntime.shutdown();
  }
  @Test
  public void frequentUniqueWindowTest2() throws InterruptedException {
    log.info("lossyFrequentWindow test2");

    SiddhiManager siddhiManager = new SiddhiManager();

    String cseEventStream = "" + "define stream purchase (cardNo string, price float);";
    String query =
        ""
            + "@info(name = 'query1') "
            + "from purchase[price >= 30]#window.lossyFrequent(0.3,0.05) "
            + "select cardNo, price "
            + "insert all events into PotentialFraud ;";

    ExecutionPlanRuntime executionPlanRuntime =
        siddhiManager.createExecutionPlanRuntime(cseEventStream + query);

    executionPlanRuntime.addCallback(
        "query1",
        new QueryCallback() {
          @Override
          public void receive(long timeStamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timeStamp, inEvents, removeEvents);
            if (inEvents != null) {
              inEventCount += inEvents.length;
            }
            if (removeEvents != null) {
              removeEventCount += removeEvents.length;
            }
            eventArrived = true;
          }
        });

    InputHandler inputHandler = executionPlanRuntime.getInputHandler("purchase");
    executionPlanRuntime.start();

    inputHandler.send(new Object[] {"3224-3244-2432-4124", 73.36f});
    for (int i = 0; i < 25; i++) {
      inputHandler.send(new Object[] {"3234-3244-2432-4124", 73.36f});
      inputHandler.send(new Object[] {"3234-3244-2432-4124", 78.36f});
      inputHandler.send(new Object[] {"1234-3244-2432-123", 86.36f});
      inputHandler.send(
          new Object[] {
            "5768-3244-2432-5646", 48.36f
          }); // this event will not include in to the window during first iteration because
              // 1+0<5*0.25
    }
    Thread.sleep(1000);
    Assert.assertEquals("Event arrived", true, eventArrived);
    Assert.assertEquals("Out Event count", 1, removeEventCount);

    executionPlanRuntime.shutdown();
  }
  @Test
  public void testUpperFunctionExtension() throws InterruptedException {
    log.info("UpperFunctionExtension TestCase");
    SiddhiManager siddhiManager = new SiddhiManager();

    String inStreamDefinition =
        "@config(async = 'true')define stream inputStream (symbol string, price long, volume long);";
    String query =
        ("@info(name = 'query1') from inputStream select symbol , str:upper(symbol) as symbolInUpperCase "
            + "insert into outputStream;");
    ExecutionPlanRuntime executionPlanRuntime =
        siddhiManager.createExecutionPlanRuntime(inStreamDefinition + query);

    executionPlanRuntime.addCallback(
        "query1",
        new QueryCallback() {
          @Override
          public void receive(long timeStamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timeStamp, inEvents, removeEvents);
            for (Event event : inEvents) {
              count++;
              if (count == 1) {
                Assert.assertEquals(
                    "ABCDEFGHIJ KLMNAAAAAAAAAA",
                    event.getData(
                        1)); // Note: Assertion doesn't count the spaces infront or in the back
                eventArrived = true;
              }
              if (count == 2) {
                Assert.assertEquals("123456XYZ ABC 78AAAAAA", event.getData(1));
                eventArrived = true;
              }
              if (count == 3) {
                Assert.assertEquals("HELLO WORLDAAAA", event.getData(1));
                eventArrived = true;
              }
            }
          }
        });

    InputHandler inputHandler = executionPlanRuntime.getInputHandler("inputStream");
    executionPlanRuntime.start();
    inputHandler.send(new Object[] {"AbCDefghiJ KLMNaaaaaaaaaa", 700f, 100l});
    inputHandler.send(new Object[] {"123456XyZ abC 78aaaaaa", 60.5f, 200l});
    inputHandler.send(new Object[] {"Hello Worldaaaa", 60.5f, 200l});
    Thread.sleep(100);
    Assert.assertEquals(3, count);
    Assert.assertTrue(eventArrived);
    executionPlanRuntime.shutdown();
  }
Beispiel #11
0
  @Test
  public void querySyncTest1() throws InterruptedException {
    log.info("querySync test1");

    SiddhiManager siddhiManager = new SiddhiManager();

    String cseEventStream =
        "" + "define stream cseEventStream (symbol string, price float, volume int);";
    String query =
        ""
            + "@info(name = 'query1') "
            + "@synchronized('true') "
            + "from cseEventStream#window.time(2 sec) "
            + "select symbol,price,volume "
            + "insert all events into outputStream ;";

    ExecutionPlanRuntime executionPlanRuntime =
        siddhiManager.createExecutionPlanRuntime(cseEventStream + query);

    executionPlanRuntime.addCallback(
        "query1",
        new QueryCallback() {
          @Override
          public void receive(long timeStamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timeStamp, inEvents, removeEvents);
            if (inEvents != null) {
              inEventCount.addAndGet(inEvents.length);
            }
            if (removeEvents != null) {
              Assert.assertTrue(
                  "InEvents arrived before RemoveEvents",
                  inEventCount.get() > removeEventCount.get());
              removeEventCount.addAndGet(removeEvents.length);
            }
            eventArrived = true;
          }
        });

    InputHandler inputHandler = executionPlanRuntime.getInputHandler("cseEventStream");
    executionPlanRuntime.start();
    inputHandler.send(new Object[] {"IBM", 700f, 0});
    inputHandler.send(new Object[] {"WSO2", 60.5f, 1});
    Thread.sleep(4000);
    Assert.assertEquals(2, inEventCount.get());
    Assert.assertEquals(2, removeEventCount.get());
    Assert.assertTrue(eventArrived);
    executionPlanRuntime.shutdown();
  }
  @Test
  public void sortWindowTest2() throws InterruptedException {
    log.info("sortWindow test2");

    SiddhiManager siddhiManager = new SiddhiManager();
    String planName = "@plan:name('sortWindow2') ";
    String cseEventStream =
        "" + "define stream cseEventStream (symbol string, price int, volume long);";
    String query =
        ""
            + "@info(name = 'query1') "
            + "from cseEventStream#window.sort(2,volume, 'asc', price, 'desc') "
            + "select price, volume "
            + "insert all events into outputStream ;";

    ExecutionPlanRuntime executionPlanRuntime =
        siddhiManager.createExecutionPlanRuntime(planName + cseEventStream + query);

    executionPlanRuntime.addCallback(
        "query1",
        new QueryCallback() {
          @Override
          public void receive(long timeStamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timeStamp, inEvents, removeEvents);
            if (inEvents != null) {
              inEventCount = inEventCount + inEvents.length;
            }
            if (removeEvents != null) {
              removeEventCount = removeEventCount + removeEvents.length;
            }
            eventArrived = true;
          }
        });

    InputHandler inputHandler = executionPlanRuntime.getInputHandler("cseEventStream");
    executionPlanRuntime.start();
    inputHandler.send(new Object[] {"WSO2", 50, 100l});
    inputHandler.send(new Object[] {"IBM", 20, 100l});
    inputHandler.send(new Object[] {"WSO2", 40, 50l});
    inputHandler.send(new Object[] {"WSO2", 100, 20l});
    inputHandler.send(new Object[] {"WSO2", 50, 50l});
    Thread.sleep(1000);
    Assert.assertEquals(5, inEventCount);
    Assert.assertEquals(3, removeEventCount);
    Assert.assertTrue(eventArrived);
    executionPlanRuntime.shutdown();
  }
  public static void main(String[] args) throws InterruptedException {
    SiddhiManager siddhiManager = new SiddhiManager();

    String cseEventStream =
        "define stream cseEventStream (symbol string, price float, volume long);";
    String query =
        "@info(name = 'query1') from cseEventStream[70 > price] select symbol,price,volume insert into outputStream ;";

    ExecutionPlanRuntime executionPlanRuntime =
        siddhiManager.createExecutionPlanRuntime(cseEventStream + query);
    executionPlanRuntime.addCallback(
        "query1",
        new QueryCallback() {
          private long chunk = 0;
          private long prevCount = 0;

          @Override
          public void receive(long timeStamp, Event[] inEvents, Event[] removeEvents) {
            count += inEvents.length;
            long currentChunk = count / 2000000;
            if (currentChunk != chunk) {
              long end = System.currentTimeMillis();
              double tp = ((count - prevCount) * 1000.0 / (end - start));
              System.out.println("Throughput = " + tp + " Event/sec");
              start = end;
              chunk = currentChunk;
              prevCount = count;
            }
          }
        });

    InputHandler inputHandler = executionPlanRuntime.getInputHandler("cseEventStream");
    executionPlanRuntime.start();
    while (true) {
      inputHandler.send(new Object[] {"WSO2", 55.6f, 100});
      inputHandler.send(new Object[] {"IBM", 75.6f, 100});
      inputHandler.send(new Object[] {"WSO2", 55.6f, 100});
      inputHandler.send(new Object[] {"IBM", 75.6f, 100});
      inputHandler.send(new Object[] {"WSO2", 55.6f, 100});
      inputHandler.send(new Object[] {"IBM", 75.6f, 100});
      inputHandler.send(new Object[] {"WSO2", 55.6f, 100});
      inputHandler.send(new Object[] {"IBM", 75.6f, 100});
      inputHandler.send(new Object[] {"WSO2", 55.6f, 100});
      inputHandler.send(new Object[] {"IBM", 75.6f, 100});
    }
  }
Beispiel #14
0
  @Test(expected = ExecutionPlanValidationException.class)
  public void testUseUndefinedFunction() throws InterruptedException {
    log.info("testUseUndefinedFunction");

    SiddhiManager siddhiManager = new SiddhiManager();
    siddhiManager.setExtension(
        "evalscript:javascript", org.wso2.siddhi.extension.evalscript.EvalJavaScript.class);
    siddhiManager.setExtension(
        "evalscript:scala", org.wso2.siddhi.extension.evalscript.EvalScala.class);
    // siddhiManager.defineFunction(concatFunc);
    String cseEventStream =
        "@config(async = 'true')define stream cseEventStream (symbol string, price float, volume long);";
    String query =
        ("@info(name = 'query1') from cseEventStream select price , undefinedFunc(symbol,' ',price) as concatStr "
            + "group by volume insert into mailOutput;");
    ExecutionPlanRuntime executionPlanRuntime =
        siddhiManager.createExecutionPlanRuntime(cseEventStream + query);

    executionPlanRuntime.addCallback(
        "query1",
        new QueryCallback() {
          @Override
          public void receive(long timeStamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timeStamp, inEvents, removeEvents);
            isReceived[0] = true;
            value[0] = inEvents[inEvents.length - 1].getData(1);
          }
        });

    isReceived[0] = false;
    value[0] = null;

    InputHandler inputHandler = executionPlanRuntime.getInputHandler("cseEventStream");
    executionPlanRuntime.start();
    inputHandler.send(new Object[] {"IBM", 700f, 100l});
    Thread.sleep(100);

    if (isReceived[0]) {
      Assert.assertEquals("IBM 700.0", value[0]);
    } else {
      throw new RuntimeException("The event has not been received");
    }

    executionPlanRuntime.shutdown();
  }
Beispiel #15
0
  @Test(expected = SiddhiParserException.class)
  public void testMissingReturnTypeWhileParsing() throws InterruptedException {
    log.info("testDefineManyFunctionsAndCallThemRandom");

    SiddhiManager siddhiManager = new SiddhiManager();
    siddhiManager.setExtension(
        "evalscript:javascript", org.wso2.siddhi.extension.evalscript.EvalJavaScript.class);
    siddhiManager.setExtension(
        "evalscript:scala", org.wso2.siddhi.extension.evalscript.EvalScala.class);

    String concatFunc1 =
        "define function concat[Scala] {\n"
            + "  var concatenatedString = \"\"\n"
            + "  for(i <- 0 until data.length) {\n"
            + "     concatenatedString += data(i).toString\n"
            + "  }\n"
            + "  concatenatedString\n"
            + "};";
    ExecutionPlanRuntime executionPlanRuntime =
        siddhiManager.createExecutionPlanRuntime(concatFunc1);
    executionPlanRuntime.shutdown();
  }
  public static void main(String[] args) throws InterruptedException {
    SiddhiManager siddhiManager = new SiddhiManager();

    String executionPlan =
        ""
            + "define stream cseEventStream (symbol string, price float, volume long);"
            + ""
            + "partition with (symbol of cseEventStream) "
            + "begin "
            + "   @info(name = 'query1') "
            + "   from cseEventStream[price > 70] "
            + "   select * "
            + "   insert into outputStream ;"
            + "end;";

    ExecutionPlanRuntime executionPlanRuntime =
        siddhiManager.createExecutionPlanRuntime(executionPlan);
    executionPlanRuntime.addCallback(
        "outputStream",
        new StreamCallback() {

          @Override
          public void receive(Event[] events) {
            //                EventPrinter.print(events);
          }
        });

    InputHandler inputHandler = executionPlanRuntime.getInputHandler("cseEventStream");
    executionPlanRuntime.start();

    while (true) {
      count++;
      inputHandler.send(new Object[] {"WSO2" + count, 55.6f, 100});
      System.out.println("Partition created :" + count);
    }
  }
Beispiel #17
0
  @Test(expected = ExecutionPlanCreationException.class)
  public void testScalaCompilationFailure() throws InterruptedException {

    log.info("testScalaCompilationFailure");

    SiddhiManager siddhiManager = new SiddhiManager();
    siddhiManager.setExtension(
        "evalscript:javascript", org.wso2.siddhi.extension.evalscript.EvalJavaScript.class);
    siddhiManager.setExtension(
        "evalscript:scala", org.wso2.siddhi.extension.evalscript.EvalScala.class);

    String concatFunc =
        "define function concat[Scala] return string {\n"
            + "  for(i <- 0 until data.length){\n"
            + "  concatenatedString += data(i).toString\n"
            + "  }\n"
            + "  concatenatedString\n"
            + "}";

    ExecutionPlanRuntime executionPlanRuntime =
        siddhiManager.createExecutionPlanRuntime(concatFunc);

    executionPlanRuntime.shutdown();
  }
Beispiel #18
0
  @Test
  public void querySyncTest4() throws InterruptedException {
    log.info("querySync test4");

    SiddhiManager siddhiManager = new SiddhiManager();

    String streams =
        ""
            + "define stream Stream1 (symbol string, price float, volume int); "
            + "define stream Stream2 (symbol string, price float, volume int); ";
    String query =
        ""
            + "@info(name = 'query1') "
            + "@synchronized('true') "
            + "from every ( e1=Stream1[price>20] -> e3=Stream1[price>20]) -> e2=Stream2[price>e1.price] "
            + "select e1.price as price1, e3.price as price3, e2.price as price2 "
            + "insert into OutputStream ;";

    ExecutionPlanRuntime executionPlanRuntime =
        siddhiManager.createExecutionPlanRuntime(streams + query);

    executionPlanRuntime.addCallback(
        "query1",
        new QueryCallback() {
          @Override
          public void receive(long timeStamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timeStamp, inEvents, removeEvents);
            if (inEvents != null) {
              for (Event event : inEvents) {
                inEventCount.incrementAndGet();
                switch (inEventCount.get()) {
                  case 1:
                    org.junit.Assert.assertArrayEquals(
                        new Object[] {55.6f, 54f, 57.7f}, event.getData());
                    break;
                  case 2:
                    org.junit.Assert.assertArrayEquals(
                        new Object[] {53.6f, 53f, 57.7f}, event.getData());
                    break;
                  default:
                    org.junit.Assert.assertSame(2, inEventCount);
                }
              }
              eventArrived = true;
            }
            if (removeEvents != null) {
              removeEventCount.addAndGet(removeEvents.length);
            }
            eventArrived = true;
          }
        });

    InputHandler stream1 = executionPlanRuntime.getInputHandler("Stream1");
    InputHandler stream2 = executionPlanRuntime.getInputHandler("Stream2");

    executionPlanRuntime.start();

    stream1.send(new Object[] {"WSO2", 55.6f, 100});
    Thread.sleep(100);
    stream1.send(new Object[] {"GOOG", 54f, 100});
    Thread.sleep(100);
    stream1.send(new Object[] {"WSO2", 53.6f, 100});
    Thread.sleep(100);
    stream1.send(new Object[] {"GOOG", 53f, 100});
    Thread.sleep(100);
    stream2.send(new Object[] {"IBM", 57.7f, 100});
    Thread.sleep(100);

    org.junit.Assert.assertEquals("Number of success events", 2, inEventCount.get());
    org.junit.Assert.assertEquals("Number of remove events", 0, removeEventCount.get());
    org.junit.Assert.assertEquals("Event arrived", true, eventArrived);

    executionPlanRuntime.shutdown();
  }
Beispiel #19
0
  @Test
  public void testDefineManyFunctionsAndCallThemRandom() throws InterruptedException {

    log.info("testDefineManyFunctionsAndCallThemRandom");

    SiddhiManager siddhiManager = new SiddhiManager();
    siddhiManager.setExtension(
        "evalscript:javascript", org.wso2.siddhi.extension.evalscript.EvalJavaScript.class);
    siddhiManager.setExtension(
        "evalscript:scala", org.wso2.siddhi.extension.evalscript.EvalScala.class);

    String concatSFunc =
        "define function concatS[Scala] return string {\n"
            + "  var concatenatedString = \"\"\n"
            + "  for(i <- 0 until data.length){\n"
            + "     concatenatedString += data(i).toString\n"
            + "  }\n"
            + "  concatenatedString\n"
            + "};\n";

    String concatJFunc =
        "define function concatJ[JavaScript] return string {\n"
            + "   var str1 = data[0].toString();\n"
            + "   var str2 = data[1].toString();\n"
            + "   var str3 = data[2].toString();\n"
            + "   var res = str1.concat(str2,str3);\n"
            + "   return res;\n"
            + "};\n";

    String toFloatSFunc =
        "define function toFloatS[Scala] return float {\n"
            + "   data(0).asInstanceOf[Long].toFloat\n"
            + "};\n";

    String toStringJFunc =
        "define function toStringJ[JavaScript] return string {\n"
            + "   return data[0].toString();\n"
            + "};\n";

    String cseEventStream =
        "@config(async = 'true')define stream cseEventStream (symbol string, price float, volume long);\n";
    String query1 =
        ("@info(name = 'query1') from cseEventStream select price , toStringJ(price) as concatStr insert into mailto1;\n");
    String query2 =
        ("@info(name = 'query2') from cseEventStream select price , toFloatS(volume) as concatStr insert into mailto2;\n");
    String query3 =
        ("@info(name = 'query3') from cseEventStream select price , concatJ(symbol,' ',price) as concatStr insert into mailto3;\n");
    String query4 =
        ("@info(name = 'query4') from cseEventStream select price , concatS(symbol,' ',price) as concatStr insert into mailto4;\n");
    ExecutionPlanRuntime executionPlanRuntime =
        siddhiManager.createExecutionPlanRuntime(
            concatSFunc
                + concatJFunc
                + toFloatSFunc
                + toStringJFunc
                + cseEventStream
                + query1
                + query2
                + query3
                + query4);

    executionPlanRuntime.addCallback(
        "query1",
        new QueryCallback() {
          @Override
          public void receive(long timeStamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timeStamp, inEvents, removeEvents);
            isReceived[0] = true;
            value[0] = inEvents[inEvents.length - 1].getData(1);
          }
        });

    executionPlanRuntime.addCallback(
        "query2",
        new QueryCallback() {
          @Override
          public void receive(long timeStamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timeStamp, inEvents, removeEvents);
            isReceived[1] = true;
            value[1] = inEvents[inEvents.length - 1].getData(1);
          }
        });

    executionPlanRuntime.addCallback(
        "query3",
        new QueryCallback() {
          @Override
          public void receive(long timeStamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timeStamp, inEvents, removeEvents);
            isReceived[2] = true;
            value[2] = inEvents[inEvents.length - 1].getData(1);
          }
        });

    executionPlanRuntime.addCallback(
        "query4",
        new QueryCallback() {
          @Override
          public void receive(long timeStamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timeStamp, inEvents, removeEvents);
            isReceived[3] = true;
            value[3] = inEvents[inEvents.length - 1].getData(1);
          }
        });

    for (int i = 0; i < isReceived.length; i++) {
      isReceived[i] = false;
    }

    for (int i = 0; i < value.length; i++) {
      value[i] = null;
    }
    InputHandler inputHandler = executionPlanRuntime.getInputHandler("cseEventStream");
    executionPlanRuntime.start();
    inputHandler.send(new Object[] {"WSO2", 50f, 6l});

    Thread.sleep(100);

    if (isReceived[0]) {
      Assert.assertEquals("50", value[0]);
    } else {
      throw new RuntimeException("The event has not been received");
    }

    if (isReceived[1]) {
      Assert.assertEquals(6f, value[1]);
    } else {
      throw new RuntimeException("The event has not been received");
    }

    if (isReceived[2]) {
      Assert.assertEquals("WSO2 50", value[2]);
    } else {
      throw new RuntimeException("The event has not been received");
    }

    if (isReceived[3]) {
      Assert.assertEquals("WSO2 50.0", value[3]);
    } else {
      throw new RuntimeException("The event has not been received");
    }

    executionPlanRuntime.shutdown();
  }
Beispiel #20
0
  @Test
  public void querySyncTest2() throws InterruptedException {
    log.info("querySync test2");

    SiddhiManager siddhiManager = new SiddhiManager();

    String executionPlan =
        ""
            + "@Plan:name('SnapshotOutputRateLimitTest3') "
            + ""
            + "define stream LoginEvents (timeStamp long, ip string);"
            + ""
            + "@info(name = 'query1') "
            + "@synchronized('true') "
            + "from LoginEvents "
            + "select ip "
            + "output snapshot every 1 sec "
            + "insert all events into uniqueIps ;";

    ExecutionPlanRuntime executionPlanRuntime =
        siddhiManager.createExecutionPlanRuntime(executionPlan);

    log.info("Running : " + executionPlanRuntime.getName());

    executionPlanRuntime.addCallback(
        "uniqueIps",
        new StreamCallback() {
          @Override
          public void receive(Event[] events) {
            EventPrinter.print(events);
            eventArrived = true;
            for (Event event : events) {
              if (event.isExpired()) {
                Assert.fail("Remove events emitted");
              } else {
                count.incrementAndGet();
                Assert.assertTrue(
                    "192.10.1.3".equals(event.getData(0)) || "192.10.1.4".equals(event.getData(0)));
              }
            }
          }
        });

    InputHandler inputHandler = executionPlanRuntime.getInputHandler("LoginEvents");

    executionPlanRuntime.start();

    inputHandler.send(new Object[] {System.currentTimeMillis(), "192.10.1.5"});
    Thread.sleep(100);
    inputHandler.send(new Object[] {System.currentTimeMillis(), "192.10.1.3"});
    Thread.sleep(2200);
    inputHandler.send(new Object[] {System.currentTimeMillis(), "192.10.1.9"});
    Thread.sleep(100);
    inputHandler.send(new Object[] {System.currentTimeMillis(), "192.10.1.4"});
    Thread.sleep(1100);

    executionPlanRuntime.shutdown();

    Assert.assertEquals("Event arrived", true, eventArrived);
    Assert.assertTrue("Number of output event value", 3 == count.get());
  }