Dear everyone,
I'm trying to extend the Search and Replace script posted by Jan in https://medini.freshdesk.com/a/forums/topics/1000092454 to replace a certain text in location attribute of ISO26262 hazardous event. However, I do not know how to specify the ISO26262_location feature in the array features. First of all, does the script work only for specific features as it says "DONT CHANGE ANYTHING BELOW THIS LINE" right above the features definition?
The code I tried is shown below. I tried three patterns but none of them worked:
/*
* Copyright (c) 2017 ANSYS medini Technologies AG
* All rights reserved.
*/
// Change the string to search as well as the string to replace hereafter
var searchWhat = "普通路";
var replaceWith = "舗装路";
// DONT CHANGE ANYTHING BELOW THIS LINE
var features = [
Metamodel.hazard.HazardousEvent.operationalSituation.ISO26262_location, // null
// Metamodel.hazard.HazardousEvent.OperationalSituation.ISO26262_location, // TypeError: Cannot read property "ISO26262_location" from undefined
// Metamodel.hazard.HazardousEvent.OperationalSituationExtension.ISO26262_location, // TypeError: Cannot read property "ISO26262_location" from undefined
];
function searchAndReplace(feature) {
console.log("start");
if (!feature) {
console.error("Well, feature was 'null' - maybe a scripting error?");
return;
}
// the feature has a metaclass
console.log("Searching ''{0}''...", feature.name);
var found = finder.findByType(feature.eContainingClass, false).filter(function (obj) {
// filter those who don't have they keyword in
var text = obj[feature.name];
console.log("Text: {0}", text);
if (text == null || text == undefined) {
return false;
}
var index = text.indexOf(searchWhat);
return index != -1;
});
if (found.isEmpty()) {
console.log("No hits. Ignore this attribute.");
return;
}
console.log("Found {0} hits. Replacing...", found.size());
// now DO it!
found.asArray().forEach(function (obj) {
var text = new java.lang.String(obj[feature.name]);
text = text.replaceAll(searchWhat, replaceWith);
obj[feature.name] = text;
});
}
features.forEach(searchAndReplace);
Regards,
Toru