index.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. "use strict";
  2. /**
  3. * 路径插件,此插件支持获取绘制的图中所有的路径。
  4. * 需要指定开始节点类型。
  5. */
  6. var __read = (this && this.__read) || function (o, n) {
  7. var m = typeof Symbol === "function" && o[Symbol.iterator];
  8. if (!m) return o;
  9. var i = m.call(o), r, ar = [], e;
  10. try {
  11. while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
  12. }
  13. catch (error) { e = { error: error }; }
  14. finally {
  15. try {
  16. if (r && !r.done && (m = i["return"])) m.call(i);
  17. }
  18. finally { if (e) throw e.error; }
  19. }
  20. return ar;
  21. };
  22. var __spread = (this && this.__spread) || function () {
  23. for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i]));
  24. return ar;
  25. };
  26. Object.defineProperty(exports, "__esModule", { value: true });
  27. exports.FlowPath = void 0;
  28. var getBpmnId_1 = require("../../bpmn/getBpmnId");
  29. var FlowPath = /** @class */ (function () {
  30. function FlowPath(_a) {
  31. var _this = this;
  32. var lf = _a.lf;
  33. this.lf = lf;
  34. this.pathes = [];
  35. // 给lf添加方法
  36. lf.getPathes = function () {
  37. if (!_this.startNodeType) {
  38. throw new Error('需要预先指定开始节点类型');
  39. }
  40. return _this.getPathes();
  41. };
  42. lf.setRawPathes = function (pathes) {
  43. _this.setPathes(pathes);
  44. };
  45. lf.getRawPathes = function () { return _this.pathes; };
  46. lf.setStartNodeType = function (type) {
  47. _this.startNodeType = type;
  48. };
  49. }
  50. FlowPath.prototype.setPathes = function (pathes) {
  51. this.pathes = pathes.map(function (_a) {
  52. var routeId = _a.routeId, name = _a.name, elements = _a.elements, type = _a.type;
  53. return ({
  54. routeId: routeId,
  55. name: name,
  56. elements: elements,
  57. type: type,
  58. similarElement: null,
  59. weight: 0,
  60. });
  61. });
  62. };
  63. FlowPath.prototype.getPathes = function () {
  64. var _this = this;
  65. var graphData = this.lf.getGraphRawData();
  66. var nodesMap = new Map();
  67. var startNodeIds = [];
  68. graphData.nodes.forEach(function (node) {
  69. nodesMap.set(node.id, {
  70. id: node.id,
  71. data: node,
  72. nextNodes: [],
  73. });
  74. if (node.type === _this.startNodeType) {
  75. startNodeIds.push(node.id);
  76. }
  77. });
  78. graphData.edges.forEach(function (edge) {
  79. var node = nodesMap.get(edge.sourceNodeId);
  80. node.nextNodes.push(edge.targetNodeId);
  81. });
  82. var pathElements = [];
  83. startNodeIds.forEach(function (id) {
  84. pathElements = pathElements.concat(_this.findPathElements(nodesMap.get(id), nodesMap, []));
  85. });
  86. return this.getNewPathes(pathElements);
  87. };
  88. FlowPath.prototype.findPathElements = function (node, nodesMap, elements) {
  89. if (elements === void 0) { elements = []; }
  90. var newPathes = __spread(elements);
  91. newPathes.push(node.id);
  92. if (node.nextNodes.length === 0) {
  93. return [newPathes];
  94. }
  95. var subPath = [];
  96. for (var i = 0; i < node.nextNodes.length; i++) {
  97. var n = nodesMap.get(node.nextNodes[i]);
  98. var p = void 0;
  99. // 循环路径
  100. var idx = newPathes.indexOf(n.id);
  101. if (idx !== -1) {
  102. p = [__spread(newPathes.slice(idx), [n.id])];
  103. }
  104. else {
  105. p = this.findPathElements(n, nodesMap, __spread(newPathes));
  106. }
  107. subPath = subPath.concat(p);
  108. }
  109. return subPath;
  110. };
  111. /**
  112. * 设置路径id
  113. * 如果存在原始路径Id, 则需要比较新路径是否在原始路径中存在相似路径
  114. * 如果有,则尽量使用原始路径。
  115. * 相似路径
  116. * 1. 如果所有的节点都相同,则必定为同一路径。(包括顺序不同)
  117. * 2. 如果新路径比原来路径少了或者多了部分节点,则记录为相似路径。基于不同的差异,标记不同的权重。
  118. * 3. 基于新路径在旧路径占用权限,设置新路径Id.
  119. * 4. 如果某一条旧路径被多条新路径标记为相同的权重,则这两条新路径都使用新Id.
  120. */
  121. FlowPath.prototype.getNewPathes = function (pathElements) {
  122. var _this = this;
  123. var pathes = [];
  124. // 由于循环路径不包括开始,所以存在重复的情况,此处去重。
  125. var LoopSet = new Set();
  126. pathElements.forEach(function (elements) {
  127. var routeId = _this.getNewId('path');
  128. var name = _this.getNewId('路径');
  129. var isLoop = _this.isLoopPath(elements);
  130. var elementStr = elements.join(',');
  131. if (!LoopSet.has(elementStr)) {
  132. LoopSet.add(elementStr);
  133. pathes.push({
  134. routeId: routeId,
  135. name: name,
  136. elements: elements,
  137. type: isLoop,
  138. weight: 0,
  139. similarElement: '',
  140. });
  141. }
  142. });
  143. var oldPathes = JSON.parse(JSON.stringify(this.pathes));
  144. // 1) 找到所有路径最相似的路径, 给旧路径标记其最接近的路径
  145. pathes.forEach(function (newPath) {
  146. for (var i = 0; i < oldPathes.length; i++) {
  147. var oldPath = oldPathes[i];
  148. var weight = _this.similar2Path(__spread(newPath.elements), __spread(oldPath.elements));
  149. if (weight > newPath.weight && oldPath.weight <= weight) {
  150. newPath.weight = weight;
  151. newPath.similarElement = oldPath;
  152. if (weight === oldPath.weight && oldPath.similarElement) {
  153. // 特殊处理,如果两个路径都与同一条旧路径有相似的权重,则这两个路径的相似路径都置空
  154. oldPath.similarElement.similarElement = null;
  155. oldPath.similarElement.weight = 0;
  156. oldPath.similarElement = null;
  157. oldPath.weight = 0;
  158. }
  159. else {
  160. oldPath.similarElement = newPath;
  161. oldPath.weight = weight;
  162. }
  163. }
  164. }
  165. });
  166. // 2) 再次遍历所有路径,如果该路径的相似路径对应的相似路径是自己,那么就设置该路径id和name为其相似路径。
  167. pathes.forEach(function (newPath) {
  168. if (newPath.similarElement && newPath.similarElement.similarElement === newPath) {
  169. newPath.routeId = newPath.similarElement.routeId;
  170. newPath.name = newPath.similarElement.name;
  171. }
  172. delete newPath.similarElement;
  173. delete newPath.weight;
  174. });
  175. this.setPathes(pathes);
  176. return pathes;
  177. };
  178. FlowPath.prototype.similar2Path = function (x, y) {
  179. var z = 0;
  180. var s = x.length + y.length;
  181. x.sort();
  182. y.sort();
  183. var a = x.shift();
  184. var b = y.shift();
  185. while (a !== undefined && b !== undefined) {
  186. if (a === b) {
  187. z++;
  188. a = x.shift();
  189. b = y.shift();
  190. }
  191. else if (a < b) {
  192. a = x.shift();
  193. }
  194. else if (a > b) {
  195. b = y.shift();
  196. }
  197. }
  198. return (z / s) * 200;
  199. };
  200. FlowPath.prototype.getNewId = function (prefix) {
  201. return prefix + "_" + getBpmnId_1.getBpmnId();
  202. };
  203. /**
  204. * 判断是否为循环路径
  205. * 由于前面进行了特殊处理,将循环部分单独提出来作为路径
  206. * 所有循环路径必定开始节点等于结束节点。
  207. */
  208. FlowPath.prototype.isLoopPath = function (elements) {
  209. var length = elements.length;
  210. return elements.indexOf(elements[length - 1]) !== length - 1 ? 1 : 0;
  211. };
  212. FlowPath.pluginName = 'flowPath';
  213. return FlowPath;
  214. }());
  215. exports.FlowPath = FlowPath;