As an update to the previous implementation proposal, please consider the following update. It is discouraged to use the "UpdateAction" event as this could result in unpredicted behaviour. The approach for preventing relation instances to be deleted, we can combine the "DeleteRelationInstance" with "DiscardRelationInstance". The approach is based on the sequence of events, as during the
DeleteRelationInstance we can capture the deleted relation and all available attributes, serialize this information and run a re-create in the
DiscardRelationInstance, as this event is triggered when the relation instance is discarded from the modelling core.
The proposal below builds upon this assumption, serialization between the events is realized using a map object, e.g. serializing in memory.
1ON_EVENT "AppInitialized"
2{
3 # ...
4 SETG mDeletedRelations: (map())
5 # ....
6}
During AppInitialized, the map for serializing is initialized.
1ON_EVENT "DeleteRelationInstance" {
2 IF (( #select the appropriate instance type#)) {
3 # get the position of the relation
4 CC "Core" GET_ATTR_VAL objid: (relationinstanceid) attrname:"Positions"
5 SETL sPositionsVal: (val)
6 # get all attributes of the relation
7 CC "Core" GET_ALL_ATTRS classid: (relationclassid)
8 SETL lAttributeIDs: (attrids)
9 SETL lAttributeMap: ("")
10 FOR sAttributeID in: (lAttributeIDs) {
11 CC "Core" GET_ATTR_VAL objid: (relationinstanceid) attrid: (VAL sAttributeID) as-string
12 SETL sAttributeValue: (val)
13 SETL sIDValuePair: (sAttributeID + "#" + sAttributeValue)
14 SETL lAttributeMap: (tokunion (lAttributeMap, sIDValuePair, "|"))
15 }
16 IF (type (mDeletedRelations[id_componentid]) = "undefined") {
17 SET sTimeStampedDeletedActions: (STR relationclassid + "|" + STR toinstanceid + "|" + STR frominstanceid + "|" + base64encode(sPositionsVal) + "|" + base64encode(lAttributeMap)+ "|" + STR relationinstanceid)
18 }
19 ELSE {
20 SET sTimeStampedDeletedActions: (tokunion (mDeletedRelations[id_componentid], STR relationclassid + "|" + STR toinstanceid + "|" + STR frominstanceid + "|" + base64encode(sPositionsVal)+ "|" + base64encode(lAttributeMap) + "|" + STR relationinstanceid))
21 }
22 SETG mDeletedRelations[id_componentid]: (sTimeStampedDeletedActions)
23}
The "DeleteRelationInstance" retrieves the values from the deleted relation and stores them into the map element
1ON_EVENT "DiscardRelationInstance" {
2 CC "Modeling" GET_ACT_MODEL
3 SETL nCreatedModelID: (modelid)
4 IF (nCreatedModelID != -1 AND LEN (mDeletedRelations) > 0) {
5 FOR sDeletedRelation in: (mDeletedRelations) {
6 IF (LEN (sDeletedRelation) > 0) {
7 EVENT_LOG msgType:"EVENT_LOG" message: (sDeletedRelation)
8 CC "Core" CREATE_CONNECTOR modelid: (modelid) fromobjid: (VAL token (sDeletedRelation, 2, "|")) toobjid: (VAL token (sDeletedRelation, 1, "|")) classid: (VAL token (sDeletedRelation, 0, "|"))
9 SETL nNewRelationID: (objid)
10 CC "Core" SET_ATTR_VAL objid: (nNewRelationID) attrname:"Positions" val: (base64decode(token (sDeletedRelation, 3, "|")))
11 SETL lAttributeValueMap: (base64decode(token (sDeletedRelation, 4, "|")))
12 FOR sAttributeKeyValue in: (lAttributeValueMap) sep:"|" {
13 SET sAttributeID: (token (sAttributeKeyValue, 0, "#"))
14 SET sAttributeValue: (token (sAttributeKeyValue, 1, "#"))
15 CC "Core" GET_ATTR_TYPE attrid: (VAL sAttributeID)
16 IF (attrtype = "INTEGER" OR attrtype = "DOUBLE" OR attrtype = "TIME") {
17 CC "Core" SET_ATTR_VAL objid: (nNewRelationID) attrid: (VAL sAttributeID) val: (VAL sAttributeValue)
18 } ELSE {
19 CC "Core" SET_ATTR_VAL objid: (nNewRelationID) attrid: (VAL sAttributeID) val: (sAttributeValue)
20 }
21 }
22 }
23 }
24 # reset map
25 SETG mDeletedRelations: (map())
26 }
27}
The event "DiscardRelationInstance" is used to re-create the missing connector.