private void runQuery(
      String epl, String fields, Object[][] expected, ContextPartitionSelector[] selectors) {
    // try FAF without prepare
    EPOnDemandQueryResult result = epService.getEPRuntime().executeQuery(epl, selectors);
    EPAssertionUtil.assertPropsPerRowAnyOrder(result.getArray(), fields.split(","), expected);

    // test prepare and execute
    EPOnDemandPreparedQuery preparedQuery = epService.getEPRuntime().prepareQuery(epl);
    EPOnDemandQueryResult resultPrepared = preparedQuery.execute(selectors);
    EPAssertionUtil.assertPropsPerRowAnyOrder(
        resultPrepared.getArray(), fields.split(","), expected);
  }
  private void runQueryAll(String epl, String fields, Object[][] expected, int numStreams) {
    ContextPartitionSelector[] selectors = new ContextPartitionSelector[numStreams];
    for (int i = 0; i < numStreams; i++) {
      selectors[i] = ContextPartitionSelectorAll.INSTANCE;
    }

    runQuery(epl, fields, expected, selectors);

    // run same query without selector
    EPOnDemandQueryResult result = epService.getEPRuntime().executeQuery(epl);
    EPAssertionUtil.assertPropsPerRowAnyOrder(result.getArray(), fields.split(","), expected);
  }
  private void runAssertionFireAndForgetInsertUpdateDelete(Object[][] expectedType) {
    EPOnDemandQueryResult result =
        epService.getEPRuntime().executeQuery("insert into MyTable(key) values ('dummy')");
    assertEventType(result.getEventType(), expectedType);

    result = epService.getEPRuntime().executeQuery("delete from MyTable where key = 'dummy'");
    assertEventType(result.getEventType(), expectedType);

    result =
        epService.getEPRuntime().executeQuery("update MyTable set key='dummy' where key='dummy'");
    assertEventType(result.getEventType(), expectedType);
  }
  public void testNamedWindowAndFireAndForget() throws Exception {
    String epl =
        "create window MyWindow.win:length(2) as SupportBean;\n"
            + "insert into MyWindow select * from SupportBean;\n"
            + "create table varagg (total sum(int));\n"
            + "into table varagg select sum(intPrimitive) as total from MyWindow;\n";
    epService.getEPAdministrator().getDeploymentAdmin().parseDeploy(epl);

    epService.getEPRuntime().sendEvent(new SupportBean("E1", 10));
    EPOnDemandQueryResult resultSelect =
        epService.getEPRuntime().executeQuery("select varagg.total as c0 from MyWindow");
    assertEquals(10, resultSelect.getArray()[0].get("c0"));

    EPOnDemandQueryResult resultDelete =
        epService
            .getEPRuntime()
            .executeQuery("delete from MyWindow where varagg.total = intPrimitive");
    assertEquals(1, resultDelete.getArray().length);

    epService.getEPRuntime().sendEvent(new SupportBean("E2", 20));
    EPOnDemandQueryResult resultUpdate =
        epService
            .getEPRuntime()
            .executeQuery(
                "update MyWindow set doublePrimitive = 100 where varagg.total = intPrimitive");
    assertEquals(100d, resultUpdate.getArray()[0].get("doublePrimitive"));

    EPOnDemandQueryResult resultInsert =
        epService
            .getEPRuntime()
            .executeQuery(
                "insert into MyWindow (theString, intPrimitive) values ('A', varagg.total)");
    EPAssertionUtil.assertProps(
        resultInsert.getArray()[0], "theString,intPrimitive".split(","), new Object[] {"A", 20});
  }
  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());
    }
  }
 private void runAssertionFireAndForgetSelectStar(Object[][] expectedType, Object[] rowValues) {
   EPOnDemandQueryResult result =
       epService.getEPRuntime().executeQuery("select * from MyTable where key = 'G1'");
   assertEventTypeAndEvent(
       result.getEventType(), expectedType, result.getArray()[0].getUnderlying(), rowValues);
 }