Skip to content
Browse files

format and CEP.scala added

  • Loading branch information...
1 parent ccc19a3 commit 56457ab3689151aeb53b6d3ae6cbd91798402801 @kassens committed Jan 13, 2011
Showing with 89 additions and 44 deletions.
  1. +47 −0 src/main/scala/CEP.scala
  2. +42 −44 src/test/scala/MySuite.scala
View
47 src/main/scala/CEP.scala
@@ -0,0 +1,47 @@
+package scala.events
+
+import scala.collection.mutable.ListBuffer
+
+class JointEventNode[T, U, V](ev1: Event[T], ev2: => Event[U], merge: (T, U) => V) extends EventNode[V] {
+ var events1: List[T] = Nil
+ var events2: List[U] = Nil
+
+ /*
+ * Reaction to event1
+ */
+ lazy val onEvt1 = (id: Int, v1: T, reacts: ListBuffer[() => Unit]) => {
+ events1 = v1 :: events1
+ for (v2 <- events2.reverse) {
+ reactions(id, merge(v1, v2), reacts)
+ }
+ }
+
+ /*
+ * Reaction to event2
+ */
+ lazy val onEvt2 = (id: Int, v2: U, reacts: ListBuffer[() => Unit]) => {
+ events2 = v2 :: events2
+ for (v1 <- events1.reverse) {
+ reactions(id, merge(v1, v2), reacts)
+ }
+ }
+
+ /*
+ * Register to the referenced events
+ */
+ protected override def deploy {
+ ev1 += onEvt1
+ ev2 += onEvt2
+ }
+
+ /*
+ * Unregister from the referenced events
+ */
+ protected override def undeploy {
+ ev1 -= onEvt1
+ ev2 -= onEvt2
+ }
+
+ override def toString = "(" + ev1 + " join " + ev2 + ")"
+
+}
View
86 src/test/scala/MySuite.scala
@@ -1,4 +1,4 @@
-import org.scalatest.{FunSuite, BeforeAndAfterEach}
+import org.scalatest.{ FunSuite, BeforeAndAfterEach }
import scala.collection.mutable.ListBuffer
import events._
@@ -9,116 +9,114 @@ class Stock {
class MySuite extends FunSuite with BeforeAndAfterEach {
var log = ListBuffer[(String, Any)]()
- def logger(name : String) = (
- (v : Any) => {
- log += ((name, v))
- ()
- }
- )
- def assertLogged(pairs : (String, Any)*) {
+ def logger(name: String) = ((v: Any) => {
+ log += ((name, v))
+ ()
+ })
+ def assertLogged(pairs: (String, Any)*) {
assert(pairs.toList === log.toList)
}
override def beforeEach() = {}
-
+
override def afterEach() = {
log.clear()
}
-
+
test("temp") {
val x = logger("hi")
x(1)
x(3)
assertLogged(("hi", 1), ("hi", 3))
}
-
+
test("imparative events") {
val s = new Stock
s.buy += logger("buy")
s.sell += logger("sell")
-
+
s.buy(4)
s.buy(3)
s.sell(5)
s.buy(1)
-
+
assertLogged(("buy", 4), ("buy", 3), ("sell", 5), ("buy", 1))
}
-
+
test("OR events") {
val s = new Stock
val order = s.buy || s.sell
order += logger("order")
-
+
s.buy(4)
s.buy(3)
s.sell(5)
s.buy(1)
-
+
assertLogged(("order", 4), ("order", 3), ("order", 5), ("order", 1))
}
-
+
test("single right cross join") {
val s = new Stock
- val buyJoinSell = s.buy join (s.sell, (a : Int, b : Int) => (a, b))
+ val buyJoinSell = s.buy join (s.sell, (a: Int, b: Int) => (a, b))
buyJoinSell += logger("bs")
-
+
s.sell(1)
s.buy(2)
s.buy(3)
s.buy(4)
-
+
assertLogged(
- ("bs", (2,1)),
- ("bs", (3,1)),
- ("bs", (4,1)))
+ ("bs", (2, 1)),
+ ("bs", (3, 1)),
+ ("bs", (4, 1)))
}
-
+
test("single left cross join") {
val s = new Stock
- val buyJoinSell = s.buy join (s.sell, (a : Int, b : Int) => (a, b))
+ val buyJoinSell = s.buy join (s.sell, (a: Int, b: Int) => (a, b))
buyJoinSell += logger("bs")
-
+
s.buy(1)
s.sell(2)
s.sell(3)
s.sell(4)
-
+
assertLogged(
- ("bs", (1,2)),
- ("bs", (1,3)),
- ("bs", (1,4)))
+ ("bs", (1, 2)),
+ ("bs", (1, 3)),
+ ("bs", (1, 4)))
}
-
+
test("single right reverse cross join") {
val s = new Stock
- val buyJoinSell = s.buy join (s.sell, (a : Int, b : Int) => (a, b))
+ val buyJoinSell = s.buy join (s.sell, (a: Int, b: Int) => (a, b))
buyJoinSell += logger("bs")
-
+
s.buy(2)
s.buy(3)
s.buy(4)
s.sell(1)
-
+
assertLogged(
- ("bs", (2,1)),
- ("bs", (3,1)),
- ("bs", (4,1)))
+ ("bs", (2, 1)),
+ ("bs", (3, 1)),
+ ("bs", (4, 1)))
}
-
+
test("single left reverse cross join") {
val s = new Stock
- val buyJoinSell = s.buy join (s.sell, (a : Int, b : Int) => (a, b))
+ val buyJoinSell = s.buy join (s.sell, (a: Int, b: Int) => (a, b))
buyJoinSell += logger("bs")
-
+
s.sell(2)
s.sell(3)
s.sell(4)
s.buy(1)
-
+
assertLogged(
- ("bs", (1,2)),
- ("bs", (1,3)),
- ("bs", (1,4)))
+ ("bs", (1, 2)),
+ ("bs", (1, 3)),
+ ("bs", (1, 4)))
}
}

0 comments on commit 56457ab

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