22
33import nl .uu .cs .iss .ga .sim2apl .core .agent .AgentID ;
44import nl .uu .cs .iss .ga .sim2apl .core .deliberation .DeliberationRunnable ;
5+ import nl .uu .cs .iss .ga .sim2apl .core .deliberation .ReschedulableResult ;
56
67import 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 .*;
119import java .util .stream .Collectors ;
1210
1311/**
@@ -29,15 +27,15 @@ public class DefaultBlockingTickExecutor<T> implements TickExecutor<T> {
2927 private final ExecutorService executor ;
3028
3129 /** The list of agents scheduled for the next tick **/
32- private final ArrayList < DeliberationRunnable <T >> scheduledRunnables ;
30+ private final Map < AgentID , DeliberationRunnable <T >> scheduledRunnables ;
3331
3432 /**
3533 * Default constructor
3634 * @param nThreads Number of threads to use to execute the agent's sense-reason-act cycles.
3735 */
3836 public DefaultBlockingTickExecutor (int nThreads ) {
3937 this .executor = Executors .newFixedThreadPool (nThreads );
40- this .scheduledRunnables = new ArrayList <>();
38+ this .scheduledRunnables = new ConcurrentHashMap <>();
4139 }
4240
4341 /**
@@ -60,11 +58,13 @@ public DefaultBlockingTickExecutor(int nThreads, Random random) {
6058 */
6159 @ Override
6260 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 ;
6868 }
6969
7070 /**
@@ -75,7 +75,7 @@ public HashMap<AgentID, List<T>> doTick() {
7575 ArrayList <DeliberationRunnable <T >> runnables ;
7676 // TODO make sure running can only happen once with some sort of mutex? How to verify if a tick is currently being executed?
7777 synchronized (this .scheduledRunnables ) {
78- runnables = new ArrayList <>(this .scheduledRunnables );
78+ runnables = new ArrayList <>(this .scheduledRunnables . values () );
7979 this .scheduledRunnables .clear ();
8080 }
8181
@@ -88,30 +88,21 @@ public HashMap<AgentID, List<T>> doTick() {
8888
8989 long startTime = System .currentTimeMillis ();
9090 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+ }
95102 }
96103 } catch (InterruptedException | ExecutionException e ) {
97104 e .printStackTrace ();
98105 }
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- // }
115106 this .stepDuration = (int ) (System .currentTimeMillis () - startTime );
116107
117108 tick ++;
@@ -150,11 +141,12 @@ public int getLastTickDuration() {
150141 public List <AgentID > getScheduledAgents () {
151142 List <AgentID > scheduledAgents = new ArrayList <>();
152143 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 ());
156148 }
157- return scheduledAgents ;
149+ // return scheduledAgents;
158150 }
159151
160152 /**
0 commit comments