dag.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. export class Edge {
  2. inputNode: Node;
  3. outputNode: Node;
  4. _linkTo(node: Node, direction: number) {
  5. if (direction <= 0) {
  6. node.inputEdges.push(this);
  7. }
  8. if (direction >= 0) {
  9. node.outputEdges.push(this);
  10. }
  11. node.edges.push(this);
  12. }
  13. link(inputNode: Node, outputNode: Node) {
  14. if (!inputNode) {
  15. throw Error('inputNode is required');
  16. }
  17. if (!outputNode) {
  18. throw Error('outputNode is required');
  19. }
  20. this.unlink();
  21. this.inputNode = inputNode;
  22. this.outputNode = outputNode;
  23. this._linkTo(inputNode, 1);
  24. this._linkTo(outputNode, -1);
  25. return this;
  26. }
  27. unlink() {
  28. let pos;
  29. const inode = this.inputNode;
  30. const onode = this.outputNode;
  31. if (!(inode && onode)) {
  32. return;
  33. }
  34. pos = inode.edges.indexOf(this);
  35. if (pos > -1) {
  36. inode.edges.splice(pos, 1);
  37. }
  38. pos = onode.edges.indexOf(this);
  39. if (pos > -1) {
  40. onode.edges.splice(pos, 1);
  41. }
  42. pos = inode.outputEdges.indexOf(this);
  43. if (pos > -1) {
  44. inode.outputEdges.splice(pos, 1);
  45. }
  46. pos = onode.inputEdges.indexOf(this);
  47. if (pos > -1) {
  48. onode.inputEdges.splice(pos, 1);
  49. }
  50. this.inputNode = null;
  51. this.outputNode = null;
  52. }
  53. }
  54. export class Node {
  55. name: string;
  56. edges: Edge[];
  57. inputEdges: Edge[];
  58. outputEdges: Edge[];
  59. constructor(name: string) {
  60. this.name = name;
  61. this.edges = [];
  62. this.inputEdges = [];
  63. this.outputEdges = [];
  64. }
  65. getEdgeFrom(from: string | Node): Edge {
  66. if (!from) {
  67. return null;
  68. }
  69. if (typeof from === 'object') {
  70. return this.inputEdges.find(e => e.inputNode.name === from.name);
  71. }
  72. return this.inputEdges.find(e => e.inputNode.name === from);
  73. }
  74. getEdgeTo(to: string | Node): Edge {
  75. if (!to) {
  76. return null;
  77. }
  78. if (typeof to === 'object') {
  79. return this.outputEdges.find(e => e.outputNode.name === to.name);
  80. }
  81. return this.outputEdges.find(e => e.outputNode.name === to);
  82. }
  83. getOptimizedInputEdges(): Edge[] {
  84. const toBeRemoved: any[] = [];
  85. this.inputEdges.forEach(e => {
  86. const inputEdgesNodes = e.inputNode.inputEdges.map(e => e.inputNode);
  87. inputEdgesNodes.forEach(n => {
  88. const edgeToRemove = n.getEdgeTo(this.name);
  89. if (edgeToRemove) {
  90. toBeRemoved.push(edgeToRemove);
  91. }
  92. });
  93. });
  94. return this.inputEdges.filter(e => toBeRemoved.indexOf(e) === -1);
  95. }
  96. }
  97. export class Graph {
  98. nodes: any = {};
  99. constructor() {}
  100. createNode(name: string): Node {
  101. const n = new Node(name);
  102. this.nodes[name] = n;
  103. return n;
  104. }
  105. createNodes(names: string[]): Node[] {
  106. const nodes: Node[] = [];
  107. names.forEach(name => {
  108. nodes.push(this.createNode(name));
  109. });
  110. return nodes;
  111. }
  112. link(input: string | string[] | Node | Node[], output: string | string[] | Node | Node[]): Edge[] {
  113. let inputArr = [];
  114. let outputArr = [];
  115. const inputNodes: Node[] = [];
  116. const outputNodes: Node[] = [];
  117. if (input instanceof Array) {
  118. inputArr = input;
  119. } else {
  120. inputArr = [input];
  121. }
  122. if (output instanceof Array) {
  123. outputArr = output;
  124. } else {
  125. outputArr = [output];
  126. }
  127. for (let n = 0; n < inputArr.length; n++) {
  128. const i = inputArr[n];
  129. if (typeof i === 'string') {
  130. const n = this.getNode(i);
  131. if (!n) {
  132. throw Error(`cannot link input node named ${i} since it doesn't exist in graph`);
  133. }
  134. inputNodes.push(n);
  135. } else {
  136. inputNodes.push(i);
  137. }
  138. }
  139. for (let n = 0; n < outputArr.length; n++) {
  140. const i = outputArr[n];
  141. if (typeof i === 'string') {
  142. const n = this.getNode(i);
  143. if (!n) {
  144. throw Error(`cannot link output node named ${i} since it doesn't exist in graph`);
  145. }
  146. outputNodes.push(n);
  147. } else {
  148. outputNodes.push(i);
  149. }
  150. }
  151. const edges: Edge[] = [];
  152. inputNodes.forEach(input => {
  153. outputNodes.forEach(output => {
  154. edges.push(this.createEdge().link(input, output));
  155. });
  156. });
  157. return edges;
  158. }
  159. createEdge(): Edge {
  160. return new Edge();
  161. }
  162. getNode(name: string): Node {
  163. return this.nodes[name];
  164. }
  165. }
  166. export const printGraph = (g: Graph) => {
  167. Object.keys(g.nodes).forEach(name => {
  168. const n = g.nodes[name];
  169. let outputEdges = n.outputEdges.map((e: Edge) => e.outputNode.name).join(', ');
  170. if (!outputEdges) {
  171. outputEdges = '<none>';
  172. }
  173. let inputEdges = n.inputEdges.map((e: Edge) => e.inputNode.name).join(', ');
  174. if (!inputEdges) {
  175. inputEdges = '<none>';
  176. }
  177. console.log(`${n.name}:\n - links to: ${outputEdges}\n - links from: ${inputEdges}`);
  178. });
  179. };