サーバーのチェーンに沿ったメッセージフローを表すモデルプログラムがあります。
public class MyModel
{
static bool x_send_msg1 = false; // has X sent msg1?
static bool y_recv_msg1 = false; // has Y received msg1?
static bool y_send_msg1 = false; // has Y sent msg1?
static bool z_send_msg1 = false; // has Z received msg1?
// (etc for more servers and more messages)
[Action]
static void YSendMsg1()
{
// Y sends Msg1 to Z
y_send_msg1 = true;
z_recv_msg1 = true;
}
static bool YSendMsg1Enabled()
{
// in the simplest case, this can happen whenever Y has received the
// message but not yet forwarded it
return y_recv_msg1 && !y_send_msg1;
}
}
もっとたくさんのメッセージがあります。各サーバーとメッセージタイプのEnabled()ロジックは少し異なりますが、状態は似ているので、次のように記述してカプセル化します。
class State
{
public bool send_msg1 = false;
public bool recv_msg1 = false;
}
public static State X = new State();
public static State Y = new State();
次に、カプセル化された状態をアクションで使用します。
[Action]
static void YSendMsg1()
{
// instead of y_qqq above, now we can write Y.qqq:
Y.send_msg1 = true;
Z.recv_msg1 = true;
}
static bool YSendMsg1Enabled()
{
return Y.recv_msg1 && !Y.send_msg1;
}
ただし、NModelでは、この方法でオブジェクトを使用して状態を保持することはできません。チェーン内のサーバーごとに1つずつ、ブール値の繰り返しグループを定義することを回避できる他の方法はありますか?