Sparx Systems Forum

Enterprise Architect => Automation Interface, Add-Ins and Tools => Topic started by: Uffe on September 21, 2017, 07:41:16 pm

Title: Create generalization set thorugh API
Post by: Uffe on September 21, 2017, 07:41:16 pm
Hi all,


I need to create a bunch of generalization sets through the API.

I know the generalization set itself is stored in t_xref, and I can work out the details of the representation, get the power type in there, all good. But: can I just add a row to t_xref, or is there some more magic I need to do to make sure my database doesn't experience a critical power excursion resulting in a core meltdown and subsequent evacuation of substantial sections of northern Europe?

De profundis clamavi ad te, qwerty...


/Uffe
Title: Re: Create generalization set thorugh API
Post by: Geert Bellekens on September 21, 2017, 08:37:17 pm
I tried that last time in order to set the Action Kind on an Action, but without succes.

The insert actually went through, but right after that EA execute a statement
SELECT * FROM t_xref WHERE
which resulted in error popping up. There was no way to intercept that exception.
Since my usecase at that moment was only to update existing action kinds I only implemented the update (that went without problems) and I did not investigate further.

Geert
Title: Re: Create generalization set thorugh API
Post by: qwerty on September 21, 2017, 08:37:51 pm
Hehe. AFAIK (!!!) you can put into t_xref what you need. The primary key is a GUID which per se imposes that you can not assume anything about its context (though we know Sparx does so by mixing partial GUIDs and the like). However, to what I have observed you are save to add what you need without having to worry about EA interfering with it. It also appears that the consistency check is sloppy (more or less) with the contents of t_xref.

Having said this, all goes without warranty ;-)

q.

P.S. I just looked into t_ref and found a very new entry SHA-256/User Setting. Very interesting...
Title: Re: Create generalization set thorugh API
Post by: Uffe on September 21, 2017, 08:42:09 pm
OK, thanks guys. I'll give it a try -- in a separate, throw-away EAP project.

On an air-gapped computer.

Inside a bank vault.

In Siberia.
Title: Re: Create generalization set thorugh API
Post by: qwerty on September 21, 2017, 08:53:26 pm
I just tried an insert and a select and both worked.

q.

P.S. The integrity check did not moan upon a scrap value I inserted in t_xref and left it as it was.
Title: Re: Create generalization set thorugh API
Post by: Uffe on September 21, 2017, 10:49:01 pm
Gentlemen!

I have returned from the untamed steppe, bearing a VBScript.

P.S. The integrity check did not moan upon a scrap value I inserted in t_xref and left it as it was.
Yeah, if you don't get it right when putting stuff in t_xref EA doesn't know when it should be deleted, but it doesn't seem to care. However, generalization sets created with the enclosed code work properly in that the t_xref rows are deleted when the corresponding connectors are. They also show up correctly in a diagram. I haven't tested anything else, but nothing's obviously broken so it seems to work just fine.

A few things.

Each generalization that is part of any generalization set has its own Xref.

There are four GUIDs to keep track of: the Xref GUID, the generalization set GUID, the connector GUID and the power type GUID (optional).

The Xref GUID has to be unique of course. The generalization set GUID has to be reused if you want to add more than one generalization to a set (which in all likelihood you do, otherwise why bother with the set at all). It is stored, fairly anonymously, in t_xref.Description (the field called 'GUID'). In my case I will be creating all my generalizations top-down in one go, so I won't need to look for existing generalization set GUIDs and pull them out of the Description. For a proper implementation, this would need to be addressed.

The connector GUID goes in t_xref.Client. A generalization set has no supplier; specify '<none>'.

The power type GUID also goes in Description, in a field called 'PowerTypeGUID'.

Namespace, Requirement, Constraint and Link are left empty.

Thanks again, fellas.


/Uffe


Code: (VBScript) [Select]
' Creates a GUID with {}'s and strips zero string terminator.
function CreateGUID
CreateGUID = Left(CreateObject("Scriptlet.TypeLib").Guid, 38)
end function

