Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

adding interval

  • Loading branch information...
commit 6204a4c05789cadd3c47eb5fc3b4d8fbaa54b722 1 parent 88cf2f9
Jan Kassens authored January 13, 2011
16  src/main/scala/CEP.scala
@@ -10,22 +10,23 @@ object time {
10 10
    */
11 11
   protected[events] def now = current
12 12
 
13  
-  def skip(seconds: Int) {
  13
+  def add(seconds: Int) {
14 14
     assert(seconds > 0)
15 15
     current += seconds
16 16
   }
17 17
 }
18 18
 
19 19
 class JointEventNode[T, U, V](ev1: Event[T], ev2: => Event[U], interval: Int, merge: (T, U) => V) extends EventNode[V] {
20  
-  var events1: List[T] = Nil
21  
-  var events2: List[U] = Nil
  20
+  var events1: List[(Int, T)] = Nil
  21
+  var events2: List[(Int, U)] = Nil
22 22
 
23 23
   /*
24 24
    * Reaction to event1
25 25
    */
26 26
   lazy val onEvt1 = (id: Int, v1: T, reacts: ListBuffer[() => Unit]) => {
27  
-    events1 = v1 :: events1
28  
-    for (v2 <- events2.reverse) {
  27
+    events1 = (time.now + interval, v1) :: events1
  28
+    events2 = events2.filter(_._1 >= time.now)
  29
+    for ((_, v2) <- events2.reverse) {
29 30
       reactions(id, merge(v1, v2), reacts)
30 31
     }
31 32
   }
@@ -34,8 +35,9 @@ class JointEventNode[T, U, V](ev1: Event[T], ev2: => Event[U], interval: Int, me
34 35
    * Reaction to event2
35 36
    */
36 37
   lazy val onEvt2 = (id: Int, v2: U, reacts: ListBuffer[() => Unit]) => {
37  
-    events2 = v2 :: events2
38  
-    for (v1 <- events1.reverse) {
  38
+    events2 = (time.now + interval, v2) :: events2
  39
+    events1 = events1.filter(_._1 >= time.now)
  40
+    for ((_, v1) <- events1.reverse) {
39 41
       reactions(id, merge(v1, v2), reacts)
40 42
     }
41 43
   }
16  src/test/scala/MySuite.scala
@@ -104,4 +104,20 @@ class MySuite extends FunSuite with OneInstancePerTest {
104 104
 
105 105
     assertLogged("bs")((1, 3), (2, 3), (1, 4), (2, 4))
106 106
   }
  107
+
  108
+  test("join with interval") {
  109
+    val buyJoinSell = buy join (sell, 10, (a: Int, b: Int) => (a, b))
  110
+    buyJoinSell += logger("bs")
  111
+
  112
+    buy(1)
  113
+    sell(2)
  114
+    sell(3)
  115
+    sell(4)
  116
+    time.add(11)
  117
+    buy(5)
  118
+    sell(6)
  119
+    sell(7)
  120
+
  121
+    assertLogged("bs")((1, 2), (1, 3), (1, 4), (5, 6), (5, 7))
  122
+  }
107 123
 }

0 notes on commit 6204a4c

Please sign in to comment.
Something went wrong with that request. Please try again.