Creating a node algorithm

Node algorithms (aka distributed algorithm) are created in three steps:

  1. Create a class that extends Node
  2. Write the algorithm through overriding methods
  3. Tell JBotSim to use your type of nodes

Here is a skeleton of algorithm where four methods are overridden

import io.jbotsim.core.Node;
import io.jbotsim.core.Message;

public class EmptyNode extends Node{
    public void onStart() {
        // JBotSim executes this method on each node upon initialization

    public void onSelection() {
        // JBotSim executes this method on a selected node

    public void onClock() {
        // JBotSim executes this method on each node in each round

    public void onMessage(Message message) {
        // JBotSim executes this method on a node every time it receives a message

Thus, coding an algorithm boils down to insert code in the above method. Most algorithms can be specified using 2 or 3 such methods. Other methods exist, and you can add your own.

Once your class is ready, you can tell JBotSim to use it as the default model. This is done through calling setDefaultNodeModel() on the topology object, passing it your class of node. Typically, this is made in the main() method, itself being located either in the same class (not very clean) or in a dedicated class, as below.

import io.jbotsim.core.Topology;
import io.jbotsim.ui.JViewer;

public class Main{
    public static void main(String[] args){
        Topology tp = new Topology();
        new JViewer(tp);

Let us now write a node algorithm that effectively does something, namely a broadcasting algorithm using messages →.