Skip to content

Commit 28e80cf

Browse files
committed
fix: fixes, handle loop workflows when ordering
1 parent 236de8d commit 28e80cf

File tree

4 files changed

+43
-7
lines changed

4 files changed

+43
-7
lines changed

src/js/app.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ export let app;
150150
"$setInputValueInElement",
151151
"$setTextValueInElement",
152152
"$addLink",
153-
"deleteLink",
153+
"$deleteLink",
154154
"$moveSelectedElements",
155155
"$resizeElement",
156156
"$openWorkflow",

src/js/constants/messages.js

+3
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,7 @@ export const MESSAGE_PASTE_ERROR = "Nothing was previously copied";
55
export const MESSAGE_SAVE_SUCCESS = "Workflow saved!";
66
export const MESSAGE_SAVE_ERROR = "The workflow couldn't be saved";
77

8+
export const MESSAGE_LOOP =
9+
"There exist a loop in the workflow, it cannot be installed";
10+
811
export const NO_PARAMETER_TEXT = "No parameter";

src/js/elements/orderElement.js

+32-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { app } from "../app";
22
import * as joint from "jointjs";
3+
import { fireAlert } from "../utils/alerts";
4+
import { MESSAGE_LOOP } from "../constants/messages";
35

46
/**
57
* order graph elements from source to target
@@ -47,29 +49,56 @@ export const orderGraph = graph => {
4749

4850
/**
4951
* get elements in order, from source to target
52+
* if a loop is found, it still orders the element
53+
* and return isLoop set to true
5054
*/
5155
export const getOrderedElements = () => {
52-
const order = [];
5356
const { graph } = app;
5457

58+
const order = [];
59+
let isLoop = false;
60+
5561
const addedBoxId = [];
5662

5763
let subgraph = new joint.dia.Graph();
5864
subgraph.addCells(graph.getCells());
5965

6066
do {
61-
const sources = subgraph.getSources();
67+
let sources = subgraph.getSources();
68+
69+
// if no root is found, but there are still elements
70+
// it means there is a loop
71+
if (!sources.length) {
72+
sources = [subgraph.getElements()[0]];
73+
fireAlert("danger", MESSAGE_LOOP);
74+
isLoop = true;
75+
}
76+
6277
for (const source of sources) {
6378
order.push(source);
6479
addedBoxId.push(source.attributes.boxId);
6580
}
81+
6682
let remainingEls = subgraph
6783
.getElements()
6884
.filter(el => !sources.includes(el));
6985
let remainingCells = subgraph.getSubgraph(remainingEls);
86+
87+
// if already added elements are in the graph again, there is a loop
88+
if (
89+
remainingCells.filter(el => addedBoxId.includes(el.attributes.boxId))
90+
.length
91+
) {
92+
fireAlert("danger", MESSAGE_LOOP);
93+
isLoop = true;
94+
}
95+
96+
remainingCells = remainingCells.filter(
97+
el => !addedBoxId.includes(el.attributes.boxId)
98+
);
7099
subgraph = new joint.dia.Graph();
71100
subgraph.addCells(remainingCells);
72101
} while (subgraph.getElements().length);
73102

74-
return order;
103+
return { elements: order, isLoop };
75104
};

src/js/workflows/saveWorkflow.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ import { getOrderedElements } from "../elements/orderElement";
1313
* @param {boolean} installation if true, send an installation request
1414
*/
1515
export const saveWorkflow = async (jsonGraph, installation = false) => {
16-
const orderedElements = getOrderedElements().filter(
16+
const { elements, isLoop } = getOrderedElements();
17+
18+
const orderedElements = elements.filter(
1719
({ attributes: { category } }) => category === CATEGORY_SERVICE
1820
);
1921

@@ -43,7 +45,8 @@ export const saveWorkflow = async (jsonGraph, installation = false) => {
4345
Value
4446
});
4547
_inputs.parameters[paramName] = DivaServices.parseParameterValue(
46-
Value
48+
Value,
49+
paramType
4750
);
4851
}
4952
const validity = Validation.checkValue(Value, paramType, values);
@@ -158,7 +161,8 @@ export const saveWorkflow = async (jsonGraph, installation = false) => {
158161
// set found errors in log
159162
app.$refs.log.setLogMessages(log);
160163

161-
await API.saveWorkflow(xml, app.workflowId, installation);
164+
const isInstallation = installation && !isLoop;
165+
await API.saveWorkflow(xml, app.workflowId, isInstallation);
162166

163167
return request;
164168
};

0 commit comments

Comments
 (0)