' Inserts a generalization set row into t_xref.
sub InsertGeneralizationSetXref(xrefGuid, genSetGuid, genSetName, isCovering, isDisjoint, powerTypeGuid, connectorGuid)
Repository.Execute("insert into t_xref " & _
"(XRefID, Name, Type, Visibility, Behavior, Partition, Description, Client, Supplier) values (" & _
"'" & xrefGuid & "', " & _
"'MOFProps', 'connector property', 'Public', 'generalizationSet', '0', " & _
"'GUID=" & genSetGuid & ";Name=" & genSetName & ";IsCovering=" & isCovering &";IsDisjoint=" & isDisjoint & _
";PowerTypeGUID=" & powerTypeGuid & ";', " & "'" & connectorGuid & "', " & _
"'<none>')")
end sub

' Creates a generalization set with the specified power type, and adds the specified connector to the set.
' Returns the GUID of the newly created generalization set.
function CreateGeneralizationSet(genSetName, isCovering, isDisjoint, powerType, connector)
dim genSetGuid

genSetGuid = CreateGUID()
InsertGeneralizationSetXref CreateGUID(), genSetGuid, genSetName, isCovering, isDisjoint, powerType.ElementGUID, connector.ConnectorGUID
CreateGeneralizationSet = genSetGuid
end function

' Adds a connector to an exidsting generalization set.
' If the generalization set attributes don't match what's in the database already, I've no idea what happens.
sub AddToGeneralizationSet(genSetGuid, genSetName, isCovering, isDisjoint, powerType, connector)
InsertGeneralizationSetXref CreateGUID(), genSetGuid, genSetName, isCovering, isDisjoint, powerType.ElementGUID, connector.ConnectorGUID
end sub

' Creates a simple class hierarchy with a generalization set.
sub FlyBySeatOfPants(package)
dim parent as EA.Element
dim child1 as EA.Element
dim child2 as EA.Element
dim powerType as EA.Element
dim connector as EA.Connector
dim genSetGuid
dim xrefGuid
dim stmt

set parent = package.Elements.AddNew("Parent", "Class")
parent.Update()
set child1 = package.Elements.AddNew("Child 1", "Class")
child1.Update()
set child2 = package.Elements.AddNew("Child 2", "Class")
child2.Update()
set powerType = package.Elements.AddNew("Power Type", "Class")
powerType.Update()
package.Elements.Refresh()

set connector = child1.Connectors.AddNew("", "Generalization")
connector.SupplierID = parent.ElementID
connector.Update()

genSetGuid = CreateGeneralizationSet(powerType.Name, "0", "0", powerType, connector)

set connector = child2.Connectors.AddNew("", "Generalization")
connector.SupplierID = parent.ElementID
connector.Update()
AddToGeneralizationSet genSetGuid, powerType.Name, "0", "0", powerType, connector
end sub
Title: Re: Create generalization set thorugh API
Post by: Paolo F Cantoni on September 22, 2017, 09:34:20 am
OK, thanks guys. I'll give it a try -- in a separate, throw-away EAP project.

On an air-gapped computer.

Inside a bank vault.

In Siberia.
That's within the purview of the FSB!  Are you sure they haven't interfered with your code?

Friday!   ;D

Paolo

PS: Nice code!  We've extended Generalization Sets to other forms of sets and so this will form the basis of common code to handle those!
Title: Re: Create generalization set thorugh API
Post by: Uffe on September 22, 2017, 05:24:24 pm
OK, thanks guys. I'll give it a try -- in a separate, throw-away EAP project.
On an air-gapped computer.
Inside a bank vault.
In Siberia.
That's within the purview of the FSB!  Are you sure they haven't interfered with your code?
Absolutely positive.

Now myove alonk. Thyere is nothink to see hyere.

Quote
PS: Nice code!  We've extended Generalization Sets to other forms of sets and so this will form the basis of common code to handle those!

Cool bananas! :)

Please fix the spelling error in the comment. :-[