Author Topic: Setting values to tags of stereotypes in C# - how to?  (Read 2650 times)

Ribeye

  • EA User
  • **
  • Posts: 23
  • Karma: +0/-0
    • View Profile
Setting values to tags of stereotypes in C# - how to?
« on: April 27, 2022, 03:41:49 am »
I am successfully assigning stereotypes to elements I am creating in C#.

When it comes to setting values of tags that the stereotype has I am failing.

I copied code from https://sparxsystems.com/forums/smf/index.php?topic=6097.0 but it is creating new tags, not setting the tag values of the stereotype.

I cant find example code anywhere.

Here is that I have come to.
        // https://sparxsystems.com/forums/smf/index.php?topic=6097.0
        public static bool addTaggedValue(EA.Element elementParam, string tagName, string value)
        {
            TaggedValue tag = elementParam.TaggedValuesEx.GetByName(tagName);
            if (tag == null)
            {
                TaggedValue element = elementParam.TaggedValuesEx.AddNew(tagName, value);
                element.Value = value;
                element.Update(); //must be executed in order to save new tagged value
                elementParam.Update(); // is it needed?
                elementParam.TaggedValues.Refresh();
                String debugString = element.FQName; // always ""
                return true;
            }
            tag.Value = value;
            tag.Update();
            elementParam.Update(); // is it needed?
            elementParam.TaggedValues.Refresh();
            return false;
        }

qwerty

  • EA Guru
  • *****
  • Posts: 13574
  • Karma: +395/-301
  • I'm no guru at all
    • View Profile
Re: Setting values to tags of stereotypes in C# - how to?
« Reply #1 on: April 27, 2022, 07:44:47 am »
I would not use the GetByName since you never know which one EA returns. Names are not unique so there might exist more than one. Use your own wrapper to find the correct TV.

elementParam.Update() is not needed since you do not change the element, and the collection for the TVs can not be changed. You can only handle it by its operations (addnew/delete).

How do you know that it creates new TVs each time?

q.

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13136
  • Karma: +547/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Setting values to tags of stereotypes in C# - how to?
« Reply #2 on: April 27, 2022, 02:38:20 pm »
Also, use the regular TaggedValues collection, not the TaggedValuesEx collection.
And the Refresh() is not needed.

Geert

Ribeye

  • EA User
  • **
  • Posts: 23
  • Karma: +0/-0
    • View Profile
Re: Setting values to tags of stereotypes in C# - how to?
« Reply #3 on: May 04, 2022, 11:34:00 pm »
Thanks for the replies, they do help.

For anyone else looking to answer this question:

I came to believe the unique name of a tag is "profileName::stereotypeName::tagName"

So my method for adding / updated the tag of an EA.Element is:

        // https://sparxsystems.com/forums/smf/index.php?topic=6097.0
        public static bool addTaggedValue(EA.Element elementParam, string profileName, string stereotypeName, string tagName, string value)
        {
            String tagId = profileName + "::" + stereotypeName + "::" + tagName;
            TaggedValue tag = elementParam.TaggedValues.GetByName(tagId);
            if (tag == null)
            {
                TaggedValue element = elementParam.TaggedValues.AddNew(tagName, value);
                element.Value = value;
                element.Update(); //must be executed in order to save new tagged value
                return true;
            }
            tag.Value = value;
            tag.Update();
            return false;
        }


For EA.Attribute and other EA.element types I needed another version with exactly the same code but replacing  EA.Element with EA.Attribute. Could not find a way to generalise EA.Element and EA.Property!


qwerty

  • EA Guru
  • *****
  • Posts: 13574
  • Karma: +395/-301
  • I'm no guru at all
    • View Profile
Re: Setting values to tags of stereotypes in C# - how to?
« Reply #4 on: May 04, 2022, 11:52:26 pm »
For EA.Attribute and other EA.element types I needed another version with exactly the same code but replacing  EA.Element with EA.Attribute. Could not find a way to generalise EA.Element and EA.Property!
It would as well be hard to generalize wheel and potato. They have nothing in common...

q.

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13136
  • Karma: +547/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Setting values to tags of stereotypes in C# - how to?
« Reply #5 on: May 05, 2022, 04:38:58 am »
Hmm, are you trying to add tagged values to a stereotyped element, that should already have the tagged value because it's defined in the stereotype?
In that case you don't need to add it manually, but simply synchronize the stereotype. This will add all the missing tagged values automatically.
You can do that from a toolbox, or using a script

Here's a script that does exactly that: https://github.com/GeertBellekens/Enterprise-Architect-VBScript-Library/blob/master/Framework/Tools/UML%20Profile/Synchronize%20Steretoypes.vbs

Geert


Daiim

  • EA User
  • **
  • Posts: 40
  • Karma: +0/-0
    • View Profile
Re: Setting values to tags of stereotypes in C# - how to?
« Reply #6 on: May 16, 2022, 07:56:56 pm »
For EA.Attribute and other EA.element types I needed another version with exactly the same code but replacing  EA.Element with EA.Attribute. Could not find a way to generalise EA.Element and EA.Property!

If you're using a dynamic language as JScript/JavaScript you can do that. Take care, EA.Element is a type, EA.Attribute, EA.Connector, EA.Package  are different types from automation perspective.


Using C# you can use dynamic as parameter type for your object.

Example in JScript:
Code: [Select]
// holds for everything that has an TaggedValues collection
function addTaggedValue(object, tagName, tagValue) {
   if (typeof(object.TaggedValues) === 'undefined')
      return null;

   var tag = null;
   for (var index = 0; index < object.TaggedValues.Count; index++) {
      var candidate = object.TaggedValues.GetAt(index);
      if (candidate.Property == tagName) {
         tag = candidate;
         break;
      }   
  }
   if (tag == null) {
      tag = object.TaggedValues.AddNew(tagName, "");
   }
   tag.Value = tagValue;
   tag.Update();

   return tag;
}

« Last Edit: May 16, 2022, 08:01:14 pm by Daiim »