diff --git a/src/main/java/neqsim/process/processmodel/ProcessModel.java b/src/main/java/neqsim/process/processmodel/ProcessModel.java index 48d1b5f53..5b627f6bf 100644 --- a/src/main/java/neqsim/process/processmodel/ProcessModel.java +++ b/src/main/java/neqsim/process/processmodel/ProcessModel.java @@ -9,30 +9,41 @@ /** * A process model that can run multiple processes in parallel. - * + * + *

* This class is a simple model that can run multiple processes in parallel. It - * can run in two modes: - * - Step mode: each process is run once in a loop, in the order they were - * added. - Continuous mode: - * each process is run in a loop until all processes are finished or a maximum - * number of iterations - * is reached. - * + * can + * run in two modes: + *

+ * + *

* You can also create groups of processes and run them separately. */ public class ProcessModel implements Runnable { - /** Logger object for class. */ + + /** Logger object for this class. */ static Logger logger = LogManager.getLogger(ProcessModel.class); - /** Map of process name -> ProcessSystem. */ + /** + * A map containing process names and their corresponding {@link ProcessSystem} + * objects. + */ private Map processes = new LinkedHashMap<>(); /** - * Map of group name -> list of process names in that group. - * - * We store process *names* here, pointing to the actual ProcessSystem in - * `processes`. - * Alternatively, you can store the ProcessSystem references directly. + * A map of group names to a list of process names in that group. + * + *

+ * We store process names here, pointing to the actual + * {@code ProcessSystem} in + * {@link #processes}. Alternatively, you can store the {@code ProcessSystem} + * references directly. */ private Map> groups = new LinkedHashMap<>(); @@ -41,6 +52,8 @@ public class ProcessModel implements Runnable { /** * Checks if the model is running in step mode. + * + * @return {@code true} if the model runs in step mode, otherwise {@code false} */ public boolean isRunStep() { return runStep; @@ -48,6 +61,9 @@ public boolean isRunStep() { /** * Sets the step mode for the process. + * + * @param runStep {@code true} to run in step mode, {@code false} for continuous + * mode */ public void setRunStep(boolean runStep) { this.runStep = runStep; @@ -55,6 +71,14 @@ public void setRunStep(boolean runStep) { /** * Adds a process to the model. + * + * @param name the name of the process to add + * @param process the {@link ProcessSystem} instance to be added + * @return {@code true} if the process was added successfully + * @throws IllegalArgumentException if {@code name} is null or empty, if + * {@code process} is null, + * or if a process with the given name already + * exists */ public boolean add(String name, ProcessSystem process) { if (name == null || name.isEmpty()) { @@ -73,6 +97,10 @@ public boolean add(String name, ProcessSystem process) { /** * Retrieves a process by its name. + * + * @param name the name of the process to retrieve + * @return the {@link ProcessSystem} corresponding to the given name, + * or {@code null} if no matching process is found */ public ProcessSystem get(String name) { return processes.get(name); @@ -80,6 +108,9 @@ public ProcessSystem get(String name) { /** * Removes a process by its name. + * + * @param name the name of the process to remove + * @return {@code true} if the process was removed, otherwise {@code false} */ public boolean remove(String name) { return processes.remove(name) != null; @@ -87,6 +118,9 @@ public boolean remove(String name) { /** * Creates a new group or clears it if it already exists. + * + * @param groupName the name of the group to create + * @throws IllegalArgumentException if {@code groupName} is null or empty */ public void createGroup(String groupName) { if (groupName == null || groupName.isEmpty()) { @@ -96,13 +130,17 @@ public void createGroup(String groupName) { } /** - * Add a process to a given group (by process name). + * Adds a process (by its name) to a given group. + * + * @param groupName the name of the group to which the process will be added + * @param processName the name of the process to add + * @throws IllegalArgumentException if there is no process with the given + * {@code processName} */ public void addProcessToGroup(String groupName, String processName) { if (!processes.containsKey(processName)) { throw new IllegalArgumentException("No process with name " + processName + " found"); } - groups.computeIfAbsent(groupName, key -> new ArrayList<>()); List groupProcesses = groups.get(groupName); if (!groupProcesses.contains(processName)) { @@ -111,7 +149,11 @@ public void addProcessToGroup(String groupName, String processName) { } /** - * Remove a process from a given group. + * Removes a process from a given group. + * + * @param groupName the name of the group from which the process will be + * removed + * @param processName the name of the process to remove */ public void removeProcessFromGroup(String groupName, String processName) { if (groups.containsKey(groupName)) { @@ -120,9 +162,13 @@ public void removeProcessFromGroup(String groupName, String processName) { } /** - * Runs all processes in the specified group (step or continuous). - * - * If the group name doesn't exist or is empty, does nothing. + * Runs all processes in the specified group, honoring the current mode (step or + * continuous). + * + *

+ * If the group name doesn't exist or is empty, this method does nothing. + * + * @param groupName the name of the group to run */ public void runGroup(String groupName) { if (!groups.containsKey(groupName) || groups.get(groupName).isEmpty()) { @@ -132,7 +178,7 @@ public void runGroup(String groupName) { List groupProcesses = groups.get(groupName); if (runStep) { - // Step mode: just run each process once in step mode + // Step mode: run each process once in step mode for (String processName : groupProcesses) { try { if (Thread.currentThread().isInterrupted()) { @@ -168,11 +214,16 @@ public void runGroup(String groupName) { } /** - * Check if the group has all processes finished. + * Checks if all processes in the specified group have finished. + * + * @param groupName the name of the group to check + * @return {@code true} if the group exists (or not) and all processes within + * the group are finished, + * otherwise {@code false} */ public boolean isGroupFinished(String groupName) { if (!groups.containsKey(groupName)) { - // no group or group doesn't exist -> consider it "finished" or throw exception + // No group found -> treat as finished or handle differently if needed return true; } for (String processName : groups.get(groupName)) { @@ -185,17 +236,19 @@ public boolean isGroupFinished(String groupName) { } /** - * The core run method. - * - * - If runStep == true, each process is run in "step" mode exactly once. - - * Otherwise (continuous - * mode), it loops up to maxIterations or until all processes are finished - * (isFinished() == true). + * The core run method for this model, which iterates over all processes. + * + *

+ * If {@code runStep} is {@code true}, each process is run exactly once in step + * mode. + * Otherwise, processes are run continuously up to {@code maxIterations} or + * until + * {@link #isFinished()} is {@code true}. */ @Override public void run() { if (runStep) { - // Step mode: just run each process once in step mode + // Step mode: just run each process once for (ProcessSystem process : processes.values()) { try { if (Thread.currentThread().isInterrupted()) { @@ -209,6 +262,7 @@ public void run() { } } } else { + // Continuous mode int iterations = 0; while (!Thread.currentThread().isInterrupted() && !isFinished() && iterations < maxIterations) { @@ -231,6 +285,8 @@ public void run() { /** * Starts this model in a new thread and returns that thread. + * + * @return the newly created {@link Thread} running this {@code ProcessModel} */ public Thread runAsThread() { Thread processThread = new Thread(this); @@ -239,7 +295,9 @@ public Thread runAsThread() { } /** - * Checks if all processes are finished. + * Checks if all processes in the model have finished. + * + * @return {@code true} if all processes are finished, otherwise {@code false} */ public boolean isFinished() { for (ProcessSystem process : processes.values()) { @@ -251,13 +309,13 @@ public boolean isFinished() { } /** - * Runs all processes in a single step (used outside of the thread model). + * Runs all processes in a single step (used outside of the threaded model). */ public void runStep() { for (ProcessSystem process : processes.values()) { try { if (Thread.currentThread().isInterrupted()) { - logger.debug("Thread was interrupted, exiting run()..."); + logger.debug("Thread was interrupted, exiting runStep()..."); return; } process.run_step(); @@ -269,7 +327,9 @@ public void runStep() { } /** - * (Optional) Creates separate threads for each process (if you need them). + * Creates separate threads for each process, if needed. + * + * @return a map of process names to their corresponding {@link Thread} objects */ public Map getThreads() { Map threads = new LinkedHashMap<>(); @@ -289,8 +349,8 @@ public Map getThreads() { * Calculates the total power consumption of all processes in the specified * unit. * - * @param unit the unit of power to be used (e.g., "kW", "MW"). - * @return the total power consumption of all processes in the specified unit. + * @param unit the power unit (e.g., "kW", "MW") + * @return the total power consumption in the given unit */ public double getPower(String unit) { double totalPower = 0.0; @@ -303,8 +363,8 @@ public double getPower(String unit) { /** * Calculates the total heater duty for all processes in the specified unit. * - * @param unit the unit for which the heater duty is to be calculated - * @return the total heater duty for the specified unit + * @param unit the heater duty unit (e.g., "kW", "MW") + * @return the total heater duty in the given unit */ public double getHeaterDuty(String unit) { double totalDuty = 0.0; @@ -317,8 +377,8 @@ public double getHeaterDuty(String unit) { /** * Calculates the total cooler duty for all processes in the specified unit. * - * @param unit the unit for which the cooler duty is to be calculated - * @return the total cooler duty for the specified unit + * @param unit the cooler duty unit (e.g., "kW", "MW") + * @return the total cooler duty in the given unit */ public double getCoolerDuty(String unit) { double totalDuty = 0.0;