public static void main(String[] args) throws Exception {

    StreamDefinition streamDefinition = new StreamDefinition();
    streamDefinition.setStreamId("TestStream");
    streamDefinition.addAttribute("att1", StreamDefinition.Type.LONG);
    streamDefinition.addAttribute("att2", StreamDefinition.Type.FLOAT);
    streamDefinition.addAttribute("att3", StreamDefinition.Type.STRING);
    streamDefinition.addAttribute("att4", StreamDefinition.Type.INT);

    siddhiManager = new SiddhiManager();
    String attributeStr =
        streamDefinition.getAttributeList().get(0).getName()
            + " "
            + streamDefinition.getAttributeList().get(0).getType().toString().toLowerCase();
    for (int i = 1; i < streamDefinition.getAttributeList().size(); i++) {
      attributeStr +=
          ","
              + streamDefinition.getAttributeList().get(i).getName()
              + " "
              + streamDefinition.getAttributeList().get(i).getType().toString().toLowerCase();
    }

    siddhiManager.defineStream(
        "define stream " + streamDefinition.getStreamId() + " ( " + attributeStr + " )");
    siddhiManager.addQuery(
        "from  TestStream " + "select att1, att2, att3, att4 " + "insert into StockQuote ;");
    siddhiManager.addCallback(
        "StockQuote",
        new org.wso2.siddhi.core.stream.output.StreamCallback() {
          @Override
          public void receive(Event[] events) {
            EventPrinter.print(events);
          }
        });

    ReorderingEventServer eventServer =
        new ReorderingEventServer(
            new EventServerConfig(7612),
            streamDefinition,
            new org.wso2.event.server.StreamCallback() {
              @Override
              public void receive(Object[] event) {
                InputHandler inputHandler = siddhiManager.getInputHandler("TestStream");
                if (inputHandler != null) {
                  try {
                    inputHandler.send(event);
                  } catch (InterruptedException e) {
                    e.printStackTrace();
                  }
                } else {
                  System.out.println("Could not retrieve stream handler");
                  throw new RuntimeException("Could not retrieve stream handler");
                }
              }
            });

    eventServer.start();

    Thread.sleep(10000);
  }
Esempio n. 2
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();
  }
  private SiddhiManager createMockSiddhiManager(
      String[] inputStreamDefinitions, String executionPlan) throws SiddhiParserException {
    SiddhiConfiguration siddhiConfig = new SiddhiConfiguration();
    siddhiConfig.setSiddhiExtensions(SiddhiExtensionLoader.loadSiddhiExtensions());
    SiddhiManager siddhiManager = new SiddhiManager(siddhiConfig);
    try {
      int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
      if (tenantId > -1) {
        DataSourceManager.getInstance().initTenant(tenantId);
      }
      List<CarbonDataSource> dataSources =
          EventProcessorValueHolder.getDataSourceService().getAllDataSources();
      for (CarbonDataSource cds : dataSources) {
        try {
          if (cds.getDSObject() instanceof DataSource) {
            siddhiManager
                .getSiddhiContext()
                .addDataSource(cds.getDSMInfo().getName(), (DataSource) cds.getDSObject());
          }
        } catch (Exception e) {
          log.error("Unable to add the datasource" + cds.getDSMInfo().getName(), e);
        }
      }
    } catch (DataSourceException e) {
      log.error("Unable to access the datasource service", e);
    }

    for (String streamDefinition : inputStreamDefinitions) {
      if (streamDefinition.trim().length() > 0) {
        siddhiManager.defineStream(streamDefinition);
      }
    }
    siddhiManager.addExecutionPlan(executionPlan);
    return siddhiManager;
  }
