diff --git a/Python/src/ConcurrentAdder/ConcurrentAdder.lf b/Python/src/ConcurrentAdder/ConcurrentAdder.lf new file mode 100644 index 00000000..9315769a --- /dev/null +++ b/Python/src/ConcurrentAdder/ConcurrentAdder.lf @@ -0,0 +1,38 @@ +target Python { + timeout: 5 sec +} + +reactor Counter(stride(1), period(1 sec)) { + state count(0); + // timer triggers reaction + timer t(0, period); + output out; + reaction(t) -> out {= + out.set(self.count) + self.count += self.stride + =} +} + +reactor Adder(bank_value(1)) { + input _in; + output out; + reaction(_in) -> out {= + out.set(_in.value + self.bank_value) + =} +} + +reactor Printer(N(4)) { + input[N] _in; + reaction(_in) {= + for i in range(self.N): + print(f"Result {_in[i].value}") + =} +} + +main reactor(stride(10), period(1 sec), N(4)) { + c = new Counter(stride=stride, period=period); + a = new[4] Adder(); + p = new Printer(N=N); + (c.out)+ -> a._in; + a.out -> p._in; +} \ No newline at end of file diff --git a/Python/src/HelloWorld/HelloWorld.lf b/Python/src/HelloWorld/HelloWorld.lf new file mode 100644 index 00000000..927f5eaa --- /dev/null +++ b/Python/src/HelloWorld/HelloWorld.lf @@ -0,0 +1,27 @@ +target Python { + timeout: 3 sec +} + +reactor Counter(stride(1)) { + state count(0) + timer t(0, 1 sec) + output out + reaction(t) -> out {= + out.set(self.count) + self.count += self.stride + =} +} + +reactor Printer { + input _in + reaction(_in) {= + # using thread-safe print function provided by the runtime + print(f"Hello World! {_in.value}.") + =} +} + +main reactor { + c = new Counter(stride = 1) + p = new Printer() + c.out -> p._in +} \ No newline at end of file diff --git a/Python/src/SharedList/SharedList.lf b/Python/src/SharedList/SharedList.lf new file mode 100644 index 00000000..df0cb6bf --- /dev/null +++ b/Python/src/SharedList/SharedList.lf @@ -0,0 +1,73 @@ +''' +This is a shared itemlist for multiple people +to appendItem or deltetItem at the same time + +Simulate when multiple clients append or delete items from the +same sharedItem list. +''' +target Python { + timeout: 1 sec +} + +reactor List { + state itemList; + timer t(0, 2 sec); + output out; + reaction(t) -> out {= + self.itemList = ["bear", "oski", "frosty"] + out.set(self.itemList) + =} +} + +reactor AddItem(index(0), item("cal")) { + input itemList; + output out; + reaction(itemList) -> out {= + # do nothing if index out of range + n = len(itemList.value) + if 0 <= self.index < n: + itemList.value.insert(self.index, self.item) + out.set(itemList.value) + =} +} + +reactor DeleteItem(index(0)) { + input itemList; + output out; + reaction(itemList) -> out {= + n = len(itemList.value) + if 0 <= self.index < n: + del itemList.value[self.index] + out.set(itemList.value) + =} +} + +reactor UpdateItem(index(0), item("update")) { + input itemList; + output out; + reaction(itemList) -> out {= + itemList.value[self.index] = self.item + out.set(itemList.value) + =} +} + +reactor Printer(N(3)) { + input[N] _in; + reaction(_in) {= + for i in range(self.N): + print(_in[i].value) + =} +} + +main reactor { + l = new List(); + add_item = new AddItem(); + delete_item = new DeleteItem(index = 1); + upadate_item = new UpdateItem(); + p = new Printer(); + l.out -> add_item.itemList; + l.out -> delete_item.itemList; + l.out -> upadate_item.itemList; + add_item.out, delete_item.out, upadate_item.out -> p._in; +} +