7

The Background

I'm working on an event library in Scala. In my library you can define events like this:

val e1 = new ImperativeEvent[Int]

You can trigger them like this:

e1(42)

You can create reactions like this:

val r1 = (i: Int) => println(i)

And attach them to the event like this:

e1 += r1

There is also some other stuff (like event transformations, compositions etc). I use the Esper CEP engine as the backend of my library. Esper uses an SQL-like language called EPL for most operations.

The Problem

I'm trying to implement some more advanced concepts like event joins. So now you can define events with multiple properties like this (using tuple types):

val e2 = new ImperativeEvent[(Int, String)]

And then join them like this:

val e3 = e1 join e2 windowLength (30) on "E1.P1 = E2.P1"

which performs a join of e1 and e2 on the last 30 occurrances of both on the condition that their respective first properties are equal.

This is alright but I'd like to get rid of the strings in my implementation to make the event expressions type checkable. I'd like to change the join expression to something like this:

val e3 = e1 join e2 windowLength (30) on e1._1 === e2._1

similar to the way it is done in eg. Squeryl. The problem with this is, I can't access the types of the elements of the tuple type...

The Question

How can I access the tuple types statically? Right now I've only managed to access them at run-time through reflection which does not help me. I'm pretty sure that what I want to achieve is not possible with tuples but I'm wondering if using HLists from the shapeless library or something similar might help in achieving my goal.

7
  • 1
    Is seems you have e1.join(e2).windowLength(30).on(e1._1 === e2._1). What is === ?
    – pedrofurla
    Sep 11, 2013 at 18:09
  • += seems an odd choice of operator for what you use it for here. It implies that ImperativeEvent is purely or principally a collection of reactions. If you have overridden + for adding any of the various things that you might add to an event, I guess that would be a bit more understandable but it still makes me queasy.
    – itsbruce
    Sep 11, 2013 at 18:14
  • @pedrofurla E1 refers to the left event of the join, E2 to the right one. The join method replaces those with the appropriate internal event IDs. The actual join is made by Esper (esper.codehaus.org), my library is mostly a wrapper around it. Esper is based on an SQL-like language called EPL, that's why it uses Strings. I'd like to add type checking to those Strings (similar to eg. Squeryl, that's where my === example comes from) but I'm not sure if this is possible with my current event representation (in which parameters are expressed as (tuple) types of the event). Sep 11, 2013 at 18:16
  • 1
    @itsbruce The idea for the += operator was borrowed from C#. Check this out for a quick introduction into C# events. Sep 11, 2013 at 18:24
  • 1
    So there´s precedent, I see, and you´re offering familiar usage to C# veterans. Nasty precedent, though ;) If it calls out to a method named something like registerHandler, that´d be what I´d use every time.
    – itsbruce
    Sep 11, 2013 at 18:33

2 Answers 2

4

Without more details on your DSL, I'm afraid it's not clear what you mean by "access the tuple types statically". Here's a simplified version of the API that has no trouble with tuple types:

class Event[T] {
  def joinOn[T2, R](ev2: Event[T2])(f: (T, T2) => R) = new Event[R]
}

You can use this as follows:

val e1 = new Event[(Int, String)]
val e2 = new Event[(Int, String)]
val e3 = e1.joinOn(e2)(_._1 == _._2)

It should be easy to see how this could be extended to supporting your join/windowLength/on syntax.

Update: I can see that your use case is complicated by the fact that you need to translate the Scala-encoded query expression to another query language. In this case, you want the on method's signature to look like:

def on[T2, R](f: (Expr[T], Expr[T2]) => Expr[R]): Event[R]

Internally, each event object would create its own Expr representation and would pass this representation into the function supplied to the on method.

The Expr type could be defined like:

trait Expr[T] {
  protected val repr: String

  def _1[A](implicit ev: T <:< Tuple2[A,_]): Expr[A] = 
    ??? // create an Expr[A] whose string representation is (repr + ".P1")

  // abstracting over tuple arities (using Shapeless)
  import shapeless._, nat._
  @scala.annotation.implicitNotFound("A tuple with at least 3 elements is required")
  type At2 = ops.tuple.At[T, _2]

  def _3(implicit at: At2): Expr[at.Out] = 
    ??? // create an Expr[at.Out] whose string representation is (repr + ".P3")

  def ===(other: Expr[T]): Expr[Boolean] =
    ??? // create an Expr[T] whose string representation is (repr + " = " + other.repr)
}

This is obviously dramatically simplified but should help to get you started.

10
  • Now that I see the solution it looks really simple... thank you! However, I've got one more question: As you can see from the example above, I actually need to create a String in the end... and it should contain the position of the element in the tuple rather than the actual element itself (eg. your example _._1 == _._2 should return a String "E1.P1 = E2.P2"). I probably need to enrich the Int and String types via implicits, but I'm not sure about getting the position of the element in the tuple... Sep 11, 2013 at 19:05
  • 1
    I added some notes on how the API could compute a string representation. Sep 11, 2013 at 19:24
  • 1
    I hadn't considered that, but it's another reason to look into Shapeless. Alternatively, you could define Tuple2Expr[T1,T2], Tuple3Expr[T1,T2,T3], etc., as implicit views of Expr[(T1, T2)], Expr[(T1,T2,T3)], etc., respectively. This non-Shapeless approach would require 22 classes and 253 method definitions! Sep 13, 2013 at 17:11
  • 1
    I've been interested in learning Shapeless lately, so I decided to look into this. It turned out to be quite simple -- see my recent edit, which defines an arity-neutral _3 method and even gives a useful compiler error if the client code fails to supply a suitable tuple type. Sep 13, 2013 at 19:42
  • 1
    Thank you for the advice! I had some problems with your suggestion but after a little experimentation and exploration of shapeless' test cases I got it working and my version looks like this: def _2(implicit at: At[T, _1]) = ValueExpr[at.Out](repr + ".P3") Note the at.Out part; in your version the element type is not set correctly. A little drawback of using Shapeless is that IDE support has some issues. Sep 14, 2013 at 1:19
0

I. There is a SynapseGrid functional reactive programming library. In the sources you can find some helpful tips.

All processing in the library is type safe. You have complete access to tuples.

For instance if I had to implement join in SynapseGrid I would define a method join of the following signature:

implicit class RichContact[T] (c:Contact[T]){ // contact == event in SynapseGrid's terminology
  ...
  def join[T2](c2:Contact[T2]):Contact[(T, T2)] = {
    // construct a contact/event that do nothing more than joining two events.
  }
}

implicit class RichTupledContact[T, T2](c:Contact[(T, T2)])
  def windowLength(len:Int):Contact[(T, T2)] = { 
    // construct the next step of processing events — window 
  }
}

and so on. Gradually building event processing greatly simplifies system's construction.

II. If, however, you need to construct everything at once then you may return some intermediate object that has construction method:

implicit class RichContact[T] (c:Contact[T]){ // contact == event in SynapseGrid's terminology
  ...
  def join[T2](c2:Contact[T2]):Contact[(T, T2)] = {
    new {
      def windowLength(len:Int) = ...
    }
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.