Skip to content

Commit

Permalink
Merge pull request #50 from lf-lang/formatting2
Browse files Browse the repository at this point in the history
Formatting all examples and combine experimental directories
  • Loading branch information
cmnrd authored Jun 21, 2023
2 parents cdfab0e + 44b5896 commit 0bdd1f1
Show file tree
Hide file tree
Showing 316 changed files with 27,775 additions and 28,351 deletions.
126 changes: 62 additions & 64 deletions C/src/ChatApplication/SimpleChat.lf
Original file line number Diff line number Diff line change
@@ -1,96 +1,94 @@
/**
* This program is a simple federated chat application written in Lingua Franca
* (LF) for two users, represented by two instances of the `ChatHandler`
* reactor, `a` and `b`. Each `ChatHandler` consists of an `InputHandler` and a
* `Printer`. The `InputHandler` is responsible for receiving user input from
* the console and sending it as a message. When the user enters a message, the
* `InputHandler` reads it, prints it, and then sends it via its `out` port. The
* message then gets relayed by the `ChatHandler` via its `send` port. On the
* receiving side, the `ChatHandler` directs the incoming message through its
* `receive` port to the `Printer` reactor, which prints the received message.
* This process creates a two-way communication where `a` and `b` can send and
* This program is a simple federated chat application written in Lingua Franca (LF) for two users,
* represented by two instances of the `ChatHandler` reactor, `a` and `b`. Each `ChatHandler`
* consists of an `InputHandler` and a `Printer`. The `InputHandler` is responsible for receiving
* user input from the console and sending it as a message. When the user enters a message, the
* `InputHandler` reads it, prints it, and then sends it via its `out` port. The message then gets
* relayed by the `ChatHandler` via its `send` port. On the receiving side, the `ChatHandler`
* directs the incoming message through its `receive` port to the `Printer` reactor, which prints
* the received message. This process creates a two-way communication where `a` and `b` can send and
* receive messages from each other.
*
* @author Byeonggil Jun (junbg@hanyang.ac.kr)
* @author Hokeun Kim (hokeunkim@berkeley.edu)
*/
target C {
keepalive: true,
coordination-options: {
advance-message-interval: 10 msec
}
keepalive: true,
coordination-options: {
advance-message-interval: 10 msec
}
}

preamble {=
#include <unistd.h>
#include <unistd.h>
=}

