私は経験豊富な Java プログラマーで、アクター ベースの Scala アプリケーションの開発を始めています。私が現在開発中のアプリケーションでは、自律的および反応的な動作の両方を示す Sender アクターの実装に対処する必要があります。シナリオは次のとおりです (疑似コード)。
Actor Sender{
Active behavior (must be active once the actor boots):
do-in-sequence{
send to Stdout A
send to Stdout B
send to Stdout C
send stop to Stdout and then exit
}
Reactive behavior (must be active once the actor boots):
as soon as received stop from StopNotifier -> send stop to Stdout and then exit
}
}
Actor Stdout{
Purely reactive behavior (i.e. wait for msg){
as soon as received A -> print A
as soon as received B -> print B
as soon as received C -> print C
as soon as received stop from Sender -> exit
}
}
Actor StopNotifier
Purely active behavior {
compute, and when some condition is met -> send stop to Sender
}
私の質問は、自律性と反応性を統合する必要がある Scala アクターの自律的な動作を表現する最良の方法はどれですか (このペーパーで説明されているように)?
言い換えれば、上記の例で Sender アクターをコーディングする最良の方法/スタイルはどれですか?
私は解決策を思いつきました(以下に報告されています)が、私はスカラの第一人者ではないので(まだ:))、実装したものがより良い/より良い解決策で改善できるかどうか知りたいです.
case object START
case object A
case object B
case object C
case object SENT_A
case object SENT_B
case object ACK_A
case object ACK_B
case object ACK_C
case object STOP
class Sender(stdout: Stdout) extends Actor {
def act() {
self!START
while (true){
receive {
case START =>
stdout!?A
self!SENT_A
case SENT_A =>
stdout!?B
self!SENT_B
case SENT_B =>
stdout!?C
stdout!?STOP
exit()
case STOP => {
Console.println("[Sender:]Received STOP, terminating")
stdout!?STOP
exit()
}
}
}
}
}
class Stdout() extends Actor {
def act() {
while (true) {
receive{
case A =>
Console.println("[Stdout:]A")
reply(ACK_A)
case B =>
Console.println("[Stdout:]B")
reply(ACK_B)
case C =>
Console.println("[Stdout:]C")
reply(ACK_C)
exit()
case STOP =>
Console.println("[Stdout:]Received STOP, terminating")
exit()
}
}
}
}
class StopNotifier(sender: Sender) extends Actor {
def act() {
/*
* The idea is that the StopNotifier should send a STOP message to the Sender
* when a certain condition is met.
* The sleep used here is just a semplification, since the detection of such
* a condition is not relevant for the example.
*/
Thread.sleep(200)
Console.println("[StopNotifier:]Sending STOP to sender")
sender ! STOP
exit()
}
}
object app extends Application {
val stdout = new Stdout
stdout.start
val sender = new Sender(stdout)
sender.start
val stopNotifier = new StopNotifier(sender)
stopNotifier.start
}
特に、現在の実装で気になるのは、StopNotifier からの STOP メッセージの受信に迅速に対応できるようにするために、Sender の各実行ステップでメッセージを自己送信する必要があったという事実です(つまり、 A、B を Stdout アクターに送信します)。物事を行う正しい方法であるには、私にはトリッキーすぎるようです:)。
私は他のscala言語構造(非同期送信、反応など)を使用して他のソリューションを開発しようとしましたが、何らかの形で他の問題/トリックに影響を与えているように見えました.
scala アクターの自律性とリアクティブ動作の統合を処理するためのより良いソリューションを持っている人はいますか?