Sparx Systems Forum

Enterprise Architect => Automation Interface, Add-Ins and Tools => Topic started by: pianoman on November 27, 2019, 12:08:52 pm

Title: Create instances only if they don't exist currently with a specific ClassifierID
Post by: pianoman on November 27, 2019, 12:08:52 pm
Hi All,

I have a script running multiple functions to automate a lot of things among which creation of instances is one of them. I have a SQL Query that specifies the ClassifierIDs from which to create instances. This script works great and all, but I want to add one more level of complexity.

I want to make sure the script doesn't create instances if there is a case that there is an instance with a specified ClassifierID existing already. My function below has inputs of classifierID, stereotype, and name provided from a query in another function. I'm looking for help in figuring out the if statement to check for an existing instance with a classifierID that will be specified by my query.

Code: [Select]
function addInstance(classifierID, stereotype, name)
Dim package
Dim newinstance AS EA.Element
set package = Repository.GetTreeSelectedPackage()
if newinstance.ClassifierID exists for some instance in the model
                      do nothing
                else if
                      do the below things
                end if
' create the new instance element
set newinstance = package.Elements.AddNew("", stereotype)
' assign a classifier element to the new instance element
newinstance.ClassifierID = classifierID
' assign a name to the new instance element
newinstance.Name = name
' update and refresh for the push to happen to EA
newinstance.Update
package.Elements.Refresh
addInstance = newinstance.ElementID

End Function

Appreciate any help! Thanks!
Title: Create instances only if they don't exist currently with a specific ClassifierID
Post by: Geert Bellekens on November 27, 2019, 03:45:32 pm
Use Repository.SqlQuery with a query similar to this:

Code: [Select]
select o.Object_ID from t_object o where o.Classifier = 123
This returns an xml string with the results of the query that you can parse then.

Alternative is to use Repository.GetElementSet with the same query.

That returns a collection of elements. This is a bit slower, especially if there are a lot of elements to be returned.

Check also https://github.com/GeertBellekens/Enterprise-Architect-VBScript-Library/blob/master/Framework/Utils/Util.vbs (https://github.com/GeertBellekens/Enterprise-Architect-VBScript-Library/blob/master/Framework/Utils/Util.vbs) for lots of utility functions that use SQLQuery

Geert
Title: Re: Create instances only if they don't exist currently with a specific ClassifierID
Post by: KP on November 27, 2019, 04:09:25 pm
Use Repository.SqlQuery with a query similar to this:

Code: [Select]
select o.Object_ID from t_object o where o.Classifier = 123
This returns an xml string with the results of the query that you can parse then.

Alternative is to use Repository.GetElementSet with the same query.

That returns a collection of elements. This is a bit slower, especially if there are a lot of elements to be returned.

Check also https://github.com/GeertBellekens/Enterprise-Architect-VBScript-Library/blob/master/Framework/Utils/Util.vbs (https://github.com/GeertBellekens/Enterprise-Architect-VBScript-Library/blob/master/Framework/Utils/Util.vbs) for lots of utility functions that use SQLQuery

Geert


If you just want to know if there are records, I think you can say something like SELECT Count(*) AS Num FROM ... etc and then see if Num is non-zero. Sorry I don't remember the exact syntax.
Title: Re: Create instances only if they don't exist currently with a specific ClassifierID
Post by: Geert Bellekens on November 27, 2019, 06:34:43 pm
Use Repository.SqlQuery with a query similar to this:

Code: [Select]
select o.Object_ID from t_object o where o.Classifier = 123
This returns an xml string with the results of the query that you can parse then.

Alternative is to use Repository.GetElementSet with the same query.

That returns a collection of elements. This is a bit slower, especially if there are a lot of elements to be returned.

Check also https://github.com/GeertBellekens/Enterprise-Architect-VBScript-Library/blob/master/Framework/Utils/Util.vbs (https://github.com/GeertBellekens/Enterprise-Architect-VBScript-Library/blob/master/Framework/Utils/Util.vbs) for lots of utility functions that use SQLQuery

Geert


If you just want to know if there are records, I think you can say something like SELECT Count(*) AS Num FROM ... etc and then see if Num is non-zero. Sorry I don't remember the exact syntax.
Yes indeed. The syntax is just fine. Just don't forget the alias 'AS Num'.
If you don't you get a very cryptic xml error message because EA tries to create an xml node without a name.

Geert