Simple Iteration
Agro-environmental models often require processes to iterate during simulation.
Concept
'IterationControl' consumes a 'count' parameter integer value, providing the loop variable 'i' as output, decremented with each iteration until done. In this one component model example, variable 'i' is not consumed by other components. However, this component, or variations of it, can be used in building model simulations requiring iteration.
Implementation
'IterationControl' consumes a 'count' parameter value, assigns it to 'i', outputs a decremented 'i' for the next iteration, and prints the decremented 'i' value in the format shown.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 |
package ex05;
import oms3.annotations.*;
public class IterationControl {
@Role(Role.PARAMETER)
@In public int count;
// flag for controlling the iteration
@Out public boolean done;
// current iteravariable
@Out int i;
@Execute
public void execute() {
if (i==0) {
i = count;
}
done = i-- > 1;
System.out.println("Count " + i);
}
}
|
OMS simulation provides iteration support (line 6). In this example, there is one component in the model, and 'count' is parameterized to 5 (iterations).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 |
import static oms3.SimBuilder.instance as OMS3
OMS3.sim {
// the iteration is controlled by the 'c' component's 'done' field.
model(while:"c.done") {
parameter {
// repeat the iteration 5 times
'c.count' 5
}
components {
// c is the counter control component.
'c' 'ex05.IterationControl'
}
}
}
|