In the previous post on Event Driven Architecture, an example was presented for the Observer Pattern. Here is another example with the message information contained in the EventArgs. Thus, we can create Agents and have then subscribe to the MessageBoard and receive the message published by other agents. In this case, we have two methods, OnARIMA that publishes the results of an ARIMA time series model, and OnSV that publishes the results of a stochastic volatility model. The result of the code below is:
Agent 1 receives the message: ARIMA estimate is High.
Agent 2 receives the message: ARIMA estimate is High.
Agent 1 receives the message: SV estimate is Low.
Agent 3 receives the message: SV estimate is Low.
Using System;
public class Subscriber{
private string m_AgentName;
public Subscriber( string name)
{
m_AgentName=name;
}
public void OnReceptionOfMessage(Object sender, EventArgs e)
{
MessageEventArgs msg=e as MessageEventArgs;
if( msg !=null){
Console.WriteLine ( m_AgentName + " receives the message: " +((MessageEventArgs)e).GetMessageInfo());
}
}
}
//The Event Body
public class MessageArgs:EventArgs{
private string m_Message;
public string GetMessageInfo(){return m_Message;}
}
public delegate void MessageEventHandler(Object sender, EventArgs e);
//Messages are published by triggering events.
public class MessageBoard{
public event MessageEventHandler ARIMA;
public event MessageEventHandler SV;
public void OnARIMA(MessageEventArgs modelReport)
{ if( ARIMA !=null)
ARIMA(this, modelReport);
}
public void OnSV(MessageEventArgs modelReport){
if( SV !=null)
SV(this, modelReport);
}
}
public class AgentModel{
public static void Main ()
{
MessageBoard msgBoard = new MessageBoard();
//Create Subscribers
Subscriber Agent1 = new Subscriber("Agent 1");
Subscriber Agent2 = new Subscriber("Agent 2");
Subscriber Agent3 = new Subscriber("Agent 3");
//Link agents to the messageboard
MsgBoard.ARIMA += Agent1.OnReceptionofMessage;
MsgBoard.ARIMA += Agent2.OnReceptionofMessage;
MsgBoard.SV += Agent1.OnReceptionofMessage;
MsgBoard.SV += Agent3.OnReceptionofMessage;
//Publish to the message board the result of the analysis
MsgBoard.OnARIMA(new MessageArgs("ARIMA estimate is High");
MsgBoard.OnSV(new MessageArgs("SV estimate is Low");
}
}
Wednesday, January 28, 2009
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment