Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
EvenSol committed Jan 14, 2025
1 parent 610c4a7 commit 6fa7385
Showing 1 changed file with 101 additions and 41 deletions.
142 changes: 101 additions & 41 deletions src/main/java/neqsim/process/processmodel/ProcessModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,41 @@

/**
* A process model that can run multiple processes in parallel.
*
*
* <p>
* 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:
* <ul>
* <li><strong>Step mode:</strong> each process is run once in a loop, in the
* order they were added.</li>
* <li><strong>Continuous mode:</strong> each process is run in a loop until all
* processes are
* finished or a maximum number of iterations is reached.</li>
* </ul>
*
* <p>
* 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<String, ProcessSystem> 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.
*
* <p>
* We store process <em>names</em> here, pointing to the actual
* {@code ProcessSystem} in
* {@link #processes}. Alternatively, you can store the {@code ProcessSystem}
* references directly.
*/
private Map<String, List<String>> groups = new LinkedHashMap<>();

Expand All @@ -41,20 +52,33 @@ 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;
}

/**
* 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;
}

/**
* 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()) {
Expand All @@ -73,20 +97,30 @@ 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);
}

/**
* 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;
}

/**
* 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()) {
Expand All @@ -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<String> groupProcesses = groups.get(groupName);
if (!groupProcesses.contains(processName)) {
Expand All @@ -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)) {
Expand All @@ -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).
*
* <p>
* 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()) {
Expand All @@ -132,7 +178,7 @@ public void runGroup(String groupName) {

List<String> 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()) {
Expand Down Expand Up @@ -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)) {
Expand All @@ -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.
*
* <p>
* 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()) {
Expand All @@ -209,6 +262,7 @@ public void run() {
}
}
} else {
// Continuous mode
int iterations = 0;
while (!Thread.currentThread().isInterrupted() && !isFinished()
&& iterations < maxIterations) {
Expand All @@ -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);
Expand All @@ -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()) {
Expand All @@ -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();
Expand All @@ -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<String, Thread> getThreads() {
Map<String, Thread> threads = new LinkedHashMap<>();
Expand All @@ -289,8 +349,8 @@ public Map<String, Thread> 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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down

0 comments on commit 6fa7385

Please sign in to comment.