Skip to content

Commit

Permalink
Fix on demand attribute and duplicate policies (#151)
Browse files Browse the repository at this point in the history
* update rules

* fix empty attachedToRef
  • Loading branch information
LaviniaStiliadou authored Apr 24, 2024
1 parent 35bf203 commit cadaf0b
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,25 @@ export default class OpenTOSCARules extends RuleProvider {
super(eventBus);
this.modeling = modeling;

function updateOnDemandStatus(elements, isOnDemand) {
elements?.forEach((element) => {
if (element?.type === consts.ON_DEMAND_POLICY) {
let host = element.host;
if (host !== null) {
host.businessObject.onDemand = isOnDemand;
}
}
});
}

eventBus.on("commandStack.elements.delete.preExecute", function (context) {
updateOnDemandStatus(context.context.elements, false);
});

eventBus.on("commandStack.elements.delete.reverted", function (context) {
updateOnDemandStatus(context.context.elements, true);
});

this.addRule("shape.create", 10000, function (context) {
let shape = context.shape;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export function synchronousPostRequest(url, type, body) {
*
* @param modeler the modeler to which the ServiceTask belongs to
* @param serviceTaskId the ID of the ServiceTask
* @returns {{}} the list of retrived policies
* @returns {{}} the list of retrieved policies
*/
export function getPolicies(modeler, serviceTaskId) {
console.log("Retrieving policies for ServiceTask with ID: ", serviceTaskId);
Expand Down Expand Up @@ -146,6 +146,7 @@ export function movePolicies(modeler, newTargetId, policies) {
onDemand: true,
});
}
policy.di.id = policy.id + "_di";
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ async function replaceByFragment(
let policies = getPolicies(modeler, task.id);
console.log("Found %i polices attached to QuantME task!", policies.length);
let attachersPlaceholder;
if (policies.length > 0) {
if (policies.length > 0 && replacementElement.$type !== "bpmn:SubProcess") {
attachersPlaceholder = modeling.createShape(
{ type: "bpmn:Task" },
{ x: 50, y: 50 },
Expand Down Expand Up @@ -367,71 +367,74 @@ async function replaceByFragment(
"Replacement was ServiceTask or QuantME task. Attaching policies..."
);
movePolicies(modeler, resultShape.id, policies);
} else {
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
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 }
);

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
);
}
// 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];
});
});
} else {
console.log(
"Type not supported for policy attachment: ",
resultShape.businessObject.$type
);
}
}
modeling.removeShape(attachersPlaceholder);
modeling.updateProperties(
elementRegistry.get(newPolicyShape.id),
propertyEntries
);
}
});
});
} else {
console.log(
"Type not supported for policy attachment: ",
resultShape.businessObject.$type
);
}

return result["success"];
Expand Down

0 comments on commit cadaf0b

Please sign in to comment.