1 | package genius.core.timeline;
|
---|
2 |
|
---|
3 | /**
|
---|
4 | * A time line, running from t = 0 (start) to t = 1 (deadline). The timeline
|
---|
5 | * renormalizes real time.
|
---|
6 | */
|
---|
7 | @SuppressWarnings("serial")
|
---|
8 | public class ContinuousTimeline extends Timeline {
|
---|
9 | private final int totalSeconds;
|
---|
10 | protected long startTime;
|
---|
11 |
|
---|
12 | /**
|
---|
13 | * Creates a timeline with a deadline of {@link #totalSeconds} number of
|
---|
14 | * seconds.
|
---|
15 | */
|
---|
16 | public ContinuousTimeline(int totalSecs) {
|
---|
17 | totalSeconds = totalSecs;
|
---|
18 | startTime = System.nanoTime();
|
---|
19 | hasDeadline = true;
|
---|
20 | }
|
---|
21 |
|
---|
22 | /**
|
---|
23 | * Gets the elapsed time in seconds. Use {@link #getTime()} for a more
|
---|
24 | * generic version.
|
---|
25 | */
|
---|
26 | public double getElapsedSeconds() {
|
---|
27 | long t2 = System.nanoTime();
|
---|
28 | return ((t2 - startTime) / 1000000000.0);
|
---|
29 | }
|
---|
30 |
|
---|
31 | /**
|
---|
32 | * Gets the elapsed time in seconds. Use {@link #getTime()} for a more
|
---|
33 | * generic version.
|
---|
34 | */
|
---|
35 | public double getElapsedMilliSeconds() {
|
---|
36 | long t2 = System.nanoTime();
|
---|
37 | return ((t2 - startTime) / 1000000.0);
|
---|
38 | }
|
---|
39 |
|
---|
40 | /**
|
---|
41 | * Gets the total negotiation time in miliseconds
|
---|
42 | */
|
---|
43 | public long getTotalMiliseconds() {
|
---|
44 | return 1000 * totalSeconds;
|
---|
45 | }
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * Gets the total negotiation time in seconds
|
---|
49 | */
|
---|
50 | public long getTotalSeconds() {
|
---|
51 | return totalSeconds;
|
---|
52 | }
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * Gets the time, running from t = 0 (start) to t = 1 (deadline). The time
|
---|
56 | * is normalized, so agents need not be concerned with the actual internal
|
---|
57 | * clock.
|
---|
58 | */
|
---|
59 | public double getTime() {
|
---|
60 | double t = getElapsedSeconds() / (double) totalSeconds;
|
---|
61 | if (t > 1)
|
---|
62 | t = 1;
|
---|
63 | return t;
|
---|
64 | }
|
---|
65 |
|
---|
66 | @Override
|
---|
67 | public double getTotalTime() {
|
---|
68 | return getTotalSeconds();
|
---|
69 | }
|
---|
70 |
|
---|
71 | @Override
|
---|
72 | public double getCurrentTime() {
|
---|
73 | return getElapsedSeconds();
|
---|
74 | }
|
---|
75 |
|
---|
76 | public void reset() {
|
---|
77 | startTime = System.nanoTime();
|
---|
78 | }
|
---|
79 | }
|
---|