Create an operation to filter the data to post

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.

random walk example 2


An operation to filter the walkers

In our example, we want that a walker goes from a model to another. As a consequence, we define a boundary (a vertical limit) and each simulation step, we check which agent has crossed that limit. Then we returns a list of walker to remove from the local model and to sent through the coupling-artefact. We define an operation for each coupling-artefact.

In order to implement an operation, you need to extend the Operation interface. The apply method is used to implement the core of the operation.

public class OperationFilterLeftSide implements Operation {

  /**
   * X boundary (e.g. when a turtle cross the vertical line at
   * xCor_verticalLimitLeft, it is removed from the model
   */
  private int xCor_verticalLimitLeft;

  /**
   * The id of the model
   */
  private int modelId;

  public OperationFilterLeftSide(int xCor, int myModelId) {
    this.xCor_verticalLimitLeft = xCor;
    this.modelId = myModelId;
  }

  public SimulData apply(SimulData someKindOfData) {

    FilteredTurtles res = new FilteredTurtles();
    ArrayList<Walker> turtlesToRemove = new ArrayList<Walker>();

    // check the class type
    if (someKindOfData.getClass().equals(ExchangedTurtles1.class||
              someKindOfData.getClass
().equals(ExchangedTurtles2.class)) {

      // get the initial list of positions
      ET eT = (ETsomeKindOfData;
      ArrayList<Walker> walkers = eT.getTurtle();

      // we are looking at the turtle to keep and those to remove from
      // the model
      for (Walker tortuga : walkers) {
       
        if (tortuga.getXPos() < xCor_verticalLimitLeft) {
          // delete this turtle
          turtlesToRemove.add(tortuga);
        }
      }

    else {
      System.err
          .println("Trying to apply an operation on the wrong type of class");
      System.exit(666);
    }
    res.setFilteredTurtleList(turtlesToRemove);
    return res;
  }

  public int getModelId() {
    return modelId;
  }
}