Conversion SPI
Connecting "non-connectable" components using conversions.
Concept
This example demonstrates the use of the OMS ConversionProvider SPI to provide generic conversion of objects exchanged by components. Without this conversion support, the OMS @In and @Out connect of incompatible types would fail.
Implementation
The 'Point' class is used (as in the First Class Object example) to instantiate a pointer object as a parameter.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 |
package ex03;
/**
*
* @author od
*/
public class Point {
int x;
int y;
public void setX(int x) {
this.x = x;
}
public void setY(int y) {
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
}
|
The 'Comp' component consumes variable x and y integer values and incorporates them into a pointer object, which is output as a parameter object.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 |
package ex03;
import oms3.annotations.*;
public class Comp {
@Out public Point point;
@Role(Role.PARAMETER)
@In public int x;
@Role(Role.PARAMETER)
@In public int y;
@Execute
public void run() {
point = new Point();
point.setX(x);
point.setY(y);
}
}
|
The parameter object 'c.point' contains integer values and must be converted by 'PointWKTConversion' (a class that implements the OMS API 'ConversionProvider') to string values (WKT = well known text). Some formatting is incorporated into the converted object.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 |
package ex03;
import oms3.ConversionProvider;
import oms3.Converter;
/** A Converter example
* ... converting from Point to String(WKT)
*
* @author od
*/
public class PointWKTConversion implements ConversionProvider {
@Override
public Converter getConverter(Class from, Class to) {
if (from == Point.class && to == String.class) {
return new Converter<Point, String>() {
@Override
public String convert(Point src, Object arg) {
return "POINT ("+ src.x + " " + src.y + ")";
}
};
}
return null;
}
}
|
'Comp1' simply consumes the converted parameter object and prints to output.
1
2
3
4
5
6
7
8
9
10
11
12
13 |
package ex03;
import oms3.annotations.*;
public class Comp1 {
@In public String wkt_point;
@Execute
public void run() {
System.out.println(wkt_point);
}
}
|
In the simulation, the model has two connected components 'Comp' and 'Comp1'. Parameter integer values 20 and 30 are consumed by 'Comp' to produce a parameter object, which is converted from integer to string values, including formatting for output. The converted parameter object is consumed by 'Comp2' which prints the contents of the object.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 |
import static oms3.SimBuilder.instance as OMS3
OMS3.sim {
model {
parameter {
// pass in the coordinated as native integer to the
// first component.
'c.x' 30
'c.y' 20
}
components {
'c' 'ex03.Comp'
'c2' 'ex03.Comp1'
}
connect {
// this connect would fail without the PointWKTConversion class as
// specified via the generic ConversionProvider SPI
// see META-INF.services/oms3.ConversionProvider
//
// @Out 'Point' -> @In 'WKT String'
// both are describing a point, just with two different types.
'c.point' 'c2.wkt_point'
}
}
}
|