예제 #1
0
  public static void main(String[] args) {
    EPServiceProvider epService = EPServiceProviderManager.getDefaultProvider();
    EPAdministrator admin = epService.getEPAdministrator();

    // Apple定义
    Map<String, Object> apple = new HashMap<String, Object>();
    apple.put("id", int.class);
    apple.put("price", int.class);

    // 注册Apple到Esper
    admin.getConfiguration().addEventType("Apple", apple);

    String epl = "select avg(price) from Apple.win:length_batch(3)";

    EPStatement state = admin.createEPL(epl);
    state.addListener(new AppleListener());

    EPRuntime runtime = epService.getEPRuntime();

    Map<String, Object> apple1 = new HashMap<String, Object>();
    apple1.put("id", 1);
    apple1.put("price", 5);
    runtime.sendEvent(apple1, "Apple");

    Map<String, Object> apple2 = new HashMap<String, Object>();
    apple2.put("id", 2);
    apple2.put("price", 2);
    runtime.sendEvent(apple2, "Apple");

    Map<String, Object> apple3 = new HashMap<String, Object>();
    apple3.put("id", 3);
    apple3.put("price", 5);
    runtime.sendEvent(apple3, "Apple");
  }
예제 #2
0
  public synchronized void update(EventBean[] newEvents, EventBean[] oldEvents) {
    if (newEvents != null) {
      ps = (Product[]) newEvents[0].get("prevwindow(i)");
      count = ps.length;
    }
    if (oldEvents != null) {
      for (int i = count - 1; i >= 0; i--) {
        Map<String, Object> event = new HashMap<String, Object>();
        event.put("p", ps[i]);
        runtime.sendEvent(event, "S");
      }

      int last = ps.length - oldEvents.length;
      Product[] temp = new Product[last];
      for (int i = 0; i < last; i++) {
        temp[i] = ps[i];
      }
      ps = temp;

      User u = new User();
      runtime.sendEvent(u);

      Map<String, Object> scount = new HashMap<String, Object>();
      scount.put("count", count);
      runtime.sendEvent(scount, "Scount");

      count -= oldEvents.length;
    }
  }
 public void run() {
   try {
     for (int i = 0; i < 1000; i++) {
       runtime.sendEvent(new MyEventA("key1", (int) (Math.random() * 1000000)));
       runtime.sendEvent(new MyEventA("key2", (int) (Math.random() * 1000000)));
       runtime.sendEvent(new MyEventB("key1", (int) (Math.random() * 1000000)));
       runtime.sendEvent(new MyEventB("key2", (int) (Math.random() * 1000000)));
     }
   } catch (Exception ex) {
     this.exception = ex;
   }
 }
예제 #4
0
  public void update(EventBean[] newData, EventBean[] oldData) {
    List<String> rank = new ArrayList<String>();
    List<Long> counts = new ArrayList<Long>();
    long pickupDate = 0;
    long dropoffDate = 0;
    long ts = 0;
    for (EventBean e : newData) {
      CountEvent c = (CountEvent) e.getUnderlying();
      rank.add(c.getRouteCode());
      counts.add(c.getCount());
      if (ts < c.getTs()) {
        pickupDate = c.getPickupDate();
        dropoffDate = c.getDropoffDate();
        ts = c.getTs();
      }
    }

    if (!rank.isEmpty()) {
      RankEvent ev = new RankEvent();
      ev.setRank(rank);
      ev.setCounts(counts);
      ev.setDropoffDate(dropoffDate);
      ev.setPickupDate(pickupDate);
      ev.setTs(ts);
      cepRT.sendEvent(ev);
    }
  }
예제 #5
0
 public void update(EventBean[] newEvents, EventBean[] oldEvents) {
   if (newEvents != null) {
     long count = (Long) newEvents[0].get("count(*)");
     Map<String, Object> acount = new HashMap<String, Object>();
     acount.put("count", (int) count);
     runtime.sendEvent(acount, "Acount");
   }
 }
