This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Join definitions in Scala

1 reply
Jiansen
Joined: 2011-04-27,
User offline. Last seen 1 year 10 weeks ago.

Hi,

I meet difficulty to use the Scala Joins Library by Philipp Haller
(http://lamp.epfl.ch/~phaller/joins/index.html )

Thinking that Scala Joins is not part of the standard library, I would
be appreciate for suggestions for my test code.

I would also like to hear stories about using Scala Joins or other
library supporting Join-Calculus in Scala.

With thanks
Jiansen

p.s.
==============
Actor-based joins:
==============

class TEST extends JoinActor {
object A extends Join
object B extends Join
object C extends Join
object D extends Join

def act() {
receive {
case A() and1 B() =>
println("1")
case C() and1 D() =>
println("2")
case D() and1 A() =>
println("3")

}
}
}

val test = new TEST
test.start()
/*
val prod = actor {
test ! test.C()
test ! test.D()
test ! test.B()
test ! test.A()
}
// test output: 1
// expected output :
1
2
or
2
1
*/
val prod = actor {
test ! test.C()
test ! test.D()
test ! test.A()
}
/* exception:..
joins.joinsActor$TEST@2b275d39: caught
java.util.NoSuchElementException: head of empty list
java.util.NoSuchElementException: head of empty list
at scala.collection.immutable.Nil$.head(List.scala:386)
......
*/

====================
Thread-based joins
====================

object test extends Application {
import events._
class TEST extends Joins {
object A extends AsyncEvent[Unit]
object B extends AsyncEvent[Unit]
object C extends AsyncEvent[Unit]
object D extends AsyncEvent[Unit]
join {
case C(_) and1 D(_) =>
Console.println("case 2")
case A(_) and1 B(_) =>
Console.println("case 1")
}
}

val obj = new TEST

spawn {
obj.C()
obj.B()
obj.A()
}
//no output
//expected output: case 1
/*
spawn {
obj.C()
obj.D()
obj.B()
obj.A()
}
// case 2
*/

Philipp Haller
Joined: 2009-01-13,
User offline. Last seen 42 years 45 weeks ago.
Re: Join definitions in Scala

Hi,

> def act() {
> receive {
> case A() and1 B() =>
> println("1")
> case C() and1 D() =>
> println("2")
> case D() and1 A() =>
> println("3")
>
> }
> }

Due to the fact that the pattern matcher optimizes away certain calls of unapply methods, you have to use `and2` in the second case and `and3` in the third case.

This is one of the main reasons why it seemed problematic to try and introduce it in the standard library.

> join {
> case C(_) and1 D(_) =>
> Console.println("case 2")
> case A(_) and1 B(_) =>
> Console.println("case 1")
> }

Again, you have to use `and2` in the second case.

I haven't tried running your code, but I hope these changes fix it.

Cheers,
Philipp

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland