PageRenderTime 37ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/app/helpers/ExerciseConfig/Compilation/Tree/Node.php

https://bitbucket.org/svanda777/recodex-api-with-lti
PHP | 219 lines | 81 code | 32 blank | 106 comment | 4 complexity | 689f496e1b9c779633b8dde1923243fc MD5 | raw file
  1. <?php
  2. namespace App\Helpers\ExerciseConfig\Compilation\Tree;
  3. use App\Helpers\ExerciseConfig\Pipeline\Box\Box;
  4. use App\Helpers\ExerciseConfig\VariablesTable;
  5. /**
  6. * Node representing Box in the compilation of exercise. It can hold additional
  7. * information regarding box which does not have to be stored there, this can
  8. * save memory during loading of pipelines and not compiling them.
  9. * @note Structure used in exercise compilation.
  10. */
  11. class Node {
  12. /**
  13. * Box connected to this node.
  14. * @var Box
  15. */
  16. private $box;
  17. /**
  18. * Identification of test to which this box belongs to.
  19. * @var string
  20. */
  21. private $testId = null;
  22. /**
  23. * Identification of pipeline to which this box belongs to.
  24. * @var string
  25. */
  26. private $pipelineId = null;
  27. /**
  28. * Identification of tasks which was compiled from corresponding box.
  29. * @var string[]
  30. */
  31. private $taskIds = [];
  32. /**
  33. * Nodes which identify themselves as parent of this node.
  34. * @var Node[]
  35. */
  36. private $parents = array();
  37. /**
  38. * Children nodes of this one.
  39. * @var Node[]
  40. */
  41. private $children = array();
  42. /**
  43. * Dependencies of this node.
  44. * @var Node[]
  45. */
  46. private $dependencies = array();
  47. /**
  48. * Node constructor.
  49. * @param PortNode $node
  50. */
  51. public function __construct(PortNode $node = null) {
  52. if ($node) {
  53. $this->box = $node->getBox();
  54. $this->pipelineId = $node->getPipelineId();
  55. $this->testId = $node->getTestId();
  56. }
  57. }
  58. /**
  59. * Get box associated with this node.
  60. * @return Box
  61. */
  62. public function getBox(): Box {
  63. return $this->box;
  64. }
  65. /**
  66. * Set box associated with this node.
  67. * @param Box $box
  68. * @return Node
  69. */
  70. public function setBox(Box $box): Node {
  71. $this->box = $box;
  72. return $this;
  73. }
  74. /**
  75. * Test identification for corresponding box.
  76. * @return string|null
  77. */
  78. public function getTestId(): ?string {
  79. return $this->testId;
  80. }
  81. /**
  82. * Set test identification of box.
  83. * @param string|null $testId
  84. * @return Node
  85. */
  86. public function setTestId(?string $testId): Node {
  87. $this->testId = $testId;
  88. return $this;
  89. }
  90. /**
  91. * Pipeline identification for corresponding box.
  92. * @return string|null
  93. */
  94. public function getPipelineId(): ?string {
  95. return $this->pipelineId;
  96. }
  97. /**
  98. * Set pipeline identification of box.
  99. * @param string|null $pipelineId
  100. * @return Node
  101. */
  102. public function setPipelineId(?string $pipelineId): Node {
  103. $this->pipelineId = $pipelineId;
  104. return $this;
  105. }
  106. /**
  107. * Return task identifications associated with this node.
  108. * If there is none, ask dependencies for their task identifications.
  109. * @return string[]
  110. */
  111. public function getTaskIds(): array {
  112. $taskIds = $this->taskIds;
  113. if (empty($taskIds)) {
  114. foreach ($this->dependencies as $dependency) {
  115. $taskIds = array_merge($taskIds, $dependency->getTaskIds());
  116. }
  117. }
  118. return array_unique($taskIds);
  119. }
  120. /**
  121. * Add task identification to internal array.
  122. * @param string $taskId
  123. */
  124. public function addTaskId(string $taskId) {
  125. $this->taskIds[] = $taskId;
  126. }
  127. /**
  128. * Get parents of this node.
  129. * @return Node[]
  130. */
  131. public function getParents(): array {
  132. return $this->parents;
  133. }
  134. /**
  135. * Add parent of this node.
  136. * @param Node $parent
  137. */
  138. public function addParent(Node $parent) {
  139. $this->parents[] = $parent;
  140. }
  141. /**
  142. * Remove given parent from this node.
  143. * @param Node $parent
  144. */
  145. public function removeParent(Node $parent) {
  146. if(($key = array_search($parent, $this->parents)) !== false){
  147. unset($this->parents[$key]);
  148. }
  149. }
  150. /**
  151. * Get children of this node.
  152. * @return Node[]
  153. */
  154. public function getChildren(): array {
  155. return $this->children;
  156. }
  157. /**
  158. * Add child to this node with specified node.
  159. * @param Node $child
  160. */
  161. public function addChild(Node $child) {
  162. $this->children[] = $child;
  163. }
  164. /**
  165. * Remove given child from children array.
  166. * @param Node $child
  167. */
  168. public function removeChild(Node $child) {
  169. if(($key = array_search($child, $this->children)) !== false){
  170. unset($this->children[$key]);
  171. }
  172. }
  173. /**
  174. * Get dependencies of this node.
  175. * @return Node[]
  176. */
  177. public function getDependencies(): array {
  178. return $this->dependencies;
  179. }
  180. /**
  181. * Add dependency of this node.
  182. * @param Node $dependency
  183. */
  184. public function addDependency(Node $dependency) {
  185. $this->dependencies[] = $dependency;
  186. }
  187. }