summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xbin/analyze-config.py3
-rwxr-xr-xlib/automata.py18
-rw-r--r--lib/functions.py40
-rw-r--r--lib/model.py8
-rw-r--r--lib/parameters.py4
5 files changed, 38 insertions, 35 deletions
diff --git a/bin/analyze-config.py b/bin/analyze-config.py
index 74718c6..fd8b982 100755
--- a/bin/analyze-config.py
+++ b/bin/analyze-config.py
@@ -183,7 +183,8 @@ def main():
)
with open("kconfigmodel.json", "w") as f:
- json.dump(model.to_json(), f, cls=NpEncoder)
+ json_model = model.to_json(with_param_name=True, param_names=symbols)
+ json.dump(json_model, f, sort_keys=True, cls=NpEncoder)
if __name__ == "__main__":
diff --git a/lib/automata.py b/lib/automata.py
index 5989a50..37fc821 100755
--- a/lib/automata.py
+++ b/lib/automata.py
@@ -274,11 +274,11 @@ class State:
new_suffix.extend(suffix)
yield new_suffix
- def to_json(self) -> dict:
+ def to_json(self, **kwargs) -> dict:
"""Return JSON encoding of this state object."""
ret = {"name": self.name, "power": None}
if self.power is not None:
- ret["power"] = self.power.to_json()
+ ret["power"] = self.power.to_json(**kwargs)
return ret
def to_dot(self) -> str:
@@ -432,7 +432,7 @@ class Transition:
ret[v] = args[k]
return ret
- def to_json(self) -> dict:
+ def to_json(self, **kwargs) -> dict:
"""Return JSON encoding of this transition object."""
ret = {
"name": self.name,
@@ -449,11 +449,11 @@ class Transition:
"timeout": None,
}
if self.duration is not None:
- ret["duration"] = self.duration.to_json()
+ ret["duration"] = self.duration.to_json(**kwargs)
if self.energy is not None:
- ret["energy"] = self.energy.to_json()
+ ret["energy"] = self.energy.to_json(**kwargs)
if self.timeout is not None:
- ret["timeout"] = self.timeout.to_json()
+ ret["timeout"] = self.timeout.to_json(**kwargs)
return ret
def to_dot(self) -> str:
@@ -785,7 +785,7 @@ class PTA:
return pta
- def to_json(self) -> dict:
+ def to_json(self, **kwargs) -> dict:
"""
Return JSON encoding of this PTA.
@@ -795,9 +795,9 @@ class PTA:
"parameters": self.parameters,
"initial_param_values": self.initial_param_values,
"state": dict(
- [[state.name, state.to_json()] for state in self.state.values()]
+ [[state.name, state.to_json(**kwargs)] for state in self.state.values()]
),
- "transitions": [trans.to_json() for trans in self.transitions],
+ "transitions": [trans.to_json(**kwargs) for trans in self.transitions],
"accepting_states": self.accepting_states,
}
return ret
diff --git a/lib/functions.py b/lib/functions.py
index 81f6ab5..78dfab8 100644
--- a/lib/functions.py
+++ b/lib/functions.py
@@ -194,7 +194,7 @@ class ModelFunction:
def webconf_function_map(self):
return list()
- def to_json(self):
+ def to_json(self, **kwargs):
"""Convert model to JSON."""
ret = {
"value": self.value,
@@ -264,8 +264,8 @@ class StaticFunction(ModelFunction):
"""
return self.value
- def to_json(self):
- ret = super().to_json()
+ def to_json(self, **kwargs):
+ ret = super().to_json(**kwargs)
ret.update({"type": "static", "value": self.value})
return ret
@@ -309,16 +309,18 @@ class SplitFunction(ModelFunction):
ret.extend(child.webconf_function_map())
return ret
- def to_json(self):
- ret = super().to_json()
- ret.update(
- {
- "type": "split",
- "paramIndex": self.param_index,
- # TODO zusätzlich paramName
- "child": dict([[k, v.to_json()] for k, v in self.child.items()]),
- }
- )
+ def to_json(self, **kwargs):
+ ret = super().to_json(**kwargs)
+ with_param_name = kwargs.get("with_param_name", False)
+ param_names = kwargs.get("param_names", list())
+ update = {
+ "type": "split",
+ "paramIndex": self.param_index,
+ "child": dict([[k, v.to_json(**kwargs)] for k, v in self.child.items()]),
+ }
+ if with_param_name and param_names:
+ update["paramName"] = param_names[self.param_index]
+ ret.update(update)
return ret
@classmethod
@@ -368,14 +370,14 @@ class SubstateFunction(ModelFunction):
return cumulative_energy / total_duration
- def to_json(self):
- ret = super().to_json()
+ def to_json(self, **kwargs):
+ ret = super().to_json(**kwargs)
ret.update(
{
"type": "substate",
"sequence": self.sequence_by_count,
- "countModel": self.count_model.to_json(),
- "subModel": self.sub_model.to_json(),
+ "countModel": self.count_model.to_json(**kwargs),
+ "subModel": self.sub_model.to_json(**kwargs),
}
)
return ret
@@ -594,8 +596,8 @@ class AnalyticFunction(ModelFunction):
js_buf = "(param, args) => " + js_buf.replace("np.", "Math.")
return [(f'"{self.model_function}"', js_buf)]
- def to_json(self):
- ret = super().to_json()
+ def to_json(self, **kwargs):
+ ret = super().to_json(**kwargs)
ret.update(
{
"type": "analytic",
diff --git a/lib/model.py b/lib/model.py
index 8cbb012..1e6bb28 100644
--- a/lib/model.py
+++ b/lib/model.py
@@ -358,7 +358,7 @@ class AnalyticModel:
)
return ret
- def to_json(self) -> dict:
+ def to_json(self, **kwargs) -> dict:
"""
Return JSON encoding of this AnalyticModel.
"""
@@ -369,7 +369,7 @@ class AnalyticModel:
for name in self.names:
for attr_name, attr in self.attr_by_name[name].items():
- ret["name"][name][attr_name] = attr.to_json()
+ ret["name"][name][attr_name] = attr.to_json(**kwargs)
return ret
@@ -796,7 +796,7 @@ class PTAModel(AnalyticModel):
self.submodel_by_name[name] = PTAModel(by_name, self.parameters, dict())
- def to_json(self):
+ def to_json(self, **kwargs):
static_model = self.get_static()
static_quality = self.assess(static_model)
param_model, param_info = self.get_fitted()
@@ -820,7 +820,7 @@ class PTAModel(AnalyticModel):
pta.update(
param_info, static_error=static_quality, function_error=analytic_quality
)
- return pta.to_json()
+ return pta.to_json(**kwargs)
def to_dot(self) -> str:
param_model, param_info = self.get_fitted()
diff --git a/lib/parameters.py b/lib/parameters.py
index d5339c5..1f6f8e3 100644
--- a/lib/parameters.py
+++ b/lib/parameters.py
@@ -525,11 +525,11 @@ class ModelAttribute:
mean = np.mean(self.data)
return f"ModelAttribute<{self.name}, {self.attr}, mean={mean}>"
- def to_json(self):
+ def to_json(self, **kwargs):
ret = {
"paramNames": self.param_names,
"argCount": self.arg_count,
- "modelFunction": self.model_function.to_json(),
+ "modelFunction": self.model_function.to_json(**kwargs),
}
return ret