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 nextgrid.api.pom.Expression;
21 import nextgrid.api.pom.ExpressionException;
22 import nextgrid.api.pom.IfThenElseProcess;
23 import nextgrid.api.pom.Process;
24 import nextgrid.api.pom.ProcessController;
25 import nextgrid.api.pom.ProcessException;
26
27
28
29
30
31
32 public final class IfThenElseProcessImpl extends ControlProcessImpl
33 implements IfThenElseProcess, WithExpression {
34
35
36
37
38 private static final long serialVersionUID = -5582636353281363056L;
39
40
41
42
43 private Expression expr;
44
45
46
47
48 public IfThenElseProcessImpl() {
49 super(2);
50 }
51
52
53
54
55
56
57 public Expression getExpression() {
58 return this.expr;
59 }
60
61
62
63
64
65
66 public void setExpression(Expression expression) {
67 this.expr = expression;
68 }
69
70
71
72
73
74
75 public Process getThen() {
76 return this.getChildren(0);
77 }
78
79
80
81
82
83
84 public Process getElse() {
85 return this.getChildren(1);
86 }
87
88
89
90
91
92
93
94 public Process setThen(Process process) {
95 Process prev = this.getChildren(0);
96 this.setChildren(0, process);
97 return prev;
98 }
99
100
101
102
103
104
105
106 public Process setElse(Process process) {
107 Process prev = this.getChildren(1);
108 if (this.getChildCount() == 0) {
109 this.setChildren(null, process);
110 } else {
111 this.setChildren(1, process);
112 }
113 return prev;
114 }
115
116
117
118
119 @Override protected void doValidate(ValidationType when)
120 throws ProcessException {
121 if (when == ValidationType.BEFORE_CHILDREN) {
122 if (this.expr == null) {
123 throw new ExpressionException("Null expression");
124 } else {
125 this.expr.validate();
126 }
127 }
128 }
129
130
131
132
133 public void run(ProcessContext ctx) throws ProcessException, InterruptedException {
134 fireProcessStarted();
135 waitForInputs();
136
137
138 Process p = (this.getExpression().boolEval(ctx, this)) ? getThen() : getElse();
139 if (p != null) {
140 ProcessController controller = p.enact(ctx);
141 controller.run();
142 }
143
144 if (ctx.isRunning()) {
145 fireProcessFinished();
146 }
147
148
149
150 }
151
152
153
154
155 @Override protected void doReset() {
156 }
157
158
159
160
161 @Override public String toString() {
162 return "IfThenElseProcess#" + this.getId();
163 }
164 }