12
12
*
13
13
* Manages a collection of processes that can be run in steps or continuously.
14
14
*
15
- * @author Even Solbraa
16
- * @version $Id: $Id
15
+ *
17
16
*/
18
17
public class ProcessModel implements Runnable {
18
+
19
19
static Logger logger = LogManager .getLogger (ProcessModel .class );
20
20
private Map <String , ProcessSystem > processes = new LinkedHashMap <>();
21
21
22
22
private boolean runStep = false ;
23
23
private int maxIterations = 50 ;
24
- private int iterations = 0 ;
25
24
26
25
/**
27
26
* Checks if the model is running in step mode.
28
- *
29
- * @return true if running in step mode, false otherwise.
30
27
*/
31
28
public boolean isRunStep () {
32
29
return runStep ;
33
30
}
34
31
35
32
/**
36
33
* Sets the step mode for the process.
37
- *
38
- * @param runStep true to enable step mode, false to disable.
39
34
*/
40
35
public void setRunStep (boolean runStep ) {
41
36
this .runStep = runStep ;
42
37
}
43
38
44
39
/**
45
40
* Adds a process to the model.
46
- *
47
- * @param name the name of the process.
48
- * @param process the process to add.
49
- * @return true if the process was added successfully.
50
41
*/
51
42
public boolean add (String name , ProcessSystem process ) {
52
43
if (name == null || name .isEmpty ()) {
@@ -65,55 +56,72 @@ public boolean add(String name, ProcessSystem process) {
65
56
66
57
/**
67
58
* Retrieves a process by its name.
68
- *
69
- * @param name the name of the process.
70
- * @return the corresponding process, or null if not found.
71
59
*/
72
60
public ProcessSystem get (String name ) {
73
61
return processes .get (name );
74
62
}
75
63
76
64
/**
77
65
* Removes a process by its name.
78
- *
79
- * @param name the name of the process to remove.
80
- * @return true if the process was removed, false otherwise.
81
66
*/
82
67
public boolean remove (String name ) {
83
68
return processes .remove (name ) != null ;
84
69
}
85
70
86
71
/**
87
- * Executes all processes, either continuously or in steps based on mode.
72
+ * The core run method.
73
+ *
74
+ * - If runStep == true, each process is run in "step" mode exactly once. - Otherwise (continuous
75
+ * mode), it loops up to maxIterations or until all processes are finished (isFinished() == true).
88
76
*/
89
77
@ Override
90
78
public void run () {
91
- for (ProcessSystem process : processes .values ()) {
92
- try {
93
- if (runStep ) {
79
+ if (runStep ) {
80
+ // Step mode: just run each process once in step mode
81
+ for (ProcessSystem process : processes .values ()) {
82
+ try {
83
+ if (Thread .currentThread ().isInterrupted ()) {
84
+ logger .debug ("Thread was interrupted, exiting run()..." );
85
+ return ;
86
+ }
94
87
process .run_step ();
95
- } else {
96
- process .run ();
88
+ } catch (Exception e ) {
89
+ System .err .println ("Error running process step: " + e .getMessage ());
90
+ e .printStackTrace ();
97
91
}
98
- } catch (Exception e ) {
99
- System .err .println ("Error running process: " + e .getMessage ());
100
- e .printStackTrace ();
101
92
}
102
- }
103
- if (!runStep ) {
104
- if (!isFinished () && iterations < maxIterations ) {
105
- iterations += 1 ;
106
- run ();
107
- } else {
108
- iterations = 0 ;
93
+ } else {
94
+ int iterations = 0 ;
95
+ while (!Thread .currentThread ().isInterrupted () && !isFinished ()
96
+ && iterations < maxIterations ) {
97
+ for (ProcessSystem process : processes .values ()) {
98
+ if (Thread .currentThread ().isInterrupted ()) {
99
+ logger .debug ("Thread was interrupted, exiting run()..." );
100
+ return ;
101
+ }
102
+ try {
103
+ process .run (); // the process's continuous run
104
+ } catch (Exception e ) {
105
+ System .err .println ("Error running process: " + e .getMessage ());
106
+ e .printStackTrace ();
107
+ }
108
+ }
109
+ iterations ++;
109
110
}
110
111
}
111
112
}
112
113
114
+ /**
115
+ * Starts this model in a new thread and returns that thread.
116
+ */
117
+ public Thread runAsThread () {
118
+ Thread processThread = new Thread (this );
119
+ processThread .start ();
120
+ return processThread ;
121
+ }
122
+
113
123
/**
114
124
* Checks if all processes are finished.
115
- *
116
- * @return true if all processes are solved, false otherwise.
117
125
*/
118
126
public boolean isFinished () {
119
127
for (ProcessSystem process : processes .values ()) {
@@ -125,11 +133,15 @@ public boolean isFinished() {
125
133
}
126
134
127
135
/**
128
- * Executes all processes in a single step.
136
+ * Runs all processes in a single step (used outside of the thread model) .
129
137
*/
130
138
public void runStep () {
131
139
for (ProcessSystem process : processes .values ()) {
132
140
try {
141
+ if (Thread .currentThread ().isInterrupted ()) {
142
+ logger .debug ("Thread was interrupted, exiting run()..." );
143
+ return ;
144
+ }
133
145
process .run_step ();
134
146
} catch (Exception e ) {
135
147
System .err .println ("Error in runStep: " + e .getMessage ());
@@ -139,20 +151,19 @@ public void runStep() {
139
151
}
140
152
141
153
/**
142
- * Runs the model as a separate thread .
154
+ * (Optional) Creates separate threads for each process (if you need them) .
143
155
*/
144
156
public Map <String , Thread > getThreads () {
145
157
Map <String , Thread > threads = new LinkedHashMap <>();
146
158
try {
147
159
for (ProcessSystem process : processes .values ()) {
148
160
Thread thread = new Thread (process );
161
+ thread .setName (process .getName () + " thread" );
149
162
threads .put (process .getName (), thread );
150
163
}
151
-
152
164
} catch (Exception ex ) {
153
165
logger .debug (ex .getMessage (), ex );
154
166
}
155
167
return threads ;
156
168
}
157
-
158
169
}
0 commit comments