forked from PlanQK/workflow-modeler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuantMETransformator.js
547 lines (498 loc) · 17.2 KB
/
QuantMETransformator.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
/**
* Copyright (c) 2024 Institute of Architecture of Application Systems -
* University of Stuttgart
*
* This program and the accompanying materials are made available under the
* terms the Apache Software License 2.0
* which is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* SPDX-License-Identifier: Apache-2.0
*/
import { layout } from "./layouter/Layouter";
import { matchesQRM } from "./QuantMEMatcher";
import { addQuantMEInputParameters } from "./InputOutputHandler";
import * as constants from "../Constants";
import { replaceHardwareSelectionSubprocess } from "./hardware-selection/QuantMEHardwareSelectionHandler";
import { replaceCuttingSubprocess } from "./circuit-cutting/QuantMECuttingHandler";
import { insertShape } from "../../../editor/util/TransformationUtilities";
import {
createTempModelerFromXml,
getModeler,
} from "../../../editor/ModelerHandler";
import {
getCamundaInputOutput,
getDefinitionsFromXml,
getRootProcess,
getSingleFlowElement,
} from "../../../editor/util/ModellingUtilities";
import { getXml } from "../../../editor/util/IoUtilities";
import { getPolicies, movePolicies } from "../../opentosca/utilities/Utilities";
import { handleQrmUpload, isQuantMETask } from "../utilities/Utilities";
import { getQProvEndpoint } from "../framework-config/config-manager";
import { getCamundaEndpoint } from "../../../editor/config/EditorConfigManager";
import { OpenTOSCAProps } from "../../opentosca/modeling/properties-provider/ServiceTaskPropertiesProvider";
import * as openToscaConsts from "../../opentosca/Constants";
import { findSplittingCandidates } from "../ui/splitting/CandidateDetector";
import { invokeScriptSplitter } from "../ui/splitting/splitter/ScriptSplitterHandler";
import { getQRMs } from "../qrm-manager";
import { rewriteWorkflow } from "../ui/splitting/WorkflowRewriter";
/**
* Initiate the replacement process for the QuantME tasks that are contained in the current process model
*
* @param xml the BPMN diagram in XML format
* @param currentQRMs the set of currently in the framework available QRMs
* @param endpointConfig endpoints of the services required for the dynamic hardware selection
*/
export async function startQuantmeReplacementProcess(
xml,
currentQRMs,
endpointConfig
) {
let startTimeStepG = Date.now();
let modeler = await createTempModelerFromXml(xml);
let modeling = modeler.get("modeling");
let elementRegistry = modeler.get("elementRegistry");
let moddle = modeler.get("moddle");
// get root element of the current diagram
const definitions = modeler.getDefinitions();
const rootElement = getRootProcess(definitions);
console.log(rootElement);
if (typeof rootElement === "undefined") {
console.log("Unable to retrieve root process element from definitions!");
return {
status: "failed",
cause: "Unable to retrieve root process element from definitions!",
};
}
// get all QuantME modeling constructs from the process
let replacementConstructs = getQuantMETasks(rootElement, elementRegistry);
console.log(
"Process contains " +
replacementConstructs.length +
" QuantME modeling constructs to replace..."
);
if (!replacementConstructs || !replacementConstructs.length) {
return { status: "transformed", xml: xml };
}
addQProvEndpoint(rootElement, elementRegistry, modeling, moddle);
// check for available replacement models for all QuantME modeling constructs
for (let replacementConstruct of replacementConstructs) {
console.log(replacementConstruct);
if (
constants.QUANTME_DATA_OBJECTS.includes(
replacementConstruct.task.$type
) ||
replacementConstruct.task.$type ===
constants.QUANTUM_HARDWARE_SELECTION_SUBPROCESS
) {
console.log("Found QuantME object of type:");
console.log(
"Hardware Selection Subprocesses and QuantME DataObjects needs no QRM. Skipping search..."
);
continue;
}
// abort transformation if at least one task can not be replaced
replacementConstruct.qrm = await getMatchingQRM(
replacementConstruct.task,
currentQRMs
);
if (!replacementConstruct.qrm) {
console.log(
"Unable to replace task with id %s. Aborting transformation!",
replacementConstruct.task.id
);
return {
status: "failed",
cause:
"Unable to replace task with id '" +
replacementConstruct.task.id +
"' by suited QRM!",
};
}
}
// first replace cutting subprocesses and insert tasks
for (let replacementConstruct of replacementConstructs) {
let replacementSuccess = false;
if (
replacementConstruct.task.$type === constants.CIRCUIT_CUTTING_SUBPROCESS
) {
replacementSuccess = await replaceCuttingSubprocess(
replacementConstruct.task,
replacementConstruct.parent,
replacementConstruct.qrm.replacement,
modeler,
definitions,
endpointConfig.transformationFrameworkEndpoint,
endpointConfig.camundaEndpoint
);
console.log("Successfully replaced Cutting Subprocess");
if (!replacementSuccess) {
console.log(
"Replacement of QuantME modeling construct with Id " +
replacementConstruct.task.id +
" failed. Aborting process!"
);
return {
status: "failed",
cause:
"Replacement of QuantME modeling construct with Id " +
replacementConstruct.task.id +
" failed. Aborting process!",
};
}
}
}
// remove already replaced circuit cutting subprocesses from replacement list
replacementConstructs = replacementConstructs.filter(
(construct) => construct.task.$type !== constants.CIRCUIT_CUTTING_SUBPROCESS
);
for (let replacementConstruct of replacementConstructs) {
let replacementSuccess = false;
if (
replacementConstruct.task.$type ===
constants.QUANTUM_HARDWARE_SELECTION_SUBPROCESS
) {
console.log("Transforming QuantumHardwareSelectionSubprocess...");
replacementSuccess = await replaceHardwareSelectionSubprocess(
replacementConstruct.task,
replacementConstruct.parent,
modeler,
endpointConfig.nisqAnalyzerEndpoint,
endpointConfig.transformationFrameworkEndpoint,
endpointConfig.camundaEndpoint
);
} else if (
constants.QUANTME_DATA_OBJECTS.includes(replacementConstruct.task.$type)
) {
console.log("Transforming QuantME Data Objects...");
// for now we delete data objects
modeling.removeShape(elementRegistry.get(replacementConstruct.task.id));
replacementSuccess = true;
} else {
console.log(
"Replacing task with id %s by using QRM: ",
replacementConstruct.task.id,
replacementConstruct.qrm
);
replacementSuccess = await replaceByFragment(
definitions,
replacementConstruct.task,
replacementConstruct.parent,
replacementConstruct.qrm.replacement,
modeler
);
}
if (!replacementSuccess) {
console.log(
"Replacement of QuantME modeling construct with Id " +
replacementConstruct.task.id +
" failed. Aborting process!"
);
return {
status: "failed",
cause:
"Replacement of QuantME modeling construct with Id " +
replacementConstruct.task.id +
" failed. Aborting process!",
};
}
}
removeDiagramElements(modeler);
console.log("Searching for splitting candidates after transformation");
const splittingCandidates = await findSplittingCandidates(modeler);
if (splittingCandidates.length > 0) {
console.log(
"Found {} splitting candidates after transformation",
splittingCandidates.length
);
console.log(splittingCandidates);
let qrmActivities = [];
for (let i = 0; i < splittingCandidates.length; i++) {
let programGenerationResult = await invokeScriptSplitter(
splittingCandidates[i],
modeler.config,
getQRMs()
);
let rewritingResult = await rewriteWorkflow(
modeler,
splittingCandidates[i],
programGenerationResult.programsBlob,
programGenerationResult.workflowBlob
);
qrmActivities = qrmActivities.concat(rewritingResult.qrms);
}
await handleQrmUpload(qrmActivities, getModeler());
}
// layout diagram after successful transformation
layout(modeling, elementRegistry, rootElement);
let updated_xml = await getXml(modeler);
console.log(updated_xml);
const elapsedTimeStepG = Date.now() - startTimeStepG;
console.log(`Time taken for step G: ${elapsedTimeStepG}ms`);
return { status: "transformed", xml: updated_xml };
}
/**
* Remove empty plane elements from false collapsed subprocesses.
*
* @param modeler
*/
function removeDiagramElements(modeler) {
let definitions = modeler.getDefinitions();
let elementsToRemove = [];
let diagrams = definitions.diagrams;
for (let i = 0; i < diagrams.length; i++) {
if (diagrams[i].plane.planeElement === undefined) {
elementsToRemove.push(diagrams[i]);
}
}
// Remove empty diagram elements from the diagrams array
for (let i = 0; i < elementsToRemove.length; i++) {
let indexToRemove = diagrams.indexOf(elementsToRemove[i]);
if (indexToRemove !== -1) {
diagrams.splice(indexToRemove, 1);
}
}
}
/**
* Get QuantME tasks from process
*/
export function getQuantMETasks(process, elementRegistry) {
// retrieve parent object for later replacement
const processBo = elementRegistry.get(process.id);
const quantmeTasks = [];
const flowElements = process.flowElements;
if (flowElements !== undefined) {
for (let i = 0; i < flowElements.length; i++) {
let flowElement = flowElements[i];
if (flowElement.$type && flowElement.$type.startsWith("quantme:")) {
quantmeTasks.push({ task: flowElement, parent: processBo });
}
// recursively retrieve QuantME tasks if subprocess is found
if (
flowElement.$type &&
(flowElement.$type === "bpmn:SubProcess" ||
flowElement.$type === constants.CIRCUIT_CUTTING_SUBPROCESS)
) {
Array.prototype.push.apply(
quantmeTasks,
getQuantMETasks(flowElement, elementRegistry)
);
}
}
return quantmeTasks;
} else {
return quantmeTasks;
}
}
/**
* Search for a matching QRM for the given task
*/
async function getMatchingQRM(task, currentQRMs) {
console.log("Number of available QRMs: ", currentQRMs.length);
console.log(task);
console.log(currentQRMs);
// check if a QRM can be found with the same id
for (let i = 0; i < currentQRMs.length; i++) {
if (await matchesQRM(currentQRMs[i], task, true)) {
return currentQRMs[i];
}
}
for (let i = 0; i < currentQRMs.length; i++) {
if (await matchesQRM(currentQRMs[i], task, false)) {
return currentQRMs[i];
}
}
return undefined;
}
/**
* Replace the given task by the content of the replacement fragment
*/
async function replaceByFragment(
definitions,
task,
parent,
replacement,
modeler
) {
let bpmnFactory = modeler.get("bpmnFactory");
let elementRegistry = modeler.get("elementRegistry");
let modeling = modeler.get("modeling");
let taskToReplace = elementRegistry.get(task.id);
console.log(
"Replacing the following task using a replacement fragment: ",
taskToReplace
);
if (!replacement) {
console.log("Replacement fragment is undefined. Aborting replacement!");
return false;
}
// get the root process of the replacement fragment
let replacementProcess = getRootProcess(
await getDefinitionsFromXml(replacement)
);
let replacementElement = getSingleFlowElement(replacementProcess);
if (replacementElement === null || replacementElement === undefined) {
console.log(
"Unable to retrieve QuantME task from replacement fragment: ",
replacement
);
return false;
}
// extract policies attached to QuantME tasks
let policies = getPolicies(modeler, task.id);
console.log("Found %i polices attached to QuantME task!", policies.length);
let attachersPlaceholder;
if (policies.length > 0 && replacementElement.$type !== "bpmn:SubProcess") {
attachersPlaceholder = modeling.createShape(
{ type: "bpmn:Task" },
{ x: 50, y: 50 },
parent,
{}
);
// attach policies to the placeholder
movePolicies(modeler, attachersPlaceholder.id, policies);
}
console.log("Replacement element: ", replacementElement);
let result = insertShape(
definitions,
parent,
replacementElement,
{},
true,
modeler,
task
);
let resultShape = result.element;
console.log("Inserted shape: ", resultShape);
// add all attributes of the replaced QuantME task to the input parameters of the replacement fragment
let inputOutputExtension = getCamundaInputOutput(
result["element"].businessObject,
bpmnFactory
);
addQuantMEInputParameters(task, inputOutputExtension, bpmnFactory);
if (attachersPlaceholder) {
// attach policies to the newly created shape if it's a single task
if (
resultShape.businessObject.$type === "bpmn:ServiceTask" ||
isQuantMETask(resultShape.businessObject)
) {
console.log(
"Replacement was ServiceTask or QuantME task. Attaching policies..."
);
movePolicies(modeler, resultShape.id, policies);
}
let attachers = attachersPlaceholder.attachers;
// if all policies are moved to the new target
if (attachers.length === 0) {
modeling.removeShape(attachersPlaceholder);
}
}
if (resultShape.businessObject.$type === "bpmn:SubProcess") {
console.log(
"Attaching policies within subprocess: ",
resultShape.businessObject
);
// get flow elements to check if they support policy attachment
let flowElements = resultShape.businessObject.flowElements;
console.log("Subprocess contains %i flow elements...", flowElements.length);
flowElements = flowElements.filter(
(flowElement) =>
(flowElement.$type === "bpmn:ServiceTask" &&
flowElement.deploymentModelUrl) ||
flowElement.$type.startsWith("quantme:")
);
console.log(
"Found %i ServiceTasks or QuantME tasks...",
flowElements.length
);
flowElements.forEach((flowElement) => {
let task = elementRegistry.get(flowElement.id);
console.log("Adding policies to task: ", task);
policies.forEach((policy) => {
console.log("Adding policy : ", policy);
if (policy.type === openToscaConsts.ON_DEMAND_POLICY) {
task.businessObject[openToscaConsts.ON_DEMAND] = "true";
}
let newPolicyShape = modeling.createShape(
{ type: policy.type },
{ x: 50, y: 50 },
task,
{ attach: true }
);
// extract the properties of for the specific policy type
let properties = OpenTOSCAProps(newPolicyShape);
if (properties !== undefined) {
let propertyEntries = {};
properties.forEach((propertyEntry) => {
let entryId = propertyEntry.id;
propertyEntries[entryId] = policy.businessObject[entryId];
});
modeling.updateProperties(
elementRegistry.get(newPolicyShape.id),
propertyEntries
);
}
});
});
} else {
console.log(
"Type not supported for policy attachment: ",
resultShape.businessObject.$type
);
}
return result["success"];
}
/**
* Add QProv endpoint to start event form.
*
* @param rootElement
* @param elementRegistry
* @param modeling
* @param moddle
*/
function addQProvEndpoint(rootElement, elementRegistry, modeling, moddle) {
if (rootElement.flowElements !== undefined) {
for (let flowElement of rootElement.flowElements) {
if (flowElement.$type === "bpmn:StartEvent") {
let startEvent = elementRegistry.get(flowElement.id);
let extensionElements =
startEvent.businessObject.get("extensionElements");
if (!extensionElements) {
extensionElements = moddle.create("bpmn:ExtensionElements");
}
let form = extensionElements.get("values").filter(function (elem) {
return elem.$type === "camunda:FormData";
})[0];
if (!form) {
form = moddle.create("camunda:FormData");
}
// remove qprov endpoint and camunda endpoint if they exist
const updatedFields = form
.get("fields")
.filter(
(element) =>
element.id !== "CAMUNDA_ENDPOINT" &&
element.id !== "QPROV_ENDPOINT"
);
form.fields = updatedFields;
const formFieldCamundaEndpoint = moddle.create("camunda:FormField", {
defaultValue: getCamundaEndpoint(),
id: "CAMUNDA_ENDPOINT",
label: "Camunda Endpoint",
type: "string",
});
form.get("fields").push(formFieldCamundaEndpoint);
const formFieldQProvEndpoint = moddle.create("camunda:FormField", {
defaultValue: getQProvEndpoint(),
id: "QPROV_ENDPOINT",
label: "QProv Endpoint",
type: "string",
});
form.get("fields").push(formFieldQProvEndpoint);
modeling.updateProperties(startEvent, {
extensionElements: extensionElements,
});
}
}
}
}