Book a Demo

Author Topic: EA - scripting  (Read 3911 times)

vladap

  • EA User
  • **
  • Posts: 79
  • Karma: +0/-0
    • View Profile
EA - scripting
« on: July 26, 2023, 11:33:05 pm »
Hello guys,

I am little bit lost in the scripting here.
I have a JScript in EA, which adds tag values to elements.

Currently the rule is that its add only to elements of specific type = Class and it works.
My issue is that I would like to limit it only to elements starting with letter "q"

I changed the function to if (currentElement.Type === "Class" && currentElement.Name.startsWith("q")) but startsWith is not recognize by EA. Any hints how to handle this?

thanks

Code: [Select]
!INC Local Scripts.EAConstants-JScript
// NOTE: Requires a package to be selected in the Project Browser
// =================================================================================
// Element API - http://www.sparxsystems.com/enterprise_architect_user_guide/12.1/automation_and_scripting/element2.html
//
function addTgvToElement()
{
var tgvName;
var tgvValue;

tgvName = "mediation_technology_type";
tgvValue = "Production";


// Show the script output window
Repository.EnsureOutputVisible( "Script" );

// Get the currently selected package in the tree to work on
var thePackage as EA.Package;
thePackage = Repository.GetTreeSelectedPackage();

if ( thePackage != null && thePackage.ParentID != 0 )
{
var testElementID = 0;

Session.Output( "JScript MANAGE ELEMENTS EXAMPLE" );
Session.Output( "=======================================" );
Session.Output( "Working on package '" + thePackage.Name + "' (ID=" +
thePackage.PackageID + ")" );

var elements as EA.Collection;
elements = thePackage.Elements;


// Navigate the elements collection. Delete the newly added element when we find it
for ( var i = 0 ; i < elements.Count ; i++ )
{
var currentElement as EA.Element;
currentElement = elements.GetAt( i );

if (currentElement.Type === "Class" && currentElement.Name.startsWith("q"))
{
Session.Output( "Element nalezen: " + currentElement.Name );
var tgv as EA.TaggedValue = null;
isNew = true;

var tgvs as EA.Collection;
tgvs = currentElement.TaggedValues;
//
for ( var j = 0 ; j < tgvs.Count ; j++ )
{
var tgvExt = tgvs.GetAt( j );
if (tgvExt.Name == tgvName)
{
tgv = tgvs.GetAt( j );
isNew = false;
break;
}
Session.Output( "cyklus: " + tgvName );
}


if (isNew == true)
{
tgv = currentElement.TaggedValues.AddNew(tgvName, "");
currentElement.TaggedValues.Refresh();
}

tgv.Value = tgvValue;
tgv.Update();
isNew = true;
}

Session.Output( "Element prejmenovan: " + currentElement.Name );

}

Session.Output( "Done!" );
}
else
{
Session.Prompt( "This script requires a package to be selected in the Project Browser.\n" +
"Please select a package in the Project Browser and try again.", promptOK );
}
}

addTgvToElement();

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +397/-301
  • I'm no guru at all
    • View Profile
Re: EA - scripting
« Reply #1 on: July 27, 2023, 02:42:36 am »
Absolutely no idea about J-stuff, but isnt't there some substr or substring operation?

q.

vladap

  • EA User
  • **
  • Posts: 79
  • Karma: +0/-0
    • View Profile
Re: EA - scripting
« Reply #2 on: July 27, 2023, 08:35:46 pm »
I wasnt able to figure out , how to use it in EA. Internet says:
Code: [Select]
&& currentElement.Name.startsWith("q")
but I receive and error that this method is not working.

vladap

  • EA User
  • **
  • Posts: 79
  • Karma: +0/-0
    • View Profile
Re: EA - scripting
« Reply #3 on: July 27, 2023, 08:44:39 pm »
Ok so I solved it with regular expression

Code: [Select]

var regex = /^T/;
for ( var i = 0 ; i < elements.Count ; i++ )
{
var currentElement as EA.Element;
currentElement = elements.GetAt( i );

if (currentElement.Type === "Class" && regex.test(currentElement.Name))

SSB6

  • EA Novice
  • *
  • Posts: 4
  • Karma: +0/-0
    • View Profile
EA - scripting
« Reply #4 on: July 28, 2023, 02:24:55 pm »
Maybe the property startswith doesn't work on JScript. Instead of using Jscript, you can go with JavaScript which will work in this scenario.