From 844586960ad6054179903988b90ee5c257a7a897 Mon Sep 17 00:00:00 2001 From: Andrei Bondarenko Date: Sat, 17 Jul 2021 15:59:36 +0200 Subject: [PATCH] Added Context abstract base class --- core/context/base.py | 59 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 core/context/base.py diff --git a/core/context/base.py b/core/context/base.py new file mode 100644 index 0000000..627dc2e --- /dev/null +++ b/core/context/base.py @@ -0,0 +1,59 @@ +from abc import ABC, abstractmethod +from state.base import State +from core.element import Element, String + + +class Context(ABC): + def __init__(self, state: State, model: Element, metamodel: Element): + self.state = state + self.model = model + self.metamodel = metamodel + + @abstractmethod + def __enter__(self): + pass + + @abstractmethod + def __exit__(self): + pass + + @abstractmethod + def instantiate(self, type_name: String, instance_name: String): + pass + + @abstractmethod + def instantiate_value(self, type_name: String, instance_name: String, value: Element): + pass + + @abstractmethod + def instantiate_link(self, type_name: String, name: String, source: String, target: String): + pass + + @abstractmethod + def delete_element(self, name: String): + pass + + @abstractmethod + def verify(self): + pass + + @abstractmethod + def list_elements(self): + pass + + def list_types(self): + # can be implemented here since we assume that metamodel + # is always in graph fo, i.e. in the MV-state graph. + unsorted = [] + model_root = self.state.read_dict(self.metamodel.id, "Model") + for elem_edge in self.state.read_outgoing(model_root): + # get element name + label_edge, = self.state.read_outgoing(elem_edge) + _, label_node = self.state.read_edge(label_edge) + label = self.state.read_value(label_node) + # find element type + elem_type_node = self.state.read_dict(label_node, "Type") + elem_type = self.state.read_value(elem_type_node) + unsorted.append(f"{label} : {elem_type if elem_type is not None else '_'}") + for i in sorted(unsorted): + print(i)