16
16
17
17
18
18
/**
19
- * Internal exercise configuration compilation service. Handles tests in
20
- * execution and their separation into appropriate sub-directories. This mainly
21
- * means modification of file variables and prefixing them with proper
22
- * directory. Mkdir tasks will be also constructed and added to resulting tree.
19
+ * Internal exercise configuration compilation service. Handles creation of the directories used during execution and
20
+ * creating dumping boxes for all created directories. Alongside that for optimized boxes the name of the directory in
21
+ * which they will be processed has to be found.
23
22
* @note Should be called after optimisation.
24
23
*/
25
24
class DirectoriesResolver {
@@ -88,22 +87,22 @@ private function createDumpResultsNode(string $testId, string $testName): Node {
88
87
/**
89
88
* Add mkdir tasks for all directories at the beginning of the tree.
90
89
* @param RootedTree $tree
91
- * @param Node[][] $testNodes indexed with testId
90
+ * @param Node[][] $directoriesNodes indexed with testId
92
91
* @param CompilationContext $context
93
92
* @param CompilationParams $params
94
93
* @return RootedTree
95
94
* @throws ExerciseConfigException
96
95
*/
97
- private function addDirectories (RootedTree $ tree , array $ testNodes ,
96
+ private function addDirectories (RootedTree $ tree , array $ directoriesNodes ,
98
97
CompilationContext $ context , CompilationParams $ params ): RootedTree {
99
- if (count ($ testNodes ) === 0 ) {
98
+ if (count ($ directoriesNodes ) === 0 ) {
100
99
return $ tree ;
101
100
}
102
101
103
102
// go through all tests
104
103
$ lastMkdirNode = null ;
105
104
$ result = new RootedTree ();
106
- foreach ($ testNodes as $ testId => $ nodes ) {
105
+ foreach ($ directoriesNodes as $ testId => $ nodes ) {
107
106
$ testName = $ context ->getTestsNames ()[$ testId ];
108
107
109
108
if ($ lastMkdirNode === null ) {
@@ -148,18 +147,31 @@ private function addDirectories(RootedTree $tree, array $testNodes,
148
147
* @throws ExerciseConfigException
149
148
*/
150
149
public function resolve (RootedTree $ tree , CompilationContext $ context , CompilationParams $ params ): RootedTree {
151
- $ testNodes = [];
150
+ // Let's break it down...
151
+ // DirectoriesResolver works in cooperation with BoxesOptimizer which optimizes the flow of the tasks and marks the
152
+ // nodes which were optimized (this effectively means settings the test-id to null). Directories resolver then
153
+ // goes through the tree and creates the directories needed for execution. If the test-id is set, it is easy and
154
+ // straightforward, if it is not set the directories have to be smartly named and generated.
155
+ // The algorithm follows... The tree is searched with breadth-first approach. Every node is processed in
156
+ // the following way. If the node belongs to the test, the test identification is recorded and children of this node
157
+ // are processed. If the node was optimised (has null test-id) then it is needed further processing. We need to
158
+ // figure out the name of the directory which will be created for this optimized node and its sub-nodes. The name of
159
+ // the directory is composed of categories of boxes in the most left sub-branch which does not have test-id set.
160
+ // Once the name is known, it is used as a directory for the processed node and all the sub-nodes in the most left
161
+ // branch of the tree. After that, children of the node are processed.
162
+
163
+ $ directoriesNodes = [];
152
164
$ stack = array_reverse ($ tree ->getRootNodes ());
153
165
while (!empty ($ stack )) {
154
166
$ current = array_pop ($ stack );
155
167
$ testId = $ current ->getTestId ();
156
168
if ($ testId !== null ) {
157
169
// all nodes of each tests are saved and further dependencies
158
170
// on mkdir tasks are set on them
159
- if (!array_key_exists ($ testId , $ testNodes )) {
160
- $ testNodes [$ testId ] = [];
171
+ if (!array_key_exists ($ testId , $ directoriesNodes )) {
172
+ $ directoriesNodes [$ testId ] = [];
161
173
}
162
- $ testNodes [$ testId ][] = $ current ;
174
+ $ directoriesNodes [$ testId ][] = $ current ;
163
175
}
164
176
165
177
// process current node
@@ -171,7 +183,7 @@ public function resolve(RootedTree $tree, CompilationContext $context, Compilati
171
183
}
172
184
}
173
185
174
- return $ this ->addDirectories ($ tree , $ testNodes , $ context , $ params );
186
+ return $ this ->addDirectories ($ tree , $ directoriesNodes , $ context , $ params );
175
187
}
176
188
177
189
}
0 commit comments