diff options
author | Thomas Schuetz | 2012-10-04 18:35:23 +0000 |
---|---|---|
committer | Thomas Schuetz | 2012-10-04 18:36:19 +0000 |
commit | c18e073a3d56e7ddf67ea99b5ec5eee07dc277f2 (patch) | |
tree | 9bbc7b8ce08fe8425f982488b9c29594572cc698 /examples | |
parent | 9898c539d6ed47ae91a3f53edc4bd44fbaa977e7 (diff) | |
download | org.eclipse.etrice-c18e073a3d56e7ddf67ea99b5ec5eee07dc277f2.tar.gz org.eclipse.etrice-c18e073a3d56e7ddf67ea99b5ec5eee07dc277f2.tar.xz org.eclipse.etrice-c18e073a3d56e7ddf67ea99b5ec5eee07dc277f2.zip |
[tutorials.simulators.trafficlight.example] added example for traffic light - start for new tutorial
Diffstat (limited to 'examples')
14 files changed, 1035 insertions, 0 deletions
diff --git a/examples/org.eclipse.etrice.tutorials.simulators.trafficlight.example/.classpath b/examples/org.eclipse.etrice.tutorials.simulators.trafficlight.example/.classpath new file mode 100644 index 000000000..e1308e9b2 --- /dev/null +++ b/examples/org.eclipse.etrice.tutorials.simulators.trafficlight.example/.classpath @@ -0,0 +1,9 @@ +<?xml version="1.0" encoding="UTF-8"?> +<classpath> + <classpathentry kind="src" path="src"/> + <classpathentry kind="src" path="src-gen"/> + <classpathentry kind="src" path="/org.eclipse.etrice.runtime.java"/> + <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/> + <classpathentry combineaccessrules="false" kind="src" path="/org.eclipse.etrice.modellib.java"/> + <classpathentry kind="output" path="bin"/> +</classpath> diff --git a/examples/org.eclipse.etrice.tutorials.simulators.trafficlight.example/.gitignore b/examples/org.eclipse.etrice.tutorials.simulators.trafficlight.example/.gitignore new file mode 100644 index 000000000..ba077a403 --- /dev/null +++ b/examples/org.eclipse.etrice.tutorials.simulators.trafficlight.example/.gitignore @@ -0,0 +1 @@ +bin diff --git a/examples/org.eclipse.etrice.tutorials.simulators.trafficlight.example/.project b/examples/org.eclipse.etrice.tutorials.simulators.trafficlight.example/.project new file mode 100644 index 000000000..4112661dd --- /dev/null +++ b/examples/org.eclipse.etrice.tutorials.simulators.trafficlight.example/.project @@ -0,0 +1,24 @@ +<?xml version="1.0" encoding="UTF-8"?> +<projectDescription> + <name>org.eclipse.etrice.tutorials.simulators.trafficlight.example</name> + <comment></comment> + <projects> + <project>org.eclipse.etrice.runtime.java</project> + </projects> + <buildSpec> + <buildCommand> + <name>org.eclipse.jdt.core.javabuilder</name> + <arguments> + </arguments> + </buildCommand> + <buildCommand> + <name>org.eclipse.xtext.ui.shared.xtextBuilder</name> + <arguments> + </arguments> + </buildCommand> + </buildSpec> + <natures> + <nature>org.eclipse.jdt.core.javanature</nature> + <nature>org.eclipse.xtext.ui.shared.xtextNature</nature> + </natures> +</projectDescription> diff --git a/examples/org.eclipse.etrice.tutorials.simulators.trafficlight.example/build.properties b/examples/org.eclipse.etrice.tutorials.simulators.trafficlight.example/build.properties new file mode 100644 index 000000000..83655578d --- /dev/null +++ b/examples/org.eclipse.etrice.tutorials.simulators.trafficlight.example/build.properties @@ -0,0 +1,2 @@ +source.. = src/,\ +src-gen/ diff --git a/examples/org.eclipse.etrice.tutorials.simulators.trafficlight.example/gen_trafficlight.example.launch b/examples/org.eclipse.etrice.tutorials.simulators.trafficlight.example/gen_trafficlight.example.launch new file mode 100644 index 000000000..e6414a456 --- /dev/null +++ b/examples/org.eclipse.etrice.tutorials.simulators.trafficlight.example/gen_trafficlight.example.launch @@ -0,0 +1,15 @@ +<?xml version="1.0" encoding="UTF-8" standalone="no"?> +<launchConfiguration type="org.eclipse.etrice.generator.launch.java.launchConfigurationType"> +<booleanAttribute key="GenInstanceDiagram" value="false"/> +<stringAttribute key="GenModelPath" value=""/> +<booleanAttribute key="Lib" value="false"/> +<listAttribute key="ModelFiles"> +<listEntry value="${workspace_loc:/org.eclipse.etrice.tutorials.simulators.trafficlight.example/model/trafficlight.example.config}"/> +<listEntry value="${workspace_loc:/org.eclipse.etrice.tutorials.simulators.trafficlight.example/model/trafficlight.example.room}"/> +</listAttribute> +<booleanAttribute key="SaveGenModel" value="false"/> +<stringAttribute key="org.eclipse.debug.core.ATTR_REFRESH_SCOPE" value="${workspace}"/> +<listAttribute key="org.eclipse.debug.ui.favoriteGroups"> +<listEntry value="org.eclipse.debug.ui.launchGroup.run"/> +</listAttribute> +</launchConfiguration> diff --git a/examples/org.eclipse.etrice.tutorials.simulators.trafficlight.example/model/trafficlight.example.config b/examples/org.eclipse.etrice.tutorials.simulators.trafficlight.example/model/trafficlight.example.config new file mode 100644 index 000000000..4b6e02dca --- /dev/null +++ b/examples/org.eclipse.etrice.tutorials.simulators.trafficlight.example/model/trafficlight.example.config @@ -0,0 +1,15 @@ +import trafficlight.example.* from "trafficlight.example.room" + +ActorInstanceConfig SSTraffic/application/light1{ + Attr ipConfig{ + Attr IPAddr="127.0.0.1" + Attr TcpPort=4441 + } +} + +ActorInstanceConfig SSTraffic/application/light2 { + Attr ipConfig{ + Attr IPAddr="127.0.0.1" + Attr TcpPort=4442 + } +}
\ No newline at end of file diff --git a/examples/org.eclipse.etrice.tutorials.simulators.trafficlight.example/model/trafficlight.example.room b/examples/org.eclipse.etrice.tutorials.simulators.trafficlight.example/model/trafficlight.example.room new file mode 100644 index 000000000..ee8019e58 --- /dev/null +++ b/examples/org.eclipse.etrice.tutorials.simulators.trafficlight.example/model/trafficlight.example.room @@ -0,0 +1,130 @@ +RoomModel trafficlight.example { + import room.basic.types.* from "../../org.eclipse.etrice.modellib.java/models/Types.room" + import room.basic.service.timing.* from "../../org.eclipse.etrice.modellib.java/models/TimingService.room" + import room.basic.service.tcp.* from "../../org.eclipse.etrice.modellib.java/models/TcpService.room" + + SubSystemClass SSTraffic { + ActorRef application:Application + ActorRef TimingService: ATimingService + LayerConnection ref application satisfied_by TimingService.timer + } + + + ActorClass Application { + Structure { + ActorRef light1: TrafficLight + ActorRef light2: TrafficLight + } + Behavior { } + } + + ActorClass TrafficLight { + Structure { + conjugated Port tcpCtrl: PTcpControl + conjugated Port tcpPayload: PTcpPayload + SAP timeout: PTimer + SAP blinkerTimeout: PTimer + Attribute ipConfig : DTcpControl + ActorRef trafficLightSocket: ATcpClient + Binding trafficLightSocket.ControlPort and tcpCtrl + Binding trafficLightSocket.PayloadPort and tcpPayload + + + } + Behavior { + + Operation sendString (text: string){" + DTcpPayload pl = new DTcpPayload(); + pl.setData(text.getBytes()); + tcpPayload.send(new DTcpPayload(1, text.length(), text.getBytes())); + "} + StateMachine { + Transition init: initial -> OpenSocket { } + Transition tr0: OpenSocket -> tp0 of Off_Blinking { + triggers { + <established: tcpCtrl> + } + } + Transition tr2: Off_Blinking -> Red { + triggers { + <timeout: timeout> + } + } + Transition tr3: Red -> Yellow { + triggers { + <timeout: timeout> + } + } + Transition tr1: Yellow -> Green { + triggers { + <timeout: timeout> + } + } + Transition tr4: Green -> tp0 of Off_Blinking { + triggers { + <timeout: timeout> + } + } + State OpenSocket { + entry { + "tcpCtrl.open(ipConfig);" + } + } + State Yellow { + entry { + "sendString(\"carLights=yellow\\n\");" + "timeout.startTimeout(1000);" + } + } + State Red { + entry { + "sendString(\"carLights=red\\n\");" + "timeout.startTimeout(1000);" + } + } + State Green { + entry { + "sendString(\"carLights=green\\n\");" + "timeout.startTimeout(1000);" + } + } + State Off_Blinking { + entry { + "timeout.startTimeout(6000);" + } + subgraph { + Transition tr0: my tp0 -> On + Transition tr1: On -> Off { + triggers { + <timeout: blinkerTimeout> + } + } + Transition tr2: Off -> On { + triggers { + <timeout: blinkerTimeout> + } + } + EntryPoint tp0 + State On { + entry { + "sendString(\"carLights=red\\n\");" + "sendString(\"pedLights=red\\n\");" + "blinkerTimeout.startTimeout(1000);" + } + exit { + "sendString(\"carLights=off\\n\");" + "sendString(\"pedLights=off\\n\");" + } + } + State Off { + entry { + "blinkerTimeout.startTimeout(1000);" + } + } + } + } + } + } + } + +}
\ No newline at end of file diff --git a/examples/org.eclipse.etrice.tutorials.simulators.trafficlight.example/src-gen/readme.txt b/examples/org.eclipse.etrice.tutorials.simulators.trafficlight.example/src-gen/readme.txt new file mode 100644 index 000000000..1dc2b35ab --- /dev/null +++ b/examples/org.eclipse.etrice.tutorials.simulators.trafficlight.example/src-gen/readme.txt @@ -0,0 +1,4 @@ +This directory is an eTrice code generation target. +It will be erased every time the generator is executed. + +DO NOT PLACE OTHER FILES HERE! diff --git a/examples/org.eclipse.etrice.tutorials.simulators.trafficlight.example/src-gen/trafficlight/example/Application.java b/examples/org.eclipse.etrice.tutorials.simulators.trafficlight.example/src-gen/trafficlight/example/Application.java new file mode 100644 index 000000000..22483d7a7 --- /dev/null +++ b/examples/org.eclipse.etrice.tutorials.simulators.trafficlight.example/src-gen/trafficlight/example/Application.java @@ -0,0 +1,77 @@ +package trafficlight.example; + +import org.eclipse.etrice.runtime.java.messaging.Address; +import org.eclipse.etrice.runtime.java.messaging.IRTObject; +import org.eclipse.etrice.runtime.java.messaging.IMessageReceiver; +import org.eclipse.etrice.runtime.java.modelbase.ActorClassBase; +import org.eclipse.etrice.runtime.java.modelbase.SubSystemClassBase; +import org.eclipse.etrice.runtime.java.modelbase.InterfaceItemBase; +import org.eclipse.etrice.runtime.java.debugging.DebuggingService; +import static org.eclipse.etrice.runtime.java.etunit.EtUnit.*; + + + + + +public class Application extends ActorClassBase { + + + + //--------------------- ports + + //--------------------- saps + + //--------------------- services + + //--------------------- interface item IDs + + + /*--------------------- attributes ---------------------*/ + /*--------------------- operations ---------------------*/ + + //--------------------- construction + public Application(IRTObject parent, String name, Address[][] port_addr, Address[][] peer_addr){ + super(parent, name, port_addr[0][0], peer_addr[0][0]); + setClassName("Application"); + + // initialize attributes + + // own ports + + // own saps + + // own service implementations + } + + + //--------------------- attribute setters and getters + + + //--------------------- port getters + + //--------------------- lifecycle functions + public void init(){ + initUser(); + } + + public void start(){ + startUser(); + } + + public void stop(){ + stopUser(); + } + + public void destroy(){ + } + + //--------------------- no state machine + @Override + public void receiveEvent(InterfaceItemBase ifitem, int evt, Object data) { + handleSystemEvent(ifitem, evt, data); + } + + @Override + public void executeInitTransition(){ + } +}; diff --git a/examples/org.eclipse.etrice.tutorials.simulators.trafficlight.example/src-gen/trafficlight/example/SSTraffic.java b/examples/org.eclipse.etrice.tutorials.simulators.trafficlight.example/src-gen/trafficlight/example/SSTraffic.java new file mode 100644 index 000000000..d5d710d47 --- /dev/null +++ b/examples/org.eclipse.etrice.tutorials.simulators.trafficlight.example/src-gen/trafficlight/example/SSTraffic.java @@ -0,0 +1,279 @@ +package trafficlight.example; + +import org.eclipse.etrice.runtime.java.messaging.MessageService; +import org.eclipse.etrice.runtime.java.messaging.RTServices; +import org.eclipse.etrice.runtime.java.messaging.Address; +import org.eclipse.etrice.runtime.java.messaging.IRTObject; +import org.eclipse.etrice.runtime.java.messaging.RTSystemServicesProtocol.*; +import org.eclipse.etrice.runtime.java.modelbase.ActorClassBase; +import org.eclipse.etrice.runtime.java.modelbase.SubSystemClassBase; +import org.eclipse.etrice.runtime.java.modelbase.InterfaceItemBase; + +import room.basic.service.tcp.*;import room.basic.service.timing.*; + + + +public class SSTraffic extends SubSystemClassBase{ + + + public SSTraffic(String name) { + super(name); + } + + @Override + public void receiveEvent(InterfaceItemBase ifitem, int evt, Object data){ + } + + @Override + public void instantiateMessageServices(){ + + RTServices.getInstance().getMsgSvcCtrl().addMsgSvc(new MessageService(this, new Address(0, 0, 0),"MessageService_Main")); + } + + @Override + public void instantiateActors(){ + + // all addresses + // Addresses for the Subsystem Systemport + Address addr_item_SystemPort_0 = new Address(0,0,123); + Address addr_item_SystemPort_1 = new Address(0,0,124); + Address addr_item_SystemPort_2 = new Address(0,0,125); + Address addr_item_SystemPort_3 = new Address(0,0,126); + Address addr_item_SystemPort_4 = new Address(0,0,127); + Address addr_item_SystemPort_5 = new Address(0,0,128); + + // actor instance /SSTraffic/application itself => Systemport Address + // TODOTJ: For each Actor, multiple addresses should be generated (actor?, systemport, debugport) + Address addr_item__SSTraffic_application = new Address(0,0,101); + // interface items of /SSTraffic/application + // actor instance /SSTraffic/application/light1 itself => Systemport Address + // TODOTJ: For each Actor, multiple addresses should be generated (actor?, systemport, debugport) + Address addr_item__SSTraffic_application_light1 = new Address(0,0,102); + // interface items of /SSTraffic/application/light1 + Address addr_item__SSTraffic_application_light1_tcpCtrl = new Address(0,0,103); + Address addr_item__SSTraffic_application_light1_tcpPayload = new Address(0,0,104); + Address addr_item__SSTraffic_application_light1_timeout = new Address(0,0,105); + Address addr_item__SSTraffic_application_light1_blinkerTimeout = new Address(0,0,106); + // actor instance /SSTraffic/application/light1/trafficLightSocket itself => Systemport Address + // TODOTJ: For each Actor, multiple addresses should be generated (actor?, systemport, debugport) + Address addr_item__SSTraffic_application_light1_trafficLightSocket = new Address(0,0,107); + // interface items of /SSTraffic/application/light1/trafficLightSocket + Address addr_item__SSTraffic_application_light1_trafficLightSocket_ControlPort = new Address(0,0,108); + Address addr_item__SSTraffic_application_light1_trafficLightSocket_PayloadPort = new Address(0,0,109); + // actor instance /SSTraffic/application/light2 itself => Systemport Address + // TODOTJ: For each Actor, multiple addresses should be generated (actor?, systemport, debugport) + Address addr_item__SSTraffic_application_light2 = new Address(0,0,110); + // interface items of /SSTraffic/application/light2 + Address addr_item__SSTraffic_application_light2_tcpCtrl = new Address(0,0,111); + Address addr_item__SSTraffic_application_light2_tcpPayload = new Address(0,0,112); + Address addr_item__SSTraffic_application_light2_timeout = new Address(0,0,113); + Address addr_item__SSTraffic_application_light2_blinkerTimeout = new Address(0,0,114); + // actor instance /SSTraffic/application/light2/trafficLightSocket itself => Systemport Address + // TODOTJ: For each Actor, multiple addresses should be generated (actor?, systemport, debugport) + Address addr_item__SSTraffic_application_light2_trafficLightSocket = new Address(0,0,115); + // interface items of /SSTraffic/application/light2/trafficLightSocket + Address addr_item__SSTraffic_application_light2_trafficLightSocket_ControlPort = new Address(0,0,116); + Address addr_item__SSTraffic_application_light2_trafficLightSocket_PayloadPort = new Address(0,0,117); + // actor instance /SSTraffic/TimingService itself => Systemport Address + // TODOTJ: For each Actor, multiple addresses should be generated (actor?, systemport, debugport) + Address addr_item__SSTraffic_TimingService = new Address(0,0,118); + // interface items of /SSTraffic/TimingService + Address addr_item__SSTraffic_TimingService_timer_0 = new Address(0,0,119); + Address addr_item__SSTraffic_TimingService_timer_1 = new Address(0,0,120); + Address addr_item__SSTraffic_TimingService_timer_2 = new Address(0,0,121); + Address addr_item__SSTraffic_TimingService_timer_3 = new Address(0,0,122); + + // instantiate all actor instances + instances = new ActorClassBase[6]; + instances[0] = new Application( + this, + "application", + // own interface item addresses + new Address[][] {{addr_item__SSTraffic_application} + }, + // peer interface item addresses + new Address[][] {{addr_item_SystemPort_0} + } + ); + instances[1] = new TrafficLight( + instances[0], + "light1", + // own interface item addresses + new Address[][] {{addr_item__SSTraffic_application_light1}, + { + addr_item__SSTraffic_application_light1_tcpCtrl + }, + { + addr_item__SSTraffic_application_light1_tcpPayload + }, + { + addr_item__SSTraffic_application_light1_timeout + }, + { + addr_item__SSTraffic_application_light1_blinkerTimeout + } + }, + // peer interface item addresses + new Address[][] {{addr_item_SystemPort_1}, + { + addr_item__SSTraffic_application_light1_trafficLightSocket_ControlPort + }, + { + addr_item__SSTraffic_application_light1_trafficLightSocket_PayloadPort + }, + { + addr_item__SSTraffic_TimingService_timer_0 + }, + { + addr_item__SSTraffic_TimingService_timer_1 + } + } + ); + instances[2] = new ATcpClient( + instances[1], + "trafficLightSocket", + // own interface item addresses + new Address[][] {{addr_item__SSTraffic_application_light1_trafficLightSocket}, + { + addr_item__SSTraffic_application_light1_trafficLightSocket_ControlPort + }, + { + addr_item__SSTraffic_application_light1_trafficLightSocket_PayloadPort + } + }, + // peer interface item addresses + new Address[][] {{addr_item_SystemPort_2}, + { + addr_item__SSTraffic_application_light1_tcpCtrl + }, + { + addr_item__SSTraffic_application_light1_tcpPayload + } + } + ); + instances[3] = new TrafficLight( + instances[0], + "light2", + // own interface item addresses + new Address[][] {{addr_item__SSTraffic_application_light2}, + { + addr_item__SSTraffic_application_light2_tcpCtrl + }, + { + addr_item__SSTraffic_application_light2_tcpPayload + }, + { + addr_item__SSTraffic_application_light2_timeout + }, + { + addr_item__SSTraffic_application_light2_blinkerTimeout + } + }, + // peer interface item addresses + new Address[][] {{addr_item_SystemPort_3}, + { + addr_item__SSTraffic_application_light2_trafficLightSocket_ControlPort + }, + { + addr_item__SSTraffic_application_light2_trafficLightSocket_PayloadPort + }, + { + addr_item__SSTraffic_TimingService_timer_2 + }, + { + addr_item__SSTraffic_TimingService_timer_3 + } + } + ); + instances[4] = new ATcpClient( + instances[3], + "trafficLightSocket", + // own interface item addresses + new Address[][] {{addr_item__SSTraffic_application_light2_trafficLightSocket}, + { + addr_item__SSTraffic_application_light2_trafficLightSocket_ControlPort + }, + { + addr_item__SSTraffic_application_light2_trafficLightSocket_PayloadPort + } + }, + // peer interface item addresses + new Address[][] {{addr_item_SystemPort_4}, + { + addr_item__SSTraffic_application_light2_tcpCtrl + }, + { + addr_item__SSTraffic_application_light2_tcpPayload + } + } + ); + instances[5] = new ATimingService( + this, + "TimingService", + // own interface item addresses + new Address[][] {{addr_item__SSTraffic_TimingService}, + { + addr_item__SSTraffic_TimingService_timer_0, + addr_item__SSTraffic_TimingService_timer_1, + addr_item__SSTraffic_TimingService_timer_2, + addr_item__SSTraffic_TimingService_timer_3 + } + }, + // peer interface item addresses + new Address[][] {{addr_item_SystemPort_5}, + { + addr_item__SSTraffic_application_light1_timeout, + addr_item__SSTraffic_application_light1_blinkerTimeout, + addr_item__SSTraffic_application_light2_timeout, + addr_item__SSTraffic_application_light2_blinkerTimeout + } + } + ); + + // apply instance attribute configurations + { + TrafficLight inst = (TrafficLight) instances[1]; + inst.getIpConfig().setIPAddr("127.0.0.1"); + inst.getIpConfig().setTcpPort(4441); + } + { + TrafficLight inst = (TrafficLight) instances[3]; + inst.getIpConfig().setIPAddr("127.0.0.1"); + inst.getIpConfig().setTcpPort(4442); + } + + // create the subsystem system port + RTSystemPort = new RTSystemServicesProtocolConjPortRepl(this, "RTSystemPort", + 0, //local ID + // own addresses + new Address[]{ + addr_item_SystemPort_0, + addr_item_SystemPort_1, + addr_item_SystemPort_2, + addr_item_SystemPort_3, + addr_item_SystemPort_4, + addr_item_SystemPort_5 + }, + // peer addresses + new Address[]{ + addr_item__SSTraffic_application, + addr_item__SSTraffic_application_light1, + addr_item__SSTraffic_application_light1_trafficLightSocket, + addr_item__SSTraffic_application_light2, + addr_item__SSTraffic_application_light2_trafficLightSocket, + addr_item__SSTraffic_TimingService + }); + } + + + @Override + public void init(){ + super.init(); + } + + @Override + public void stop(){ + super.stop(); + } + +}; diff --git a/examples/org.eclipse.etrice.tutorials.simulators.trafficlight.example/src-gen/trafficlight/example/SSTrafficRunner.java b/examples/org.eclipse.etrice.tutorials.simulators.trafficlight.example/src-gen/trafficlight/example/SSTrafficRunner.java new file mode 100644 index 000000000..1009770b6 --- /dev/null +++ b/examples/org.eclipse.etrice.tutorials.simulators.trafficlight.example/src-gen/trafficlight/example/SSTrafficRunner.java @@ -0,0 +1,24 @@ +/** + * @author generated by eTrice + * + * this class contains the main function running component SSTraffic + * it instantiates SSTraffic and starts and ends the lifecycle + */ + +package trafficlight.example; + +import org.eclipse.etrice.runtime.java.modelbase.SubSystemRunnerBase; + +class SSTrafficRunner extends SubSystemRunnerBase { + + /** + * main function + * creates the main component and starts and stops the lifecycle + */ + public static void main(String[] args) { + // instantiate the main component + SSTraffic main_component = new SSTraffic("SSTraffic"); + + run(main_component, args); + } +}; diff --git a/examples/org.eclipse.etrice.tutorials.simulators.trafficlight.example/src-gen/trafficlight/example/TrafficLight.java b/examples/org.eclipse.etrice.tutorials.simulators.trafficlight.example/src-gen/trafficlight/example/TrafficLight.java new file mode 100644 index 000000000..fb53e94e4 --- /dev/null +++ b/examples/org.eclipse.etrice.tutorials.simulators.trafficlight.example/src-gen/trafficlight/example/TrafficLight.java @@ -0,0 +1,432 @@ +package trafficlight.example; + +import org.eclipse.etrice.runtime.java.messaging.Address; +import org.eclipse.etrice.runtime.java.messaging.IRTObject; +import org.eclipse.etrice.runtime.java.messaging.IMessageReceiver; +import org.eclipse.etrice.runtime.java.modelbase.ActorClassBase; +import org.eclipse.etrice.runtime.java.modelbase.SubSystemClassBase; +import org.eclipse.etrice.runtime.java.modelbase.InterfaceItemBase; +import org.eclipse.etrice.runtime.java.debugging.DebuggingService; +import static org.eclipse.etrice.runtime.java.etunit.EtUnit.*; + +import room.basic.service.tcp.*; +import room.basic.service.timing.*; + +import room.basic.service.tcp.PTcpControl.*; +import room.basic.service.tcp.PTcpPayload.*; +import room.basic.service.timing.PTimer.*; + + + +public class TrafficLight extends ActorClassBase { + + + + //--------------------- ports + protected PTcpControlConjPort tcpCtrl = null; + protected PTcpPayloadConjPort tcpPayload = null; + + //--------------------- saps + protected PTimerConjPort timeout = null; + protected PTimerConjPort blinkerTimeout = null; + + //--------------------- services + + //--------------------- interface item IDs + public static final int IFITEM_tcpCtrl = 1; + public static final int IFITEM_tcpPayload = 2; + public static final int IFITEM_timeout = 3; + public static final int IFITEM_blinkerTimeout = 4; + + + /*--------------------- attributes ---------------------*/ + DTcpControl ipConfig; + /*--------------------- operations ---------------------*/ + public void sendString(String text) { + + DTcpPayload pl = new DTcpPayload(); + pl.setData(text.getBytes()); + tcpPayload.send(new DTcpPayload(1, text.length(), text.getBytes())); + } + + //--------------------- construction + public TrafficLight(IRTObject parent, String name, Address[][] port_addr, Address[][] peer_addr){ + super(parent, name, port_addr[0][0], peer_addr[0][0]); + setClassName("TrafficLight"); + + // initialize attributes + ipConfig = new DTcpControl(); + + // own ports + tcpCtrl = new PTcpControlConjPort(this, "tcpCtrl", IFITEM_tcpCtrl, 0, port_addr[IFITEM_tcpCtrl][0], peer_addr[IFITEM_tcpCtrl][0]); + tcpPayload = new PTcpPayloadConjPort(this, "tcpPayload", IFITEM_tcpPayload, 0, port_addr[IFITEM_tcpPayload][0], peer_addr[IFITEM_tcpPayload][0]); + + // own saps + timeout = new PTimerConjPort(this, "timeout", IFITEM_timeout, 0, port_addr[IFITEM_timeout][0], peer_addr[IFITEM_timeout][0]); + blinkerTimeout = new PTimerConjPort(this, "blinkerTimeout", IFITEM_blinkerTimeout, 0, port_addr[IFITEM_blinkerTimeout][0], peer_addr[IFITEM_blinkerTimeout][0]); + + // own service implementations + } + + + //--------------------- attribute setters and getters + public void setIpConfig (DTcpControl ipConfig) { + this.ipConfig = ipConfig; + } + public DTcpControl getIpConfig () { + return this.ipConfig; + } + + + //--------------------- port getters + public PTcpControlConjPort getTcpCtrl (){ + return this.tcpCtrl; + } + public PTcpPayloadConjPort getTcpPayload (){ + return this.tcpPayload; + } + public PTimerConjPort getTimeout (){ + return this.timeout; + } + public PTimerConjPort getBlinkerTimeout (){ + return this.blinkerTimeout; + } + + //--------------------- lifecycle functions + public void init(){ + initUser(); + } + + public void start(){ + startUser(); + } + + public void stop(){ + stopUser(); + } + + public void destroy(){ + } + + + /* state IDs */ + public static final int STATE_Off_Blinking = 2; + public static final int STATE_OpenSocket = 3; + public static final int STATE_Yellow = 4; + public static final int STATE_Red = 5; + public static final int STATE_Green = 6; + public static final int STATE_Off_Blinking_On = 7; + public static final int STATE_Off_Blinking_Off = 8; + + /* transition chains */ + public static final int CHAIN_Off_Blinking_TRANS_tr1_FROM_On_TO_Off_BY_timeoutblinkerTimeout = 1; + public static final int CHAIN_Off_Blinking_TRANS_tr2_FROM_Off_TO_On_BY_timeoutblinkerTimeout = 2; + public static final int CHAIN_TRANS_INITIAL_TO__OpenSocket = 3; + public static final int CHAIN_TRANS_tr0_FROM_OpenSocket_TO_Off_Blinking_tp0_BY_establishedtcpCtrl = 4; + public static final int CHAIN_TRANS_tr1_FROM_Yellow_TO_Green_BY_timeouttimeout = 5; + public static final int CHAIN_TRANS_tr2_FROM_Off_Blinking_TO_Red_BY_timeouttimeout = 6; + public static final int CHAIN_TRANS_tr3_FROM_Red_TO_Yellow_BY_timeouttimeout = 7; + public static final int CHAIN_TRANS_tr4_FROM_Green_TO_Off_Blinking_tp0_BY_timeouttimeout = 8; + + /* triggers */ + public static final int POLLING = 0; + public static final int TRIG_blinkerTimeout__timeout = IFITEM_blinkerTimeout + EVT_SHIFT*PTimer.OUT_timeout; + public static final int TRIG_tcpCtrl__established = IFITEM_tcpCtrl + EVT_SHIFT*PTcpControl.OUT_established; + public static final int TRIG_timeout__timeout = IFITEM_timeout + EVT_SHIFT*PTimer.OUT_timeout; + + // state names + protected static final String stateStrings[] = {"<no state>","<top>","Off_Blinking", + "OpenSocket", + "Yellow", + "Red", + "Green", + "Off_Blinking_On", + "Off_Blinking_Off" + }; + + // history + protected int history[] = {NO_STATE,NO_STATE,NO_STATE,NO_STATE,NO_STATE,NO_STATE,NO_STATE,NO_STATE,NO_STATE}; + + private void setState(int new_state) { + DebuggingService.getInstance().addActorState(this,stateStrings[new_state]); + if (stateStrings[new_state]!="Idle") { + System.out.println(getInstancePath() + " -> " + stateStrings[new_state]); + } + this.state = new_state; + } + + /* Entry and Exit Codes */ + protected void entry_OpenSocket() { + tcpCtrl.open(ipConfig); + } + protected void entry_Yellow() { + sendString("carLights=yellow\n"); + timeout.startTimeout(1000); + } + protected void entry_Red() { + sendString("carLights=red\n"); + timeout.startTimeout(1000); + } + protected void entry_Green() { + sendString("carLights=green\n"); + timeout.startTimeout(1000); + } + protected void entry_Off_Blinking() { + timeout.startTimeout(6000); + } + protected void entry_Off_Blinking_On() { + sendString("carLights=red\n"); + sendString("pedLights=red\n"); + blinkerTimeout.startTimeout(1000); + } + protected void exit_Off_Blinking_On() { + sendString("carLights=off\n"); + sendString("pedLights=off\n"); + } + protected void entry_Off_Blinking_Off() { + blinkerTimeout.startTimeout(1000); + } + + /* Action Codes */ + + /** + * calls exit codes while exiting from the current state to one of its + * parent states while remembering the history + * @param current - the current state + * @param to - the final parent state + * @param handler - entry and exit codes are called only if not handler (for handler TransitionPoints) + */ + private void exitTo(int current, int to, boolean handler) { + while (current!=to) { + switch (current) { + case STATE_OpenSocket: + this.history[STATE_TOP] = STATE_OpenSocket; + current = STATE_TOP; + break; + case STATE_Yellow: + this.history[STATE_TOP] = STATE_Yellow; + current = STATE_TOP; + break; + case STATE_Red: + this.history[STATE_TOP] = STATE_Red; + current = STATE_TOP; + break; + case STATE_Green: + this.history[STATE_TOP] = STATE_Green; + current = STATE_TOP; + break; + case STATE_Off_Blinking: + this.history[STATE_TOP] = STATE_Off_Blinking; + current = STATE_TOP; + break; + case STATE_Off_Blinking_On: + if (!handler) exit_Off_Blinking_On(); + this.history[STATE_Off_Blinking] = STATE_Off_Blinking_On; + current = STATE_Off_Blinking; + break; + case STATE_Off_Blinking_Off: + this.history[STATE_Off_Blinking] = STATE_Off_Blinking_Off; + current = STATE_Off_Blinking; + break; + } + } + } + + /** + * calls action, entry and exit codes along a transition chain. The generic data are cast to typed data + * matching the trigger of this chain. The ID of the final state is returned + * @param chain - the chain ID + * @param generic_data - the generic data pointer + * @return the ID of the final state + */ + private int executeTransitionChain(int chain, InterfaceItemBase ifitem, Object generic_data) { + switch (chain) { + case CHAIN_TRANS_INITIAL_TO__OpenSocket: + { + return STATE_OpenSocket; + } + case CHAIN_TRANS_tr0_FROM_OpenSocket_TO_Off_Blinking_tp0_BY_establishedtcpCtrl: + { + entry_Off_Blinking(); + return STATE_Off_Blinking_On; + } + case CHAIN_TRANS_tr2_FROM_Off_Blinking_TO_Red_BY_timeouttimeout: + { + return STATE_Red; + } + case CHAIN_TRANS_tr3_FROM_Red_TO_Yellow_BY_timeouttimeout: + { + return STATE_Yellow; + } + case CHAIN_TRANS_tr1_FROM_Yellow_TO_Green_BY_timeouttimeout: + { + return STATE_Green; + } + case CHAIN_TRANS_tr4_FROM_Green_TO_Off_Blinking_tp0_BY_timeouttimeout: + { + entry_Off_Blinking(); + return STATE_Off_Blinking_On; + } + case CHAIN_Off_Blinking_TRANS_tr1_FROM_On_TO_Off_BY_timeoutblinkerTimeout: + { + return STATE_Off_Blinking_Off; + } + case CHAIN_Off_Blinking_TRANS_tr2_FROM_Off_TO_On_BY_timeoutblinkerTimeout: + { + return STATE_Off_Blinking_On; + } + } + return NO_STATE; + } + + /** + * calls entry codes while entering a state's history. The ID of the final leaf state is returned + * @param state - the state which is entered + * @param handler - entry code is executed if not handler + * @return - the ID of the final leaf state + */ + private int enterHistory(int state, boolean handler, boolean skip_entry) { + while (true) { + switch (state) { + case STATE_OpenSocket: + if (!(skip_entry || handler)) entry_OpenSocket(); + // in leaf state: return state id + return STATE_OpenSocket; + case STATE_Yellow: + if (!(skip_entry || handler)) entry_Yellow(); + // in leaf state: return state id + return STATE_Yellow; + case STATE_Red: + if (!(skip_entry || handler)) entry_Red(); + // in leaf state: return state id + return STATE_Red; + case STATE_Green: + if (!(skip_entry || handler)) entry_Green(); + // in leaf state: return state id + return STATE_Green; + case STATE_Off_Blinking: + if (!(skip_entry || handler)) entry_Off_Blinking(); + // state has a sub graph + // without init transition + state = this.history[STATE_Off_Blinking]; + break; + case STATE_Off_Blinking_On: + if (!(skip_entry || handler)) entry_Off_Blinking_On(); + // in leaf state: return state id + return STATE_Off_Blinking_On; + case STATE_Off_Blinking_Off: + if (!(skip_entry || handler)) entry_Off_Blinking_Off(); + // in leaf state: return state id + return STATE_Off_Blinking_Off; + case STATE_TOP: + state = this.history[STATE_TOP]; + break; + } + skip_entry = false; + } + //return NO_STATE; // required by CDT but detected as unreachable by JDT because of while (true) + } + + public void executeInitTransition() { + int chain = CHAIN_TRANS_INITIAL_TO__OpenSocket; + int next = executeTransitionChain(chain, null, null); + next = enterHistory(next, false, false); + setState(next); + } + + /* receiveEvent contains the main implementation of the FSM */ + public void receiveEvent(InterfaceItemBase ifitem, int evt, Object generic_data) { + int trigger = ifitem.getLocalId() + EVT_SHIFT*evt; + int chain = NOT_CAUGHT; + int catching_state = NO_STATE; + boolean is_handler = false; + boolean skip_entry = false; + + if (!handleSystemEvent(ifitem, evt, generic_data)) { + switch (this.state) { + case STATE_OpenSocket: + switch(trigger) { + case TRIG_tcpCtrl__established: + { + chain = CHAIN_TRANS_tr0_FROM_OpenSocket_TO_Off_Blinking_tp0_BY_establishedtcpCtrl; + catching_state = STATE_TOP; + } + break; + } + break; + case STATE_Yellow: + switch(trigger) { + case TRIG_timeout__timeout: + { + chain = CHAIN_TRANS_tr1_FROM_Yellow_TO_Green_BY_timeouttimeout; + catching_state = STATE_TOP; + } + break; + } + break; + case STATE_Red: + switch(trigger) { + case TRIG_timeout__timeout: + { + chain = CHAIN_TRANS_tr3_FROM_Red_TO_Yellow_BY_timeouttimeout; + catching_state = STATE_TOP; + } + break; + } + break; + case STATE_Green: + switch(trigger) { + case TRIG_timeout__timeout: + { + chain = CHAIN_TRANS_tr4_FROM_Green_TO_Off_Blinking_tp0_BY_timeouttimeout; + catching_state = STATE_TOP; + } + break; + } + break; + case STATE_Off_Blinking_On: + switch(trigger) { + case TRIG_blinkerTimeout__timeout: + { + chain = CHAIN_Off_Blinking_TRANS_tr1_FROM_On_TO_Off_BY_timeoutblinkerTimeout; + catching_state = STATE_Off_Blinking; + } + break; + case TRIG_timeout__timeout: + { + chain = CHAIN_TRANS_tr2_FROM_Off_Blinking_TO_Red_BY_timeouttimeout; + catching_state = STATE_TOP; + } + break; + } + break; + case STATE_Off_Blinking_Off: + switch(trigger) { + case TRIG_blinkerTimeout__timeout: + { + chain = CHAIN_Off_Blinking_TRANS_tr2_FROM_Off_TO_On_BY_timeoutblinkerTimeout; + catching_state = STATE_Off_Blinking; + } + break; + case TRIG_timeout__timeout: + { + chain = CHAIN_TRANS_tr2_FROM_Off_Blinking_TO_Red_BY_timeouttimeout; + catching_state = STATE_TOP; + } + break; + } + break; + } + } + if (chain != NOT_CAUGHT) { + exitTo(this.state, catching_state, is_handler); + int next = executeTransitionChain(chain, ifitem, generic_data); + next = enterHistory(next, is_handler, skip_entry); + setState(next); + } + } + + //****************************************** + // END of generated code for FSM + //****************************************** +}; diff --git a/examples/org.eclipse.etrice.tutorials.simulators.trafficlight.example/tmp/log/SSTraffic_Async.seq b/examples/org.eclipse.etrice.tutorials.simulators.trafficlight.example/tmp/log/SSTraffic_Async.seq new file mode 100644 index 000000000..f3782be0a --- /dev/null +++ b/examples/org.eclipse.etrice.tutorials.simulators.trafficlight.example/tmp/log/SSTraffic_Async.seq @@ -0,0 +1,22 @@ +#generated MSC for Trace2UML + /SSTraffic >-- /SSTraffic/application executeInitialTransition + /SSTraffic >-- /SSTraffic/application/light1 executeInitialTransition + /SSTraffic >-- /SSTraffic/application/light1/trafficLightSocket executeInitialTransition + /SSTraffic >-- /SSTraffic/application/light2 executeInitialTransition + /SSTraffic >-- /SSTraffic/application/light2/trafficLightSocket executeInitialTransition + /SSTraffic >-- /SSTraffic/TimingService executeInitialTransition + /SSTraffic --> /SSTraffic/application executeInitialTransition + /SSTraffic --> /SSTraffic/application/light1 executeInitialTransition + /SSTraffic/application/light1 >-- /SSTraffic/application/light1/trafficLightSocket open + /SSTraffic/application/light1 >>> OpenSocket + /SSTraffic --> /SSTraffic/application/light1/trafficLightSocket executeInitialTransition + /SSTraffic/application/light1/trafficLightSocket >>> closed + /SSTraffic --> /SSTraffic/application/light2 executeInitialTransition + /SSTraffic/application/light2 >-- /SSTraffic/application/light2/trafficLightSocket open + /SSTraffic/application/light2 >>> OpenSocket + /SSTraffic --> /SSTraffic/application/light2/trafficLightSocket executeInitialTransition + /SSTraffic/application/light2/trafficLightSocket >>> closed + /SSTraffic --> /SSTraffic/TimingService executeInitialTransition + /SSTraffic/TimingService >>> Operational + /SSTraffic/application/light1 --> /SSTraffic/application/light1/trafficLightSocket open + /SSTraffic/application/light1/trafficLightSocket >-- /SSTraffic/application/light1 error diff --git a/examples/org.eclipse.etrice.tutorials.simulators.trafficlight.example/tmp/log/SSTraffic_Sync.seq b/examples/org.eclipse.etrice.tutorials.simulators.trafficlight.example/tmp/log/SSTraffic_Sync.seq new file mode 100644 index 000000000..58543452e --- /dev/null +++ b/examples/org.eclipse.etrice.tutorials.simulators.trafficlight.example/tmp/log/SSTraffic_Sync.seq @@ -0,0 +1 @@ +#generated MSC for Trace2UML |