forked from lf-lang/playground-lingua-franca
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SharedList.lf
73 lines (65 loc) · 1.62 KB
/
SharedList.lf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
'''
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;
}