Scala Actors Send Message to Super Class - scala

I'm trying to make a base class that uses actors, kind of like so:
import scala.actors.Actor
case class FooBar()
class ParentActor extends Actor {
def act {
loop {
react {
case f: FooBar =>
println("Parent Foo")
case _ =>
println("Parent something")
}
}
}
}
And then I want a child class to look like so:
class ChildActor extends ParentActor {
override def act {
loop {
react {
case i: Integer =>
println("Child int")
case default =>
println("Child Base")
super ! default
}
}
}
}
My end goal is to provide a base functionality in the parent actor class that will be executed if the child actor does not react to that case class. I tried doing
super ! Message
But that throws an error:
'.' expected but identifier found.
How can I pass a message from the ChildActor to the base ParentActor act function?

The code which processes the received messages is the PartialFunction which is passed to the react method - in other words it's that case block/closure. It's not possible to call directly any closures which are declared inside other methods (including the superclass' methods), unless that method somehow passes you a reference to the closure instance. And anyways code reuse through inheritance should be avoided.
Since a case block is a PartialFunction, it's possible to compose multiple such blocks using the orElse method. That way you can combine multiple case blocks so that it will fall back to the next one if there is no match for a message.
You don't need inheritance to compose the handlers - just expose the "parent handler" somewhere as a function and compose them before calling react. Or to reduce the boilerplate and make ChildActor unaware of the code in the parent, override the react method in the ParentActor and do the composing of the handlers there before passing it to super.react.

Related

Scala: How to extend an Akka actor?

I wrote an Akka base-actor that can handle some common messages. I want to reuse this basic behavior in a sub-actor, by extending the base-actor (not by composition of the base-actor).
I have seen several approaches in previous questions. They are all valid but also may be improved:
How extend behaviour of super actor in akka
and
How do I best share behavior among Akka actors?
To make the implementation cleaner, I am trying to achieve the following:
When defining the sub-actor, I try to extend only the base-actor (not both Actor and sub-actor). However, I was not able to force the compiler to do this.
I also try to not rename the receive partial function, because it is a kind of convention.
Here is a sample of my implementation:
//This is the base-actor that implements the common behavior
trait BetterActor extends Actor {
abstract override def receive = {
super.receive orElse { case _ => println("Missing pattern!") }
}
}
//This is the actor that implements the additional behavior.
//I actually wanted to extend BetterActor. It was not possible.
class MyActor extends Actor {
def receive = {
case i: Int =>
println(s"Yay!!! Got $i")
}
}
Here are two alternative ways how I can create an instance of the sub-actor, which combines both the common and the additional behaviors:
//1.
val myBetterActor = system.actorOf(Props(new MyActor with BetterActor), "myBetterActor")
//2.
class MyBetterActor extends MyActor with BetterActor
val myBetterActor = system.actorOf(Props[MyBetterActor], "myBetterActor")
Finally, I may invoke the sub-actor by:
myBetterActor ! 2
myBetterActor ! "a"
Problems with my implementation:
When I create an instance of the sub-actor, only then I can mix in the base-actor, either directly (1) or by defining a new class that mixes in the base-actor.
Like I said before, I would prefer to mix in the base-actor when I define the sub-actor. Not when I try to create an instance of the sub-actor.
P.S. It's fascinating that the designers of this framework did not see it necessary to make this common requirement easy to accomplish.
i suppose as long as you're not trying to override behavior with BetterActor, you could try something like this:
trait BetterActor extends Actor {
override def unhandled(message: Any): Unit = message match {
case e: CommonMessage => println("COMMON!")
case e => super.unhandled(e)
}
}
class MyActor extends BetterActor {
override def receive = {
case i: Int => println(s"Yay!!! Got $i")
}
}

Strategy pattern in Akka

This is an continuation of my previous question How do I get around type erasure on Akka receive method
I have 10 type of events which extends from Event that I need to handle.
I want to implement business logic for each event in separate trait, because because mixing all 10 event handler functions will produce several hundreds(if not thousands) lines of code.
I don't want to create different Actor types for each event. For example:
class Event1Actor extend Actor{
def receive ={
case Event1(e) => //event1 Business Logic
}
}
class Event2Actor extend Actor{
def receive ={
case Event2(e) => //event2 Business Logic
}
}
and the same Event3Actor, Event4Actor,etc....
Such code seems ugly to me, because I need to implement business Logic inside each Actor.
Implementing 10 different traits and 10 different Actor classes seems also as bad design.
I'm seeking for some kind generic solution based on design pattern, for example strategy pattern.
case class EventOperation[T <: Event](eventType: T)
class OperationActor extends Actor {
def receive = {
case EventOperation(eventType) => eventType.execute
}
}
trait Event {
def execute //implement execute in specific event class
}
class Event1 extends Event {/*execute implemented with business logic*/}
class Event2 extends Event {/*execute implemented with business logic*/}
hope this is what you are looking for and helps, I have used this patternt to remove the redundant amount of actors wrapping all actions under a single actor executing different type of events.
You could try something like this, which involves auto-composing receive functionality via a base trait. First the code:
case class Event1(s:String)
case class Event2(i:Int)
case class Event3(f:Float)
trait EventHandlingActor extends Actor{
var handlers:List[Receive] = List.empty
override def preStart = {
val composedReceive = handlers.foldLeft(receive)((r,h) => r.orElse(h))
context.become(composedReceive)
}
def addHandler(r:Receive) {
handlers = r :: handlers
}
def receive = PartialFunction.empty[Any,Unit]
}
trait Event1Handling{ me:EventHandlingActor =>
addHandler{
case Event1(s) => println(s"${self.path.name} handling event1: $s")
}
}
trait Event2Handling{ me:EventHandlingActor =>
addHandler{
case Event2(i) => println(s"${self.path.name} handling event2: $i")
}
}
trait Event3Handling{ me:EventHandlingActor =>
addHandler{
case Event3(f) => println(s"${self.path.name} handling event3: $f")
}
}
So you can see in the EventHandlingActor trait we set up a List of type Receive that can be added to by each specific handling trait that we stack into a concrete actor. Then you can see the definitions of the handling functionality for each event defined in a separate trait that is calling addHandler to add another piece of handling functionality. In preStart for any kind of EventHandlingActor impl the receive functions will be composed together with receive being the starting point (empty by default) before hot-swapping out the receive impl with context.become.
Now for a couple of impl actors as an example:
class MyEventHandlingActor extends EventHandlingActor
with Event1Handling with Event2Handling with Event3Handling
case class SomeOtherMessage(s:String)
class MyOtherEventHandlingActor extends EventHandlingActor with Event1Handling{
override def receive = {
case SomeOtherMessage(s) => println(s"otherHandler handling some other message: $s")
}
}
The first one only handles events, so all it needs to do is define which ones it handles my mixing in the appropriate traits. The second one handles one type of event but also some other message that is not an event. This class overrides the default empty receive and provides functionality to handle the non-event message.
If we tested the code like so:
val system = ActorSystem("test")
val handler = system.actorOf(Props[MyEventHandlingActor], "handler")
handler ! Event1("foo")
handler ! Event2(123)
handler ! Event3(123.456f)
val otherHandler = system.actorOf(Props[MyOtherEventHandlingActor], "otherHandler")
otherHandler ! Event1("bar")
otherHandler ! SomeOtherMessage("baz")
Then we would see output similar to this (with the order changing due to asynch handling of messages):
otherHandler handling event1: bar
handler handling event1: foo
handler handling event2: 123
handler handling event3: 123.456

Is it possible to compose FSMs in Akka?

For regular actors, they can be composed. But, I can't seem to find anything about doing it with FSMs. And there isn't a receive block to use += to add things to. Does anyone have any experience trying to generalize an FSM?
I can't provide a code example, because I don't have any code yet because I don't know if I can compose FSMs.
You can compose state behaviour using partial functions in the same way that you compose Actor partial functions. The when() function requires a partial function of type scala.PartialFunction[FSM.this.Event, FSM.this.State]). In the example below, I extend the state Jibber with a partial function (e.g. some common behaviour) declared in the trait. You can use exactly the same technique to extend the onTermination behaviour from a trait using a PF declared as scala.PartialFunction[StopEvent, Unit].
I have lifted significant common behaviour out of some complex FSMs using this technique.
package monster
import akka.actor._
import monster.FlyingSpaghetti._
// State and data objects
object FlyingSpaghetti {
trait State
object Jibber extends State
object Jabber extends State
object Ping
object Done
}
// Trait with common behaviour behaviour
trait FlyingSpaghetti extends Actor with FSM[State,String]{
val layer: StateFunction = {
case Event(Done,s) ⇒
println("Done behaviour layered")
stop()
}
}
class Monster() extends FlyingSpaghetti {
startWith(Jibber,"jabber")
self ! Ping
println("Starting")
// First, do the common behaviour PF then do specialised behaviour
when(Jibber) (layer orElse {
case Event(Ping,"jabber") ⇒
println("jabber")
goto(Jabber) using "jibber"
case Event(Done,s) ⇒
println("Done jabbering")
stop()
})
when(Jabber) {
case Event(Ping,"jibber") ⇒
println("jibber")
goto(Jibber) using "jabber"
case Event(Done,s) ⇒
println("Done jibbering")
stop()
}
}
object Run extends App {
val system = ActorSystem("mySystem")
val rattle = system.actorOf(Props[Monster])
rattle ! Ping
rattle ! Ping
rattle ! Done
Thread.sleep(100)
system.shutdown()
}

