-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
RIC-659 adding Class with State machine sample
- Loading branch information
Rajesh Kumar
authored and
Rajesh Kumar
committed
Dec 5, 2023
1 parent
7ceb7a7
commit a1f7a46
Showing
6 changed files
with
132 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
[[rt::decl]] | ||
` | ||
class ILogger { | ||
public: | ||
// Log a message | ||
virtual void logMsg(RTString str) = 0; | ||
}; | ||
` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// A passive class with a state machine implementing the traffic light logic | ||
class Light { | ||
[[rt::header_preface]] | ||
` | ||
#include "ILogger.art.h" | ||
` | ||
|
||
[[rt::decl]] | ||
` | ||
public: | ||
Light(ILogger*); | ||
private: | ||
ILogger* theLogger; | ||
` | ||
[[rt::impl]] | ||
` | ||
Light::Light(ILogger* logger) : theLogger(logger) { | ||
rtg_init1(); | ||
} | ||
` | ||
|
||
/* Trigger Operations */ | ||
trigger switchLights(); | ||
|
||
/* State Machine */ | ||
statemachine { | ||
state Red, Green, Yellow; | ||
initial -> Red; | ||
goGreen: Red -> Green on switchLights() | ||
` | ||
theLogger->logMsg("Green"); | ||
`; | ||
goYellow: Green -> Yellow on switchLights() | ||
` | ||
theLogger->logMsg("Yellow"); | ||
`; | ||
goRed: Yellow -> Red when `true ` | ||
` | ||
theLogger->logMsg("Red"); | ||
`; | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
protocol LogProtocol { | ||
/* In Events */ | ||
/* Out Events */ | ||
out logMsg(`RTString`); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// A Logger class that implements an interface ILogger through which the passive class Light | ||
// can send events. | ||
capsule Logger { | ||
|
||
/* Ports */ | ||
service behavior port p~ : LogProtocol; | ||
behavior port log : Log; | ||
/* Parts */ | ||
/* Connectors */ | ||
/* State Machine */ | ||
statemachine { | ||
state State { | ||
entry | ||
` | ||
log.log("Logger ready to log messages"); | ||
log.commit(); | ||
`; | ||
log: on p.logMsg | ||
` | ||
log.log(rtdata, &RTType_RTString); | ||
log.commit(); | ||
`; | ||
}; | ||
initial -> State; | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// This capsule implements a traffic light by means of a passive class Light which contains the state machine. | ||
capsule TrafficLight : `ILogger` { | ||
[[rt::header_preface]] | ||
` | ||
#include "ILogger.art.h" | ||
#include "Light.h" | ||
` | ||
[[rt::decl]] | ||
` | ||
public: | ||
// Log a message | ||
virtual void logMsg(RTString str) override; | ||
|
||
private: | ||
Light* theLight; | ||
` | ||
[[rt::impl]] | ||
` | ||
void TrafficLight_Actor::logMsg( RTString str ) | ||
{ | ||
logPort.logMsg(str).send(); | ||
} | ||
` | ||
/* Ports */ | ||
behavior port timer : Timing; | ||
behavior port logPort : LogProtocol; | ||
/* Parts */ | ||
part logger : Logger; | ||
/* Connectors */ | ||
connect logPort with logger.p; | ||
/* State Machine */ | ||
statemachine { | ||
state RunningLights { | ||
entry | ||
` | ||
theLight->switchLights(); | ||
`; | ||
|
||
}; | ||
switchLights: RunningLights -> RunningLights on timer.timeout; | ||
initial -> RunningLights | ||
` | ||
theLight = new Light(this); | ||
timer.informEvery(RTTimespec(2,0)); | ||
`; | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
//Transformation Configuration File | ||
let tc = TCF.define(TCF.CPP_TRANSFORM); | ||
tc.targetFolder = "TrafficLight_target"; | ||
tc.topCapsule = "TrafficLight"; |