How to create the model-artefacts for the two Netlogo simulators.
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.
Implementation : model-artefact
The process is very similar to the
one describe in
the previous tutorial
(extend the
Interface
GenericModelArtefact).
We just need to add
methods in each model-artefacts in order to
deal with the deletion and the addition of a walker in the model.
Functions
Add (remove) a walker to (from) a model
Since I found that Netlogo had a strange behaviour with the "die" command, I use "ht" and "st" (hide and show turtle commands) to make the walkers disappear (appear) from (to) a model or. This is a hack due to Netlogo, if you want to do the same with another simulator, be sure that your simulator implement a method that allows you to add or remove an agent (e.g. a walker, a turtle...)
/**
* Add a turtle in the center
*/
private void addWalker(Walker tortuga) {
String cmd = "ask walker "+tortuga.getID()+" [ setxy 0 0 st set heading " + tortuga.getHeading() + " ]";
try {
App.app.command(cmd);
} catch (CompilerException e) {
e.printStackTrace();
}
}
public void removeWalkers(ArrayList<Walker> walkersToRemove) {
for (Walker tortuga : walkersToRemove) {
removeWalker(tortuga);
}
}
private void removeWalker(Walker tortuga) {
String cmd = "ask walker "+tortuga.getID()+" [ ht ]";
try {
App.app.command(cmd);
} catch (CompilerException e) {
e.printStackTrace();
}
}