reactor InputHandler {
preamble {=
// Global buffer.
char buf[256];
void* read_input(void* response) {
int c;
int i = 0;
while(1) {
printf("Press Enter a message.\n");
while((c = getchar()) != '\n') {
buf[i++] = c;
if (c == EOF) {
lf_request_stop();
break;
}
}
buf[i] = 0;
printf("User input: %s\n", buf);
i = 0;
preamble {=
// Global buffer.
char buf[256];
void* read_input(void* response) {
int c;
int i = 0;
while(1) {
printf("Press Enter a message.\n");
while((c = getchar()) != '\n') {
buf[i++] = c;
if (c == EOF) {
lf_request_stop();
break;
}
}
buf[i] = 0;
printf("User input: %s\n", buf);
i = 0;

char* ptr = buf;
// The following copies the char*, not the string.
lf_schedule_copy(response, 0, &ptr, 1);
if (c == EOF) {
break;
}
}
return NULL;
char* ptr = buf;
// The following copies the char*, not the string.
lf_schedule_copy(response, 0, &ptr, 1);
if (c == EOF) {
break;
}
=}
}
return NULL;
}
=}

physical action response: string
output out: string
physical action response: string
output out: string

reaction(startup) -> response {=
lf_thread_t thread_id;
lf_thread_create(&thread_id, &read_input, response);
=}
reaction(startup) -> response {=
lf_thread_t thread_id;
lf_thread_create(&thread_id, &read_input, response);
=}

reaction(response) -> out {=
printf("Reacting to physical action at %lld\n", lf_time_logical_elapsed());
lf_set(out, response->value);
=}
reaction(response) -> out {=
printf("Reacting to physical action at %lld\n", lf_time_logical_elapsed());
lf_set(out, response->value);
=}
}

reactor Printer {
input in: string
input in: string

reaction(in) {= printf("Received: %s\n", in->value); =}
reaction(in) {= printf("Received: %s\n", in->value); =}
}

reactor ChatHandler {
input receive: string
output send: string
u = new InputHandler()
r = new Printer()
input receive: string
output send: string
u = new InputHandler()
r = new Printer()

reaction(u.out) -> send {= lf_set(send, u.out->value); =}
reaction(u.out) -> send {= lf_set(send, u.out->value); =}

reaction(receive) -> r.in {= lf_set(r.in, receive->value); =}
reaction(receive) -> r.in {= lf_set(r.in, receive->value); =}
}

federated reactor SimpleChat {
a = new ChatHandler()
a = new ChatHandler()

b = new ChatHandler()
b.send -> a.receive
a.send -> b.receive
b = new ChatHandler()
b.send -> a.receive
a.send -> b.receive
}
87 changes: 41 additions & 46 deletions C/src/Delay.lf
Original file line number Diff line number Diff line change
@@ -1,62 +1,57 @@
/**
* This (rather trivial) example illustrates a logical action used
* to model a delay. The delay is also realized a second time
* using the `after` keyword.
*
* This (rather trivial) example illustrates a logical action used to model a delay. The delay is
* also realized a second time using the `after` keyword.
*
* @author Marten Lohstroh
* @author Edward A. Lee
*/
target C {timeout: 1 sec};
target C {
timeout: 1 sec
}

main reactor {
ramp = new Ramp();
delay = new Delay2();
print = new Print();
ramp.y -> delay.x;
delay.y -> print.x;
ramp2 = new Ramp();
print2 = new Print();
ramp2.y -> print2.x after 50 msec;
ramp = new Ramp()
delay = new Delay2()
print = new Print()
ramp.y -> delay.x
delay.y -> print.x

ramp2 = new Ramp()
print2 = new Print()
ramp2.y -> print2.x after 50 msec
}

/**
* Generate a counting sequence with outputs every 100 msec.
*/
/** Generate a counting sequence with outputs every 100 msec. */
reactor Ramp {
timer t(0, 100 msec);
output y:int;
state count:int(0);
reaction(t) -> y {=
lf_set(y, self->count);
self->count++;
=}
timer t(0, 100 msec)
output y: int
state count: int = 0

reaction(t) -> y {=
lf_set(y, self->count);
self->count++;
=}
}

/**
* Realize a logical delay of 50 msec.
*/
/** Realize a logical delay of 50 msec. */
reactor Delay2 {
logical action a(50 msec):int;
input x:int;
output y:int;
reaction(a) -> y {=
lf_set(y, a->value);
=}
reaction(x) -> a {=
lf_schedule_int(a, 0, x->value);
=}
logical action a(50 msec): int
input x: int
output y: int

reaction(a) -> y {= lf_set(y, a->value); =}

reaction(x) -> a {= lf_schedule_int(a, 0, x->value); =}
}

/**
* Print the (elapsed) logical and physical times at which inputs are received.
*/
/** Print the (elapsed) logical and physical times at which inputs are received. */
reactor Print {
input x:int;
reaction(x) {=
printf("Logical time: %lld, Physical time %lld"
", Value: %d\n",
lf_time_logical_elapsed(),
lf_time_physical_elapsed(), x->value);
=}
input x: int

reaction(x) {=
printf("Logical time: %lld, Physical time %lld"
", Value: %d\n",
lf_time_logical_elapsed(),
lf_time_physical_elapsed(), x->value);
=}
}
42 changes: 19 additions & 23 deletions C/src/DistributedDatabase/FederatedDatabase.lf
Original file line number Diff line number Diff line change
@@ -1,33 +1,29 @@
/**
* Federated version of ReplicatedDatabase.
* This is identical, except that it is federated.
*
* Federated version of ReplicatedDatabase. This is identical, except that it is federated.
*
* @author Edward A. Lee
* @author Soroush Bateni
*/
target C {
timeout: 5 sec,
coordination: centralized
timeout: 5 sec,
coordination: centralized
}

import Platform from "ReplicatedDatabase.lf"

federated reactor (
query_period:time(1 sec),
num_remote_inputs:int(1)
) {
a = new Platform(
query_period = query_period,
update_period = 5 sec,
update_amount = 100,
name = "San Francisco",
num_remote_inputs = num_remote_inputs);
b = new Platform(
query_period = query_period,
update_period = 1 sec,
update_amount = -20,
name = "Berkeley",
num_remote_inputs = num_remote_inputs);
b.publish -> a.update;
a.publish -> b.update;
federated reactor(query_period: time = 1 sec, num_remote_inputs: int = 1) {
a = new Platform(
query_period=query_period,
update_period = 5 sec,
update_amount=100,
name = "San Francisco",
num_remote_inputs=num_remote_inputs)
b = new Platform(
query_period=query_period,
update_period = 1 sec,
update_amount=-20,
name="Berkeley",
num_remote_inputs=num_remote_inputs)
b.publish -> a.update
a.publish -> b.update
}
Loading

0 comments on commit 0bdd1f1

Please sign in to comment.