scala syntax understanding about loop and receive in actor

I noticed it is legal to write this scala code:
val fussyActor = actor {
loop {
receive {
case s: String => println("I got a String: " + s)
case _ => println("I have no idea what I just got.")
}
}
}
I know from documentation that actor is a trait, which has loop and receive value members. But how is it possible to stack these methods like above? Is it implementing or overriding these methods? I am quite confused at this syntax. Please provide some good references/pointers.
First, a standard disclaimer. Scala Actors have been deprecated in favor of Akka Actors. If you want to continue down the path of learning to use Actors with Scala, you should look into Akka instead of researching Scala Actors.
Now, about your question. There are a couple of things in play here, so let's first start out with what you need to do in order to define a new Scala Actor. If you look at the Scala Actor trait, you see that there is one abstract method that you must provide called act():Unit. This is a method that takes no inputs and returns no inputs. It defines the actors behavior. So in it's simplest form, a custom Scala Actor could be:
class MyActor extends Actor{
def act(){
}
}
Now this not a very interesting actor as it does nothing. Now, one way to provide behavior is to invoke the receive method, providing a PartialFunction[Any,R] where R is a generic return type. You could do that like so:
class MyActor extends Actor{
def act(){
receive{
case "foo" => println("bar")
}
}
}
So now if this actor receives a message "foo", it will print "bar". The problem here is that this will only happen for the first message and then won't do anything after. To fix that. we can wrap our call to receive with a call to loop to make it continue to do the provided receive call for each received message:
class MyActor extends Actor{
def act(){
loop{
receive{
case "foo" => println("bar")
}
}
}
}
So this is now starting to look like your example a bit more. We are leveraging the loop and receive methods that come with the Actor trait in order to give this actor behavior. Lastly, instead of defining an explicit class as my actor, I can define one on the fly with the actor method on the Actor companion object. That method takes a function body that will be used as the act impl like this:
def actor(body: => Unit){
val a = new Actor {
def act() = body
override final val scheduler: IScheduler = parentScheduler
}
}
So in your example, you are creating a new actor implementation on the fly and providing an impl for act that will loop and call receive continually with the partial function you supplied for message handling.
Hopefully this clarifies things a bit. The only method you are "overriding" (providing an impl for) is act. When you see loop and receive, those are not overrides; they are simply calls to those existing methods on the Actor trait.
Actually it's illegal without import Actor._.
Your code without that import:
val fussyActor = Actor.actor {
Actor.loop {
Actor.receive { ... }
}
}
actor, loop and receive are methods of object Actor.
def actor(body: ⇒ Unit): Actor
def loop(body: ⇒ Unit): Unit
def receive[A](f: PartialFunction[Any, A]): A
Method actor accepts by-name Untit parameter body - some code block to execute in separate thread and creates an Actor with act method implemented using parameter body.
Method loop accepts by-name Untit parameter body - some code block to execute in infinite loop.
Method receive accepts PartialFunction as parameter f and calls f with message as parameter. It takes message from actor associated with current thread.
Note that scala actors are deprecated, you should use akka actors. See The Scala Actors Migration Guide.

Passing collections in scala actors

When I need to pass typed collections to an actor, I get an "unchecked" warning in my react method:
val actor = actor {
loop {
react {
case a:List[String] => // do something
}
}
}
How can I work around this? I tried boxing collection in a separate class (but that is ugly and cumbersome), and just casting collection (case a:List[_] => a.asInstanceOf[List[String]]) after receiving it by actor is not type-safe and dangerous.
Because the JVM does not keep track of the type of generics, you can't know that a List[_] is a List[String] unless you examine every element and check that it is a string. Your best bet is in fact to box the collection in a separate class. It need not be so bad!
case class StringsBox(ls: List[String]) {}
//...
myactor ! StringsBox( List("these","are","strings") )
//...
react {
case StringsBox(ls) => /* Now you have your List[String] */
}