« Back to Scenario Collection

Calculation of the width of a relation

Combination View Flat View Tree View
Threads [ Previous | Next ]
toggle
Calculation of the width of a relation
Answer
12/20/15 1:33 PM
Hello,
I like to connect to classes with a relation. This relation should have a variable showing the value of the relationship. This works as well as showing the variable top or left right. 

However, I currently use a fixed with of the text (3cm). This means, a long text looks quite ugly on a long relation and on a short realtion the text might run into another class. Is it possible to calculate the with of a relation to define the with of the text?

Here is the graphrep extract:

 1GRAPHREP
 2AVAL bColor:"Color"
 3AVAL bLR:"Show Value Left / Right"
 4AVAL bShowValue:"Schow Value"
 5
 6PEN w:0.02cm color:bColor)
 7SHADOW mode:off
 8
 9EDGE
10
11START
12FILL color:bColor)
13POLYGON 3 x1:0cm y1:.1cm x2:-0.3cm y2:0.0cm x3:0cm y3:-.1cm
14
15MIDDLE
16FONT h:8pt bold color:bColor)
17IF (bLR="1") {
18ATTR "Value" line-break: words x:0.10cm y:0.00cm w:l:3cm h:b
19}
20IF (bLR="0") {
21ATTR "Value" line-break: words x:0.00cm y:0.00cm w:c:3cm h:b
22}
23
24END
25FILL color:bColor)
26POLYGON 3 x1:-.3cm y1:.1cm x2:0cm y2:0cm x3:-.3cm y3:-.1cm


Thank you for any idea!

Best regards,

Philipp

RE: Calculation of the width of a relation
Answer
12/21/15 10:57 AM as a reply to Philipp Küller.
You can trigger an AdoScript code that calculates the length of the relation and save the result into a hidden attribute, if the relation is created with the event handler "AfterCreateModelingConnector".
To calculate the length of the relation you neeed the position information of the source and target object. The AdoScript below shows yo how you can implement this scenario, which you also can find in the attached library as an example.

 1ON_EVENT "AfterCreateModelingConnector" {
 2
 3#Save the pregiven values
 4  SET nModelId: (modelid)
 5  SET nObjId: (objid)
 6  SET nClassId: (classid)
 7  SET nFromObjId: (fromobjid)
 8  SET nToObjId: (toobjid)
 9
10#Get position of the source instance
11  CC "Core" GET_ATTR_VAL objid: (nFromObjId) attrname:"Position"
12  SET nFromObjPosition: (val)
13
14#Get position of the target instance
15  CC "Core" GET_ATTR_VAL objid: (nToObjId) attrname:"Position"
16  SET nToObjPosition: (val)
17
18#Parse the position attributes of the source and target instance and save the values you need in variables
19  LEO parse: (nFromObjPosition) get-tmm-value:n_x_fromobj:"x" get-tmm-value:n_y_fromobj:"y"
20                                              get-tmm-value:n_w_fromobj:"w" get-tmm-value:n_h_fromobj:"h"
21  LEO parse: (nToObjPosition) get-tmm-value:n_x_toobj:"x" get-tmm-value:n_y_toobj:"y"
22                                             get-tmm-value:n_w_toobj:"w" get-tmm-value:n_h_toobj:"h"
23
24# If your source and target instance are resizebale then request also the height and the width of the instances.
25# Otherwise take the values you defined in the GraphRep.
26
27# Differenciate if the target and source instance are in the same line -vertically and horizontally.
28# Note: These are simple conditions for demonstration
29  IF (n_y_fromobj=(n_y_toobj)) {
30    SET n_xDist: (abs(CMS(n_x_fromobj-n_x_toobj))-(CMS(n_w_fromobj/2)+CMS(n_w_toobj/2)))
31    SET n_yDist: (abs(CMS(n_y_fromobj-n_y_toobj))
32  } ELSIF (n_x_fromobj=n_x_toobj) {
33    SET n_yDist: (abs(CMS(n_y_fromobj-n_y_toobj))-(CMS(n_h_fromobj/2)+CMS(n_h_toobj/2)))
34    SET n_xDist: (abs(CMS(n_x_fromobj-n_x_toobj)))
35  } ELSE {
36    SET n_xDist: (abs(CMS(n_x_fromobj-n_x_toobj))-(CMS(n_w_fromobj/2)+CMS(n_w_toobj/2)))
37    SET n_yDist: (abs(CMS(n_y_fromobj-n_y_toobj))-(CMS(n_h_fromobj/2)+CMS(n_h_toobj/2)))
38  }
39
40# Calculate the distance with the formula of PythagorasSET nDistance: (sqrt(pow(n_xDist, 2)+pow(n_yDist, 2)))
41# Set the value of the calculated distance into a hidden attribute so you can call it from the graphrep of the relation
42  CC "Core" GET_ATTR_ID classid: (nClassId) attrname:"Distance"
43  SET nDistanceAttrId: (attrid)
44  CC "Core" SET_ATTR_VAL objid: (nObjId) attrid: (nDistanceAttrId) val: (nDistance)
45}


Now you can call the attribute value of the Distance attribute in the GraphRep and set the width of the text as follows:


1GRAPHREP
2
3AVAL nDistance:"Distance"
4SET vDistance: (CM nDistance)
5
6MIDDLE
7ATTR "Name" x:0.1cm w:c: (vDistance) h:b



.
Attachments: ADOxx 1.5 Experimentation Library.abl (13.6k)

RE: Calculation of the width of a relation
Answer
1/5/16 1:13 PM as a reply to Mehmet Albayrak.
Thanks for your answer - works quite good!

Do you have any idea how to deal with the situation that the distance might changes afterwards (e.g. an endpoint moves)? I tried different event handler but without sucess.

Thanks and best regards,

Philipp

RE: Calculation of the width of a relation
Answer
1/8/16 7:05 AM as a reply to Philipp Küller.
As the position is an attribute you can work wih the event handler "SetAttributeValue". Note that you have to trigger this event in two cases: 

-) movement of an instance of the source class
-) movement of an instance of the target class

