From 0c259efb7b27cccdc4547a85bc80016ae88a0524 Mon Sep 17 00:00:00 2001 From: Matthias Date: Thu, 4 Jun 2026 10:31:14 +0200 Subject: [PATCH 01/16] Apply different corrections so that the simulations are correctly generated --- structSimV1/pom.xml | 5 ++ .../experimenthandling/Environment.java | 39 +++++++++--- .../ExperimentPlanGenerator.java | 41 ++++++++++++- .../ExperimentResultHandler.java | 4 +- .../ExperimentSimulatorHandler.java | 5 +- .../experimenthandling/Parameter.java | 1 + .../structuredsim/interfaces/AModifier.java | 3 +- .../interfaces/IManageParametersFile.java | 10 ++++ .../interfaces/IStartSimulation.java | 1 - .../interfaces/StartProgram.java | 19 +++++- .../structuredsim/util/FileManagement.java | 60 ++++++++++++++++++- 11 files changed, 161 insertions(+), 27 deletions(-) diff --git a/structSimV1/pom.xml b/structSimV1/pom.xml index 269efa9..ddfb6a8 100644 --- a/structSimV1/pom.xml +++ b/structSimV1/pom.xml @@ -26,5 +26,10 @@ jacob 1.18 + + org.apache.logging.log4j + log4j-api + 2.24.3 + diff --git a/structSimV1/src/main/java/ch/hevs/silab/structuredsim/experimenthandling/Environment.java b/structSimV1/src/main/java/ch/hevs/silab/structuredsim/experimenthandling/Environment.java index fda0a15..048fc2f 100644 --- a/structSimV1/src/main/java/ch/hevs/silab/structuredsim/experimenthandling/Environment.java +++ b/structSimV1/src/main/java/ch/hevs/silab/structuredsim/experimenthandling/Environment.java @@ -20,6 +20,8 @@ package ch.hevs.silab.structuredsim.experimenthandling; +import ch.hevs.silab.structuredsim.test.ModifierClass1; + import java.util.ArrayList; import java.util.List; import java.util.Vector; @@ -49,7 +51,6 @@ public class Environment implements Comparable { * * @param id : ID * @param setOfParameters : List of Parameters of this Environment - * @param idParamChanged : ID from the parameters changed in this environment * @param probability : Probability */ public Environment(int id, Vector setOfParameters, double probability) { @@ -65,7 +66,21 @@ public Environment(int id, Vector setOfParameters, double probability */ public Environment(int id, Environment e){ this.id = id; - this.setOfParameters = e.setOfParameters; + // this.setOfParameters = e.setOfParameters; + + /* When creating a child from a parent environment, + the constructor was using a reference of the parent environment's parameters. + That means that when a modifier was applied to a child, + it was also implicitly applied to the parent. + This was inducing incorrect simulations when multiple modifiers were involved. + That's why I changed this constructor to take a copy of the parameters + instead of references + */ + this.setOfParameters = new Vector(); + for (Parameter p : e.setOfParameters) { + this.setOfParameters.add(new Parameter(p)); // Constructor by copy + } + this.probability = e.probability; this.trace = new ArrayList(); for(String s :e.trace){ @@ -137,10 +152,8 @@ public String toStringModifier() { for(String s : trace){ result += " " + s; } - - return "Simulation ID : " + id + "\t" + "Modifier implemented : " + result ; + return "Simulation ID : " + id + "\t Probability : " + probability + "\t Modifier implemented : " + result; } - public List getTrace() { return trace; @@ -150,10 +163,18 @@ public void setTrace(List trace) { this.trace = trace; } + /** + Allow to order environments by ascending order of probability + */ @Override - public int compareTo(Environment arg0) { - return (int)(this.probability - arg0.getProbability()); + public int compareTo(Environment other) { + /* + Changed the method because with the previous implementation, + it casted every value to 0 if they were between -1 and 1 + (which would be the case for probabilities values) + With a Double.compare, it correcly returns the corresponding value : -1, 1 or 0. + */ + return Double.compare(this.probability, other.probability); } - -} +} \ No newline at end of file diff --git a/structSimV1/src/main/java/ch/hevs/silab/structuredsim/experimenthandling/ExperimentPlanGenerator.java b/structSimV1/src/main/java/ch/hevs/silab/structuredsim/experimenthandling/ExperimentPlanGenerator.java index 840ba51..18a4c54 100644 --- a/structSimV1/src/main/java/ch/hevs/silab/structuredsim/experimenthandling/ExperimentPlanGenerator.java +++ b/structSimV1/src/main/java/ch/hevs/silab/structuredsim/experimenthandling/ExperimentPlanGenerator.java @@ -96,30 +96,60 @@ private void createNextEnvironments (Environment baseEnv){ while(!toExplore.isEmpty()){ + /* + This break statement is required in order to stop the program at the right time. + Without it, the program would keep exploring the current queue until it is empty, + which is not what we want I guess ?? + */ + if(options.getTypeOfCuttOfPlanning().equals("INT") && cpt >= options.getCuttOfPlanning()){ + break; + } + parentEnv = toExplore.remove(0); for (AModifier modifier : listModifiers) { idCpt ++; + currentEnv = new Environment(idCpt, parentEnv); + currentEnv = modifier.applyModifier(currentEnv); + currentEnv.getTrace().add(modifier.getName() ); //for(String str : currentEn) System.out.println("--------------------------------------------" +currentEnv.id + " " + currentEnv.trace.toString()); + currentEnv.setProbability(parentEnv.getProbability() * modifier.getProbability()); if(options.getTypeOfCuttOfPlanning().equals("CRITERIA") && currentEnv.getProbability() > options.getStopCriteria()){ toExplore.add(currentEnv); + } if(options.getTypeOfCuttOfPlanning().equals("INT")&& - cpt <= options.getCuttOfPlanning()){ + idCpt <= options.getCuttOfPlanning()) + //cpt <= options.getCuttOfPlanning()) + /* + I don't think this second condition matters anymore + because of the break statement at the start + of the while loop + */ + + { toExplore.add(currentEnv); } + + + /* + Not sure why that else break statement was there... + It just prevented the files to be created + if the type of cut was not INT. + */ + /* else{ break; - } + }*/ fm.createNewFolderSimulation(currentEnv, glueCode); addEnvToQueue(currentEnv); @@ -128,7 +158,11 @@ private void createNextEnvironments (Environment baseEnv){ } cpt ++; System.out.println("CPT : " + cpt); - Collections.sort(toExplore); + + /* We sort the environments by reverse order, + to put the most probable one in first position */ + toExplore.sort(Collections.reverseOrder()); + } } @@ -165,6 +199,7 @@ public void run() { //Generate the number of Environment that we want long currentTime = System.currentTimeMillis(); + System.out.println("option = " + options.getTypeOfCuttOfPlanning()); switch(options.getTypeOfCuttOfPlanning()){ case "INT" : diff --git a/structSimV1/src/main/java/ch/hevs/silab/structuredsim/experimenthandling/ExperimentResultHandler.java b/structSimV1/src/main/java/ch/hevs/silab/structuredsim/experimenthandling/ExperimentResultHandler.java index 46c362f..0678d88 100644 --- a/structSimV1/src/main/java/ch/hevs/silab/structuredsim/experimenthandling/ExperimentResultHandler.java +++ b/structSimV1/src/main/java/ch/hevs/silab/structuredsim/experimenthandling/ExperimentResultHandler.java @@ -32,7 +32,7 @@ /** * Name : ExperimentResultHandler *

- * Description : This thread is for displaying where the results are save + * Description : This thread is for displaying where the results are saved *

* Date : 25 july 2017 * @@ -57,8 +57,6 @@ public class ExperimentResultHandler implements Runnable { * : BlockingQueue to get the list fulfilled * @param glueCode * : Class of the glueCode - * @param options - * : options object */ public ExperimentResultHandler(BlockingQueue resultsQueue, Object glueCode, FileManagement fm, Options o) { this.resultsQueue = resultsQueue; diff --git a/structSimV1/src/main/java/ch/hevs/silab/structuredsim/experimenthandling/ExperimentSimulatorHandler.java b/structSimV1/src/main/java/ch/hevs/silab/structuredsim/experimenthandling/ExperimentSimulatorHandler.java index af371a1..703368c 100644 --- a/structSimV1/src/main/java/ch/hevs/silab/structuredsim/experimenthandling/ExperimentSimulatorHandler.java +++ b/structSimV1/src/main/java/ch/hevs/silab/structuredsim/experimenthandling/ExperimentSimulatorHandler.java @@ -54,8 +54,6 @@ public class ExperimentSimulatorHandler implements Runnable { * * @param environnementQueue * : The BlockignQueue where the environment will be saved - * @param simulationHandler - * : Thread of the simulation * @param o * : options object * @param glueCode @@ -74,8 +72,7 @@ public ExperimentSimulatorHandler(BlockingQueue environnementQueue, /** * Method to save the result - * @param e : Environment - * @return path where the file will be save + * @return path where the file will be saved */ @Override public void run() { diff --git a/structSimV1/src/main/java/ch/hevs/silab/structuredsim/experimenthandling/Parameter.java b/structSimV1/src/main/java/ch/hevs/silab/structuredsim/experimenthandling/Parameter.java index e4f2a84..2ce309e 100644 --- a/structSimV1/src/main/java/ch/hevs/silab/structuredsim/experimenthandling/Parameter.java +++ b/structSimV1/src/main/java/ch/hevs/silab/structuredsim/experimenthandling/Parameter.java @@ -44,6 +44,7 @@ public Parameter(String key, double value) { this.key = key; this.value = value; } + /** * Constructor diff --git a/structSimV1/src/main/java/ch/hevs/silab/structuredsim/interfaces/AModifier.java b/structSimV1/src/main/java/ch/hevs/silab/structuredsim/interfaces/AModifier.java index d103060..923df11 100644 --- a/structSimV1/src/main/java/ch/hevs/silab/structuredsim/interfaces/AModifier.java +++ b/structSimV1/src/main/java/ch/hevs/silab/structuredsim/interfaces/AModifier.java @@ -61,8 +61,7 @@ public AModifier(){ * use the method getParameterToModify to get the Parameter.

Example : Parameters newParam = getParameterToModify(env.getSetOfParameters(), options.getValueToChange); * * - * @param env : Environment. An Environment is one state of the simulation at the instant T. - * @param o : options for the simulation + * @param env : Environment. An Environment is one state of the simulation at the instant T. * @return : environment. */ public abstract Environment applyModifier(Environment env); diff --git a/structSimV1/src/main/java/ch/hevs/silab/structuredsim/interfaces/IManageParametersFile.java b/structSimV1/src/main/java/ch/hevs/silab/structuredsim/interfaces/IManageParametersFile.java index 272d8d1..6ca7e38 100644 --- a/structSimV1/src/main/java/ch/hevs/silab/structuredsim/interfaces/IManageParametersFile.java +++ b/structSimV1/src/main/java/ch/hevs/silab/structuredsim/interfaces/IManageParametersFile.java @@ -19,6 +19,7 @@ * */ package ch.hevs.silab.structuredsim.interfaces; +import java.io.InputStream; import java.util.Vector; import ch.hevs.silab.structuredsim.experimenthandling.Parameter; @@ -44,6 +45,15 @@ public interface IManageParametersFile { */ public Vector readParametersFile(String parametersFilePath); + + + /** + * Method to read parameters from an InputStream instead + * @author Matthias Gaillard + */ + public Vector readParametersFile(InputStream inputStream); + + /** * Method to write a new file of parameters that will be used by your * simulator EACH TIME that a Parameter change ! diff --git a/structSimV1/src/main/java/ch/hevs/silab/structuredsim/interfaces/IStartSimulation.java b/structSimV1/src/main/java/ch/hevs/silab/structuredsim/interfaces/IStartSimulation.java index fa996b8..8b64304 100644 --- a/structSimV1/src/main/java/ch/hevs/silab/structuredsim/interfaces/IStartSimulation.java +++ b/structSimV1/src/main/java/ch/hevs/silab/structuredsim/interfaces/IStartSimulation.java @@ -33,7 +33,6 @@ public interface IStartSimulation { /** *Method to start the simulation that is an external application. The result are save in file txt - * @param baseEnvToSimulate : Environment that will get the simulation */ public void startSimulation(String pathToInputFile); diff --git a/structSimV1/src/main/java/ch/hevs/silab/structuredsim/interfaces/StartProgram.java b/structSimV1/src/main/java/ch/hevs/silab/structuredsim/interfaces/StartProgram.java index 724c7b0..9f1742e 100644 --- a/structSimV1/src/main/java/ch/hevs/silab/structuredsim/interfaces/StartProgram.java +++ b/structSimV1/src/main/java/ch/hevs/silab/structuredsim/interfaces/StartProgram.java @@ -21,6 +21,7 @@ import java.io.IOException; +import java.io.InputStream; import java.util.Vector; import java.util.concurrent.BlockingQueue; import java.util.concurrent.PriorityBlockingQueue; @@ -31,6 +32,7 @@ import ch.hevs.silab.structuredsim.experimenthandling.ExperimentSimulatorHandler; import ch.hevs.silab.structuredsim.experimenthandling.Options; import ch.hevs.silab.structuredsim.experimenthandling.Parameter; +import ch.hevs.silab.structuredsim.test.Simulation; import ch.hevs.silab.structuredsim.util.FileManagement; /** @@ -52,13 +54,13 @@ public class StartProgram { * @param glueCode : glue code object * @throws IOException */ - public static void startProgram(String pathConfigFile, Object glueCode) throws IOException { + public static void startProgram(InputStream pathConfigFile, Object glueCode) throws IOException { FileManagement fm = new FileManagement(); // Create an instance of the "GlueCode" ASimulationSystemHandler glueCodeClass = (ASimulationSystemHandler) glueCode; -// glueCodeClass.fileManagement = fm; + // glueCodeClass.fileManagement = fm; // Load the configuration properties file Options o = fm.loadDataFromPropertiesFile(pathConfigFile); @@ -67,9 +69,20 @@ public static void startProgram(String pathConfigFile, Object glueCode) throws I // Get the List of the Parameters Vector listParam = null; - listParam = glueCodeClass.readParametersFile(o.getPathParameters()); + //Previous version + // listParam = glueCodeClass.readParametersFile(o.getPathParameters()); + /* + New version by Matthias Gaillard + Could not make it work with the previous version + using a simple path, so I used InputStream instead. + */ + InputStream isParams = Simulation.class + .getClassLoader() + .getResourceAsStream(o.getPathParameters()); + listParam = glueCodeClass.readParametersFile(isParams); + // Select the Parameter to change Environment baseEnv = new Environment(0, listParam, 1); diff --git a/structSimV1/src/main/java/ch/hevs/silab/structuredsim/util/FileManagement.java b/structSimV1/src/main/java/ch/hevs/silab/structuredsim/util/FileManagement.java index f1a122d..b04496a 100644 --- a/structSimV1/src/main/java/ch/hevs/silab/structuredsim/util/FileManagement.java +++ b/structSimV1/src/main/java/ch/hevs/silab/structuredsim/util/FileManagement.java @@ -143,7 +143,7 @@ public void copyFile(String file, String destination) { File dest = new File(destination); try { - Files.copy(fileToCopy.toPath(), dest.toPath()); + Files.copy(fileToCopy.toPath(), dest.toPath(), StandardCopyOption.REPLACE_EXISTING); } catch (Exception e) { logger.error("This file in this folder already exist"); //e.printStackTrace(); @@ -258,7 +258,8 @@ public Options loadDataFromPropertiesFile(String filePath) { e.printStackTrace(); logger.error("Error to load Data from Properties file"); } - String cuttOfValue = ""; + //String cuttOfValue = ""; + String cuttOfValue = properties.getProperty("cuttOfPlanning", ""); for (String key : properties.stringPropertyNames()) { @@ -311,6 +312,61 @@ public Options loadDataFromPropertiesFile(String filePath) { return options; } + /** + * Same method as the previous one, + * but using an InputStream instead of a String + * @param ips + * @return Options + */ + public Options loadDataFromPropertiesFile(InputStream ips) { + Properties properties = new Properties(); + try { + properties.load(ips); + ips.close(); + } catch (IOException e) { + e.printStackTrace(); + logger.error("Error to load Data from Properties file"); + } + + // 1. D'abord, lire toutes les valeurs simples + options.setPathParameters(properties.getProperty("pathParameters")); + options.setFolderPathOUT(properties.getProperty("pathOUT")); + options.setPathSimulator(properties.getProperty("pathSimulator")); + options.setPathToSimulatorResultFile(properties.getProperty("pathToSimulatorResultFile")); + + // 2. Ensuite, traiter les valeurs interdépendantes + String cuttOfValue = properties.getProperty("cuttOfPlanning", ""); + String typeCuttOf = properties.getProperty("typeCuttOfPlanning", ""); + + options.setTypeOfCuttOfPlanning(typeCuttOf); + + switch (typeCuttOf) { + case "INT": + options.setCuttOfPlanning(Integer.parseInt(cuttOfValue)); + break; + case "DAY": + Calendar cal1 = Calendar.getInstance(); + cal1.set(Calendar.DATE, Integer.parseInt(cuttOfValue)); + options.setCuttOfPlanningH(cal1); + break; + case "HOURS": + Calendar cal2 = Calendar.getInstance(); + cal2.set(Calendar.HOUR_OF_DAY, Integer.parseInt(cuttOfValue)); + options.setCuttOfPlanningH(cal2); + break; + case "MINUTES": + Calendar cal3 = Calendar.getInstance(); + cal3.set(Calendar.MINUTE, Integer.parseInt(cuttOfValue)); + options.setCuttOfPlanningH(cal3); + break; + case "CRITERIA": + options.setStopCriteria(Double.parseDouble(cuttOfValue)); + break; + } + + return options; + } + /** * Method to write something in a properties file parameters : *