JBotSim examples

HelloWorld

Very basic HelloWorld that builds a default empty topology. The user can add, delete, or move wireless nodes with the mouse (left click, right click, and drag & drop, respectively). A contextual menu also allows to tune the communication range (or sensing range, if revelant) of the nodes (just click on the picture to run a similar applet in popup window).

HelloWorld.java
import jbotsim.Topology;
import jbotsim.ui.JViewer;

public class HelloWorld {
    public static void main(String[] args) {
        new JViewer(new Topology());
    }
}


  javac -cp jbotsim.jar HelloWorld.java (compilation)
  java -cp jbotsim.jar:. HelloWorld (execution)



RedGreen Algorithms

All the algorithms below implement the same principle (the nodes are colored in red if they are isolated, in green otherwise). They illustrate the various approaches that can be used in JBotSim to code an algorithm. The first two work at a graph level, while the third one works at a message level.
In this first version, every node periodically checks its neighborhood and update its color accordingly.
RedGreenNodeV1.java
import jbotsim.Clock;
import jbotsim.Node;
import jbotsim.event.ClockListener;

public class RedGreenNodeV1 extends Node implements ClockListener{
    public RedGreenNodeV1(){
        Clock.addClockListener(this, 10);
    }
    public void onClock() {
        if (super.getNeighbors().size()>0)
            super.setProperty("color", "green");
        else
            super.setProperty("color", "red");
    }
}

Here is the corresponding main method:
MainV1.java
import jbotsim.Node;
import jbotsim.Topology;
import jbotsim.ui.JViewer;

...
public static void main(String[] args) {
    Node.setModel("default", new RedGreenNodeV1());
    new JViewer(new Topology());
}

In the second version, the nodes update their color whenever a change occur in their connectivity (they are directly notified). (The main method is similar to that of the previous example.)
RedGreenNodeV2.java
import jbotsim.Link;
import jbotsim.Node;
import jbotsim.event.NodeListener;

public class RedGreenNodeV2 extends Node implements NodeListener{
    public RedGreenNodeV2(){
        super.setProperty("color", "red");
        super.addNodeListener(this);
    }
    public void linkAdded(Link l) {
        super.setProperty("color", "green");
    }
    public void linkRemoved(Link l) {
        if (super.getNeighbors().size()==0)
            super.setProperty("color", "red");
    }
    public void nodeMoved(Node n) {}
    public void propertyChanged(Node n, String key) {}
}


The third version reflects a more practical scenario. Here, the nodes don't have direct information about their neighborhood; they acquire it by means of exchanging HELLO messages (or beacons) at regular interval, and update their colors accordingly. (The delay between two consecutive messages is intentionally set to a larger value: 30 time units (=300ms) to make it visible.)
RedGreenNodeV3.java
import jbotsim.Node;
import jbotsim.Clock;
import jbotsim.Message;
import jbotsim.event.ClockListener;
import jbotsim.event.MessageListener;

public class RedGreenNodeV3 extends Node implements ClockListener, MessageListener{
    int lastReceptionDate = -30;
    public RedGreenNodeV3(){
        Clock.addClockListener(this, 30);
        super.addMessageListener(this);
    }
    public void onClock() {
        super.send(null, "HELLO");
        if (lastReceptionDate < Clock.currentTime() - 30)
            super.setProperty("color", "red");
    }
    public void onMessage(Message msg) {
        super.setProperty("color", "green");
        lastReceptionDate=Clock.currentTime();
    }
}



All the above versions of the RedGreen principle were implemented in a distributed fashion (the colors are updated by the nodes themselves). The code below shows a centralized implementation, where a single object updates all the nodes based on listening topological events of the whole network. (The visible behavior of this version is exactly the same as that of the distributed version V2.)
RedGreenCentralized.java
import jbotsim.Link;
import jbotsim.Node;
import jbotsim.Topology;
import jbotsim.event.TopologyListener;

public class RedGreenCentralized implements TopologyListener{

    public void nodeAdded(Node n) {
        n.setProperty("color", n.getNeighbors().size()==0?"red":"green");
    }
    public void linkAdded(Link l) {
        for (Node n : l.endpoints())
            n.setProperty("color", "green");
    }

    public void linkRemoved(Link l) {
        for (Node n : l.endpoints())
            if (n.getNeighbors().size()==0)
                n.setProperty("color", "red");
    }
    public void nodeRemoved(Node n) {}
    public void propertyChanged(Topology t, String key) {}
}

The corresponding main class:
MainCentralized.java
import jbotsim.Topology;
import jbotsim.ui.JViewer;

public class MainCentralized {
    public static void main(String[] args) {
        Topology tp=new Topology();
        tp.addTopologyListener(new RedGreenCentralized());
        new JViewer(tp);
    }
}


RandomWayPoint

A possible adaptation of the famous Random Way Point mobility model (nodes repeatedly move towards a random destination).

RandomWayPoint.java
import java.awt.Point;
import java.util.Random;

import jbotsim.Node;

public class RandomWayPoint{
    private static Random r=new Random();
    public static void init(Node node){
        node.setProperty("target", new Point(r.nextInt(400), r.nextInt(300)));
    }
    public static void move(Node node){
        Point target=(Point)node.getProperty("target");
        node.setDirection(target);
    	node.move(5);
        if(node.distance(target)<5)
            node.setProperty("target", new Point(r.nextInt(400), r.nextInt(300)));
    }
}

And a possible class of Node using RandomWayPoint's capabilities:
RWPNode.java
import jbotsim.Clock;
import jbotsim.Node;
import jbotsim.event.ClockListener;

public class RWPNode extends Node implements ClockListener{
    public RWPNode(){
        RandomWayPoint.init(this);
        Clock.addClockListener(this, 10);
    }
    public void onClock() {
        RandomWayPoint.move(this);
    }
}



Combination of several algorithms

Combines the RandomWayPoint mobility model with the first RedGreen algorithms described above.
RedGreenNodeV4.java
import jbotsim.Clock;
import jbotsim.Node;
import jbotsim.event.ClockListener;

public class RedGreenNodeV4 extends Node implements ClockListener{
    public RedGreenNodeV4(){
        RandomWayPoint.init(this);
        Clock.addClockListener(this, 10);
    }
    public void onClock() {
        RandomWayPoint.move(this);
        if (super.getNeighbors().size()>0)
            super.setProperty("color", "green");
        else
            super.setProperty("color", "red");
    }
}


Biconnecting a network of mobile robots using virtual angular forces

This more complex example accompanies an identically named paper. The principle is to use attraction and repulsion forces together with angular forces to let a network of randomly spread mobile entities self-organize in a biconnected fashion. The interactive demo is available here.


The code is too big to be shown here. Please contact us if you are interested.


Last modified: Fri Jan 8 08:39:22 EST 2010