Esempio n. 4
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();
  }
  protected void enableEngineAction(
      String streamName,
      EngineActionType engineActionType,
      Map<String, Object> engineActionParams,
      StreamOperationServiceWithoutMetrics streamOperationService) {

    if (!streamStatusDao.isEngineActionEnabled(streamName, engineActionType)) {

      String engineActionQueryId =
          siddhiManager.addQuery(
              QueryFactory.createQuery()
                  .from(QueryFactory.inputStream(streamName))
                  .insertInto(STREAMING.STATS_NAMES.SINK_STREAM_PREFIX.concat(streamName)));

      BaseEngineAction engineAction = null;

      if (engineActionType == EngineActionType.FIRE_RULES) {

        engineAction =
            new DroolsEngineAction(
                droolsConnectionContainer,
                engineActionParams,
                siddhiManager,
                streamOperationService);
      }

      siddhiManager.addCallback(
          engineActionQueryId,
          callbackService.addEngineCallback(streamName, engineActionType, engineAction));

      streamStatusDao.enableEngineAction(
          streamName, engineActionType, engineActionParams, engineActionQueryId);
    }
  }
  public void dropStream(String streamName) {

    Map<String, QueryDTO> attachedQueries = streamStatusDao.get(streamName).getAddedQueries();
    for (String queryId : attachedQueries.keySet()) {
      siddhiManager.removeQuery(queryId);
    }
    siddhiManager.removeStream(streamName);
    streamStatusDao.remove(streamName);
  }
 public void removeQuery(String queryId, String streamName) {
   siddhiManager.removeQuery(queryId);
   streamStatusDao.removeQuery(streamName, queryId);
   for (Map.Entry<String, StreamStatusDTO> streamStatus : streamStatusDao.getAll().entrySet()) {
     String temporalStreamName = streamStatus.getKey();
     if (siddhiManager.getStreamDefinition(temporalStreamName) == null) {
       this.dropStream(temporalStreamName);
     }
   }
 }
Esempio n. 8
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();
    }
  }
Esempio n. 9
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();
  }
Esempio n. 10
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();
  }
 public String addQuery(String streamName, String queryString) {
   String queryId = siddhiManager.addQuery(queryString);
   streamStatusDao.addQuery(streamName, queryId, queryString);
   for (StreamDefinition streamDefinition : siddhiManager.getStreamDefinitions()) {
     // XXX refactor to obtain exactly siddhi inferred streams.
     streamStatusDao.createInferredStream(
         streamDefinition.getStreamId(),
         castToColumnNameTypeValue(streamDefinition.getAttributeList()));
   }
   return queryId;
 }
  @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();
  }
 public void send(String streamName, List<ColumnNameTypeValue> columns) throws ServiceException {
   try {
     siddhiManager
         .getInputHandler(streamName)
         .send(
             SiddhiUtils.getOrderedValues(siddhiManager.getStreamDefinition(streamName), columns));
   } catch (InterruptedException e) {
     throw new ServiceException(
         String.format("Error sending data to stream %s, column data: %s", streamName, columns),
         e);
   }
 }
  @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();
  }
Esempio n. 15
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();
  }
  public List<StratioStreamingMessage> list() {
    List<StratioStreamingMessage> result = new ArrayList<>();
    for (StreamDefinition streamDefinition : siddhiManager.getStreamDefinitions()) {
      if (suitableToList(streamDefinition.getStreamId())) {
        StratioStreamingMessage message = new StratioStreamingMessage();
        for (Attribute attribute : streamDefinition.getAttributeList()) {
          message.addColumn(
              new ColumnNameTypeValue(
                  attribute.getName(), this.getStreamingType(attribute.getType()), null));
        }
        StreamStatusDTO streamStatus = streamStatusDao.get(streamDefinition.getStreamId());

        if (streamStatus != null) {
          Map<String, QueryDTO> attachedQueries = streamStatus.getAddedQueries();

          for (Map.Entry<String, QueryDTO> entry : attachedQueries.entrySet()) {
            message.addQuery(new StreamQuery(entry.getKey(), entry.getValue().getQueryRaw()));
          }
          message.setUserDefined(streamStatus.getUserDefined());
          message.setActiveActions(
              streamStatusDao.getEnabledActions(streamDefinition.getStreamId()));
        }

        message.setStreamName(streamDefinition.getStreamId());

        result.add(message);
      }
    }

    return result;
  }
  @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();
  }
  @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();
  }
  public int enlargeStream(
      String streamName, List<ColumnNameTypeValue> columns, Boolean raiseException)
      throws ServiceException {
    int addedColumns = 0;
    StreamDefinition streamMetaData = siddhiManager.getStreamDefinition(streamName);
    for (ColumnNameTypeValue columnNameTypeValue : columns) {
      if (!SiddhiUtils.columnAlreadyExistsInStream(
          columnNameTypeValue.getColumn(), streamMetaData)) {
        addedColumns++;
        // JPFM -- Updating the columns in streamStatusDao
        streamStatusDao.addColumn(streamName, columnNameTypeValue);
        streamMetaData.attribute(
            columnNameTypeValue.getColumn(), getSiddhiType(columnNameTypeValue.getType()));
      } else {
        if (raiseException) {
          throw new ServiceException(
              String.format(
                  "Alter stream error, Column %s already " + "exists.",
                  columnNameTypeValue.getColumn()));
        }
      }
    }

    return addedColumns;
  }
