2014-02-12 17 views
1

我嘗試使用Akka actors來實現不同的行爲。在我的方法中, 特徵定義了消息處理行爲。具體的演員混合在這些特質中,然後通過使用部分函數鏈接來構建他們的接收函數時,選擇他們想要的行爲。Akka:部分函數鏈接和發送者行爲

不幸的是,考慮到消息的發送者 的實例,似乎存在一些麻煩。如以下控制檯消息所示,愛麗絲不能確定 消息「好」的發送者是否是鮑勃。

alice send Fine?鮑勃 鮑勃回答好到演員[阿卡:// StrategiesSystem /用戶/愛麗絲] Alice收到良好的演員[阿卡:// StrategiesSystem/deadLetters]

正如你會在我的代碼看到預期的結果是,鮑勃應該停止,而不是這種情況。

您的幫助將被優惠。

在此先感謝,

最大。

import akka.actor._ 
import scala.util.Random 

//The messages which can be exchanged 
sealed trait Move 
case object StartMessage extends Move 
case object StopMessage extends Move 
case object Fine extends Move 
case object Good extends Move 
case object Bad extends Move 

//The general class representing my actors 
abstract class MyActor(val others: List[String]) extends Actor{ 
    //its name 
    val name=self.path.name 

    //it knows to choose on interlocutor 
    val interlocteur=Random.shuffle(others.filterNot(p=> p==name)).head 

    //All the actors are able to interpret the start/stop messages 
    def metaReceive : Receive= { 
     case StartMessage =>//start to ask question 
      println(name+" send Fine? to "+interlocteur) 
      context.actorSelection("../"+interlocteur) ! Fine 
     case StopMessage => 
      println(name+" stops") 
      context.stop(self) 
    } 
} 

//An optimistic actor says it is fine 
trait Optimistic{ 
    self: MyActor => 
    def handleFine:Receive = { 
    case Fine => 
     println(self.name+" replies Good to "+sender) 
     sender ! Good 
    } 
} 

//A pessimistic actor says it is not fine 
trait Pessimistic{ 
    self: MyActor => 
    def handleFine:Receive = { 
    case Fine => 
     println(self.name+" replies Bad to "+sender) 
     sender ! Bad 
    } 
} 

//An interpretor is an actor which is able to understand the reply 
trait Interpretor{ 
    self: MyActor => 
    def handleAnswer:Receive = { 
     case Good => 
      println(name+" receives Good from "+sender) 
      sender ! StopMessage 
     case Bad => 
      println(name+" receives Bad from "+sender) 
      sender ! StopMessage 
    } 
} 

//My basic classes 
class MyOptimisticActor(others: List[String]) extends MyActor(others) with Optimistic{ 
    override def receive = metaReceive orElse handleFine //orElse ... 
} 

class MyPessimisticActor(others: List[String]) extends MyActor(others) with Pessimistic{ 
    override def receive = metaReceive orElse handleFine //orElse ... 
} 
class MyInterpretorActor(others: List[String]) extends MyActor(others) with Interpretor{ 
    override def receive = metaReceive orElse handleAnswer 
} 

//My application 
object TestStrategies extends Application { 
    val system = ActorSystem("StrategiesSystem") 
    val names= List("alice","bob","carla") 
    val alice = system.actorOf(Props(new MyInterpretorActor(names)), name = "alice")// alice is able to ask question and interpret answer 
    val bob = system.actorOf(Props(new MyOptimisticActor(names)), name = "bob") // bob is able to reply and it is fine 
    val carla = system.actorOf(Props(new MyPessimisticActor(names)), name = "carla") //carla is able to reply and it is not fine 
    alice ! StartMessage //alice must ask a question 
} 

回答

1

不要使用

self: MyActor => 

使用

this: MyActor => 

代替。

+0

你能解釋一下這個區別嗎?特別是它修復了什麼? –

+0

self:MyActor =>覆蓋Akka Actor特徵中的隱式最終值自我。 –

+0

另外,將以下選項添加到scalac中:-Ywarn-shadowing –