summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Friesel <derf@finalrewind.org>2019-02-21 12:28:27 +0100
committerDaniel Friesel <derf@finalrewind.org>2019-02-21 12:28:27 +0100
commitd734d1a5deaa6a05d126f32f48e7dd5abcdd2ba5 (patch)
tree009551b830ad613d5bdb1f48b06155de9d17a558
parent46e69c4f402414d93e6efb2a7138e1fc13bb66d3 (diff)
PTA: Add loader for legacy (dfatool.pl) JSON format
-rwxr-xr-xlib/automata.py34
1 files changed, 34 insertions, 0 deletions
diff --git a/lib/automata.py b/lib/automata.py
index fbfaf36..be0b3d8 100755
--- a/lib/automata.py
+++ b/lib/automata.py
@@ -267,6 +267,9 @@ class PTA:
Compatible with the to_json method.
"""
+ if 'transition' in json_input:
+ return cls.from_legacy_json(json_input)
+
kwargs = {}
for key in ('state_names', 'parameters', 'initial_param_values'):
if key in json_input:
@@ -299,6 +302,37 @@ class PTA:
return pta
+ @classmethod
+ def from_legacy_json(cls, json_input: dict):
+ """
+ Return a PTA created from the provided JSON data.
+
+ Compatible with the legacy dfatool/perl format.
+ """
+ kwargs = {
+ 'parameters' : list(),
+ 'initial_param_values': list(),
+ }
+
+ for param in sorted(json_input['parameter'].keys()):
+ kwargs['parameters'].append(param)
+ kwargs['initial_param_values'].append(json_input['parameter'][param]['default'])
+
+ pta = cls(**kwargs)
+
+ for name, state in json_input['state'].items():
+ pta.add_state(name, power = float(state['power']['static']))
+
+ for trans_name in sorted(json_input['transition'].keys()):
+ transition = json_input['transition'][trans_name]
+ destination = transition['destination']
+ if type(destination) == list:
+ destination = destination[0]
+ for origin in transition['origins']:
+ pta.add_transition(origin, destination, trans_name)
+
+ return pta
+
def to_json(self) -> dict:
"""
Return JSON encoding of this PTA.