Skip to content

Commit

Permalink
README++
Browse files Browse the repository at this point in the history
  • Loading branch information
Geoffrey Hendrey committed Oct 24, 2023
1 parent 27e7475 commit 61e3b1f
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1580,20 +1580,27 @@ that does not exist in the JSON template. Such non-materialized nodes come in 2
The MetaInfo graph for our example above is:
![Planning](https://raw.githubusercontent.com/geoffhendrey/jsonataplay/main/stated%20metainfo%20algorithms%20-%20Page%202.svg)
## DFS Tree Traversal
The tree traversal, which we will call `findDeps` begins by picking any node, `node`, and calling `findDeps(node)`
The core algorithm is in the method `TemplateProcessor.topologicalSort`. This method is passed a metaImnfo node, and
recursively calls itself as it `followDependencies` and `followChildren`. A scope is used to detect circular
dependencies within a given subgraph.
```javascript
const listDependencies = (metaInfo) => {
markAsVisited(metaInfo);
addToScope(metaInfo);
`findDeps(node)`:
1. For each `node` => `findDeps(node)`
1. For each non-materialized, non-visited, dependency, `dep of node.nonMaterialDeps` =>`findDeps(parent(dep))`
3. For each ordinary, non-visited, dependency, `dep of node.deps` => `findDeps(dep)`
4. If hasExpression(node) => `addToPlan(node)`
4. For each non-visited child, `c of node.children` => `findDeps(c)`
followDependencies(metaInfo);
emit(metaInfo);
followChildren(metaInfo);
removeFromScope(metaInfo);
}
```
The only 'interesting' thing differentiating this from standard DFS tree-walking and dependency following, is that
there is special treatment for non-materialized nodes. when a non-materialized node is encountered as a result of
following a dependency, the algorithm travels to the parent of non-materialized node until it finds a node with an
expression. This ensures that when an expression references a field that must be generated by another expression,
that the other expression is emitted first into the plan.
expression. The step labled `2` in the diagram below illustrates how the non-materialized node is followed up to its
materialized parent expression.
Assuming we call findDeps on node `/b/f` the diagram below shows the order (1,2,3,4) in which links are traversed. Since
the algorithm is DFS, _expression nodes_ are added in reverse traversal order: `['/a/c/g/i', '/b/e', '/b/f']`
Expand Down

0 comments on commit 61e3b1f

Please sign in to comment.