Skip to content

Commit

Permalink
Merge pull request #732 from revol-xut/master
Browse files Browse the repository at this point in the history
Formatted C++ examples for better readability
  • Loading branch information
lhstrh authored Nov 7, 2021
2 parents 74deae4 + 7ffb722 commit 56da63c
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 116 deletions.
53 changes: 26 additions & 27 deletions example/Cpp/src/CarBrake/CarBrake.lf
Original file line number Diff line number Diff line change
Expand Up @@ -14,45 +14,44 @@ reactor Camera {

reactor BrakingAssistant {
input frame: void;
output trigger_brake: void;

output trigger_brake: void;

state counter: int(0);
state counter: int(0);

reaction(frame) -> trigger_brake {=
reaction(frame) -> trigger_brake {=
// processing takes some time
std::this_thread::sleep_for(10ms);
std::this_thread::sleep_for(10ms);

if (counter % 10 == 0) {
std::cout << "[automatic] Send the brake signal - " << get_physical_time() << std::endl;
trigger_brake.set();
}
counter++;
counter++;
=}
}

reactor BrakePedal {
physical action pedal;
output trigger_brake: void;

state thread: std::thread;

reaction(startup) -> pedal {=
this->thread = std::thread([&] () {
// press the brake pedal roughly every second
while (true) {
std::this_thread::sleep_for(1005ms);
std::cout << "[manual] Pressing the break pedal - " << get_physical_time() << std::endl;
pedal.schedule(0ms);
}
while (true) {
std::this_thread::sleep_for(1005ms);
std::cout << "[manual] Pressing the break pedal - " << get_physical_time() << std::endl;
pedal.schedule(0ms);
}
});
=}

reaction(pedal) -> trigger_brake {=
std::cout << "[manual] Send the brake signal - " << get_physical_time() << std::endl;
trigger_brake.set();
std::cout << "[manual] Send the brake signal - " << get_physical_time() << std::endl;
trigger_brake.set();
=}

reaction(shutdown) {=
thread.join();
=}
Expand All @@ -63,17 +62,17 @@ reactor Brake {
#include <thread>
=}

input brake_assistant: void;
input brake_pedal: void;
input brake_assistant: void;
input brake_pedal: void;

reaction(brake_pedal) {=
reaction(brake_pedal) {=
std::cout << "[system] Brake triggered - " << get_physical_time() << std::endl;
std::cout << "[system] source: manual" << std::endl;
=} deadline (3msecs) {=
std::cout << "\033[1;31m[error]\033[0m Deadline on manual braking violated - " << get_physical_time() << std::endl;
=}

reaction(brake_assistant) {=
reaction(brake_assistant) {=
std::cout << "[system] Brake triggered - " << get_physical_time() << std::endl;
std::cout << "[system] source: assistant" << std::endl;
=} deadline (15msecs) {=
Expand All @@ -83,11 +82,11 @@ reactor Brake {

main reactor {
camera = new Camera();
assistant = new BrakingAssistant();
pedal = new BrakePedal();
brake = new Brake();
assistant = new BrakingAssistant();
pedal = new BrakePedal();
brake = new Brake();

camera.frame -> assistant.frame;
assistant.trigger_brake -> brake.brake_assistant;
pedal.trigger_brake -> brake.brake_pedal;
camera.frame -> assistant.frame;
assistant.trigger_brake -> brake.brake_assistant;
pedal.trigger_brake -> brake.brake_pedal;
}
72 changes: 35 additions & 37 deletions example/Cpp/src/CarBrake/CarBrake2.lf
Original file line number Diff line number Diff line change
Expand Up @@ -19,45 +19,45 @@ reactor Camera {

reactor BrakingAssistant {
input frame: void;
output trigger_brake: void;
output trigger_brake: void;


state counter: int(0);
state counter: int(0);

reaction(frame) -> trigger_brake {=
reaction(frame) -> trigger_brake {=
// processing takes some time
std::this_thread::sleep_for(10ms);
std::this_thread::sleep_for(10ms);

if (counter % 10 == 0) {
std::cout << "[automatic] Send the brake signal - " << get_physical_time() << std::endl;
trigger_brake.set();
}
counter++;
counter++;
=}
}

reactor BrakePedal {
physical action pedal;
output trigger_brake: void;

state thread: std::thread;

reaction(startup) -> pedal {=
this->thread = std::thread([&] () {
// press the brake pedal roughly every second
while (true) {
std::this_thread::sleep_for(1005ms);
std::cout << "[manual] Pressing the break pedal - " << get_physical_time() << std::endl;
pedal.schedule(0ms);
}

while (true) {
std::this_thread::sleep_for(1005ms);
std::cout << "[manual] Pressing the break pedal - " << get_physical_time() << std::endl;
pedal.schedule(0ms);
}
});
=}

reaction(pedal) -> trigger_brake {=
std::cout << "[manual] Send the brake signal - " << get_physical_time() << std::endl;
trigger_brake.set();
std::cout << "[manual] Send the brake signal - " << get_physical_time() << std::endl;
trigger_brake.set();
=}

reaction(shutdown) {=
thread.join();
=}
Expand All @@ -68,17 +68,17 @@ reactor Brake {
#include <thread>
=}

input brake_assistant: void;
input brake_pedal: void;
reaction(brake_pedal) {=
input brake_assistant: void;
input brake_pedal: void;

reaction(brake_pedal) {=
std::cout << "[system] Brake triggered - " << get_physical_time() << std::endl;
std::cout << "[system] source: manual" << std::endl;
=} deadline (3msecs) {=
std::cout << "\033[1;31m[error]\033[0m Deadline on manual braking violated - " << get_physical_time() << std::endl;
=}
reaction(brake_assistant) {=

reaction(brake_assistant) {=
std::cout << "[system] Brake triggered - " << get_physical_time() << std::endl;
std::cout << "[system] source: assistant" << std::endl;
=} deadline (15msecs) {=
Expand All @@ -88,27 +88,25 @@ reactor Brake {

reactor Braking {
input brake_assistant: void;

pedal = new BrakePedal();
brake = new Brake();

pedal.trigger_brake -> brake.brake_pedal;
brake_assistant -> brake.brake_assistant;
pedal = new BrakePedal();
brake = new Brake();

pedal.trigger_brake -> brake.brake_pedal;
brake_assistant -> brake.brake_assistant;
}

reactor Vision {
output trigger_brake: void;

camera = new Camera();
assistant = new BrakingAssistant();
assistant = new BrakingAssistant();

camera.frame -> assistant.frame;
assistant.trigger_brake -> trigger_brake;
camera.frame -> assistant.frame;
assistant.trigger_brake -> trigger_brake;
}

main reactor {
braking = new Braking();
vision = new Vision();
vision.trigger_brake ~> braking.brake_assistant;
braking = new Braking();
vision = new Vision();

vision.trigger_brake ~> braking.brake_assistant;
}
17 changes: 9 additions & 8 deletions example/Cpp/src/Patterns/FullyConnected_00_Broadcast.lf
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,26 @@ target Cpp {
reactor Node(bank_index: size_t(0), num_nodes: size_t(4)) {
input[num_nodes] in: size_t
output out: size_t

reaction (startup) -> out{=
std::cout << "Hello from node " << bank_index << "!\n";
// broadcast my ID to everyone
out.set(bank_index);
std::cout << "Hello from node " << bank_index << "!\n";
// broadcast my ID to everyone
out.set(bank_index);
=}

reaction (in) {=
std::cout << "Node " << bank_index << " received messages from ";
for (auto& port : in) {
for (auto& port : in) {
if (port.is_present()) {
std::cout << *port.get() << ", ";
}
}
}
std::cout << '\n';
=}
}

main reactor(num_nodes: size_t(4)) {
nodes = new[num_nodes] Node(num_nodes=num_nodes);
(nodes.out)+ -> nodes.in;
}
}

17 changes: 9 additions & 8 deletions example/Cpp/src/Patterns/FullyConnected_01_Addressable.lf
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,26 @@ target Cpp {
reactor Node(bank_index: size_t(0), num_nodes: size_t(4)) {
input[num_nodes] in: size_t
output[num_nodes] out: size_t

reaction (startup) -> out{=
std::cout << "Hello from node " << bank_index << "!\n";
// send my ID only to my right neighbour
out[(bank_index + 1) % num_nodes].set(bank_index);
std::cout << "Hello from node " << bank_index << "!\n";
// send my ID only to my right neighbour
out[(bank_index + 1) % num_nodes].set(bank_index);
=}

reaction (in) {=
std::cout << "Node " << bank_index << " received messages from ";
for (auto& port : in) {
for (auto& port : in) {
if (port.is_present()) {
std::cout << *port.get() << ", ";
}
}
}
std::cout << '\n';
=}
}

main reactor(num_nodes: size_t(4)) {
nodes = new[num_nodes] Node(num_nodes=num_nodes);
nodes.out -> interleaved(nodes.in);
}
}

29 changes: 15 additions & 14 deletions example/Cpp/src/Patterns/MatrixConnectedRowsAndColumns.lf
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,34 @@ target Cpp {

public preamble {=
struct Pos {
size_t col;
size_t row;
size_t col;
size_t row;
};

std::ostream& operator<<(std::ostream& os, const Pos& pos);
=}

private preamble {=
private preamble {=
std::ostream& operator<<(std::ostream& os, const Pos& pos) {
os << '(' << pos.col << ',' << pos.row << ')';
return os;
}
os << '(' << pos.col << ',' << pos.row << ')';
return os;
}
=}

reactor Node(bank_index: size_t(0), row_index: size_t(0), num_rows: size_t(4), num_cols:size_t(4)) {
input[num_cols] fromRow: Pos
input[num_rows] fromCol: Pos

output toRowAndCol: Pos

state pos: Pos{bank_index, row_index}

reaction (startup) -> toRowAndCol {=
std::cout << "Hello from " << pos << '\n';
// send my position to everyone else in my row and column
toRowAndCol.set(pos);
=}

reaction (fromRow) {=
std::cout << pos << " received row messages from: ";
for (auto& port : fromRow) {
Expand All @@ -46,7 +46,7 @@ reactor Node(bank_index: size_t(0), row_index: size_t(0), num_rows: size_t(4), n
}
std::cout << '\n';
=}

reaction (fromCol) {=
std::cout << pos << " received col messages from: ";
for (auto& port : fromCol) {
Expand All @@ -60,10 +60,10 @@ reactor Node(bank_index: size_t(0), row_index: size_t(0), num_rows: size_t(4), n

reactor Row(bank_index: size_t(0), num_rows:size_t(4), num_cols:size_t(4)) {
nodes = new[num_cols] Node(row_index=bank_index, num_rows=num_rows, num_cols=num_cols)

input[{=num_rows * num_cols=}] fromCol: Pos
output[num_cols] toCol: Pos

(nodes.toRowAndCol)+ -> nodes.fromRow
nodes.toRowAndCol -> toCol
fromCol -> interleaved(nodes.fromCol)
Expand All @@ -72,4 +72,5 @@ reactor Row(bank_index: size_t(0), num_rows:size_t(4), num_cols:size_t(4)) {
main reactor (num_rows:size_t(4), num_cols:size_t(4)) {
rows = new[num_rows] Row(num_rows=num_rows, num_cols=num_cols)
(rows.toCol)+ -> rows.fromCol;
}
}

Loading

0 comments on commit 56da63c

Please sign in to comment.