How to create the coupling-artefacts in order to exchange the walkers
Example with two Netlogo random walk models
The models used are a simple random
walk where, for each simulation step,
agents (so-called walkers) choose a random direction and step forward.
In our example, the walker can go from one model to another. That is,
we define one boundary in each model, when a walker cross that
boundary, it is sent to the other model.
The Netlogo models specifications available
here and
here.
The exchanged data will be here the walkers positions (x,y) and the
walker ID. The output port of one model is linked to the input of
the
other.
Creating a coupling-artefact
The process to create the coupling-artefact is very similar to
the previous one. The difference lies in the fact that wa have two different coupling-artefacts but still one
input port and one
output port for each. Each port is associated with a class (here the ExchangedTurtles ones).
The coupling-artefact input port
The role of coupling-artefact input port is allow model-agent to
post the data (from the model to the external world). In order to build
a coupling-artefact input
port, you need to extend the abstract calls
InputPort.
An important thing to note is that each coupling-artefact port is
dedicated to a certain class. In our example, we want to post data of
class ExchangedTurtles1 (if they come from the random walk model 1) or
ExchangedTurtles2 (if they come from the random walk model 2).
Since our implementation (darjeeling) relies upon
Java Messaging Service (JMS),
we need to provide some parameters in order for the input port to post
the data to the JMS. These parameters are currently the topic name,
and the connection factory name (for further details see
LinkInstallJMS).
/**
* This class represent one part (IN port) of the coupling artefact that allows
* the agent to read data from it. In this implementation the coupling-artefact
* is in charge of the agents positions.
*
* @author Julien Siebert
* @since Darjeeling
*/
public class IPCA_ExchangedTurtles2 extends InputPort<ExchangedTurtles2> implements
JmsPort {
// jms parameters
private String topicName;
private String connectionFactoryName;
// end of jms parameters
/**
* COnstructor
*/
public IPCA_ExchangedTurtles2(Class<ExchangedTurtles2> ObjectClassToSendToJms,
String NameOfInputPortToJms) {
super(ObjectClassToSendToJms, NameOfInputPortToJms);
setJMSConnexionParameters();
init();
}
public void init() {
try {
jmsInputBinding.init(topicName, connectionFactoryName);
} catch (NamingException e) {
e.printStackTrace();
} catch (JMSException e) {
e.printStackTrace();
}
}
public void setJMSConnexionParameters() {
topicName = "shared_data";
connectionFactoryName = "shared_data_topic_connection_factory";
}
}
The coupling-artefact output port
The role of coupling-artefact output port is allow the model-agent to
read the external data. In order to build a coupling-artefact output
port, you need to extend the abstract calls
OutputPort.
An important thing to note is that each coupling-artefact port is
dedicated to a certain class. In our example, we want to read data of
class ExchangedTurtles1 (if they come from the random walk model 1) or
ExchangedTurtles2 (if they come from the random walk model 2).
Since our implementation (darjeeling) relies upon
Java Messaging Service (JMS),
we need to provide some parameters in order for the output port to read
the data from the JMS. These parameters are currently the topic name,
and the connection factory name (for further details see
LinkInstallJMS).
/**
* This class represent one part (OUT port) of the coupling artefact that allows
* the agent to read data from it. In this implementation the coupling-artefact
* is in charge of the agents positions.
*
* @author Julien Siebert
* @since Darjeeling
*/
public class OPCA_ExchangedTurtles1 extends OutputPort<ExchangedTurtles1> implements
JmsPort {
// jms parameters
private String topicName;
private String connectionFactoryName;
// end of jms parameters
/**
* Constructor
*/
public OPCA_ExchangedTurtles1(Class<ExchangedTurtles1> ObjectClassToReadFromJms,
String NameOfOutputPortFromJms) {
super(ObjectClassToReadFromJms, NameOfOutputPortFromJms);
setJMSConnexionParameters();
init();
}
public void init() {
try {
jmsOutputBinding.init(topicName, connectionFactoryName);
} catch (NamingException e) {
e.printStackTrace();
} catch (JMSException e) {
e.printStackTrace();
}
}
public void setJMSConnexionParameters() {
topicName = "shared_data";
connectionFactoryName = "shared_data_topic_connection_factory";
}
}