error adding existing elements to a view

Started by Mate, May 20, 2024, 22:27:10 PM

Previous topic - Next topic

Mate

I have written a script ( this is my third jArchi script, so still learning  :) ) that selects elements in a model based on a combination of properties and adds these to a new view. All seems to be as expected except for adding the element to the view. Below is the script followed by the error that I am getting. I expect I am not doing something right. Any guidance is much appreciated.

console.show();
console.clear();
console.log("Start \r");

debug = true;

var propertyDict = {
    "source": "Legacy",
    "state": "Enabled",
    "taxonomyL0": "Common"   
};

function findMatchingElements(properties, matchAll) {
    try {
        return $(selection).find("element").filter(e => {
            debug ? console.log("Checking element: " + e.name + "\r") : true;
            var matches = matchAll ? true : false;
            for (var key in properties) {
                var elementProp = e.prop(key) ? e.prop(key).trim().toLowerCase() : "";
                var filterProp = properties[key] ? properties[key].trim().toLowerCase() : "";
                debug ? console.log("Property: " + key + ", Element: " + elementProp + ", Filter: " + filterProp + "\r") : true;

                if (matchAll) {
                    if (elementProp !== filterProp) {
                        matches = false;
                        break;
                    }
                } else {
                    if (elementProp === filterProp) {
                        matches = true;
                        break;
                    }
                }
            }
            return matches;
        });
    } catch (error) {
        console.error("Error finding matching elements: " + error);
        return [];
    }
}

function addElementsToView(elements, viewName) {
    try {
        var View = $("view").filter(v => v.name === viewName).first();
        if (!View) {
            debug ? console.log("Creating a new view called: " + viewName + "\r") : true;
            View = model.createArchimateView(viewName);     
        } else {
            debug ? console.log("Selecting the existing view: " + viewName + "\r") : true;
        }

        elements.each(e => {
            debug ? console.log("Adding element: " + e.name + " to the view: " + viewName + "\r") : true;
            View.add(e, 100, 100, 140, 60); // x, y, width, height
        });

        // view.openInUI();
        debug ? console.log("Elements added to view: " + viewName) : true;
    } catch (error) {
        console.error("Error adding elements to view: " + error);
    }
}

function main() {
    try {
        if (!model) {
            throw new Error("No model selected. Please select a model before running the script.");
        }
        var matchAllProperties = true; // Set to false to match any property
        var matchingElements = findMatchingElements(propertyDict, matchAllProperties);
        debug ? console.log("Matching elements: " + matchingElements.length + "\r") : true;
        addElementsToView(matchingElements, propertyDict.source);
    } catch (error) {
        console.error("Error in main function: " + error);
    }
}

main();
console.log("done \r");

QuoteError adding elements to view: TypeError: invokeMember (add) on com.archimatetool.script.dom.model.ArchimateDiagramModelProxy failed due to: no applicable overload found (overloads: [Method[public com.archimatetool.script.dom.model.DiagramModelConnectionProxy com.archimatetool.script.dom.model.ArchimateDiagramModelProxy.add(com.archimatetool.script.dom.model.ArchimateRelationshipProxy,com.archimatetool.script.dom.model.DiagramModelComponentProxy,com.archimatetool.script.dom.model.DiagramModelComponentProxy)], Method[public com.archimatetool.script.dom.model.DiagramModelObjectProxy com.archimatetool.script.dom.model.ArchimateDiagramModelProxy.add(com.archimatetool.script.dom.model.ArchimateElementProxy,int,int,int,int,boolean)], Method[public com.archimatetool.script.dom.model.DiagramModelObjectProxy com.archimatetool.script.dom.model.ArchimateDiagramModelProxy.add(com.archimatetool.script.dom.model.ArchimateElementProxy,int,int,int,int)]], arguments: [JavaObject[application-service: AzureSQLAnalytics(DefaultWorkspace-ef74c932-a6bc-4e9e-aca8-d91d2710dce1-NCUS) (com.archimatetool.script.dom.model.DiagramModelObjectProxy)] (HostObject), 100 (Integer), 100 (Integer), 140 (Integer), 60 (Integer)])

Mate

seems to work as expected if the selection is the model or a folder. If the selection is a view then I get this error. Do I need to do some additional transformation for elements selected from a view vs elements selected from the whole model or a folder in the model?

Phil Beauvoir

#2
Hi,

The error means that there is no method with the signature:

View.add(diagramObject, x, y, w, h);

Your script is getting objects based on the current selection here:

return $(selection).find("element").filter(e => {

But if the current selection is a View then the elements returned are actually diagram objects, not model concepts. In that case you need to reference the underlying concept like this:

View.add(e.concept, 100, 100, 140, 60);

BTW if you want to select all elements in the model rather than the current selection you can just do this:

return $("element").filter(e => {

Some possible tips:

1. You probably know this but I'll mention it anyway. You don't actually need a "main()" function, you can just put that code block at the start of the script.
2. Because you are catching exceptions in a try/catch block you don't get to see the line number in the script where the error occurred in the console output. I had to remove the try/catch to see the line number where the problem occurred.

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

Mate

Thank you, Phil. I now know my mistake and the distinction between diagram objects and model concepts. Thank you for the explanation.

Also grateful for the tips on JS programming. I only occasionally dabble in programming, and this is my first foray into JS and jArchi. So am most thankful for all the guidance that can help me improve :).