Skip to content

Commit

Permalink
adding the group (try catch option)
Browse files Browse the repository at this point in the history
meanwhile the catch block is only one block and cannot be two block connected, if there is to block connected(b1 -> b2) it will go out of the group after the token leave the first one (b1)
  • Loading branch information
maor226 committed Apr 6, 2022
1 parent d0c10c8 commit 0626099
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 42 deletions.
6 changes: 6 additions & 0 deletions NodeLibrary.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ def node_manipulator(self, node: DiagramNode) -> None:
######################################################################################
class SyncType(NodeType):

def __init__(self):
self.stop = False

def type_string(self) -> str:
return "sync"

Expand Down Expand Up @@ -435,3 +438,6 @@ def synchronization(self, tokens: Sequence[Dict], sync: Sequence[Dict], node: Di
node.loog += tokens

return (tokens, [])


######################################################################################
16 changes: 16 additions & 0 deletions Tests/TTT_maor.flow
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,21 @@ Tie = 'game over in tie'
wa3 [type=waitall, waitall="AllButOne(path ,X)", at= 'toblock']
rq3 [type=sync, req="[O(toblock)]", width=200,priority = 7]

######################################################################################
#fork
st4 -> wa4 -> rq4;
wt4;

st4 [type=start, initial="[{'fork' : fork , 'defend' : defend} for (fork,defend) in [([4,1,2],[7,3])] ]", width=400];
wa4 [type=waitall, waitall="AllButOne(fork ,O)", at= 'toFork']
rq4 [type=sync, req="[O(toFork)]", width=200,priority = 6]
wt4 [type=sync, wait="[X(d) for d in defend]", width=200]
group {
wa4;
rq4;
wt4;
}

######################################################################################

#WinChecker
Expand Down Expand Up @@ -120,6 +135,7 @@ Tie = 'game over in tie'
rqX1[type = sync , req="[X(i)]"]

######################################################################################

# For debugging
class hidden [color = none, style = none, textcolor = white];
stt -> listener -> logger -> listener [style = "none"];
Expand Down
Binary file modified Tests/TTT_maor.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified Tests/TTT_maor_run/1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified Tests/TTT_maor_run/2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 3 additions & 3 deletions Tests/group_test.flow
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
blockdiag {
st -> req1 -> req2;
st -> wt1;
wt2 -> req3;
st -> wt2;
wt1 -> req3;

st [type=start];
req1 [type=sync, req="['1']"];
Expand All @@ -14,8 +14,8 @@ blockdiag {
group {
req1;
req2;
wt1;
wt2;
wt1;
}


Expand Down
Binary file added Tests/group_test.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
32 changes: 0 additions & 32 deletions Tests/testLiat.py

This file was deleted.

89 changes: 82 additions & 7 deletions flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,50 @@
node_types = (StartType(), SyncType(), LoopType(), PassType(), PermutationType(),
JoinType(), WaitForSetType(), LoggerType(), WaitAll())

global _id
_id = 0


def traverse_nodes(n):
if not hasattr(n, 'nodes'):
yield n
else:
# TODO: Group
for nn in n.nodes:
# Add nn.group = n ?
yield from traverse_nodes(nn)


def traverse_nodes_group(n):
if not hasattr(n, 'nodes'):
pass
else:
for nn in n.nodes:
# Add nn.group = n ?
yield from traverse_nodes_group(nn)
if type(nn) is NodeGroup:
yield nn


def get_group_nodes(g):
n = [n for n in g.nodes]
for e in g.edges:
if e.node2 in n:
n.remove(e.node2)
yield e.node2
gr = g
while gr is not diagram and len(nodes) != 0:
gr = g.group
for e in gr.edges:
if e.node2 in n:
n.remove(e.node2)
yield e.node2


def setup_diagram(diagram):
global nodes
global nodes_group
nodes = [n for n in traverse_nodes(diagram)]
nodes_group = [n for n in traverse_nodes_group(diagram)]

for n in nodes:
n.pred = []
Expand All @@ -60,9 +90,19 @@ def setup_diagram(diagram):
if nt.type_string() == n.type:
n.node_type = nt
nt.node_manipulator(n)
break

if n.node_type is None:
raise AttributeError("Unknown type '" + n.type + "'")
for g in nodes_group:
g.stop_group = False
g.conn_nodes = [n for n in get_group_nodes(g)]
g.groupStop = set(g.nodes) - set(g.conn_nodes)
for cn in g.conn_nodes:
if not hasattr(cn, "stop_nodes"):
cn.stop_nodes = []
for sn in g.groupStop:
cn.stop_nodes.append(sn)

build_predessessors_field(diagram)
print_diagram(diagram, sys.argv[1])
Expand Down Expand Up @@ -104,17 +144,52 @@ def print_state(terminal_output=True):


def step_to_next_state(diagram):
global _id
tmp, changed = {}, False
for n in nodes:
tmp[n] = [t for pn, p in n.pred for t in
pn.node_type.transformation(pn.tokens, pn, p)] + n.node_type.keep(n.tokens, n)
if n not in tmp.keys():
tmp[n] = []
for pn, p in n.pred:
trans = [t for t in pn.node_type.transformation(pn.tokens, pn, p)]
if pn.group != n.group:
if hasattr(n, "stop_nodes"):
for sn in n.stop_nodes:
if sn not in tmp.keys():
tmp[sn] = []
for t in trans:
if "ID" not in t.keys():
t["ID"] = _id
_id += 1
tmp[sn].extend(trans)
tmp[n].extend(trans)

tmp[n].extend(n.node_type.keep(n.tokens, n))

# for g in nodes_group:
# for n in g.conn_nodes:
# for t in n.tokens + n.sync:
# for sn in g.groupStop:
# if t["ID"] not in [tt["ID"] for tt in (sn.tokens + sn.sync)]:
# if t in n.tokens:
# n.tokens.remove(t)
# elif t in n.sync:
# n.sync.remove(t)

for n in nodes:
tmp[n], n.sync = n.node_type.synchronization(tmp[n], n.sync, n)
if n.tokens != tmp[n]:
changed = True
n.tokens = tmp[n]

for g in nodes_group:
for n in g.conn_nodes:
for t in n.tokens + n.sync:
for sn in g.groupStop:
if t["ID"] not in [tt["ID"] for tt in (sn.tokens + sn.sync)]:
if t in n.tokens:
n.tokens.remove(t)
elif t in n.sync:
n.sync.remove(t)
return changed


Expand All @@ -133,7 +208,7 @@ def select_event(diagram):
if diagram.event_selection_mechanism == 'random':
pass
elif diagram.event_selection_mechanism == 'priority':
if len(candidates)>0:
if len(candidates) > 0:
p = max(candidates, key=lambda e: int(e[1]))[1]
candidates = [e for e in candidates if e[1] == p]
else:
Expand All @@ -142,7 +217,7 @@ def select_event(diagram):

if len(candidates) == 0:
raise EndOfRunException()

return random.choice(candidates)[0]


Expand Down Expand Up @@ -174,10 +249,10 @@ def run_diagram(diagram):
while True:
while step_to_next_state(diagram):
print_state()

e = select_event(diagram)
print("*** Event:", e, "***")

wake_up_tokens(diagram, e)
except EndOfRunException:
pass
Expand Down

0 comments on commit 0626099

Please sign in to comment.