Skip to content

Commit

Permalink
Merge 7309df3 into bfbd980
Browse files Browse the repository at this point in the history
  • Loading branch information
mgoeminne committed Jan 25, 2016
2 parents bfbd980 + 7309df3 commit 52bdf98
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 1 deletion.
27 changes: 27 additions & 0 deletions sismic/io/text.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from sismic.model import Statechart, StateMixin
import itertools

__all__ = ['export_to_tree']


def export_to_tree(statechart: Statechart, spaces=3) -> str:
"""
Provides a textual representation of the hierarchy of states belonging to
a statechart. Only states are represented.
:param statechart: A statechart to consider
:param spaces: The number of space characters used to represent a level of depth in the state hierarchy.
:return: A textual representation of hierarchy of states belonging to
a statechart.
"""
def to_tree(state: str, statechart: Statechart, spaces):
children = sorted(statechart.children_for(state))

if len(children) == 0:
return [state]
else:
trees = list(map(lambda child: to_tree(child, statechart, spaces), children))
flat_trees = list(itertools.chain.from_iterable(trees))
children_repr = list(map(lambda x: spaces*' ' + x, flat_trees))
return [state] + children_repr

return to_tree(statechart.root, statechart, spaces)
39 changes: 38 additions & 1 deletion tests/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,41 @@ def test_export_valid(self):
ex_1 = io.export_to_yaml(sc_1)
sc_2 = io.import_from_yaml(ex_1)

self.assertTrue(sc_2.validate())
self.assertTrue(sc_2.validate())

class ExportToTreeTests(unittest.TestCase):

def setUp(self):
files_t = ['actions', 'composite', 'deep_history', 'infinite', 'internal', 'nested_parallel',
'nondeterministic', 'parallel', 'simple', 'timer']

self.results = [
['root', ' s1', ' s2', ' s3'],
['root', ' s1', ' s1a', ' s1b', ' s1b1', ' s1b2', ' s2'],
['root', ' active', ' active.H*', ' concurrent_processes', ' process_1',
' s11', ' s12', ' s13', ' process_2', ' s21',
' s22', ' s23', ' pause', ' stop'],
['root', ' s1', ' s2', ' stop'],
['root', ' active', ' s1', ' s2'],
['root', ' s1', ' p1', ' r1', ' i1', ' j1', ' k1',
' r2', ' i2', ' j2', ' k2', ' y', ' p2',
' r3', ' i3', ' j3', ' k3', ' z', ' r4',
' i4', ' j4', ' k4', ' x'],
['root', ' s1', ' s2', ' s3'],
['root', ' s1', ' p1', ' a1', ' b1', ' c1', ' initial1', ' p2',
' a2', ' b2', ' c2', ' initial2'],
['root', ' final', ' s1', ' s2', ' s3'],
['root', ' s1', ' s2', ' s3', ' s4']
]

self.files = []
for filename in files_t:
with open(os.path.join('tests', 'yaml', filename + '.yaml')) as f:
self.files.append('\n'.join(f.readlines()))

def test_export(self):
from sismic.io.text import export_to_tree

for f, r in zip(self.files, self.results):
statechart = io.import_from_yaml(f)
self.assertEquals(export_to_tree(statechart), r)

0 comments on commit 52bdf98

Please sign in to comment.