Archi Forum

Archi Plug-ins => jArchi => Topic started by: AdGerrits on August 16, 2018, 21:47:01 PM

Title: jArchi issue
Post by: AdGerrits on August 16, 2018, 21:47:01 PM
Hi Phil, I'm not experienced enough in using javascript/jArchi so great chance it's not a bug but an error on my side.
I've been trying to understand whats going on in this short script but I rest my case...

Why is 'type' not replaced in the first selection as expected ( as  'name' is) just like in the second selection?
btw: I select the goal-element with name '1' before running the code.

$(selection).each(function(e){
   console.log('Before Name=', e.name, '-Type=', e.type);
   e.name = '2';
   e.type = 'principle';
   console.log('After Name=', e.name, '-Type=', e.type);
});
/* output:
Before Name=1-Type=goal
After Name=2-Type=goal  => (huh?)
*/

console.log('------');
$('.2').each(function(e){
   console.log('Before Name=', e.name, '-Type=', e.type);
   e.name = '3';
   e.type = 'principle';
   console.log('After Name=', e.name, '-Type=', e.type);
});
/* output:
Before Name=2-Type=goal
After Name=3-Type=principle  => (as expected)
*/



Title: Re: jArchi issue
Post by: Phil Beauvoir on August 16, 2018, 21:52:37 PM
Hi Ad,

it depends on the "selection". If you are selecting visual objects in a diagram then you have to access the underlying ArchiMate concept in order to set its type. So:

$(selection).each(function(e){
   console.log('Before Name=', e.name, '-Type=', e.type);
   e.name = '2';
   e.concept.type = 'principle';
   console.log('After Name=', e.name, '-Type=', e.type);
});

Title: Re: jArchi issue
Post by: AdGerrits on August 17, 2018, 08:48:47 AM
Thanks. Difference between visual elements and concepts is much clearer now.
One additional remark: 'API-overview' says "visual objects inherit name, description and properties if they are linked to an underlying ArchiMate concept". But it looks like  "type" is also inherited but read-only (I can display type via 'selected-element.type' but cannot change it).
Title: Re: jArchi issue
Post by: Phil Beauvoir on August 17, 2018, 10:06:18 AM
Yes, in the case of a visual object, "type" returns the type of the underlying ArchiMate object as a convenience method (and not something like "diagram-model-archimate-object"). It is the equivalent of:

var type = visualArchiMateObject.concept.type;

is the same as:

var type = visualArchiMateObject.type;

And so setType(type) does not work on a visual object directly because setType() is only a method on an ArchiMate concept type (element or relationship).

To change the underlying type (and this will change all instances of it in all diagrams) then you need to do:

visualObject.concept.type = "node";
Title: Re: jArchi issue
Post by: AdGerrits on August 17, 2018, 10:39:53 AM
Understood. One last question after experimenting for a while: is there a way to exit a script early? Return doesn't seem to work (and exit() is a bit rough...).
Title: Re: jArchi issue
Post by: Phil Beauvoir on August 17, 2018, 10:42:25 AM
You could throw an exception?
Title: Re: jArchi issue
Post by: Jean-Baptiste Sarrodie on August 17, 2018, 19:21:50 PM
Hi,

It's true that exit is a bit hard (it simply kills Archi itself). We should make it unavailable (see this post (https://stackoverflow.com/questions/31127641/how-stop-nashorn-from-allowing-the-quit-function)).

Instead, we could write a 'buitin' exit function that simply throw an exception...

Regards,

JB
Title: Re: jArchi issue
Post by: Phil Beauvoir on August 17, 2018, 20:03:37 PM
Done and done.  8)

Next version of jArchi redefines exit() to throw an exception which is caught by jArchi and a nice, polite message is written to the console:

"Exited at line 13 - have a nice day, Ad!"

(OK, maybe not the "have a nice day" bit...)
Title: Re: jArchi issue
Post by: AdGerrits on August 24, 2018, 07:58:31 AM
Nice. Even without the 'have a nice day' part more than satisfied ;)