base.py 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. # Copyright 2023 Modelling, Simulation and Design Lab (MSDL)
  2. # at the University of Antwerp (http://msdl.uantwerpen.be/).
  3. #
  4. # Licensed under the GNU Public License v3 (the "License");
  5. # you may not use this file except in compliance with the
  6. # License. You may obtain a copy of the License at
  7. # https://www.gnu.org/licenses/gpl-3.0.en.html
  8. #
  9. # Unless required by applicable law or agreed to in writing,
  10. # software distributed under the License is distributed on an
  11. # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
  12. # either express or implied. See the License for the specific
  13. # language governing permissions and limitations under the
  14. # License.
  15. from abc import ABC, abstractmethod
  16. from typing import Any, List, Tuple, Optional, Union
  17. from uuid import UUID, uuid4
  18. primitive_types = (int, float, str, bool)
  19. INTEGER = ("Integer",)
  20. FLOAT = ("Float",)
  21. STRING = ("String",)
  22. BOOLEAN = ("Boolean",)
  23. TYPE = ("Type",)
  24. type_values = (INTEGER, FLOAT, STRING, BOOLEAN, TYPE)
  25. Node = UUID
  26. Edge = UUID
  27. Element = Union[Node, Edge]
  28. class State(ABC):
  29. """
  30. Abstract base class for MvS CRUD interface defined in:
  31. http://msdl.cs.mcgill.ca/people/yentl/files/thesis.pdf
  32. This code is based on:
  33. https://msdl.uantwerpen.be/git/yentl/modelverse/src/master/state/modelverse_state
  34. """
  35. @staticmethod
  36. def new_id() -> UUID:
  37. """
  38. Generates a new UUID
  39. """
  40. return uuid4()
  41. @staticmethod
  42. def is_valid_datavalue(value: Any) -> bool:
  43. """
  44. Checks whether value type is supported.
  45. Args:
  46. value: value whose type needs to be checked
  47. Returns:
  48. True if value type is supported, False otherwise.
  49. """
  50. if isinstance(value, tuple) and value in type_values:
  51. return True
  52. if not isinstance(value, primitive_types):
  53. return False
  54. elif isinstance(value, int) and not (-2**63 <= value <= 2**63 - 1):
  55. return False
  56. return True
  57. def purge(self):
  58. """
  59. Implements a garbage collection routine for implementations that don't have automatic garbage collection.
  60. """
  61. pass
  62. # =========================================================================
  63. # CREATE
  64. # =========================================================================
  65. @abstractmethod
  66. def create_node(self) -> Node:
  67. """
  68. Creates node.
  69. Returns:
  70. The created node.
  71. """
  72. pass
  73. @abstractmethod
  74. def create_edge(self, source: Element, target: Element) -> Optional[Edge]:
  75. """
  76. Creates edge. Source and target elements should already exist.
  77. Args:
  78. source: source element of edge
  79. target: target element of edge
  80. Returns:
  81. The created edge, None if source or target element doesn't exist.
  82. """
  83. pass
  84. @abstractmethod
  85. def create_nodevalue(self, value: Any) -> Optional[Node]:
  86. """
  87. Creates node containing value.
  88. Args:
  89. value: value to assign to new node
  90. Returns:
  91. The created node, None if type of value is not supported.
  92. """
  93. pass
  94. @abstractmethod
  95. def create_dict(self, source: Element, value: Any, target: Element) -> None:
  96. """
  97. Creates named edge between two graph elements.
  98. Args:
  99. source: source element of edge
  100. value: edge label
  101. target: target element of edge
  102. Returns:
  103. Nothing.
  104. """
  105. pass
  106. # =========================================================================
  107. # READ
  108. # =========================================================================
  109. @abstractmethod
  110. def read_root(self) -> Node:
  111. """
  112. Reads state's root node.
  113. Returns:
  114. The state's root node.
  115. """
  116. pass
  117. @abstractmethod
  118. def read_value(self, node: Node) -> Optional[Any]:
  119. """
  120. Reads value of given node.
  121. Args:
  122. node: node whose value to read
  123. Returns:
  124. If node exists, value stored in node, else None.
  125. """
  126. pass
  127. @abstractmethod
  128. def read_outgoing(self, elem: Element) -> Optional[List[Edge]]:
  129. """
  130. Retrieves edges whose source is given element.
  131. Args:
  132. elem: source element of edges to retrieve
  133. Returns:
  134. If elem exists, list of edges whose source is elem, else None.
  135. """
  136. pass
  137. @abstractmethod
  138. def read_incoming(self, elem: Element) -> Optional[List[Edge]]:
  139. """
  140. Retrieves edges whose target is given element.
  141. Args:
  142. elem: target element of edges to retrieve
  143. Returns:
  144. If elem exists, list of edges whose target is elem, else None.
  145. """
  146. pass
  147. @abstractmethod
  148. def read_edge(self, edge: Edge) -> Tuple[Optional[Node], Optional[Node]]:
  149. """
  150. Reads source and target of given edge.
  151. Args:
  152. edge: edge whose source and target to read
  153. Returns:
  154. If edge exists, tuple containing source (first) and target (second) node, else (None, None)
  155. """
  156. pass
  157. @abstractmethod
  158. def read_dict(self, elem: Element, value: Any) -> Optional[Element]:
  159. """
  160. Reads element connected to given element through edge with label = value.
  161. Args:
  162. elem: source element
  163. value: edge label
  164. Returns:
  165. If elem doesn't exist or no edge is found with given label, None, else target element of edge with label = value originating from source.
  166. """
  167. pass
  168. @abstractmethod
  169. def read_dict_keys(self, elem: Element) -> Optional[List[Element]]:
  170. """
  171. Reads labels of outgoing edges starting in given node.
  172. Args:
  173. elem: source element
  174. Returns:
  175. If elem exists, list of (unique) edge labels, else None.
  176. """
  177. pass
  178. @abstractmethod
  179. def read_dict_edge(self, elem: Element, value: Any) -> Optional[Edge]:
  180. """
  181. Reads edge between two elements connected through edge with label = value.
  182. Args:
  183. elem: source element
  184. value: edge label
  185. Returns:
  186. If elem doesn't exist or no edge is found with given label, None, else edge with label = value originating from source.
  187. """
  188. pass
  189. @abstractmethod
  190. def read_dict_node(self, elem: Element, value_node: Node) -> Optional[Element]:
  191. """
  192. Reads element connected to given element through edge with label node = value_node.
  193. Args:
  194. elem: source element
  195. value_node: edge label node
  196. Returns:
  197. If elem exists, target element of edge with label stored in value_node originating from elem, else None.
  198. """
  199. pass
  200. @abstractmethod
  201. def read_dict_node_edge(self, elem: Element, value_node: Node) -> Optional[Edge]:
  202. """
  203. Reads edge connecting two elements through edge with label node = value_node.
  204. Args:
  205. elem: source element
  206. value_node: edge label node
  207. Returns:
  208. If elem exists, edge with label node = value_node, originating from source, else None.
  209. """
  210. pass
  211. @abstractmethod
  212. def read_reverse_dict(self, elem: Element, value: Any) -> Optional[List[Element]]:
  213. """
  214. Retrieves a list of all elements that have an outgoing edge, having label = value, towards the passed element.
  215. Args:
  216. elem: target element
  217. value: edge label
  218. Returns:
  219. If elem exists, list of elements with an outgoing edge with label = value towards elem, else None.
  220. """
  221. pass
  222. # =========================================================================
  223. # UPDATE
  224. # =========================================================================
  225. """
  226. Updates are done by performing subsequent CREATE and DELETE operations:
  227. http://msdl.cs.mcgill.ca/people/yentl/files/thesis.pdf
  228. """
  229. # =========================================================================
  230. # DELETE
  231. # =========================================================================
  232. @abstractmethod
  233. def delete_node(self, node: Node) -> None:
  234. """
  235. Deletes given node from state graph.
  236. Args:
  237. node: node to be deleted
  238. Returns:
  239. None
  240. """
  241. pass
  242. @abstractmethod
  243. def delete_edge(self, edge: Edge) -> None:
  244. """
  245. Deletes given edge from state graph.
  246. Args:
  247. edge: edge to be deleted
  248. Returns:
  249. None
  250. """
  251. pass