public void testCorrel() {
    // further math tests can be found in the view unit test
    EPAdministrator admin = epService.getEPAdministrator();
    admin.getConfiguration().addEventType("Market", SupportMarketDataBean.class);
    EPStatement statement =
        admin.createEPL(
            "select * from Market.std:groupwin(symbol).win:length(1000000).stat:correl(price, volume, feed)");
    SupportUpdateListener listener = new SupportUpdateListener();
    statement.addListener(listener);

    assertEquals(Double.class, statement.getEventType().getPropertyType("correlation"));

    String[] fields = new String[] {"symbol", "correlation", "feed"};

    epService.getEPRuntime().sendEvent(new SupportMarketDataBean("ABC", 10.0, 1000L, "f1"));
    EPAssertionUtil.assertProps(
        listener.assertOneGetNewAndReset(), fields, new Object[] {"ABC", Double.NaN, "f1"});

    epService.getEPRuntime().sendEvent(new SupportMarketDataBean("DEF", 1.0, 2L, "f2"));
    EPAssertionUtil.assertProps(
        listener.assertOneGetNewAndReset(), fields, new Object[] {"DEF", Double.NaN, "f2"});

    epService.getEPRuntime().sendEvent(new SupportMarketDataBean("DEF", 2.0, 4L, "f3"));
    EPAssertionUtil.assertProps(
        listener.assertOneGetNewAndReset(), fields, new Object[] {"DEF", 1.0, "f3"});

    epService.getEPRuntime().sendEvent(new SupportMarketDataBean("ABC", 20.0, 2000L, "f4"));
    EPAssertionUtil.assertProps(
        listener.assertOneGetNewAndReset(), fields, new Object[] {"ABC", 1.0, "f4"});
  }
  public RealtimeSummaryStmt(EPAdministrator admin) {
    //
    // Min,Max,Average total latency from the events (difference in time between A and C) over the
    // past 30 minutes.
    // Min,Max,Average latency between events A/B (time stamp of B minus A) and B/C (time stamp of C
    // minus B).
    //
    String stmtTotal =
        "select min(latencyAC) as minLatencyAC, "
            + "max(latencyAC) as maxLatencyAC, "
            + "avg(latencyAC) as avgLatencyAC, "
            + "min(latencyAB) as minLatencyAB, "
            + "max(latencyAB) as maxLatencyAB, "
            + "avg(latencyAB) as avgLatencyAB, "
            + "min(latencyBC) as minLatencyBC, "
            + "max(latencyBC) as maxLatencyBC, "
            + "avg(latencyBC) as avgLatencyBC "
            + "from CombinedEvent.win:time(30 min)";

    totalsStatement = admin.createEQL(stmtTotal);

    //
    // Min,Max,Average latency grouped by (a) customer ID and (b) supplier ID.
    // In other words, metrics on the the latency of the orders coming from each customer and going
    // to each supplier.
    //
    String stmtCustomer =
        "select customerId,"
            + "min(latencyAC) as minLatency,"
            + "max(latencyAC) as maxLatency,"
            + "avg(latencyAC) as avgLatency "
            + "from CombinedEvent.win:time(30 min) "
            + "group by customerId";

    byCustomerStatement = admin.createEQL(stmtCustomer);

    String stmtSupplier =
        "select supplierId,"
            + "min(latencyAC) as minLatency,"
            + "max(latencyAC) as maxLatency,"
            + "avg(latencyAC) as avgLatency "
            + "from CombinedEvent.win:time(30 min) "
            + "group by supplierId";

    bySupplierStatement = admin.createEQL(stmtSupplier);
  }
  public static void main(String[] args) {
    EPServiceProvider esProvider = EPServiceProviderManager.getDefaultProvider();
    EPAdministrator admin = esProvider.getEPAdministrator();
    EPRuntime er = esProvider.getEPRuntime();

    Review r1 = new Review();
    r1.setReviewId(1);
    r1.setComment("r1");

    Book b1 = new Book();
    b1.setAuthor("b1");
    b1.setBookId(1);
    b1.setReview(r1);

    Book b2 = new Book();
    b2.setAuthor("b2");
    b2.setBookId(2);

    Item i1 = new Item();
    i1.setItemId(1);
    i1.setProductId(1);
    i1.setPrice(1.11);
    i1.setAmount(2);

    // 添加Bean对象
    MediaOrder mo1 = new MediaOrder();
    Books bs = new Books();
    Items is = new Items();
    List<Item> items = new ArrayList<Item>();
    List<Book> books = new ArrayList<Book>();
    items.add(i1);
    books.add(b1);
    books.add(b2);
    mo1.setOrderId(1);
    bs.setBook(books);
    is.setItem(items);
    mo1.setItems(is);
    mo1.setBooks(bs);

    String mediaOrder = MediaOrder.class.getName();
    String epl = "select * from " + mediaOrder + "[books.book]";
    EPStatement stat1 = admin.createEPL(epl);
    stat1.addListener(new SelectContainedListener());

    er.sendEvent(mo1);
  }
  public void testLinest() {
    // further math tests can be found in the view unit test
    EPAdministrator admin = epService.getEPAdministrator();
    admin.getConfiguration().addEventType("Market", SupportMarketDataBean.class);
    EPStatement statement =
        admin.createEPL(
            "select * from Market.std:groupwin(symbol).win:length(1000000).stat:linest(price, volume, feed)");
    SupportUpdateListener listener = new SupportUpdateListener();
    statement.addListener(listener);

    assertEquals(Double.class, statement.getEventType().getPropertyType("slope"));
    assertEquals(Double.class, statement.getEventType().getPropertyType("YIntercept"));
    assertEquals(Double.class, statement.getEventType().getPropertyType("XAverage"));
    assertEquals(Double.class, statement.getEventType().getPropertyType("XStandardDeviationPop"));
    assertEquals(
        Double.class, statement.getEventType().getPropertyType("XStandardDeviationSample"));
    assertEquals(Double.class, statement.getEventType().getPropertyType("XSum"));
    assertEquals(Double.class, statement.getEventType().getPropertyType("XVariance"));
    assertEquals(Double.class, statement.getEventType().getPropertyType("YAverage"));
    assertEquals(Double.class, statement.getEventType().getPropertyType("YStandardDeviationPop"));
    assertEquals(
        Double.class, statement.getEventType().getPropertyType("YStandardDeviationSample"));
    assertEquals(Double.class, statement.getEventType().getPropertyType("YSum"));
    assertEquals(Double.class, statement.getEventType().getPropertyType("YVariance"));
    assertEquals(Long.class, statement.getEventType().getPropertyType("dataPoints"));
    assertEquals(Long.class, statement.getEventType().getPropertyType("n"));
    assertEquals(Double.class, statement.getEventType().getPropertyType("sumX"));
    assertEquals(Double.class, statement.getEventType().getPropertyType("sumXSq"));
    assertEquals(Double.class, statement.getEventType().getPropertyType("sumXY"));
    assertEquals(Double.class, statement.getEventType().getPropertyType("sumY"));
    assertEquals(Double.class, statement.getEventType().getPropertyType("sumYSq"));

    String[] fields = new String[] {"symbol", "slope", "YIntercept", "feed"};

    epService.getEPRuntime().sendEvent(new SupportMarketDataBean("ABC", 10.0, 50000L, "f1"));
    EPAssertionUtil.assertProps(
        listener.assertOneGetNewAndReset(),
        fields,
        new Object[] {"ABC", Double.NaN, Double.NaN, "f1"});

    epService.getEPRuntime().sendEvent(new SupportMarketDataBean("DEF", 1.0, 1L, "f2"));
    EventBean theEvent = listener.assertOneGetNewAndReset();
    EPAssertionUtil.assertProps(
        theEvent, fields, new Object[] {"DEF", Double.NaN, Double.NaN, "f2"});
    assertEquals(1d, theEvent.get("XAverage"));
    assertEquals(0d, theEvent.get("XStandardDeviationPop"));
    assertEquals(Double.NaN, theEvent.get("XStandardDeviationSample"));
    assertEquals(1d, theEvent.get("XSum"));
    assertEquals(Double.NaN, theEvent.get("XVariance"));
    assertEquals(1d, theEvent.get("YAverage"));
    assertEquals(0d, theEvent.get("YStandardDeviationPop"));
    assertEquals(Double.NaN, theEvent.get("YStandardDeviationSample"));
    assertEquals(1d, theEvent.get("YSum"));
    assertEquals(Double.NaN, theEvent.get("YVariance"));
    assertEquals(1L, theEvent.get("dataPoints"));
    assertEquals(1L, theEvent.get("n"));
    assertEquals(1d, theEvent.get("sumX"));
    assertEquals(1d, theEvent.get("sumXSq"));
    assertEquals(1d, theEvent.get("sumXY"));
    assertEquals(1d, theEvent.get("sumY"));
    assertEquals(1d, theEvent.get("sumYSq"));
    // above computed values tested in more detail in RegressionBean test

    epService.getEPRuntime().sendEvent(new SupportMarketDataBean("DEF", 2.0, 2L, "f3"));
    EPAssertionUtil.assertProps(
        listener.assertOneGetNewAndReset(), fields, new Object[] {"DEF", 1.0, 0.0, "f3"});

    epService.getEPRuntime().sendEvent(new SupportMarketDataBean("ABC", 11.0, 50100L, "f4"));
    EPAssertionUtil.assertProps(
        listener.assertOneGetNewAndReset(), fields, new Object[] {"ABC", 100.0, 49000.0, "f4"});
  }
  public void testStats() {
    EPAdministrator epAdmin = epService.getEPAdministrator();
    String filter = "select * from " + SupportMarketDataBean.class.getName();

    EPStatement priceLast3Stats =
        epAdmin.createEPL(filter + ".std:groupwin(symbol).win:length(3).stat:uni(price)");
    priceLast3Stats.addListener(priceLast3StatsListener);

    EPStatement volumeLast3Stats =
        epAdmin.createEPL(filter + ".std:groupwin(symbol).win:length(3).stat:uni(volume)");
    volumeLast3Stats.addListener(volumeLast3StatsListener);

    EPStatement priceAllStats = epAdmin.createEPL(filter + ".std:groupwin(symbol).stat:uni(price)");
    priceAllStats.addListener(priceAllStatsListener);

    EPStatement volumeAllStats =
        epAdmin.createEPL(filter + ".std:groupwin(symbol).stat:uni(volume)");
    volumeAllStats.addListener(volumeAllStatsListener);

    Vector<Map<String, Object>> expectedList = new Vector<Map<String, Object>>();
    for (int i = 0; i < 3; i++) {
      expectedList.add(new HashMap<String, Object>());
    }

    sendEvent(SYMBOL_CISCO, 25, 50000);
    sendEvent(SYMBOL_CISCO, 26, 60000);
    sendEvent(SYMBOL_IBM, 10, 8000);
    sendEvent(SYMBOL_IBM, 10.5, 8200);
    sendEvent(SYMBOL_GE, 88, 1000);

    EPAssertionUtil.assertPropsPerRow(
        priceLast3StatsListener.getLastNewData(), makeMap(SYMBOL_GE, 88));
    EPAssertionUtil.assertPropsPerRow(
        priceAllStatsListener.getLastNewData(), makeMap(SYMBOL_GE, 88));
    EPAssertionUtil.assertPropsPerRow(
        volumeLast3StatsListener.getLastNewData(), makeMap(SYMBOL_GE, 1000));
    EPAssertionUtil.assertPropsPerRow(
        volumeAllStatsListener.getLastNewData(), makeMap(SYMBOL_GE, 1000));

    sendEvent(SYMBOL_CISCO, 27, 70000);
    sendEvent(SYMBOL_CISCO, 28, 80000);

    EPAssertionUtil.assertPropsPerRow(
        priceAllStatsListener.getLastNewData(), makeMap(SYMBOL_CISCO, 26.5d));
    EPAssertionUtil.assertPropsPerRow(
        volumeAllStatsListener.getLastNewData(), makeMap(SYMBOL_CISCO, 65000d));
    EPAssertionUtil.assertPropsPerRow(
        priceLast3StatsListener.getLastNewData(), makeMap(SYMBOL_CISCO, 27d));
    EPAssertionUtil.assertPropsPerRow(
        volumeLast3StatsListener.getLastNewData(), makeMap(SYMBOL_CISCO, 70000d));

    sendEvent(SYMBOL_IBM, 11, 8700);
    sendEvent(SYMBOL_IBM, 12, 8900);

    EPAssertionUtil.assertPropsPerRow(
        priceAllStatsListener.getLastNewData(), makeMap(SYMBOL_IBM, 10.875d));
    EPAssertionUtil.assertPropsPerRow(
        volumeAllStatsListener.getLastNewData(), makeMap(SYMBOL_IBM, 8450d));
    EPAssertionUtil.assertPropsPerRow(
        priceLast3StatsListener.getLastNewData(), makeMap(SYMBOL_IBM, 11d + 1 / 6d));
    EPAssertionUtil.assertPropsPerRow(
        volumeLast3StatsListener.getLastNewData(), makeMap(SYMBOL_IBM, 8600d));

    sendEvent(SYMBOL_GE, 85.5, 950);
    sendEvent(SYMBOL_GE, 85.75, 900);
    sendEvent(SYMBOL_GE, 89, 1250);
    sendEvent(SYMBOL_GE, 86, 1200);
    sendEvent(SYMBOL_GE, 85, 1150);

    double averageGE = (88d + 85.5d + 85.75d + 89d + 86d + 85d) / 6d;
    EPAssertionUtil.assertPropsPerRow(
        priceAllStatsListener.getLastNewData(), makeMap(SYMBOL_GE, averageGE));
    EPAssertionUtil.assertPropsPerRow(
        volumeAllStatsListener.getLastNewData(), makeMap(SYMBOL_GE, 1075d));
    EPAssertionUtil.assertPropsPerRow(
        priceLast3StatsListener.getLastNewData(), makeMap(SYMBOL_GE, 86d + 2d / 3d));
    EPAssertionUtil.assertPropsPerRow(
        volumeLast3StatsListener.getLastNewData(), makeMap(SYMBOL_GE, 1200d));

    // Check iterator results
    expectedList.get(0).put("symbol", SYMBOL_CISCO);
    expectedList.get(0).put("average", 26.5d);
    expectedList.get(1).put("symbol", SYMBOL_IBM);
    expectedList.get(1).put("average", 10.875d);
    expectedList.get(2).put("symbol", SYMBOL_GE);
    expectedList.get(2).put("average", averageGE);
    EPAssertionUtil.assertPropsPerRow(priceAllStats.iterator(), expectedList);

    expectedList.get(0).put("symbol", SYMBOL_CISCO);
    expectedList.get(0).put("average", 27d);
    expectedList.get(1).put("symbol", SYMBOL_IBM);
    expectedList.get(1).put("average", 11d + 1 / 6d);
    expectedList.get(2).put("symbol", SYMBOL_GE);
    expectedList.get(2).put("average", 86d + 2d / 3d);
    EPAssertionUtil.assertPropsPerRow(priceLast3Stats.iterator(), expectedList);
  }
  public static void main(String[] args) {
    EPServiceProvider epService = EPServiceProviderManager.getDefaultProvider();
    EPAdministrator admin = epService.getEPAdministrator();
    EPRuntime runtime = epService.getEPRuntime();

    String SelectEvents = SelectEvents.class.getName();

    String epl1 = "create window SelectWindow.win:keepall() as select * from " + SelectEvents;
    String epl2 = "insert into SelectWindow select * from " + SelectEvents;

    // 注册EPL语句
    admin.createEPL(epl1);
    admin.createEPL(epl2);

    // 事件1
    SelectEvents se1 = new SelectEvents("se1", 1);
    runtime.sendEvent(se1);
    System.out.println("Send SelectEvent 1: " + se1);

    // 事件2
    SelectEvents se2 = new SelectEvents("se2", 2);
    runtime.sendEvent(se2);
    System.out.println("Send SelectEvent 2: " + se2);

    /* 操作语句
     * 1. 查找窗体中的所有事件
     * 2. 更新size = 2事件的name = "update1"
     * 3. 删除所有size < 2的事件
     */
    String select = "select * from SelectWindow";
    String update = "update SelectWindow set name='update1' where size = 2";
    String delete = "delete from SelectWindow where size < 2";

    System.out.println("\nSelect SelectWindow!");
    // 类似于数据库的查询操作,返回结果集
    EPOnDemandQueryResult selectResult = epService.getEPRuntime().executeQuery(select);
    EventBean[] events = selectResult.getArray();
    for (EventBean eb : events) {
      System.out.println(eb.getUnderlying());
    }

    // 更新size=2的事件,将name改为'update1'
    System.out.println("\nUpdate SelectEvent(size = 2) in SelectWindow!");
    EPOnDemandQueryResult updateResult = epService.getEPRuntime().executeQuery(update);
    events = updateResult.getArray();
    for (EventBean eb : events) {
      System.out.println(eb.getUnderlying());
    }

    System.out.println("\nSelect SelectWindow!");
    selectResult = epService.getEPRuntime().executeQuery(select);
    events = selectResult.getArray();
    for (EventBean eb : events) {
      System.out.println(eb.getUnderlying());
    }

    // 删除size<2的事件
    System.out.println("\nDelete SelectEvent(size < 2) in SelectWindow!");
    EPOnDemandQueryResult deleteResult = epService.getEPRuntime().executeQuery(delete);
    events = deleteResult.getArray();
    for (EventBean eb : events) {
      System.out.println(eb.getUnderlying());
    }

    System.out.println("\nSelect SelectWindow!");
    selectResult = epService.getEPRuntime().executeQuery(select);
    events = selectResult.getArray();
    for (EventBean eb : events) {
      System.out.println(eb.getUnderlying());
    }
  }
  public static void main(String[] args) {

    EPServiceProvider epService = EPServiceProviderManager.getDefaultProvider();
    EPAdministrator admin = epService.getEPAdministrator();
    EPRuntime runtime = epService.getEPRuntime();

    String triggerEvent = OnUpdateTrigger.class.getName();
    String updateEvent = OnUpdateEvent.class.getName();

    // 创建窗口
    String epl1 = "create window OnUpdateWindow.win:keepall() as select * from " + updateEvent;
    String epl2 = "insert into OnUpdateWindow select * from " + updateEvent;

    /* 触发条件 trigger>0
     * 获取更新前数值,initial是关键字,所以不能省略
     */
    String epl3 =
        "on "
            + triggerEvent
            + "(trigger>0) as out update OnUpdateWindow as ouw set size=out.trigger, price=initial.size where out.trigger<ouw.price";
    String epl4 = "on " + triggerEvent + "(trigger=0) select ouw.* from OnUpdateWindow as ouw";

    System.out.println("Create Window: " + epl1);
    System.out.println("Update Trigger sentence: " + epl3);
    System.out.println();

    admin.createEPL(epl1);
    admin.createEPL(epl2);
    EPStatement state3 = admin.createEPL(epl3);
    state3.addListener(new OnUpdateWindowListener());

    EPStatement state4 = admin.createEPL(epl4);
    state4.addListener(new OnUpdateSelectWindowListener());

    /* 事件1
     * size=1,price=2
     */
    OnUpdateEvent oue1 = new OnUpdateEvent("oue1", 1, 2);
    runtime.sendEvent(oue1);
    System.out.println("Send OnUpdateEvent 1: " + oue1);

    /* 事件2
     * size=2,price=3
     */
    OnUpdateEvent oue2 = new OnUpdateEvent("oue2", 2, 3);
    runtime.sendEvent(oue2);
    System.out.println("Send OnUpdateEvent 2: " + oue2);

    /* 事件3
     * size=3,price=4
     */
    OnUpdateEvent oue3 = new OnUpdateEvent("oue3", 3, 4);
    runtime.sendEvent(oue3);
    System.out.println("Send OnUpdateEvent 3: " + oue3);

    /*
     * 触发1
     * trigger = 0
     * 对应epl4
     * 查看所有事件
     */
    OnUpdateTrigger ost1 = new OnUpdateTrigger(0);
    System.out.println("\nSend OnUpdateTrigger " + ost1);
    runtime.sendEvent(ost1);

    /*
     * 触发2
     * trigger = 2
     * 对应epl3
     * 只输出out.trigger<ouw.price的事件2、3
     * 然后被更新
     *     事件2: size = 2, price = 2
     *     事件3: size = 2, price = 3
     */
    OnUpdateTrigger ost2 = new OnUpdateTrigger(2);
    System.out.println("\nSend OnUpdateTrigger " + ost2);
    runtime.sendEvent(ost2);

    /*
     * 触发3
     * trigger = 2
     * 对应epl4
     * 输出所有事件
     *     事件1: size = 1, price = 2(没有更新过)
     *     事件2: size = 2, price = 2
     *     事件3: size = 2, price = 3
     */
    OnUpdateTrigger ost3 = new OnUpdateTrigger(0);
    System.out.println("\nSend OnUpdateTrigger " + ost3);
    runtime.sendEvent(ost3);
  }
  public static void main(String[] args) {
    EPServiceProvider esProvider = EPServiceProviderManager.getDefaultProvider();
    EPAdministrator admin = esProvider.getEPAdministrator();
    EPRuntime er = esProvider.getEPRuntime();

    String limit = LimitEvent.class.getName();
    String follow = FollowedEvent.class.getName();

    /* 在每次触发完成前最多只保留2个a事件,触发条件为b的size值大于a的age */
    String epl = "every a=" + limit + " -[2]> b=" + follow + "(size > a.age)";
    System.out.println("EPL: " + epl + "\n");

    // 注册EPL语句
    EPStatement statement = admin.createPattern(epl);
    statement.addListener(new LimitFollowedListener());

    System.out.println("First Send!\n");

    // 事件1,age = 1
    LimitEvent l1 = new LimitEvent(1);
    System.out.println("Send Event: " + l1);
    er.sendEvent(l1);

    // 事件2,age = 2
    LimitEvent l2 = new LimitEvent(2);
    System.out.println("Send Event: " + l2);
    er.sendEvent(l2);

    // 事件3,age = 0
    LimitEvent l3 = new LimitEvent(0);
    System.out.println("Send Event: " + l3);
    er.sendEvent(l3);

    // 事件4,size = 3
    /*
     * 满足触发条件,但由于[2]的限制
     * 只能有age = 1,age = 2,两个事件
     */
    FollowedEvent f1 = new FollowedEvent();
    f1.setSize(3);
    System.out.println("Send Event: " + f1);
    er.sendEvent(f1);

    FollowedEvent f2 = new FollowedEvent();
    f2.setSize(4);
    System.out.println("Send Event: " + f2);
    er.sendEvent(f2);

    System.out.println();
    System.out.println("Second Send!\n");
    /*
    System.out.println("Send Event: " + l1);
    er.sendEvent(l1);
    System.out.println("Send Event: " + l2);
    er.sendEvent(l2);
    */
    /*
     * 由于当第一次触发时,[2]已经满足
     * 所以当第二次触发时,不用等待直接触发
     */
    System.out.println("Send Event: " + l3);
    er.sendEvent(l3);
    System.out.println("Send Event: " + f1);
    er.sendEvent(f1);
  }