summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xbin/test_automata.py23
-rwxr-xr-xlib/automata.py18
2 files changed, 25 insertions, 16 deletions
diff --git a/bin/test_automata.py b/bin/test_automata.py
index 2b6b3d4..aafc50b 100755
--- a/bin/test_automata.py
+++ b/bin/test_automata.py
@@ -85,24 +85,29 @@ example_json_1 = {
],
}
+def dfs_tran_to_name(runs: list, with_args: bool) -> list:
+ if with_args:
+ return list(map(lambda run: list(map(lambda x: (x[0].name, x[1]), run)), runs))
+ return list(map(lambda run: list(map(lambda transition: transition.name, run)), runs))
+
class TestPTA(unittest.TestCase):
def test_dfs(self):
pta = PTA(['IDLE', 'TX'])
pta.add_transition('UNINITIALIZED', 'IDLE', 'init')
pta.add_transition('IDLE', 'TX', 'send')
pta.add_transition('TX', 'IDLE', 'txComplete')
- self.assertEqual(list(pta.dfs(0)), [['init']])
- self.assertEqual(list(pta.dfs(1)), [['init', 'send']])
- self.assertEqual(list(pta.dfs(2)), [['init', 'send', 'txComplete']])
- self.assertEqual(list(pta.dfs(3)), [['init', 'send', 'txComplete', 'send']])
+ self.assertEqual(dfs_tran_to_name(pta.dfs(0), False), [['init']])
+ self.assertEqual(dfs_tran_to_name(pta.dfs(1), False), [['init', 'send']])
+ self.assertEqual(dfs_tran_to_name(pta.dfs(2), False), [['init', 'send', 'txComplete']])
+ self.assertEqual(dfs_tran_to_name(pta.dfs(3), False), [['init', 'send', 'txComplete', 'send']])
pta = PTA(['IDLE'])
pta.add_transition('UNINITIALIZED', 'IDLE', 'init')
pta.add_transition('IDLE', 'IDLE', 'set1')
pta.add_transition('IDLE', 'IDLE', 'set2')
- self.assertEqual(list(pta.dfs(0)), [['init']])
- self.assertEqual(sorted(pta.dfs(1)), [['init', 'set1'], ['init', 'set2']])
- self.assertEqual(sorted(pta.dfs(2)), [['init', 'set1', 'set1'],
+ self.assertEqual(list(map(lambda x: list(map(lambda y: y.name, x)), pta.dfs(0))), [['init']])
+ self.assertEqual(sorted(map(lambda x: list(map(lambda y: y.name, x)), pta.dfs(1))), [['init', 'set1'], ['init', 'set2']])
+ self.assertEqual(sorted(map(lambda x: list(map(lambda y: y.name, x)), pta.dfs(2))), [['init', 'set1', 'set1'],
['init', 'set1', 'set2'],
['init', 'set2', 'set1'],
['init', 'set2', 'set2']])
@@ -122,8 +127,8 @@ class TestPTA(unittest.TestCase):
def test_from_json_dfs(self):
pta = PTA.from_json(example_json_1)
- self.assertEqual(sorted(pta.dfs(1)), [['init', 'init'], ['init', 'send'], ['init', 'setTxPower']])
- self.assertEqual(sorted(pta.dfs(1, with_arguments = True)),
+ self.assertEqual(sorted(dfs_tran_to_name(pta.dfs(1), False)), [['init', 'init'], ['init', 'send'], ['init', 'setTxPower']])
+ self.assertEqual(sorted(dfs_tran_to_name(pta.dfs(1, with_arguments = True), True)),
[
[('init', ()), ('init', ())],
[('init', ()), ('send', ('"foo"', 3))],
diff --git a/lib/automata.py b/lib/automata.py
index e27aa91..640da96 100755
--- a/lib/automata.py
+++ b/lib/automata.py
@@ -88,19 +88,19 @@ class State:
if with_arguments:
if trans.argument_combination == 'cartesian':
for args in itertools.product(*trans.argument_values):
- yield [(trans.name, args)]
+ yield [(trans, args)]
else:
for args in zip(*trans.argument_values):
- yield [(trans.name, args)]
+ yield [(trans, args)]
else:
- yield [trans.name]
+ yield [trans]
else:
for trans in self.outgoing_transitions.values():
for suffix in trans.destination.dfs(depth - 1, with_arguments = with_arguments):
if with_arguments:
if trans.argument_combination == 'cartesian':
for args in itertools.product(*trans.argument_values):
- new_suffix = [(trans.name, args)]
+ new_suffix = [(trans, args)]
new_suffix.extend(suffix)
yield new_suffix
else:
@@ -109,11 +109,11 @@ class State:
else:
arg_values = [tuple()]
for args in arg_values:
- new_suffix = [(trans.name, args)]
+ new_suffix = [(trans, args)]
new_suffix.extend(suffix)
yield new_suffix
else:
- new_suffix = [trans.name]
+ new_suffix = [trans]
new_suffix.extend(suffix)
yield new_suffix
@@ -347,6 +347,9 @@ class PTA:
destination = transition['destination']
arguments = list()
argument_values = list()
+ is_interrupt = False
+ if transition['level'] == 'epilogue':
+ is_interrupt = True
if type(destination) == list:
destination = destination[0]
for arg in transition['parameters']:
@@ -354,7 +357,8 @@ class PTA:
argument_values.append(arg['values'])
for origin in transition['origins']:
pta.add_transition(origin, destination, trans_name,
- arguments = arguments, argument_values = argument_values)
+ arguments = arguments, argument_values = argument_values,
+ is_interrupt = is_interrupt)
return pta