diff --git a/org.lflang/src/lib/ts/reactor-ts b/org.lflang/src/lib/ts/reactor-ts index 47830e13bf..a0eb024cfe 160000 --- a/org.lflang/src/lib/ts/reactor-ts +++ b/org.lflang/src/lib/ts/reactor-ts @@ -1 +1 @@ -Subproject commit 47830e13bf2d32ed5012a51b720a4df54c9afcbe +Subproject commit a0eb024cfe944b073cebdb739e0e52735dde3e35 diff --git a/test/TypeScript/src/federated/DistributedCount.lf b/test/TypeScript/src/federated/DistributedCount.lf new file mode 100644 index 0000000000..988b10b012 --- /dev/null +++ b/test/TypeScript/src/federated/DistributedCount.lf @@ -0,0 +1,51 @@ +/** + * Test a particularly simple form of a distributed deterministic system + * where a federation that receives timestamped messages has only those + * messages as triggers. Therefore, no additional coordination of the + * advancement of time (HLA or Ptides) is needed. + * @author Edward A. Lee + * @author Hokeun Kim + */ + target TypeScript { + timeout: 5 sec +}; + +// TODO(hokeun): Replace the following reactor with import Count from "../lib/Count.lf"; +// once TypeScript serialization is implemented. +reactor Count(offset:time(0), period:time(1 sec)) { + output out:string; + timer t(offset, period); + state count:number(1); + reaction(t) -> out {= + out = String(count++); + =} +} + +reactor Print { + // TODO(hokeun): Change type of inp to number once + // once TypeScript serialization is implemented. + input inp:string; + state c:number(1); + reaction(inp) {= + const elapsedTime = util.getElapsedLogicalTime(); + console.log("At time " + elapsedTime + ", received " + inp); + if (inp !== String(c)) { + util.requestErrorStop("Expected to receive " + c + "."); + } + if (elapsedTime.isEqualTo(TimeValue.msec(200).add(TimeValue.sec(c - 1)))) { + util.requestErrorStop("Expected received time to be " + TimeValue.msec(200).add(TimeValue.sec(c - 1)) + "."); + } + c++; + =} + reaction(shutdown) {= + if (c != 6) { + util.reportError("Expected to receive 5 items."); + } + =} +} + +federated reactor DistributedCount(offset:time(200 msec)) { + c = new Count(); + p = new Print(); + c.out -> p.inp after offset; +} diff --git a/test/TypeScript/src/federated/HelloDistributed.lf b/test/TypeScript/src/federated/HelloDistributed.lf index 78cd15f8d1..61dcb84731 100644 --- a/test/TypeScript/src/federated/HelloDistributed.lf +++ b/test/TypeScript/src/federated/HelloDistributed.lf @@ -1,52 +1,50 @@ /** - * A test-version of distributed HelloWorld in the example directory, - * example/TypeScript/src/DistributedHelloWorld/HelloWorld.lf - * This is to make sure that the federated execution for TypeScript target - * always works when there is a relevant change. - * - * @author Edward A. Lee - * @author Hokeun Kim + * Test a particularly simple form of a distributed deterministic system + * where a federation that receives timestamped messages has only those + * messages as triggers. Therefore, no additional coordination of the + * advancement of time (HLA or Ptides) is needed. + * @author Edward A. Lee + * @author Hokeun Kim */ -// TODO(hokeun): Add a check for the number of received messages to be exactly 2. target TypeScript { timeout: 2 secs }; -/** - * Reactor that generates a sequence of messages, one per second. - * The message will be a string consisting of a root string followed - * by a count. - * @param root The root string. - * @output message The message. - */ -reactor MessageGenerator(root:string("")) { - // Output type char* instead of string is used for dynamically - // allocated character arrays (as opposed to static constant strings). - output message:string; - state count:number(1); - // Send first message after 1 sec so that the startup reactions - // do not factor into the transport time measurement on the first message. - timer t(1 sec, 1 sec); - reaction(t) -> message {= - message = root + " " + count++; - console.log(`At time (elapsed) logical tag ${util.getElapsedLogicalTime()}, source sends message: ${message}`); +reactor Source { + output out:string; + reaction(startup) -> out {= + console.log("Sending 'Hello World!' message from source federate."); + out = "Hello World!"; + util.requestStop(); =} } -/** - * Reactor that prints the current tag and an incoming string. - * - * @input message The message. - */ -reactor PrintMessage { - input message:string; - reaction(message) {= - console.log(`PrintMessage: At (elapsed) logical time ${util.getElapsedLogicalTime()}, receiver receives: ${message}`); +reactor Destination { + input inp:string; + state received:boolean(false); + reaction(startup) {= + console.log("Destination started."); + =} + reaction(inp) {= + console.log(`At logical time ${util.getElapsedLogicalTime()}, destination received: $inp`); + if (inp !== "Hello World!") { + util.requestErrorStop("ERROR: Expected to receive 'Hello World!'"); + } + received = true; + =} + reaction(shutdown) {= + console.log("Shutdown invoked."); + if (!received) { + util.reportError("Destination did not receive the message."); + } =} } -federated reactor HelloDistributed { - source = new MessageGenerator(root = "Hello World"); - print = new PrintMessage(); - source.message -> print.message; -} \ No newline at end of file +federated reactor HelloDistributed at localhost { + reaction(startup) {= + console.log("Printing something in top-level federated reactor."); + =} + s = new Source(); // Reactor s is in federate Source + d = new Destination(); // Reactor d is in federate Destination + s.out -> d.inp; // This version preserves the timestamp. +}