Advertisement
shchuko

possible-exec-analysis

Dec 22nd, 2021
1,768
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.05 KB | None | 0 0
  1. a, b = 0, 0
  2. p, q = 1, 1
  3.  
  4. states = set()
  5.  
  6. new = 'NEW'
  7. visited = 'VISITED'
  8. no_steps = 'NO_STEPS'
  9.  
  10.  
  11. def checker(a, b, p, q, thread_finished):
  12.     state_str = f'[P{p},Q{q},{a},{b}]'
  13.  
  14.     if thread_finished:
  15.         return None, no_steps, a, b, p, q
  16.     if states.issuperset([state_str]):
  17.         return state_str, visited, a, b, p, q
  18.     states.add(state_str)
  19.     return state_str, new, a, b, p, q
  20.  
  21.  
  22. def p_step(a, b, p, q):
  23.     if p == 1:
  24.         a = 1
  25.         p = 2
  26.     elif p == 2:
  27.         if b == 0:
  28.             p = 3
  29.     elif p == 3:
  30.         p = 4
  31.     elif p == 4:
  32.         a = 0
  33.         p = 1
  34.     return checker(a, b, p, q, False)
  35.  
  36.  
  37. def q_step(a, b, p, q):
  38.     thread_finished = False
  39.     if q == 1:
  40.         b = 1
  41.         q = 2
  42.     elif q == 2:
  43.         if a == 0:
  44.             q = 4
  45.         else:
  46.             q = 3
  47.     elif q == 3:
  48.         b = 0
  49.         q = 1
  50.     elif q == 4:
  51.         thread_finished = True
  52.     return checker(a, b, p, q, thread_finished)
  53.  
  54.  
  55. def bfs(a, b, p, q, state_from):
  56.     global states, new, visited
  57.     state_to, cont, p_res, q_res, a_res, b_res = p_step(a, b, p, q)
  58.     append = ''
  59.  
  60.     if cont == visited:
  61.         if state_from == state_to:
  62.             append = ', loop'
  63.         else:
  64.             append = ', cycle in graph'
  65.     if cont != no_steps:
  66.         print(f'{state_from} -> {state_to} # P step{append}')
  67.     else:
  68.         print(f'# P terminated')
  69.  
  70.     if cont == new:
  71.         bfs(p_res, q_res, a_res, b_res, state_to)
  72.  
  73.     state_to, cont, p_res, q_res, a_res, b_res = q_step(a, b, p, q)
  74.     append = ''
  75.     if cont == visited:
  76.         if state_from == state_to:
  77.             append = ', loop'
  78.         else:
  79.             append = ', cycle in graph'
  80.     if cont != no_steps:
  81.         print(f'{state_from} -> {state_to} # Q step{append}')
  82.     else:
  83.         print(f'# Unable to perform Q step from {state_from} - thread terminated')
  84.  
  85.     if cont == new:
  86.         bfs(p_res, q_res, a_res, b_res, state_to)
  87.  
  88.  
  89. states.add(f'[P{p},Q{q},{a},{b}]')
  90. bfs(a, b, p, q, f'[P{p},Q{q},{a},{b}]')
  91.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement