base.py 8.0 KB

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