-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJavaExample.java
More file actions
114 lines (96 loc) · 4.92 KB
/
JavaExample.java
File metadata and controls
114 lines (96 loc) · 4.92 KB
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import ca.jahed.rtpoet.papyrusrt.PapyrusRTReader;
import ca.jahed.rtpoet.papyrusrt.PapyrusRTWriter;
import ca.jahed.rtpoet.papyrusrt.generators.CppCodeGenerator;
import ca.jahed.rtpoet.papyrusrt.rts.SystemPorts;
import ca.jahed.rtpoet.papyrusrt.rts.primitivetype.RTInt;
import ca.jahed.rtpoet.rtmodel.*;
import ca.jahed.rtpoet.rtmodel.sm.RTPseudoState;
import ca.jahed.rtpoet.rtmodel.sm.RTState;
import ca.jahed.rtpoet.rtmodel.sm.RTStateMachine;
import ca.jahed.rtpoet.rtmodel.sm.RTTransition;
import ca.jahed.rtpoet.utils.RTDeepCopier;
import ca.jahed.rtpoet.utils.RTEqualityHelper;
import ca.jahed.rtpoet.utils.RTModelValidator;
import ca.jahed.rtpoet.visualizer.RTVisualizer;
import java.util.ArrayList;
public class JavaExample {
public static RTModel createPingerPonger() {
RTProtocol ppProtocol = RTProtocol.builder("PPProtocol")
.output(RTSignal.builder("ping").parameter(RTParameter.builder("round", RTInt.INSTANCE)))
.input(RTSignal.builder("pong").parameter(RTParameter.builder("round", RTInt.INSTANCE)))
.build();
RTCapsule pinger = RTCapsule.builder("Pinger")
.attribute(RTAttribute.builder("count", RTInt.INSTANCE))
.port(RTPort.builder("ppPort", ppProtocol).external())
.port(SystemPorts.log("log"))
.statemachine(
RTStateMachine.builder()
.state(RTPseudoState.initial("initial"))
.state(RTState.builder("playing"))
.transition(
RTTransition.builder("initial", "playing")
.action("this->count = 1;\nppPort.ping(count).send();")
)
.transition(
RTTransition.builder("playing", "playing")
.trigger("ppPort", "pong")
.action("log.log(\"Round %d: got pong!\", round);\nppPort.ping(++count).send();")
)
)
.build();
RTCapsule ponger = RTCapsule.builder("Ponger")
.port(RTPort.builder("ppPort", ppProtocol).external().conjugate())
.port(SystemPorts.log("log"))
.statemachine(
RTStateMachine.builder()
.state(RTPseudoState.initial("initial"))
.state(RTState.builder("playing"))
.transition(RTTransition.builder("initial", "playing"))
.transition(
RTTransition.builder("playing", "playing")
.trigger("ppPort", "ping")
.action("log.log(\"Round %d: got ping!\", round);\nppPort.pong(round++).send();")
)
)
.build();
RTCapsule top = RTCapsule.builder("Top")
.part(RTCapsulePart.builder("pinger", pinger))
.part(RTCapsulePart.builder("ponger", ponger))
.connector(RTConnector.builder()
.end1(RTConnectorEnd.builder("ppPort", "pinger"))
.end2(RTConnectorEnd.builder("ppPort", "ponger"))
)
.build();
return RTModel.builder("PingerPonger", top)
.capsule(pinger)
.capsule(ponger)
.protocol(ppProtocol)
.build();
}
public static void main(String[] args) {
// create model
RTModel pingerPonger = createPingerPonger();
// validate it
RTModelValidator validator = new RTModelValidator(pingerPonger, false);
if (!validator.validate(false)) {
System.out.println(validator.getMessages());
return;
}
// write it
PapyrusRTWriter.write("PingerPonger.uml", pingerPonger);
// you can read it again
RTModel asRead = PapyrusRTReader.read("PingerPonger.uml");
// semantic equality (not really, but forgiving. e.g., diff in element names are ignored)
assert (RTEqualityHelper.isEqual(asRead, pingerPonger));
// copy it && modify something
// You can use an RTDeepCopier to copy multiple elements. Copies are cached so an element is never copied twice.
RTModel copy = (RTModel) new RTDeepCopier().copy(pingerPonger);
copy.getTop().getCapsule().getConnectors().clear();
// get 'semantic' difference
System.out.println(RTEqualityHelper.diff(pingerPonger, copy));
// generate the code
CppCodeGenerator.Companion.generate(asRead, "code");
// Draw the model
RTVisualizer.INSTANCE.draw(asRead, new ArrayList<>());
}
}