From 78641cf75f833f2caba666fbd7c79bdab4a29a2e Mon Sep 17 00:00:00 2001
From: David Antunes <david.antunes-da-silva@irit.fr>
Date: Thu, 23 Jun 2022 15:40:13 +0200
Subject: [PATCH] Add isFinished() and waitUntilFinish() in Schedulable
 interface

---
 src/example/philosophes/MainPhilosophe.java        |  6 ++++--
 src/example/philosophes/Philosopher.java           |  6 +++---
 src/mas/core/Schedulable.java                      | 10 ++++++++++
 .../implementation/schedulers/AsyncCycling.java    | 14 ++++++++++++++
 src/mas/implementation/schedulers/FairCycling.java | 14 ++++++++++++++
 .../implementation/schedulers/FairPosCycling.java  | 10 ++++++++++
 .../schedulers/variations/TwoDCycling.java         |  2 +-
 7 files changed, 56 insertions(+), 6 deletions(-)

diff --git a/src/example/philosophes/MainPhilosophe.java b/src/example/philosophes/MainPhilosophe.java
index 64e0367..aaa05b0 100644
--- a/src/example/philosophes/MainPhilosophe.java
+++ b/src/example/philosophes/MainPhilosophe.java
@@ -2,7 +2,6 @@ package example.philosophes;
 
 import mas.core.Schedulable;
 import mas.implementation.schedulers.FairCycling;
-import mas.implementation.schedulers.variations.ThreeStepCycling;
 
 public class MainPhilosophe {
 
@@ -28,7 +27,7 @@ public class MainPhilosophe {
             philosophers[i].setRightPhilosopher(philosophers[(i+1) % nAgents]);
         }
 
-        Schedulable scheduler = new ThreeStepCycling(philosophers);
+        Schedulable scheduler = new FairCycling(philosophers);
         scheduler.setSleep(100);
         scheduler.start();
 
@@ -47,6 +46,7 @@ public class MainPhilosophe {
 
         scheduler.resume();
 
+
         try {
             Thread.sleep(5000);
         } catch (InterruptedException e) {
@@ -55,6 +55,8 @@ public class MainPhilosophe {
 
         scheduler.stop();
 
+        scheduler.waitUntilFinish();
+
 
         final long endTime = System.nanoTime();
         System.out.println("Total execution time: " + (endTime / 1000000 - startTime / 1000000) + " microseconds");
diff --git a/src/example/philosophes/Philosopher.java b/src/example/philosophes/Philosopher.java
index 18831e4..5ae3fb2 100644
--- a/src/example/philosophes/Philosopher.java
+++ b/src/example/philosophes/Philosopher.java
@@ -65,13 +65,13 @@ public class Philosopher extends Agent {
 
     @Override
     public void perceive() {
-        System.out.println("Philosopher num " + id + " perceive");
+        //System.out.println("Philosopher num " + id + " perceive");
         criticallity = computeCriticallity();
     }
 
     @Override
     public void decide() {
-        System.out.println("Philosopher num " + id + " decide");
+        //System.out.println("Philosopher num " + id + " decide");
 
         State nextState = state;
         switch (state) {
@@ -118,7 +118,7 @@ public class Philosopher extends Agent {
 
     @Override
     public void act() {
-        System.out.println("Philosopher num " + id + " act");
+        //System.out.println("Philosopher num " + id + " act");
         scheduler.addCyclable(new Waste(id));
     }
 
diff --git a/src/mas/core/Schedulable.java b/src/mas/core/Schedulable.java
index a868b93..f090832 100644
--- a/src/mas/core/Schedulable.java
+++ b/src/mas/core/Schedulable.java
@@ -50,4 +50,14 @@ public interface Schedulable {
      * @return
      */
     boolean stopCondition();
+
+    /**
+     * TODO
+     */
+    boolean isFinished();
+
+    /**
+     * TODO
+     */
+    void waitUntilFinish();
 }
diff --git a/src/mas/implementation/schedulers/AsyncCycling.java b/src/mas/implementation/schedulers/AsyncCycling.java
index 2b5073c..1e09c47 100644
--- a/src/mas/implementation/schedulers/AsyncCycling.java
+++ b/src/mas/implementation/schedulers/AsyncCycling.java
@@ -74,6 +74,20 @@ public class AsyncCycling implements Schedulable {
         return false;
     }
 
+    @Override
+    public boolean isFinished() {
+        return executor.isTerminated();
+    }
+
+    @Override
+    public void waitUntilFinish() {
+        try {
+            executor.awaitTermination(Long.MAX_VALUE,TimeUnit.NANOSECONDS);
+        } catch (InterruptedException e) {
+            e.printStackTrace();
+        }
+    }
+
     @Override
     public int getSleep() {
         return sleep;
diff --git a/src/mas/implementation/schedulers/FairCycling.java b/src/mas/implementation/schedulers/FairCycling.java
index 2383a56..429e191 100644
--- a/src/mas/implementation/schedulers/FairCycling.java
+++ b/src/mas/implementation/schedulers/FairCycling.java
@@ -63,6 +63,20 @@ public class FairCycling implements Schedulable {
         return false;
     }
 
+    @Override
+    public boolean isFinished() {
+        return executor.isTerminated();
+    }
+
+    @Override
+    public void waitUntilFinish() {
+        try {
+            executor.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
+        } catch (InterruptedException e) {
+            e.printStackTrace();
+        }
+    }
+
     protected void onCycleEnds() {
 
     }
diff --git a/src/mas/implementation/schedulers/FairPosCycling.java b/src/mas/implementation/schedulers/FairPosCycling.java
index 59bec2b..7881682 100644
--- a/src/mas/implementation/schedulers/FairPosCycling.java
+++ b/src/mas/implementation/schedulers/FairPosCycling.java
@@ -46,4 +46,14 @@ public class FairPosCycling implements Schedulable {
     public boolean stopCondition() {
         return false;
     }
+
+    @Override
+    public boolean isFinished() {
+        return false;
+    }
+
+    @Override
+    public void waitUntilFinish() {
+
+    }
 }
diff --git a/src/mas/implementation/schedulers/variations/TwoDCycling.java b/src/mas/implementation/schedulers/variations/TwoDCycling.java
index 78cf03a..0a886d2 100644
--- a/src/mas/implementation/schedulers/variations/TwoDCycling.java
+++ b/src/mas/implementation/schedulers/variations/TwoDCycling.java
@@ -27,7 +27,7 @@ public class TwoDCycling extends FairCycling {
      */
     private List<Consumer<TwoDCycling>> onChange = new ArrayList<>();
 
-    public enum State {
+    protected enum State {
         /**
          * The scheduler is waiting to start
          */
-- 
GitLab