Two connected Components
In this exercise a simulation with two connected
components is created. Data flows into
the first and then the second component where it
gets printed out.
Concept
The concept of this setup is shown in the figure below. Two components are created within the simulation and connected by their output and input.
Figure 1: Connecting Two Components
The example simulation is named ex01_TwoConnectedComps.sim. The simulation defines the components, the initial parameter and it connects the two components. The first component c1 receives its input ("world") from the simulation as parameter in, alters the input internally and passes it out to the second component (c2) that prints this value to the console.
Implementation
The first Component is shown in Listing 1. It defines two fields, one for input and the
other one for output.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 |
package ex01;
import oms3.annotations.*;
public class Comp1 {
@Role(Role.PARAMETER)
@In public String in;
@Out public String out;
@Execute
public void execute() {
out = in + " peace";
}
}
|
Listing 1: Component ex0.Comp1.java
The input field can be found in line 8. It is annotated with the @In annotation. It is also public in order to access it in a simulation file. The Role annotation in line 7 provides an additional hint for OMS to threat the input field also as parameter. The output field is listed at Line 10. It is tagged with the @Out annotation.
The execute method (Line 13) consists of only one statement. The out value is set from the in value, appended with another string. Note that an output value should usually appear at the LHS of an expression since it is written to. The input fields are accessed RHS (read access).
1
2
3
4
5
6
7
8
9
10
11
12
13 |
package ex01;
import oms3.annotations.*;
public class Comp2 {
@In public String in;
@Execute
public void execute() {
System.out.println(in);
}
}
|
Listing 2: Component ex0.Comp2.java
........
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 |
import static oms3.SimBuilder.instance as OMS3
/*
* Two connected components.
* Parameter passes from 'sim' file into 'Comp1', 'Comp1'
* modifies it and passes it further to 'Comp2' which prints it out.
*/
OMS3.sim {
model {
components {
"c1" "ex01.Comp1"
"c2" "ex01.Comp2"
}
connect {
// componentname,outfieldname" -> "componentname,infieldname"
"c1.out" "c2.in"
}
parameter {
// feed the beginning of the pipeline!
"c1.in" "world"
}
}
}
|
Listing 3: The simulation file.