@Test
 public void testCreatingFilterPatternQuery() {
   Query query = QueryFactory.createQuery();
   query.from(
       QueryFactory.sequenceStream(
           Sequence.next(
               QueryFactory.inputStream("e1", "Stream1"),
               QueryFactory.inputStream("e2", "Stream1")),
           Expression.value(2000)));
   query.insertInto("OutStream");
   query.select(
       QueryFactory.outputSelector()
           .select("symbol", Expression.variable("e1", "symbol"))
           .select("avgPrice", "avg", Expression.variable("e2", 0, "price"))
           .groupBy("e1", "symbol")
           .having(
               Condition.compare(
                   Expression.variable("avgPrice"),
                   Condition.Operator.GREATER_THAN,
                   Expression.value(50))));
 }
예제 #2
0
  @Test
  public void testPatternWithinQuery1() throws InterruptedException {
    log.info("testPatternWithin1 - OUT 1");

    SiddhiManager siddhiManager = new SiddhiManager();

    siddhiManager.defineStream(
        QueryFactory.createStreamDefinition()
            .name("Stream1")
            .attribute("symbol", Attribute.Type.STRING)
            .attribute("price", Attribute.Type.FLOAT)
            .attribute("volume", Attribute.Type.INT));
    siddhiManager.defineStream(
        QueryFactory.createStreamDefinition()
            .name("Stream2")
            .attribute("symbol", Attribute.Type.STRING)
            .attribute("price", Attribute.Type.FLOAT)
            .attribute("volume", Attribute.Type.INT));

    Query query = QueryFactory.createQuery();
    query.from(
        QueryFactory.patternStream(
            Pattern.followedBy(
                Pattern.every(
                    QueryFactory.inputStream("e1", "Stream1")
                        .filter(
                            Condition.compare(
                                Expression.variable("price"),
                                Condition.Operator.GREATER_THAN,
                                Expression.value(20)))),
                QueryFactory.inputStream("e2", "Stream2")
                    .filter(
                        Condition.compare(
                            Expression.variable("price"),
                            Condition.Operator.GREATER_THAN,
                            Expression.variable("e1", "price")))),
            Expression.value(1000)));

    query.insertInto("OutStream");
    query.project(
        QueryFactory.outputProjector()
            .project("symbol1", Expression.variable("e1", "symbol"))
            .project("symbol2", Expression.variable("e2", "symbol")));

    String queryReference = siddhiManager.addQuery(query);
    siddhiManager.addCallback(
        queryReference,
        new QueryCallback() {
          @Override
          public void receive(long timeStamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timeStamp, inEvents, removeEvents);
            if (eventCount == 0) {
              Assert.assertArrayEquals(new Object[] {"GOOG", "IBM"}, inEvents[0].getData());
            } else {
              Assert.fail();
            }
            eventCount++;
            eventArrived = true;
          }
        });
    InputHandler stream1 = siddhiManager.getInputHandler("Stream1");
    InputHandler stream2 = siddhiManager.getInputHandler("Stream2");
    stream1.send(new Object[] {"WSO2", 55.6f, 100});
    Thread.sleep(1500);
    stream1.send(new Object[] {"GOOG", 54f, 100});
    Thread.sleep(500);
    stream2.send(new Object[] {"IBM", 55.7f, 100});
    Thread.sleep(500);

    siddhiManager.shutdown();

    Assert.assertEquals("Number of success events", 1, eventCount);
    Assert.assertEquals("Event arrived", true, eventArrived);
  }