Skip to content

Commit

Permalink
Added instructions for triggering contained reactors
Browse files Browse the repository at this point in the history
  • Loading branch information
edwardalee committed Aug 29, 2023
1 parent 252f965 commit 41c4168
Show file tree
Hide file tree
Showing 6 changed files with 167 additions and 0 deletions.
13 changes: 13 additions & 0 deletions packages/documentation/code/c/src/Triggering.lf
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
target C
reactor Inside {
input x: int
reaction(x) {=
printf("Received %d\n", x->value);=
=}
}
main reactor {
i = new Inside()
reaction(startup) -> i.x {=
lf_set(i.x, 42);
=}
}
13 changes: 13 additions & 0 deletions packages/documentation/code/cpp/src/Triggering.lf
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
target Cpp
reactor Inside {
input x: int
reaction(x) {=
std::cout << "Received " << std::to_string(*x.get()) << std::endl;
=}
}
main reactor {
i = new Inside()
reaction(startup) -> i.x {=
i.x.set(42);
=}
}
13 changes: 13 additions & 0 deletions packages/documentation/code/py/src/Triggering.lf
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
target Python
reactor Inside {
input x
reaction(x) {=
print(f"Received {x.value}")
=}
}
main reactor {
i = new Inside()
reaction(startup) -> i.x {=
i.x.set(42);
=}
}
13 changes: 13 additions & 0 deletions packages/documentation/code/rs/src/Triggering.lf
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
target Rust
reactor Inside {
input x: u32
reaction(x) {=
println!("Received {}", ctx.get(x).unwrap());
=}
}
main reactor {
i = new Inside()
reaction(startup) -> i.x {=
ctx.set(i__x, 42);
=}
}
13 changes: 13 additions & 0 deletions packages/documentation/code/ts/src/Triggering.lf
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
target TypeScript
reactor Inside {
input x: number
reaction(x) {=
console.log("Received ${x}");
=}
}
main reactor {
i = new Inside()
reaction(startup) -> i.x {=
i.x = 42
=}
}
102 changes: 102 additions & 0 deletions packages/documentation/copy/en/topics/Reactions and Methods.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ main reactor Alignment {
std::cout << "s = " << std::to_string(s) << std::endl;
=}
}
```

```lf-py
Expand Down Expand Up @@ -94,6 +95,7 @@ main reactor Alignment {
console.log(`s = ${s}`)
=}
}
```

```lf-rs
Expand All @@ -115,6 +117,7 @@ main reactor Alignment {
println!("s = {}", self.s);
=}
}
```

$end(Alignment)$
Expand Down Expand Up @@ -143,6 +146,7 @@ reactor Overwriting {
lf_set(y, self->s);
=}
}
```

```lf-cpp
Expand All @@ -163,6 +167,7 @@ reactor Overwriting {
y.set(s);
=}
}
```

```lf-py
Expand Down Expand Up @@ -199,6 +204,7 @@ reactor Overwriting {
y = s
=}
}
```

```lf-rs
Expand Down Expand Up @@ -258,6 +264,7 @@ main reactor {
}
=}
}
```

```lf-py
Expand All @@ -284,6 +291,7 @@ main reactor {
}
=}
}
```

```lf-rs
Expand All @@ -307,6 +315,97 @@ $end(Contained)$

This instantiates the above `Overwriting` reactor and monitors its outputs.

## Triggering Contained Reactors

A reaction can set the input of a contained reactor, thereby triggering its reactions, as illustrated in the following example:

$start(Triggering)$

```lf-c
target C
reactor Inside {
input x: int
reaction(x) {=
printf("Received %d\n", x->value);=
=}
}
main reactor {
i = new Inside()
reaction(startup) -> i.x {=
lf_set(i.x, 42);
=}
}
```

```lf-cpp
target Cpp
reactor Inside {
input x: int
reaction(x) {=
std::cout << "Received " << std::to_string(*x.get()) << std::endl;
=}
}
main reactor {
i = new Inside()
reaction(startup) -> i.x {=
i.x.set(42);
=}
}
```

```lf-py
target Python
reactor Inside {
input x
reaction(x) {=
print(f"Received {x.value}")
=}
}
main reactor {
i = new Inside()
reaction(startup) -> i.x {=
i.x.set(42);
=}
}
```

```lf-ts
target TypeScript
reactor Inside {
input x: number
reaction(x) {=
console.log("Received ${x}");
=}
}
main reactor {
i = new Inside()
reaction(startup) -> i.x {=
i.x = 42
=}
}
```

```lf-rs
target Rust
reactor Inside {
input x: u32
reaction(x) {=
println!("Received {}", ctx.get(x).unwrap());
=}
}
main reactor {
i = new Inside()
reaction(startup) -> i.x {=
ctx.set(i__x, 42);
=}
}
```

$end(Triggering)$

The reaction to $startup$ declares the input port of the inside reactor as an effect and then sets it with value 42.
This will cause the inside reactor's reaction to execute and print `Received 42`.

## Method Declaration

<div class="lf-ts lf-rs">
Expand Down Expand Up @@ -354,6 +453,7 @@ main reactor Methods {
lf_print("2 + 40 = %d", getFoo());
=}
}
```

```lf-cpp
Expand All @@ -372,6 +472,7 @@ main reactor Methods {
std::cout << "2 + 40 = " << getFoo() << '\n';
=}
}
```

```lf-py
Expand All @@ -390,6 +491,7 @@ main reactor Methods {
print(f"2 + 40 = {self.getFoo()}.")
=}
}
```

```lf-ts
Expand Down

0 comments on commit 41c4168

Please sign in to comment.