1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.replaceGenerics = replaceGenerics;
|
7 | exports.addTypeConversion = addTypeConversion;
|
8 |
|
9 | var _graphtools = require('@buggyorg/graphtools');
|
10 |
|
11 | var graphtools = _interopRequireWildcard(_graphtools);
|
12 |
|
13 | function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
|
14 |
|
15 | var grlib = require('graphlib');
|
16 |
|
17 |
|
18 |
|
19 | function isInPort(node) {
|
20 | return node['nodeType'] === 'inPort' && node['isTrans'] === undefined && !node['isTrans'];
|
21 | }
|
22 |
|
23 | function isOutPort(node) {
|
24 | return node['nodeType'] === 'outPort' && node['isTrans'] === undefined && !node['isTrans'];
|
25 | }
|
26 |
|
27 | function cloneGraph(graph) {
|
28 | return grlib.json.read(grlib.json.write(graph));
|
29 | }
|
30 |
|
31 | function processOfEdge(edgeName) {
|
32 | return edgeName.split('_').slice(0, -2).join('_');
|
33 | }
|
34 |
|
35 | function portOfEdge(graph, nodeName) {
|
36 |
|
37 | return graph.node(nodeName)['portName'];
|
38 | }
|
39 |
|
40 | function getOutputs(graph, nodeId) {
|
41 | return graph.node(nodeId)['outputPorts'];
|
42 | }
|
43 |
|
44 | function getInputs(graph, nodeId) {
|
45 | return graph.node(nodeId)['inputPorts'];
|
46 | }
|
47 |
|
48 | function genericInputs(graph, node) {
|
49 | var genericInputs = [];
|
50 | if (graph.node(node)['inputPorts'] !== undefined) {
|
51 | var inputPorts = graph.node(node)['inputPorts'];
|
52 | var inputNames = Object.keys(inputPorts);
|
53 | for (var i = 0; i < inputNames.length; i++) {
|
54 | if (inputPorts[inputNames[i]] === 'generic') {
|
55 | genericInputs = genericInputs.concat([inputNames[i]]);
|
56 | }
|
57 | }
|
58 | }
|
59 | return genericInputs;
|
60 | }
|
61 |
|
62 | function genericOutputs(graph, node) {
|
63 | var genericOutputs = [];
|
64 | if (graph.node(node)['outputPorts'] !== undefined) {
|
65 | var outputPorts = graph.node(node)['outputPorts'];
|
66 | var outputNames = Object.keys(outputPorts);
|
67 | for (var i = 0; i < outputNames.length; i++) {
|
68 | if (outputPorts[outputNames[i]] === 'generic') {
|
69 | genericOutputs = genericOutputs.concat([outputNames[i]]);
|
70 | }
|
71 | }
|
72 | }
|
73 | return genericOutputs;
|
74 | }
|
75 |
|
76 | function replaceGenerics(processGraph) {
|
77 | var nodes = processGraph.nodes();
|
78 | for (var j = 0; j < nodes.length; j++) {
|
79 | var paths = graphtools.walkPort.walkBack(processGraph, nodes[j], genericInputs);
|
80 | var path = paths[0];
|
81 | if (path.length >= 2) {
|
82 | var genericInput = genericInputs(processGraph, path[1]);
|
83 | var outputs = graphtools['walkPort'].predecessorPort(processGraph, path[1], genericInput[0]);
|
84 | var type = processGraph.node(path[0])['outputPorts'][outputs[0]];
|
85 | for (var k = 1; k < path.length; k++) {
|
86 | genericInput = genericInputs(processGraph, path[k]);
|
87 | for (var l = 0; l < genericInput.length; l++) {
|
88 | processGraph.node(path[k])['inputPorts'][genericInput[l]] = type;
|
89 | }
|
90 | var genericOutput = genericOutputs(processGraph, path[k]);
|
91 | for (var m = 0; m < genericOutput.length; m++) {
|
92 | processGraph.node(path[k])['outputPorts'][genericOutput[m]] = type;
|
93 | }
|
94 | }
|
95 | }
|
96 | }
|
97 | return processGraph;
|
98 | }
|
99 |
|
100 | function addTypeConversion(processGraph, convertGraph) {
|
101 | var newProcessGraph = cloneGraph(processGraph);
|
102 |
|
103 | var _iteratorNormalCompletion = true;
|
104 | var _didIteratorError = false;
|
105 | var _iteratorError = undefined;
|
106 |
|
107 | try {
|
108 | for (var _iterator = newProcessGraph.edges()[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
|
109 | var edge = _step.value;
|
110 |
|
111 |
|
112 | var v = newProcessGraph.node(edge.v);
|
113 | var w = newProcessGraph.node(edge.w);
|
114 | if (isOutPort(v) && isInPort(w)) {
|
115 | var labelIn = edge.v;
|
116 | var labelOut = edge.w;
|
117 |
|
118 | var processV = processOfEdge(labelIn);
|
119 | var processW = processOfEdge(labelOut);
|
120 | var portNameV = portOfEdge(newProcessGraph, labelIn);
|
121 | var portNameW = portOfEdge(newProcessGraph, labelOut);
|
122 | var typeV = getOutputs(newProcessGraph, processV)[portNameV];
|
123 | var typeW = getInputs(newProcessGraph, processW)[portNameW];
|
124 |
|
125 |
|
126 | if (typeV !== typeW) {
|
127 | newProcessGraph.removeEdge(labelIn, labelOut);
|
128 | var parentV = processGraph.parent(labelIn);
|
129 |
|
130 | var dijkstra = grlib.alg.dijkstra(convertGraph, typeV);
|
131 | var way = [typeW];
|
132 | while (way[way.length - 1] !== typeV) {
|
133 | way.push(dijkstra[way[way.length - 1]].predecessor);
|
134 | }
|
135 | for (var k = way.length - 1; k >= 1; k--) {
|
136 | var number = way.length - k;
|
137 | var id = labelIn + ':' + labelOut + '_' + number;
|
138 |
|
139 |
|
140 | var meta = 'translator/' + way[k] + '_to_' + way[k - 1];
|
141 | newProcessGraph.setNode(id, { 'nodeType': 'process', 'id': meta, 'atomic': 'true' });
|
142 | newProcessGraph.setNode(id + '_PORT_in', { 'nodeType': 'inPort', 'portName': 'input', 'isTrans': true });
|
143 | newProcessGraph.setNode(id + '_PORT_out', { 'nodeType': 'outPort', 'portName': 'output', 'isTrans': true });
|
144 |
|
145 | newProcessGraph.setEdge(id + '_PORT_in', id);
|
146 | newProcessGraph.setEdge(id, id + '_PORT_out');
|
147 |
|
148 | newProcessGraph.setParent(id, parentV);
|
149 | newProcessGraph.setParent(id + '_PORT_in', parentV);
|
150 | newProcessGraph.setParent(id + '_PORT_out', parentV);
|
151 |
|
152 |
|
153 | if (k === way.length - 1) {
|
154 | newProcessGraph.setEdge(labelIn, id + '_PORT_in');
|
155 | } else {
|
156 | newProcessGraph.setEdge(labelIn + ':' + labelOut + '_' + (number - 1) + '_PORT_out', id + '_PORT_in');
|
157 | }
|
158 |
|
159 | if (k === 1) {
|
160 | newProcessGraph.setEdge(id + '_PORT_out', labelOut);
|
161 | }
|
162 | }
|
163 | }
|
164 | }
|
165 | }
|
166 | } catch (err) {
|
167 | _didIteratorError = true;
|
168 | _iteratorError = err;
|
169 | } finally {
|
170 | try {
|
171 | if (!_iteratorNormalCompletion && _iterator.return) {
|
172 | _iterator.return();
|
173 | }
|
174 | } finally {
|
175 | if (_didIteratorError) {
|
176 | throw _iteratorError;
|
177 | }
|
178 | }
|
179 | }
|
180 |
|
181 | return newProcessGraph;
|
182 | } |
\ | No newline at end of file |