Skip to content

Commit 59a964b

Browse files
committed
Merge remote-tracking branch 'origin/fix/86-clockmanager-behavior' into develop
2 parents c0542e7 + 5ac4d55 commit 59a964b

File tree

5 files changed

+360
-6
lines changed

5 files changed

+360
-6
lines changed

CHANGELOG.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,28 @@ This file lists modifications introduced by each version.
44

55
## [Unreleased]
66

7+
### ClockManager class modifications
8+
9+
**Bug fixes in ClockManager:**
10+
11+
[[issue 86]][issue: #86]
12+
13+
* The modification of the `ClockManager`'s internal time value (the one actually used by the `Topology`) now coincides
14+
with the ticking of the `Clock`.
15+
16+
Now, the clock is first incremented and then the `Scheduler` is called.
17+
It was previously swapped, resulting in some user interactions happening before the call to the `Scheduler` (and thus
18+
the `MessageEngine` and all `Node.onClock()`).
19+
Everything is now properly synchronized.
20+
21+
**Symbol modification in ClockManager**
22+
23+
* `ClockManager.currentTime()` now returns an `int` instead of an `Integer`
24+
25+
This value could never be `null`. The return value being an object was a remnant of previous versions.
26+
27+
[issue: #86]: https://github.com/jbotsim/JBotSim/issues/86
28+
729
### New icon in the jbotsim-icons module
830

931
`jbotsim-icons`/`io.jbotsim.ui.icons`
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
* Copyright 2008 - 2019, Arnaud Casteigts and the JBotSim contributors <contact@jbotsim.io>
3+
*
4+
*
5+
* This file is part of JBotSim.
6+
*
7+
* JBotSim is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU Lesser General Public License as published by
9+
* the Free Software Foundation, either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* JBotSim is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU Lesser General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU Lesser General Public License
18+
* along with JBotSim. If not, see <https://www.gnu.org/licenses/>.
19+
*
20+
*/
21+
22+
package io.jbotsim.core;
23+
24+
import io.jbotsim.core.event.ClockListener;
25+
import io.jbotsim.ui.JViewer;
26+
27+
/**
28+
* This class purpose is to check/showcase behaviors linked to the ClockManager
29+
*/
30+
public class ClockManagerTestMain {
31+
public static void main(String[] args) {
32+
Topology topology = new Topology();
33+
topology.setDefaultNodeModel(ClockNode.class);
34+
topology.setTimeUnit(2000);
35+
36+
topology.addClockListener(new MyClockListener(topology));
37+
38+
topology.addNode(100, 100);
39+
40+
new JViewer(topology);
41+
42+
// topology.start();
43+
}
44+
45+
public static class ClockNode extends Node {
46+
public ClockNode() {
47+
// msg("constructor");
48+
}
49+
50+
@Override
51+
public void onStart() {
52+
msg("onStart");
53+
}
54+
55+
@Override
56+
public void onStop() {
57+
msg("onStop");
58+
}
59+
60+
@Override
61+
public void onPreClock() {
62+
msg("onPreClock");
63+
}
64+
65+
@Override
66+
public void onClock() {
67+
msg("onClock");
68+
}
69+
70+
@Override
71+
public void onPostClock() {
72+
msg("onPostClock");
73+
}
74+
75+
@Override
76+
public void onSelection() {
77+
msg("onSelection");
78+
}
79+
80+
private void msg(String methodName) {
81+
System.out.println("Id=" + getID() + " " + methodName + ": " + getTime());
82+
}
83+
}
84+
85+
private static class MyClockListener implements ClockListener {
86+
private final Topology topology;
87+
88+
public MyClockListener(Topology topology) {
89+
this.topology = topology;
90+
printTime();
91+
}
92+
93+
@Override
94+
public void onClock() {
95+
printTime();
96+
}
97+
98+
private void printTime() {
99+
System.out.println("listener.printTime: " + topology.getTime());
100+
}
101+
}
102+
}

build.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ subprojects {
5959

6060

6161
dependencies{
62+
testCompile(
63+
'org.mockito:mockito-core:+'
64+
)
6265
testImplementation(
6366
'org.junit.jupiter:junit-jupiter-api:5.1.0',
6467
'org.junit.jupiter:junit-jupiter-params:5.1.0',

lib/jbotsim-core/src/main/java/io/jbotsim/core/ClockManager.java

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,40 @@
3131
* <p>The {@link ClockManager} is used by the {@link Topology} object to implement its clock mechanism.</p>
3232
*/
3333
public class ClockManager {
34+
final static int CLOCK_INITIAL_VALUE = 0;
35+
3436
Topology tp;
3537
HashMap<ClockListener, Integer> listeners = new HashMap<>();
3638
HashMap<ClockListener, Integer> countdown = new HashMap<>();
3739
Class<? extends Clock> clockModel = null;
3840
Clock clock = null;
39-
Integer time = 0;
40-
Integer timeUnit = 10; // duration of a round in ms
41+
int time = CLOCK_INITIAL_VALUE;
42+
int timeUnit = 10; // duration of a round in ms
4143
int nbPauses = 0;
44+
private boolean firstRound = true;
4245

4346
ClockManager(Topology topology) {
4447
this.tp = topology;
4548
}
4649

4750
public void onClock() {
51+
incrementTime();
52+
callScheduler();
53+
}
54+
55+
private void incrementTime() {
56+
/*
57+
* The time is not incremented after resets in order to be sure that:
58+
* - the first onClock() calls match the CLOCK_INITIAL_VALUE and,
59+
* - the value of the clock match for first onStart() and onClock() calls
60+
*/
61+
if(firstRound)
62+
firstRound = false;
63+
else
64+
time++;
65+
}
66+
67+
private void callScheduler() {
4868
List<ClockListener> expiredListeners = new ArrayList<>();
4969
for (ClockListener cl : listeners.keySet()) {
5070
countdown.put(cl, countdown.get(cl) - 1);
@@ -54,7 +74,6 @@ public void onClock() {
5474
}
5575
}
5676
tp.getScheduler().onClock(tp, expiredListeners);
57-
time++;
5877
}
5978

6079
/**
@@ -138,7 +157,7 @@ public void setTimeUnit(int timeUnit){
138157
* Returns the current round number.
139158
* @return the current time.
140159
*/
141-
public Integer currentTime() {
160+
public int currentTime() {
142161
return time;
143162
}
144163

@@ -200,9 +219,10 @@ public void resume() {
200219
}
201220

202221
/**
203-
* Sets the clock time to 0.
222+
* Sets the clock time back to 0.
204223
*/
205224
public void reset() {
206-
time = 0;
225+
firstRound = true;
226+
time = CLOCK_INITIAL_VALUE;
207227
}
208228
}

0 commit comments

Comments
 (0)