« Back

Restrict classes to connect within 1 relation class

Combination View Flat View Tree View
Threads [ Previous | Next ]
toggle
Restrict classes to connect within 1 relation class
community relation class cardinality documentation
Answer
27/03/14 12:13
Is there a solution for below? Thanks in advance!

1.  We have classes A-Z which all are “children” of the master class "Alphabet”.
2. We have a relation class which connects any class to any class as long as they belong to the “Alphabet” class.
3. This relation changes its “name" based on which elements we connect (as we did during the Skype workshop).

Question 1: I need to restrict some classes A-Z from connecting to each other. I know how to do it with different relation classes but I need just one relation in general. (E.g. all classes can connect except for B which cannot be connected to C,T,Y and P which cannot be connected to B,A,K).
Question 2: Is it possible to enter a custom text for the "error" message?

RE: Restrict classes to connect within 1 relation class
on_event event handler beforecreateinstance
Answer
04/12/13 07:18 as a reply to Natalia Sudakova.
Dear Natalia,
yes, there is a solution for the scenario you mentioned. As you know, the ADOxx event "CreateRelationInstance" is triggered after drawing a connector between two objects. There is another ADOxx event, triggered before creating the connector and its name is   "BeforeCreateRelationInstance" . This event allows you to implement decisions before the connector is drawn and even cancel the drawing of the connection if the two objects are incompatible to your criteria.

The event  BeforeCreateRelationInstance is triggered before a connector is created and has the following parameters:
          frominstid (type: INTEGER)        -  the ID of the "from" instance,
          toinstid (type: INTEGER)             - the ID of the "to" instance,
          relationclassid (type: INTEGER) - the ID of the relation class
          componentid (type: INTEGER)   - the ID of the affected components (library, model ...).

The event handler can be exited at any time using one of the following exit values:
    0 - no abortion
   -1 - abortion without error
   -2 - abortion with error
  >0 - abort with core error code

I would recoomend that you implement an evnet handler for this event, similar to the event handler for the event CreateRelationInstance and define the tests that should be made. For the cases that are incompatible, you can abort the creation of the connector using the EXIT command with the value -1.

If you want an error message to be displayed, use the ERRORBOX command before the EXIT command.

Below I send you a simple example for the cases in your inquiry.

 1ON_EVENT "BeforeCreateRelationInstance" {
 2#Event triggered when a relation instance (connector) is created
 3#componentid contains the ID of the affected component
 4#relationclassid the ID of the new relation's class
 5#frominstid the ID of the from-instance of the relation
 6#toinstd the ID of the to-instance of the new relation.
 7#The Event Handler returns one of these values: 0 - no abortion, -1 - abortion without error, -2 - abortion with error, >0 - abort with core error code.
 8
 9SET id_relationclassid: (relationclassid)
10SET id_frominstanceid: (frominstid)
11SET id_toinstanceid: (toinstid)
12SET id_componentid: (componentid)
13
14CC "Core" GET_CLASS_NAME classid: (relationclassid)
15 # --> RESULT ecode: intValue classname: strValue isrel: intValue
16SET str_classname: (classname)
17IF (str_classname = "any2any") {
18  #determine the classname of the start object
19 CC "Core" GET_CLASS_ID objid: (id_frominstanceid)
20  #--> RESULT ecode: intValue classid: intValue isrel: intValue .
21 SET id_fromClassID: (classid)
22 CC "Core" GET_CLASS_NAME classid: (id_fromClassID)
23  #--> RESULT ecode: intValue classname: strValue isrel: intValue .
24 SET str_fromClassName: (classname)
25  #determine the classname of the target object
26 CC "Core" GET_CLASS_ID objid: (id_toinstanceid)
27  #--> RESULT ecode: intValue classid: intValue isrel: intValue .
28 SET id_toClassID: (classid)
29 CC "Core" GET_CLASS_NAME classid: (id_toClassID)
30  #--> RESULT ecode: intValue classname: strValue isrel: intValue .
31 SET str_toClassName: (classname)
32
33 #test "compatibility" between start and target objects
34
35 IF (str_fromClassName = "B") {
36  IF ( (str_toClassName = "C") OR (str_toClassName = "T") OR (str_toClassName = "Y") )   {
37   CC "AdoScript" ERRORBOX "The connector cannot be drawn between the two objects!"
38   EXIT -1
39  }
40 }
41 ELSIF (str_fromClassName = "P")  {
42  IF ( (str_toClassName = "B") OR (str_toClassName = "A") OR (str_toClassName = "K") )     {
43   CC "AdoScript" ERRORBOX "The connector cannot be drawn between the two objects!"
44   EXIT -1
45  }
46 }
47
48}
49}