Esempio n. 21
0
  @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 List<StreamDefinition> getSiddhiStreams(
     String[] inputStreamDefinitions, String queryExpressions) throws SiddhiParserException {
   SiddhiManager siddhiManager = createMockSiddhiManager(inputStreamDefinitions, queryExpressions);
   List<org.wso2.siddhi.query.api.definition.StreamDefinition> streamDefinitions =
       siddhiManager.getStreamDefinitions();
   List<StreamDefinition> databridgeStreamDefinitions =
       new ArrayList<StreamDefinition>(streamDefinitions.size());
   for (org.wso2.siddhi.query.api.definition.StreamDefinition siddhiStreamDef :
       streamDefinitions) {
     StreamConfiguration streamConfig = new StreamConfiguration(siddhiStreamDef.getStreamId());
     StreamDefinition databridgeStreamDef =
         EventProcessorUtil.convertToDatabridgeStreamDefinition(siddhiStreamDef, streamConfig);
     databridgeStreamDefinitions.add(databridgeStreamDef);
   }
   siddhiManager.shutdown();
   return databridgeStreamDefinitions;
 }
  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});
    }
  }
 public void createInternalStream(String streamName, List<ColumnNameTypeValue> columns) {
   StreamDefinition newStream = QueryFactory.createStreamDefinition().name(streamName);
   for (ColumnNameTypeValue column : columns) {
     newStream.attribute(column.getColumn(), getSiddhiType(column.getType()));
   }
   siddhiManager.defineStream(newStream);
   streamStatusDao.createInferredStream(streamName, columns);
 }
Esempio n. 25
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();
  }
  /** Bolt get saved and reloaded, this to redo the configurations. */
  private void init() {
    siddhiManager = new SiddhiManager(new SiddhiConfiguration());
    log = Logger.getLogger(SiddhiBolt.class);

    if (definitions != null) {
      for (String definition : definitions) {

        if (definition.contains("define stream")) {
          siddhiManager.defineStream(definition);
        } else if (definition.contains("define partition")) {
          siddhiManager.definePartition(definition);
        } else {
          throw new RuntimeException("Invalid definition : " + definition);
        }
      }
    }

    if (queries != null) {

      for (String query : queries) {
        siddhiManager.addQuery(query);
      }
    }

    for (final String streamId : exportedStreamIds) {
      log.info("Adding callback for stream - " + streamId);
      siddhiManager.addCallback(
          streamId,
          new StreamCallback() {
            @Override
            public void receive(Event[] events) {

              for (Event event : events) {
                collector.emit(event.getStreamId(), Arrays.asList(event.getData()));
                if (log.isDebugEnabled()) {
                  log.debug(
                      "Sending Processed event : " + event.getStreamId() + "=>" + event.toString());
                }
              }
            }
          });
    }
  }
  public void disableAction(String streamName, StreamAction action) {
    streamStatusDao.disableAction(streamName, action);

    if (streamStatusDao.getEnabledActions(streamName).size() == 0) {
      String actionQueryId = streamStatusDao.getActionQuery(streamName);
      if (actionQueryId != null) {
        siddhiManager.removeQuery(actionQueryId);
      }
      callbackService.remove(streamName);
    }
  }
Esempio n. 28
0
  @Test(expected = ExecutionPlanValidationException.class)
  public void testMissingReturnType() {

    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);

    ExecutionPlan.executionPlan("test")
        .defineFunction(
            (new FunctionDefinition()
                .id("concat")
                .language("Scala")
                .body(
                    "var concatenatedString = \"\"\n"
                        + "for(i <- 0 until data.length){\n"
                        + "  concatenatedString += data(i).toString\n"
                        + "}\n"
                        + "concatenatedString")));
  }
Esempio n. 29
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();
  }
Esempio n. 30
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();
  }