-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalgorithm.py
More file actions
96 lines (67 loc) · 2.85 KB
/
algorithm.py
File metadata and controls
96 lines (67 loc) · 2.85 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File: algorithm.py
# Authors: Alejandro Cirugeda & Juancarlos Quintana
# Description:
# In this file is the execution of the algorithm of the validation. We create a initial global state and using depth-first algortith
# to travel across the reachability graph and checking if contain any deadlock.
#
# CONSRAINTS:
# - The stack size is limited to 100 elements
from finiteMachine import *
from bitStateHashing import *
from node import *
def algorithm(machines):
# create file for the hashing table
bit_state_hashing = BitState("hashingtable.txt")
bit_state_hashing.open_file()
print("\n\n -------------Algorith start -------------")
initial_node = create_initial_node(machines)
# ----------- create the stack and add the initial state ---------------
initial_node.visit_node
stack = []
stack.append(initial_node)
initial_node.visit_node(bit_state_hashing)
deadlock_counter = 0
states_visited = 1 #count the initial state
# start of the loop
while stack:
# We check the last element of the stack without removing it
actual_node = stack[-1]
child_node = actual_node.get_next_node(machines)
if (child_node == None): #if no node is created, it dont have more transition left
stack.pop()
else:
if(not child_node.is_node_visited(bit_state_hashing)):
child_node.visit_node(bit_state_hashing) #mark as visited and add to stack
states_visited += 1
if( child_node.check_deadlock() ): deadlock_counter += 1
if(len(stack) < 100): # We limited the size of the stack to 100
stack.append(child_node)
#close the file with the hashing table
print("Algorithm finished with %d states visited" % states_visited)
bit_state_hashing.close_file()
return deadlock_counter
def create_initial_node(fsm):
"""
Return a Node instance with the information of the inital node necesary for the algorithm.
It will be the initial global state and the initial transactions of the fsm.
"""
global_state = create_initial_global_state(len(fsm))
# check transitions of the state and add them to the stack
transitions_list = []
for i in range(len(fsm)):
state = global_state[i][i]
machine = fsm[i]
transitions = machine.get_transition(state)
for t in transitions:
transitions_list.append(t)
node = Node(global_state, transitions_list)
return node
def create_initial_global_state(n_machines):
"""
Will return a matrix representing the inital global state:
All channels empty and in the state 0 in every machine.
"""
matrix = [['' for i in range(n_machines)] for j in range(n_machines)]
for i in range(n_machines):
matrix[i][i] = '0'
return matrix