can i use stringify on jarchi collections and object

Started by Gerald Groot Roessink, December 23, 2022, 16:28:42 PM

Previous topic - Next topic

Gerald Groot Roessink

Hi,

Just to see what is in there I tried to translate an object to text, like this:

$('element').each(function(x) {

console.log( JSON.stringify(x), x);
});

The x-part shows me type and name of all elements and the stringify-part comes up empty.

A jArchi object is not an javascript-object?

Please respond.

Gerald


Jean-Baptiste Sarrodie

Hi,

Quote from: Gerald Groot Roessink on December 23, 2022, 16:28:42 PMA jArchi object is not an javascript-object?

No, a jArchi object is a Java object you can access through javascript.

Regards,

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

Gerald Groot Roessink

Thanks, you are always quick.
I was hoping for this answer, but the small snippet of code I provided only returns empty stringified objects while the original is not empty. I don't understand.
Gerald

Jean-Baptiste Sarrodie

Hi,

Quote from: Gerald Groot Roessink on December 23, 2022, 19:03:40 PMthe small snippet of code I provided only returns empty stringified objects while the original is not empty.

Yes, that's normal, because 'x' being an element, it is in fact a JAVA object. This means that JSON.stringify has no way to inspect it and get its attributes/members to print them.

You can read more about this limitation of JSON.stringify and Nashorn/GraalVM here: https://mail.openjdk.org/pipermail/nashorn-dev/2013-September/002012.html

I haven't done an in depth search, so maybe there's now some workaround based on GraalVM.

Regards,

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

Gerald Groot Roessink

As I understand it, stringify works with arrays and objects defined in the code but not with arrays and objects retrieved from Archi. Bummer, as is stated in the link you shared. Haven't given up yet, but no workaround so far.
Gerald

Jean-Baptiste Sarrodie

Hi,

Could you share your use-case? Maybe there's another way of doing what you want to achieve, and maybe I can help...

Regards,

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

Gerald Groot Roessink

Hello JB,

Thanks for offering your help

Maybe I got lost in the woods a bit. In general I was looking for something to inspect an Archi-object in the console. A small convenient tool to get to the bottom. As you showed this is not in the framework jArchi is build on. I understand now that the addition of a toJSON-function to the object would remedy it. But this is not a big deal and not on my wishlist.


My real use case was: finding duplicate relations and eliminate them as garbage collection.

Actually I now how to find them. I found a concise piece of code in the forum:

        // select those relationships with duplicate type, source, target
        duprel=$(rel.source).outRels(rel.type).filter(function(d) {
            return     d.target.equals(rel.target) &&  rel.id != d.id;
        }).first();

With the last extra condition I can print all duplicate relations in a model or export them as Excel. In our main model hundred+. What I would like the script to do is select at random one of the duplicate relations and merge others with it. No questions asked.

Selecting the element to merge the others with is something I can't figure out yet.

I hope you have something similar in your repertoire.

Thanks in advance
Gerald