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