JBotSim
JBotSim copied to clipboard
Strange behavior of clocks
Hi,
The attached program aims at comparing behaviours of DefaultClock and JClock objects.
Here is what I have noticed:
- First, clocks behave differently. Even if implementations differ, they are just timers, so it's quiet surprising.
- Second, if you play with the timeunit and the interval of time between two node creation, you'll see strange behaviours of both clocks.
- With
JClock, node creation increments time. - With
DefaultClock, all nodes are created in the same round.
- With
Note JClock behaves also differently out of the IDE; I'm wondering why....
Also, I used the fix proposed in issue: #89.
Perhaps I miss coffee this morning :)
package io.jbotsim.core;
import io.jbotsim.core.event.ClockListener;
import io.jbotsim.ui.JClock;
public class ClocksBehaviorTest {
static Topology topology;
static final int TIMEUNIT = 3_000;
static final int NODE_CREATION_DELAY = TIMEUNIT / 3;
public static void main(String[] args) {
topology = new Topology();
topology.setDefaultNodeModel(ClockNode.class);
topology.setTimeUnit(TIMEUNIT);
topology.addClockListener(new MyClockListener());
//topology.setClockModel(JClock.class);
topology.setClockModel(DefaultClock.class);
topology.start();
try {
System.err.println("TIME UNIT=" + TIMEUNIT);
System.err.println("CRT NODE UNIT=" + NODE_CREATION_DELAY);
Thread.sleep(2 * TIMEUNIT);
for (int i = 0; i < 20; i++) {
topology.addNode(new ClockNode());
Thread.sleep(NODE_CREATION_DELAY);
topology.removeNode(topology.getNodes().get(0));
}
Thread.sleep(4_000);
System.exit(0);
} catch (InterruptedException e) {
e.printStackTrace(System.err);
}
}
public static class ClockNode extends Node {
@Override
public void setID(int ID) {
super.setID(ID);
msg("add node ");
}
@Override
public void onStart() {
msg("onStart");
}
@Override
public void onStop() {
msg("onStop");
}
@Override
public void onClock() {
msg("onClock");
}
private void msg(String method) {
System.out.println("Id=" + getID() + " " + method + ": " + topology.getTime());
}
}
private static class MyClockListener implements ClockListener {
@Override
public void onClock() {
printTime();
}
private void printTime() {
System.out.println("listener.printTime: " + topology.getTime());
}
}
}
@pictavien , could you please the command your run out of the IDE ?
export CLASSPATH=$PWD/apps/test-classes/build/classes/java/main:$PWD/fats/fat-jbotsim-full/build/libs/jbotsim-full-standalone-1.1.1-dev-SNAPSHOT.jar
./gradlew jars
java io.jbotsim.core.ClocksBehaviorTest
By the way, if you have a simpler way to do that, I'll take it.
If I'm not mistaken, thanks to a proposition from @darrivau , if you add a class -- say MyHelloWorldMain.java -- which contains "public static void main(" in the apps/examples module, you should be able to run it executing ./gradlew runMyHelloWorldMain at the root of the repository.
The magic happens in apps/examples/build.gradle.
It worked for me with your file.
Investigation continues...
Using the the fix proposed in #89, I have tested both the JClock and DefaultClock from IDEA and the console (using this method: https://github.com/jbotsim/JBotSim/issues/90#issuecomment-579653921 ).
With the fix:
| JClock - fixed | DefaultClock | |
|---|---|---|
| Console (gradlew) | First onClock on 0 all addNode on 0 very last onClock on 1 |
First two onClock on 0 and 1 all addNode on 1 very last onClock on 2 |
| IDEA | " | " |
With the fix, the behavior is equivalent. I take it that the difference of clock numbers should come the possible start-up time difference possibly induced by:
- the way we implemented the test
- the difference of implementation between the
DefaultClockand theJClock
Without the fix:
| JClock - no-fix | DefaultClock | |
|---|---|---|
| Console (gradlew) | First two onClock on 0 and 1 all addNode on incrementing clock up to 21 last onClock on 22 and 23 |
First two onClock on 0 and 1 all addNode on 1 very last onClock on 2 |
| IDEA | " | " |
From these results and the command line you provided, I think that the JClock behavioring differently in console might come from a non-fixed version of the JClock lingering in your classpath (jbotsim-full-standalone-1.1.1-dev-SNAPSHOT.jar).
@pictavien , would that solve this part of the issue?
Yes, you were right. I missed this detail :)