Skip to content

Commit

Permalink
RIC-659 adding Class with State machine sample
Browse files Browse the repository at this point in the history
  • Loading branch information
Rajesh Kumar authored and Rajesh Kumar committed Dec 5, 2023
1 parent 7ceb7a7 commit a1f7a46
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 0 deletions.
8 changes: 8 additions & 0 deletions art-samples/TrafficLight/ILogger.art
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;
};
`
42 changes: 42 additions & 0 deletions art-samples/TrafficLight/Light.art
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");
`;
};
};
5 changes: 5 additions & 0 deletions art-samples/TrafficLight/LogProtocol.art
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
protocol LogProtocol {
/* In Events */
/* Out Events */
out logMsg(`RTString`);
};
26 changes: 26 additions & 0 deletions art-samples/TrafficLight/Logger.art
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;
};
};
47 changes: 47 additions & 0 deletions art-samples/TrafficLight/TrafficLight.art
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));
`;
};
};
4 changes: 4 additions & 0 deletions art-samples/TrafficLight/app.tcjs
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";

0 comments on commit a1f7a46

Please sign in to comment.