{"ast":null,"code":"function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }\n\nfunction _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }\n\nfunction _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }\n\nimport { NodeType } from './types';\nconst OP_NODE_PREFIX = 'opNode_';\n/* misc */\n\nconst genNodeUID = (type, id) => {\n  switch (type) {\n    case NodeType.Op:\n      return `${OP_NODE_PREFIX}${id}`;\n\n    case NodeType.Input:\n    case NodeType.Output:\n      return `${id}`;\n  }\n};\n\nconst assignNodeUID = (type, node) => {\n  const process = (node, i) => {\n    const uid = genNodeUID(type, i);\n    return _objectSpread({}, node, {\n      uid\n    });\n  };\n\n  return node.map(process);\n};\n\nconst createNodeFinder = graph => {\n  if (!graph) {\n    return () => undefined;\n  }\n\n  const reverseInputIdx = graph.input.reduce((memo, input, i) => {\n    memo[genNodeUID(NodeType.Input, input.name)] = i;\n    return memo;\n  }, {});\n  const reverseOutputIdx = graph.output.reduce((memo, input, i) => {\n    memo[genNodeUID(NodeType.Output, input.name)] = i;\n    return memo;\n  }, {});\n  return (type, nodeUID) => {\n    switch (type) {\n      case NodeType.Input:\n        {\n          const idx = reverseInputIdx[nodeUID];\n          return idx == undefined ? undefined : graph.input[idx];\n        }\n\n      case NodeType.Output:\n        {\n          const idx = reverseOutputIdx[nodeUID];\n          return idx == undefined ? undefined : graph.output[idx];\n        }\n\n      case NodeType.Op:\n        {\n          const idx = +nodeUID.replace(OP_NODE_PREFIX, '');\n          return graph.node[idx];\n        }\n    }\n  };\n};\n\nconst relationPush = (nodeRelationMapping, nodeUID, key, value) => {\n  const leaf = nodeRelationMapping[nodeUID] || {\n    input: [],\n    output: []\n  };\n  leaf[key].push(value);\n  nodeRelationMapping[nodeUID] = leaf;\n};\n\nconst traverseRelation = (nodeMapping, process) => {\n  for (const [nodeUID, relations] of Object.entries(nodeMapping)) {\n    const {\n      input,\n      output\n    } = relations;\n    input.forEach(inputTo => {\n      output.forEach(outputTo => {\n        process(nodeUID, inputTo, outputTo);\n      });\n    });\n  }\n};\n\nconst buildNodeRelationMapping = nodeList => {\n  return nodeList.reduce((memo, node) => {\n    const uid = node.uid; // reverse\n\n    (node.output || []).forEach(v => relationPush(memo, v, 'input', uid));\n    (node.input || []).forEach(v => relationPush(memo, v, 'output', uid));\n    return memo;\n  }, {});\n};\n\nconst expandRelations = nodeMapping => {\n  const briefLayer = {\n    nodes: [],\n    edges: []\n  }; // a tmp node the middle man between input & output\n\n  const detailLayer = {\n    nodes: [],\n    edges: []\n  };\n  traverseRelation(nodeMapping, (bridge, inputTo, outputTo) => {\n    detailLayer.nodes.push({\n      key: bridge,\n      label: bridge,\n      shape: 'diamond',\n      class: 'output',\n      type: NodeType.Output\n    });\n    detailLayer.edges.push([inputTo, bridge]);\n    detailLayer.edges.push([bridge, outputTo]);\n    briefLayer.edges.push([inputTo, outputTo]);\n  });\n  return {\n    briefLayer,\n    detailLayer\n  };\n};\n\nconst extractInputLayer = (nodeRelationMapping, findNode) => {\n  const nodes = [];\n  const edges = [];\n\n  for (const [nodeUID, relations] of Object.entries(nodeRelationMapping)) {\n    if (relations.input.length !== 0) {\n      continue;\n    }\n\n    const sepIdx = nodeUID.indexOf('@');\n    const inputNodeUID = sepIdx > 0 ? nodeUID.slice(0, sepIdx) : nodeUID;\n    const inputNode = findNode(NodeType.Input, inputNodeUID);\n    nodes.push({\n      key: inputNodeUID,\n      type: NodeType.Input,\n      label: `\nid: ${inputNode.name}\ntype: ${inputNode.data_type}\ndims: ${inputNode.shape.join(' × ')}\n`,\n      shape: 'rect',\n      class: 'input'\n    });\n    relations.output.forEach(o => edges.push([inputNodeUID, o]));\n  }\n\n  return {\n    nodes,\n    edges\n  };\n};\n\nconst extractOutputLayer = nodeRelationMapping => {\n  const nodes = [];\n  const edges = [];\n\n  for (const [nodeUID, relations] of Object.entries(nodeRelationMapping)) {\n    if (relations.output.length !== 0) {\n      continue;\n    }\n\n    nodes.push({\n      key: nodeUID,\n      type: NodeType.Output,\n      label: nodeUID,\n      shape: 'diamond',\n      class: 'output'\n    });\n\n    for (const inputNode of relations.input) {\n      edges.push([nodeUID, inputNode]);\n    }\n  }\n\n  return {\n    nodes,\n    edges\n  };\n};\n\nexport const collectDagFacts = graph => {\n  const findNode = createNodeFinder(graph);\n  const nodeList = assignNodeUID(NodeType.Op, graph ? graph.node : []);\n  const nodeRelationMapping = buildNodeRelationMapping(nodeList);\n  const inputLayer = extractInputLayer(nodeRelationMapping, findNode);\n  const outputLayer = extractOutputLayer(nodeRelationMapping);\n  const backboneNodes = nodeList.map(n => ({\n    key: n.uid,\n    type: NodeType.Op,\n    label: n.opType,\n    shape: 'rect',\n    class: 'operator'\n  }));\n  const {\n    briefLayer: bl,\n    detailLayer: dl\n  } = expandRelations(nodeRelationMapping);\n  const briefLayer = {\n    nodes: backboneNodes,\n    edges: bl.edges\n  };\n  const detailLayer = {\n    nodes: briefLayer.nodes.concat(dl.nodes),\n    edges: dl.edges\n  };\n  return {\n    briefLayer,\n    detailLayer,\n    inputLayer,\n    outputLayer,\n    findNode\n  };\n};","map":null,"metadata":{},"sourceType":"module"}