예제 #6
0
  public void update(EventBean[] newData, EventBean[] oldData) {

    //    	numRuns++;
    //    	if(numRuns % 1000 == 0)
    //        	System.out.println("PHONE CALL LISTENER: " + numRuns);

    if (!dc.stats.isStarted()) {
      dc.stats.start();
      StatsHolder.start();
    }

    Vote v = null;
    PhoneCall pc = (PhoneCall) newData[0].getUnderlying();
    LOG.debug("exec VoteSP\t" + pc.toParams());

    // IF WE'RE USING VOLT SPs
    if (dc instanceof VoltDBSPConnector) {
      v = ((VoltDBSPConnector) dc).runSP1(pc);
    }
    // OTHERWISE
    else {
      boolean exists = dc.realContestant(pc.contestantNumber);
      long numVotes = dc.numTimesVoted(pc.phoneNumber);
      String state = dc.getState(pc.phoneNumber);

      if (!exists) {
        dc.stats.addStat(VoterConstants.VOTE_KEY, pc);
        dc.closeWorkflow(pc);
        return;
      }

      if (numVotes >= VoterConstants.MAX_VOTES) {
        dc.stats.addStat(VoterConstants.VOTE_KEY, pc);
        dc.closeWorkflow(pc);
        return;
      }

      v = new Vote(pc, state, System.nanoTime());
      dc.insertVote(v);
    }

    dc.stats.addStat(VoterConstants.VOTE_KEY, pc);
    if (v != null) {
      v.startTime = System.nanoTime();

      EPRuntime cepRT = epService.getEPRuntime();
      cepRT.sendEvent(v);
    } else {
      dc.closeWorkflow(pc);
    }
  }
예제 #7
0
  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);
  }
예제 #8
0
 private void sendTimer(long timeInMSec) {
   CurrentTimeEvent theEvent = new CurrentTimeEvent(timeInMSec);
   EPRuntime runtime = epService.getEPRuntime();
   runtime.sendEvent(theEvent);
 }
예제 #9
0
  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());
    }
  }
예제 #10
0
  /** @param args */
  public static void main(String[] args) {
    EPServiceProvider epService = EPServiceProviderManager.getDefaultProvider();

    EPAdministrator admin = epService.getEPAdministrator();

    Map<String, Object> mapDef = new HashMap<String, Object>();
    mapDef.put("p", Product.class);

    Map<String, Object> mapDef1 = new HashMap<String, Object>();
    mapDef1.put("count", int.class);

    admin.getConfiguration().addEventType("S", mapDef);
    admin.getConfiguration().addEventType("Scount", mapDef1);
    admin.getConfiguration().addEventType("Acount", mapDef1);

    EPRuntime runtime = epService.getEPRuntime();

    String product = Product.class.getName();
    String close = User.class.getName();

    String input = "select irstream *, prevwindow(i) from " + product + ".win:time(5 sec) as i";
    String window = "create window ABC.win:keepall() as select * from S";
    String output =
        "insert into ABC select * from pattern[every a=S -[1]> b=S(b.p.price>=(a.p.price*1.05))]";
    String acount = "on " + close + " select and delete count(*) from ABC";
    String result = "every b=Acount -> every a=Scount(b.count+1=a.count)";

    EPStatement state1 = admin.createEPL(input);
    state1.addListener(new InputListener(runtime));
    admin.createEPL(window);
    admin.createEPL(output);
    EPStatement state2 = admin.createEPL(acount);
    state2.addListener(new AcountListener(runtime));
    EPStatement state3 = admin.createPattern(result);
    state3.addListener(new Result3Listener());

    Product esb = new Product();
    esb.setPrice(1);
    esb.setType("esb");
    runtime.sendEvent(esb);

    Product eos = new Product();
    eos.setPrice(2);
    eos.setType("eos");
    runtime.sendEvent(eos);

    Product esb1 = new Product();
    esb1.setPrice(3);
    esb1.setType("esb");
    runtime.sendEvent(esb1);

    Product eos1 = new Product();
    eos1.setPrice(5);
    eos1.setType("eos");
    runtime.sendEvent(eos1);

    Product esb2 = new Product();
    esb2.setPrice(6);
    esb2.setType("esb");
    runtime.sendEvent(esb2);

    Product eos3 = new Product();
    eos3.setPrice(7);
    eos3.setType("eos");
    runtime.sendEvent(eos3);

    System.out.println();
  }
예제 #11
0
  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);
  }
예제 #12
0
  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);
  }