我尝试实现使用阿卡演员不同的行为。 在我的方法,特征定义消息处理的行为。 具体演员拌入这些特质,然后挑选其中的行为,他们希望通过使用部分功能链接建立他们的接收功能时。
不幸的是,它似乎还有考虑的发件人的instanciation一些麻烦。 如下面的控制台消息所示爱丽丝无法indetify消息“好”,这是摆锤的发送者。
艾丽斯发送罚款? 到鲍勃鲍勃回复良好至演员[阿卡:// StrategiesSystem /用户/爱丽丝]甲收到好从演员[阿卡:// StrategiesSystem / deadLetters]
正如您将在我的代码中看到预期的结果是,鲍勃应该停止这是情况并非如此。
您的帮助将aprreciated。
提前致谢,
最大。
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
}