|
| 1 | +import { ENVIModelerEdge, ENVIModelerNode } from '@idl/types/mcp'; |
| 2 | + |
| 3 | +/** |
| 4 | + * Creates a map of all connections between nodes |
| 5 | + * |
| 6 | + * Useful for validation that parameters can be connected to |
| 7 | + * one another and for checking if aggregators need to |
| 8 | + * be used |
| 9 | + */ |
| 10 | +export function BuildConnectionMap( |
| 11 | + nodes: ENVIModelerNode[], |
| 12 | + edges: ENVIModelerEdge[], |
| 13 | +) { |
| 14 | + /** Create map for nodes */ |
| 15 | + const nodeMap: { [key: string]: ENVIModelerNode } = {}; |
| 16 | + for (let i = 0; i < nodes.length; i++) { |
| 17 | + nodeMap[nodes[i].id] = nodes[i]; |
| 18 | + } |
| 19 | + |
| 20 | + /** Build connectivity so that we can verify matching types */ |
| 21 | + const connectionMap: { |
| 22 | + // node we connect to |
| 23 | + [nodeId: string]: { |
| 24 | + // parameter we connect to and [nodeid, parameterName] |
| 25 | + [inParamName: string]: [[string, string]]; |
| 26 | + }; |
| 27 | + } = {}; |
| 28 | + |
| 29 | + /** |
| 30 | + * Validate all of our edges |
| 31 | + */ |
| 32 | + for (let i = 0; i < edges.length; i++) { |
| 33 | + const edge = edges[i]; |
| 34 | + |
| 35 | + // extract nodes |
| 36 | + const from = nodeMap[edge.from]; |
| 37 | + const to = nodeMap[edge.to]; |
| 38 | + |
| 39 | + // save to connections |
| 40 | + if (!(edge.to in connectionMap)) { |
| 41 | + connectionMap[edge.to] = {}; |
| 42 | + } |
| 43 | + |
| 44 | + // save connection map |
| 45 | + const toConnect = connectionMap[edge.to]; |
| 46 | + |
| 47 | + // validate from parameters |
| 48 | + for (let j = 0; j < edge.from_parameters.length; j++) { |
| 49 | + const fromParam = edge.from_parameters[j]; |
| 50 | + |
| 51 | + /** |
| 52 | + * Get the index to update |
| 53 | + * |
| 54 | + * If 1:1 for parameters, then they connect to each other |
| 55 | + * |
| 56 | + * If we have 2 from one node and one on the second, they all go |
| 57 | + * to the first |
| 58 | + */ |
| 59 | + const idxUpdate = |
| 60 | + edge.to_parameters.length === edge.from_parameters.length ? j : 0; |
| 61 | + |
| 62 | + const toParam = edge.to_parameters[idxUpdate]; |
| 63 | + if (!(toParam in toConnect)) { |
| 64 | + toConnect[toParam] = [[from.id, fromParam]]; |
| 65 | + } else { |
| 66 | + toConnect[toParam].push([from.id, fromParam]); |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + return connectionMap; |
| 72 | +} |
0 commit comments