2
2
3
3
import nl .uu .cs .iss .ga .sim2apl .core .agent .AgentID ;
4
4
import nl .uu .cs .iss .ga .sim2apl .core .deliberation .DeliberationRunnable ;
5
+ import nl .uu .cs .iss .ga .sim2apl .core .deliberation .ReschedulableResult ;
5
6
6
7
import java .util .*;
7
- import java .util .concurrent .ExecutionException ;
8
- import java .util .concurrent .ExecutorService ;
9
- import java .util .concurrent .Executors ;
10
- import java .util .concurrent .Future ;
8
+ import java .util .concurrent .*;
11
9
import java .util .stream .Collectors ;
12
10
13
11
/**
@@ -29,15 +27,15 @@ public class DefaultBlockingTickExecutor<T> implements TickExecutor<T> {
29
27
private final ExecutorService executor ;
30
28
31
29
/** The list of agents scheduled for the next tick **/
32
- private final ArrayList < DeliberationRunnable <T >> scheduledRunnables ;
30
+ private final Map < AgentID , DeliberationRunnable <T >> scheduledRunnables ;
33
31
34
32
/**
35
33
* Default constructor
36
34
* @param nThreads Number of threads to use to execute the agent's sense-reason-act cycles.
37
35
*/
38
36
public DefaultBlockingTickExecutor (int nThreads ) {
39
37
this .executor = Executors .newFixedThreadPool (nThreads );
40
- this .scheduledRunnables = new ArrayList <>();
38
+ this .scheduledRunnables = new ConcurrentHashMap <>();
41
39
}
42
40
43
41
/**
@@ -60,11 +58,13 @@ public DefaultBlockingTickExecutor(int nThreads, Random random) {
60
58
*/
61
59
@ Override
62
60
public boolean scheduleForNextTick (DeliberationRunnable <T > agentDeliberationRunnable ) {
63
- if (!this .scheduledRunnables .contains (agentDeliberationRunnable )) {
64
- this .scheduledRunnables .add (agentDeliberationRunnable );
65
- return true ;
66
- }
67
- return false ;
61
+ this .scheduledRunnables .put (agentDeliberationRunnable .getAgentID (), agentDeliberationRunnable );
62
+ // if (!this.scheduledRunnables.containsKey(agentDeliberationRunnable.getAgentID())) {
63
+ // this.scheduledRunnables.add(agentDeliberationRunnable);
64
+ // return true;
65
+ // }
66
+ // return false;
67
+ return true ;
68
68
}
69
69
70
70
/**
@@ -75,7 +75,7 @@ public HashMap<AgentID, List<T>> doTick() {
75
75
ArrayList <DeliberationRunnable <T >> runnables ;
76
76
// TODO make sure running can only happen once with some sort of mutex? How to verify if a tick is currently being executed?
77
77
synchronized (this .scheduledRunnables ) {
78
- runnables = new ArrayList <>(this .scheduledRunnables );
78
+ runnables = new ArrayList <>(this .scheduledRunnables . values () );
79
79
this .scheduledRunnables .clear ();
80
80
}
81
81
@@ -88,30 +88,21 @@ public HashMap<AgentID, List<T>> doTick() {
88
88
89
89
long startTime = System .currentTimeMillis ();
90
90
try {
91
- List <Future <List <T >>> currentAgentFutures = this .executor .invokeAll (runnables );
92
- for (int i = 0 ; i < currentAgentFutures .size (); i ++) {
93
- agentPlanActions .put (runnables .get (i ).getAgentID (),
94
- currentAgentFutures .get (i ).get ().stream ().filter (Objects ::nonNull ).collect (Collectors .toList ())); // TODO will this work?
91
+ List <Future <ReschedulableResult <T >>> currentAgentFutures = this .executor .invokeAll (runnables );
92
+ // for(int i = 0; i < currentAgentFutures.size(); i++) {
93
+ // agentPlanActions.put(runnables.get(i).getAgentID(),
94
+ // currentAgentFutures.get(i).get().getResult().stream().filter(Objects::nonNull).collect(Collectors.toList())); // TODO will this work?
95
+ // }
96
+ for (Future <ReschedulableResult <T >> futures : currentAgentFutures ) {
97
+ ReschedulableResult <T > result = futures .get ();
98
+ agentPlanActions .put (result .getAgentID (), result .getResult ().stream ().filter (Objects ::nonNull ).collect (Collectors .toList ()));
99
+ if (result .isReschedule ()) {
100
+ this .scheduleForNextTick (result .getDeliberationRunnable ());
101
+ }
95
102
}
96
103
} catch (InterruptedException | ExecutionException e ) {
97
104
e .printStackTrace ();
98
105
}
99
- // for(DeliberationRunnable<T> dr : runnables) {
100
- // try {
101
- // List<T> currentAgentActions = this.executor.submit(dr).get();
102
- // currentAgentActions = currentAgentActions.stream().filter(Objects::nonNull).collect(Collectors.toList());
103
- //
104
- // List<String> currentAgentActionStrings = new ArrayList<>();
105
- // for (Object action: currentAgentActions) {
106
- // currentAgentActionStrings.add((String) action);
107
- // }
108
- //
109
- // agentPlanActions.put(dr.getAgentID(), currentAgentActionStrings);
110
- //
111
- // } catch (InterruptedException | ExecutionException e) {
112
- // e.printStackTrace();
113
- // }
114
- // }
115
106
this .stepDuration = (int ) (System .currentTimeMillis () - startTime );
116
107
117
108
tick ++;
@@ -150,11 +141,12 @@ public int getLastTickDuration() {
150
141
public List <AgentID > getScheduledAgents () {
151
142
List <AgentID > scheduledAgents = new ArrayList <>();
152
143
synchronized (this .scheduledRunnables ) {
153
- for (DeliberationRunnable runnable : this .scheduledRunnables ) {
154
- scheduledAgents .add (runnable .getAgentID ());
155
- }
144
+ // for(DeliberationRunnable<T> runnable : this.scheduledRunnables) {
145
+ // scheduledAgents.add(runnable.getAgentID());
146
+ // }
147
+ return new ArrayList <>(this .scheduledRunnables .keySet ());
156
148
}
157
- return scheduledAgents ;
149
+ // return scheduledAgents;
158
150
}
159
151
160
152
/**
0 commit comments