Add Trigger to Transition

407 Views Asked by At

How does one add a trigger to the "Properties-Constraints-Triggers" area of a state machine transition in Enterprise Architect? The quotes are how you get there manually within EA.

What I've Tried

Below, this actually results in the trigger being added to the state machine, but I need to link it to a specific transition. Variable stateMachine is of type EA.Element.

EA.Element trigger = (EA.Element)stateMachine.Elements.AddNew("trigger", "Trigger");

State is of type EA.Element. connector is of type EA.Connector (the specific StateFlow transition that I'm trying to add the trigger to). All of these appear to do absolutely nothing. In fact, Constraints seems to be an empty collection, even after I add the new items.

state.Constraints.AddNew("trigger", "Trigger");
connector.Constraints.AddNew("trigger", "Trigger");
state.StateTransitions.AddNew("trigger", "Trigger");

I've dug through the EA help with no luck as well. The search functionality isn't so good though, so it's possible I've missed something.

Interesting Information (May be on to something)

The MiscData for the Connector type seems to have the State Flow trigger information. According to a very reliable book (thank you Mr. Kilian), PDATA1 of MiscData seems to be the State Flow trigger's name. But how do I add a new trigger? MiscData is Read Only.

1

There are 1 best solutions below

3
On BEST ANSWER

This particular relation (from Transition to Trigger) is probably not supported by the API as such. In that case you'll have to use a workaround.

First you'll need to figure out where exactly EA stores this information. The easiest to figure this out is to start from an empty model, create only the elements you need (state machine, two states, a transition and a trigger) and create the link using the EA GUI.

Then inspect the database. Since you only have these few elements in there it should be quite easy to find where EA has stored the relationship.

If I had to guess I would say that it will probably be stored in the t_xref table, or else somewhere in a style or styleEx column (or the Pdata column as you indicated yourself). Then you'll need to add this info directly into the database using an SQL update or insert query and the undocumented Repository.Execute operation. You can find an example of such a thing on github:

        /// <summary>
        /// copy the workingset tot the given user
        /// </summary>
        /// <param name="user">the user to copy the working set to</param>
        /// <param name="overwrite">if true then the first workingset found with the same name
        /// for the given user will be overwritten</param>
        public void copyToUser(User user, bool overwrite)
        {
            if (overwrite)
            {
                //check if a workingset with the same name already exists
                WorkingSet workingSetToOverwrite = this.model.workingSets.Find(w => 
                                                                    w.user != null
                                                                    && w.user.login == user.login 
                                                                    && w.name == this.name);
                if (workingSetToOverwrite != null)
                {
                    workingSetToOverwrite.delete();
                }
            }
            string insertQuery = @"insert into t_document (DocID,DocName, Notes, Style,ElementID, ElementType,StrContent,BinContent,DocType,Author,DocDate )
                                select '"+Guid.NewGuid().ToString("B")+@"',d.DocName, d.Notes, d.Style,
                                d.ElementID, d.ElementType,d.StrContent,d.BinContent,d.DocType,'" + user.fullName + @"',d.DocDate from t_document d
                                where d.DocID like '"+this.ID+"'";
            this.model.executeSQL(insertQuery);

        }