1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package com.gridsystems.nextgrid.api.pom;
19
20 import java.util.Map;
21
22 import nextgrid.api.pom.Expression;
23 import nextgrid.api.pom.ExpressionException;
24 import nextgrid.api.pom.Process;
25 import nextgrid.api.pom.ProcessController;
26 import nextgrid.api.pom.ProcessException;
27 import nextgrid.api.pom.Reference;
28 import nextgrid.api.pom.WhileDoProcess;
29
30
31
32
33
34
35 public final class WhileDoProcessImpl extends ControlProcessImpl
36 implements WhileDoProcess, WithExpression {
37
38
39
40
41 private static final long serialVersionUID = -1324090284824130863L;
42
43
44
45
46 private Expression expr;
47
48
49
50
51 private int loop;
52
53
54 private int index;
55
56
57
58
59 public WhileDoProcessImpl() {
60 super(1);
61 }
62
63
64
65
66
67
68 public Expression getExpression() {
69 return this.expr;
70 }
71
72
73
74
75
76
77 public void setExpression(Expression expression) {
78 this.expr = expression;
79 }
80
81
82
83
84 @Override
85 protected void doValidate(ValidationType when) throws ProcessException {
86 if (when == ValidationType.BEFORE_CHILDREN) {
87 if (this.expr == null) {
88 throw new ExpressionException("Null expression");
89 } else {
90 this.expr.validate();
91 }
92 }
93 }
94
95
96
97
98 @Override protected void doReset() {
99 resetChildren();
100 this.loop = 0;
101 this.index = 0;
102 }
103
104
105
106
107
108
109
110
111 public void run(ProcessContext ctx) throws ProcessException, InterruptedException {
112 fireProcessStarted();
113
114
115 while (true) {
116 this.loop += 1;
117
118
119 for (Map.Entry<String, Reference<?>> entry : this.getLocalVars().entrySet()) {
120 entry.getValue().reset();
121 }
122
123 waitForInputs();
124 if (!this.getExpression().boolEval(ctx, this)) {
125 break;
126 }
127
128 Process[] children = getChildren();
129 int count = (children == null) ? 0 : children.length;
130
131 while (this.index < count) {
132 if (!ctx.isRunning()) {
133 return;
134 }
135 ProcessController controller = children[this.index].enact(ctx);
136 controller.run();
137
138 this.index++;
139 }
140 this.index = 0;
141 }
142
143 if (ctx.isRunning()) {
144 fireProcessFinished();
145 }
146 }
147
148
149
150
151 @Override public String toString() {
152 return "WhileDoProcess#" + this.getId();
153 }
154 }