Set Properties for specialization

Started by cadmageren, October 15, 2024, 13:12:19 PM

Previous topic - Next topic

cadmageren

I am trying to make a script, that will update all elements with a given specialization, with a specific property. But I can't see how to get the specialization? Looking in the XML structure, specializations are called "profile".

console.show();
console.clear();
console.log(model.name);
function SetSpecProperties(profileName, propertyName, defaultValue) {
    // Find the profile with the given profileName and get its id

let profile = $('profile').filter(function(p) {
        return p.prop("name") === profileName;
    }).first();

    if (!profile) {
        console.log(`Profile "${profileName}" not found.`);
        return;
    }

    let profileId = 'profile.id';

    // Get all elements that have the matching profiles id
    let elementsWithProfile = $('element').filter(function (e) {
        return e.prop("profiles") === profileId;
    });

    // Iterate through the elements and add the property if it doesn't exist
    elementsWithProfile.each(function (element) {
        if (!element.prop(propertyName)) {
            element.prop(propertyName, defaultValue);
        }
    });

    console.log(`${elementsWithProfile.size()} elements updated with property "${propertyName}" and default value "${defaultValue}".`);
}

SetSpecProperties("Server", "ID", "DK");

console.log("Done")

Phil Beauvoir

If you value and use Archi, please consider making a donation!
Ask your ArchiMate related questions to the ArchiMate Community's Discussion Board.

cadmageren

Thanks Phil - being the first day working with jArchi :D

Working now as expected.

function SetSpecProperties(specialization, propertyName, defaultValue, labelExpression) {
    $("element").forEach(function(element) {
        if (element.specialization == specialization) {
            if (!element.prop(propertyName)) {
                element.prop(propertyName, defaultValue);
                console.log('Property created for ' + element.name);
            }
$(element).objectRefs().each(function(e) {
e.labelExpression=labelExpression
                console.log(e.type + ' ' + e.name + " is used in view: " + ": " + e.labelExpression);
            });
}
    });
}
SetSpecProperties("Server", "ID", "DK", "${name}\n${property:ID}");