In the end a longer example to show the usage of the Scripting API and most of the available globals. The script searches for system elements that have an ASIL set and clears it by setting it back to "NONE". The script is selection sensitive and requires an active selected object as scope.
// lets check the selection first
if (!selection || selection.length != 1) {
throw "No or invalid selection - please select a single object!";
}
/**
* Filter function - only accepts elements that have an ASIL set.
* @returns {Boolean}
*/
function hasASIL(element) {
// has safety information sub-element?
if (element.safetyInformation == undefined) {
return false;
}
// has ASIL set to something?
if (element.safetyInformation.asil == undefined) {
return false;
}
return "NONE" != element.safetyInformation.asil;
};
// find all elements that have an ASIL set and clear it
var elements = Global.getFinder(selection[0]).findByType(Metamodel.sysml.SysMLElement).filter(hasASIL);
if (elements.isEmpty()) {
console.error("No elements to proceed - bail out");
throw "No elements found with an ASIL set.";
}
console.log("Found {0} elements that have an ASIL set", elements.size());
// directly iterate over the finder result
elements.forEach(function clearASIL(element) {
console.log("ASIL of element ''{0}'' is {1} but will be reset to NONE", element.name, element.safetyInformation.asil);
element.safetyInformation.asil = "NONE";
});