The rest of the implementation is equivalent to the implementation above after you query the source/target instance position.

RE: Calculation of the width of a relation
Answer
1/7/16 3:44 PM as a reply to Philipp Küller.
In order to have this functionality also by changing the position of the instances, you can also work with the event "SetAttributeValue". Please have a look at the following code, which is also integrated in the attached library. 
This AdoScript shows you how you can extend the above scenario also by changing the position of the source or target instance.

 1ON_EVENT "SetAttributeValue" {
 2
 3#Save the pregiven values of the event
 4SET nObjId: (instid)
 5SET nAttrId: (attrid)
 6SET nModelId: (modelid)
 7SET sOldVal: (oldval)
 8
 9
10CC "Core" GET_CLASS_ID classname:"A"
11SET nAClassId: (classid)
12CC "Core" GET_ATTR_ID classid: (nAClassId) attrname:"Position"
13SET nPositionAAttrId: (attrid)
14
15#Trigger the Functionality if the Position attribute is changed
16IF (nAttrId = nPositionAAttrId) {   
17  CC "Core" GET_ATTR_VAL objid: (nObjId) attrname:"Position"
18  SET nFromObjPosition: (val)   
19  CC "Core" GET_OBJ_NAME objid: (nObjId)
20  SET sObjName: (objname)   
21  CC "Core"  GET_CLASS_ID objid: (nObjId)
22  SET nInstClassId: (classid)   
23
24#Get Connectors and their Endpoints of the classes A and B
25  IF (nInstClassId=nAClassId) {
26    CC "AQL"  EVAL_AQL_EXPRESSION expr: ("{\"" + sObjName + "\":\"A\"}->\"a2b\"") modelid: (nModelId)
27    SET sConnectedObjIds: (objids) 
28    IF (sConnectedObjIds="") {
29      EXIT
30    }   
31    CC "AQL" EVAL_AQL_EXPRESSION expr: ("{\"" + sObjName + "\":\"A\"}-><\"a2b\">") modelid: (nModelId)
32    SET sConnectorIds: (objids)
33  } ELSE {   
34    CC "AQL" EVAL_AQL_EXPRESSION expr: ("{\"" + sObjName + "\":\"B\"}<-\"a2b\"") modelid: (nModelId)   
35    SET sConnectedObjIds: (objids)
36    IF (sConnectedObjIds="") {
37      EXIT
38    }    
39    CC "AQL" EVAL_AQL_EXPRESSION expr: ("{\"" + sObjName + "\":\"B\"}<-<\"a2b\">") modelid: (nModelId)   
40    SET sConnectorIds: (objids)   
41  }   
42
43  CC "Core" GET_ATTR_VAL objid: (VAL sConnectedObjIds) attrname:"Position"   
44  SET nToObjPosition: (val)
45
46  LEO parse: (nFromObjPosition) get-tmm-value:n_x_fromobj:"x" get-tmm-value:n_y_fromobj:"y"
47         get-tmm-value:n_w_fromobj:"w" get-tmm-value:n_h_fromobj:"h"
48  LEO parse: (nToObjPosition) get-tmm-value:n_x_toobj:"x" get-tmm-value:n_y_toobj:"y"
49         get-tmm-value:n_w_toobj:"w" get-tmm-value:n_h_toobj:"h"
50
51# If your source and target instance are resizebale then request also the height and the width of the instances.
52# Otherwise take the values you defined in the GraphRep.
53# Differenciate if the target and source instance are in the same line -vertically and horizontally
54  IF (n_y_fromobj=(n_y_toobj)) {
55    SET n_xDist: (abs(CMS(n_x_fromobj-n_x_toobj))-(CMS(n_w_fromobj/2)+CMS(n_w_toobj/2)))
56    SET n_yDist: (abs(CMS(n_y_fromobj-n_y_toobj))
57  } ELSIF (n_x_fromobj=n_x_toobj) {
58    SET n_yDist: (abs(CMS(n_y_fromobj-n_y_toobj))-(CMS(n_h_fromobj/2)+CMS(n_h_toobj/2)))
59    SET n_xDist: (abs(CMS(n_x_fromobj-n_x_toobj)))
60  } ELSE {
61    SET n_xDist: (abs(CMS(n_x_fromobj-n_x_toobj))-(CMS(n_w_fromobj/2)+CMS(n_w_toobj/2)))
62    SET n_yDist: (abs(CMS(n_y_fromobj-n_y_toobj))-(CMS(n_h_fromobj/2)+CMS(n_h_toobj/2)))
63  }
64# Calculate the distance with the formula of Pythagoras
65  SET nDistance: (sqrt(pow(n_xDist, 2)+pow(n_yDist, 2)))
66# Set the value of the calculated distance into a hidden attribute so you can call it from the graphrep of the relation
67  CC "Core" GET_CLASS_ID classname:"a2b"SET nRelationClassId: (classid)
68  CC "Core" GET_ATTR_ID classid: (nRelationClassId) attrname:"Distance"
69  SET nDistanceAttrId: (attrid)
70  CC "Core" SET_ATTR_VAL objid: (VAL sConnectorIds) attrid: (nDistanceAttrId) val: (nDistance) 
71}


.
Attachments: ADOxx 1.5 Experimentation Library.abl (16.7k)