Unverified Commit 2451c80b authored by Marvin Meiers's avatar Marvin Meiers
Browse files

symphony: add parameters dict to Component, Channel, and App

Additionally, add helper functions for the conversion of the
parameters dict to and from JSON.
parent 242b3dd2
...@@ -122,7 +122,7 @@ class Component(util_base.IdObj): ...@@ -122,7 +122,7 @@ class Component(util_base.IdObj):
super().__init__() super().__init__()
self.system = s self.system = s
self.ifs: list[Interface] = [] self.ifs: list[Interface] = []
s.parameters = {} self.parameters: dict[tp.Any, tp.Any] = {}
s._add_component(self) s._add_component(self)
self.name: str | None = None self.name: str | None = None
...@@ -145,6 +145,7 @@ class Component(util_base.IdObj): ...@@ -145,6 +145,7 @@ class Component(util_base.IdObj):
json_obj["module"] = self.__class__.__module__ json_obj["module"] = self.__class__.__module__
json_obj["system"] = self.system.id() json_obj["system"] = self.system.id()
json_obj["name"] = self.name json_obj["name"] = self.name
json_obj["parameters"] = util_base.dict_to_json(self.parameters)
interfaces_json = [] interfaces_json = []
for inf in self.interfaces(): for inf in self.interfaces():
...@@ -158,6 +159,9 @@ class Component(util_base.IdObj): ...@@ -158,6 +159,9 @@ class Component(util_base.IdObj):
def fromJSON(cls, system: System, json_obj: dict) -> Component: def fromJSON(cls, system: System, json_obj: dict) -> Component:
instance = super().fromJSON(json_obj) instance = super().fromJSON(json_obj)
instance.name = util_base.get_json_attr_top_or_none(json_obj, "name") instance.name = util_base.get_json_attr_top_or_none(json_obj, "name")
instance.parameters = util_base.json_to_dict(
util_base.get_json_attr_top(json_obj, "parameters")
)
instance.system = system instance.system = system
system._add_component(instance) system._add_component(instance)
...@@ -243,6 +247,7 @@ class Channel(util_base.IdObj): ...@@ -243,6 +247,7 @@ class Channel(util_base.IdObj):
self.a.connect(self) self.a.connect(self)
self.b: Interface = b self.b: Interface = b
self.b.connect(self) self.b.connect(self)
self.parameters: dict[tp.Any, tp.Any] = {}
a.component.system._add_channel(self) a.component.system._add_channel(self)
def interfaces(self) -> list[Interface]: def interfaces(self) -> list[Interface]:
...@@ -270,12 +275,16 @@ class Channel(util_base.IdObj): ...@@ -270,12 +275,16 @@ class Channel(util_base.IdObj):
json_obj["latency"] = self.latency json_obj["latency"] = self.latency
json_obj["interface_a"] = self.a.id() json_obj["interface_a"] = self.a.id()
json_obj["interface_b"] = self.b.id() json_obj["interface_b"] = self.b.id()
json_obj["parameters"] = util_base.dict_to_json(self.parameters)
return json_obj return json_obj
@classmethod @classmethod
def fromJSON(cls, system: System, json_obj: dict) -> Channel: def fromJSON(cls, system: System, json_obj: dict) -> Channel:
instance = super().fromJSON(json_obj) instance = super().fromJSON(json_obj)
instance.latency = int(util_base.get_json_attr_top(json_obj, "latency")) instance.latency = int(util_base.get_json_attr_top(json_obj, "latency"))
instance.parameters = util_base.json_to_dict(
util_base.get_json_attr_top(json_obj, "parameters")
)
inf_id_a = int(util_base.get_json_attr_top(json_obj, "interface_a")) inf_id_a = int(util_base.get_json_attr_top(json_obj, "interface_a"))
inf_id_b = int(util_base.get_json_attr_top(json_obj, "interface_b")) inf_id_b = int(util_base.get_json_attr_top(json_obj, "interface_b"))
......
...@@ -37,12 +37,14 @@ class Application(utils_base.IdObj): ...@@ -37,12 +37,14 @@ class Application(utils_base.IdObj):
def __init__(self, h: sys_host.Host) -> None: def __init__(self, h: sys_host.Host) -> None:
super().__init__() super().__init__()
self.host: sys_host.Host = h self.host: sys_host.Host = h
self.parameters: dict[tp.Any, tp.Any] = {}
def toJSON(self) -> dict: def toJSON(self) -> dict:
json_obj = super().toJSON() json_obj = super().toJSON()
json_obj["type"] = self.__class__.__name__ json_obj["type"] = self.__class__.__name__
json_obj["module"] = self.__class__.__module__ json_obj["module"] = self.__class__.__module__
json_obj["host"] = self.host.id() json_obj["host"] = self.host.id()
json_obj["parameters"] = utils_base.dict_to_json(self.parameters)
return json_obj return json_obj
@classmethod @classmethod
...@@ -50,6 +52,9 @@ class Application(utils_base.IdObj): ...@@ -50,6 +52,9 @@ class Application(utils_base.IdObj):
instance = super().fromJSON(json_obj) instance = super().fromJSON(json_obj)
host_id = utils_base.get_json_attr_top(json_obj, "host") host_id = utils_base.get_json_attr_top(json_obj, "host")
instance.host = system.get_comp(host_id) instance.host = system.get_comp(host_id)
instance.parameters = utils_base.json_to_dict(
utils_base.get_json_attr_top(json_obj, "parameters")
)
return instance return instance
......
...@@ -23,6 +23,7 @@ ...@@ -23,6 +23,7 @@
import abc import abc
import itertools import itertools
import importlib import importlib
import typing as tp
class IdObj(abc.ABC): class IdObj(abc.ABC):
...@@ -114,3 +115,73 @@ def get_cls_by_json(json_obj: dict): ...@@ -114,3 +115,73 @@ def get_cls_by_json(json_obj: dict):
type_name = get_json_attr_top(json_obj, "type") type_name = get_json_attr_top(json_obj, "type")
module_name = get_json_attr_top(json_obj, "module") module_name = get_json_attr_top(json_obj, "module")
return get_cls_from_type_module(type_name, module_name) return get_cls_from_type_module(type_name, module_name)
def _has_base_type(obj: tp.Any) -> bool:
return isinstance(obj, (str, int, float, bool, type(None)))
def _obj_to_json(obj: tp.Any) -> tp.Any:
if _has_base_type(obj):
return obj
elif isinstance(obj, list):
return list_tuple_to_json(obj)
elif isinstance(obj, tuple):
return list_tuple_to_json(obj)
elif isinstance(obj, dict):
return dict_to_json(obj)
else:
has_attribute(obj, "toJSON")
return obj.toJSON()
def list_tuple_to_json(list: list | tuple) -> list:
json_list = []
for element in list:
json_list.append(_obj_to_json(element))
return json_list
def dict_to_json(data: dict) -> dict:
json_obj = {}
for key, value in data.items():
key_json = _obj_to_json(key)
value_json = _obj_to_json(value)
assert(key_json not in json_obj)
json_obj[key_json] = value_json
return json_obj
def _json_obj_to_dict(obj: tp.Any) -> tp.Any:
if _has_base_type(obj):
return obj
elif isinstance(obj, list):
return json_array_to_list(obj)
elif isinstance(obj, dict):
return _json_dict_to_obj(obj)
else:
raise ValueError(f"cannot parse object with type {type(obj)} from json")
def _json_dict_to_obj(json_obj: dict) -> tp.Any:
if "type" in json_obj and "module" in json_obj:
# this seems to be a Python object that was converted to JSON
cls = get_cls_from_type_module(json_obj["type"], json_obj["module"])
has_attribute(cls, "fromJSON")
return cls.fromJSON(json_obj)
else:
# this seems to be a plain dict
return json_to_dict(json_obj)
def json_array_to_list(array: list) -> list:
data = []
for element in array:
data.append(_json_obj_to_dict(element))
return data
def json_to_dict(json_obj: dict) -> dict:
data = {}
for key, value in json_obj.items():
key_dict = _json_obj_to_dict(key)
value_dict = _json_obj_to_dict(value)
assert(key_dict not in data)
data[key_dict] = value_dict
return data
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment