Archi Forum

Archi Plug-ins => jArchi => Topic started by: TimHale on May 09, 2019, 22:26:32 PM

Title: jarchi: how to select object at the far end of a relationship?
Post by: TimHale on May 09, 2019, 22:26:32 PM
I'm having difficulty selecting the object at the far end of a relationship.

I have one or more objects from $(selection).
I'm iterating through the selection objects with a for loop, setting var thisObject = $(selection).get(i);
then I'm iterating through each object's connections with a for loop, setting var thisConnection = $(thisObject).rels().get(j);

Once I have a connection instance, how can I select thisConnection.sourceEnds() or thisConnection.targetEnds(), so that I can retrieve some attributes found there?

If I'm understanding the Archi metamodel correctly, sourceEnds/targetEnds work with the base relationship concepts, not with the connection instance on the view.

Thanks
Tim
Title: Re: jarchi: how to select object at the far end of a relationship?
Post by: Phil Beauvoir on May 09, 2019, 22:35:18 PM
Depends on the selection.

If the selection is from the Model Tree then rels() will return Relationship concepts. And with a Relationship you can get the single source and target at the end of the relationship:

var relationship = $(thisObject).rels().get(j);
var source = relationship.source;
var target= relationship.target;

And all other methods on a concept.

But if the selection is on a View you will get visual connections and objects. You can use the same .source and .target methods but you'll need to get the underlying concept with .concept.
Title: Re: jarchi: how to select object at the far end of a relationship?
Post by: TimHale on May 12, 2019, 14:20:31 PM
this is how I solved it.  Is this what you were describing?

function findConnectionEndObject(thisConnection, position) { // connection is a connection object; position is one of "source" or "target"
   var returnVal = {
      success: false,
      object:     ""
   }
   
   // select the desired connection end in the model tree
   if (position == "source") {
      var testObjectConns = $(thisConnection.concept.source);
   }
   else {
      var testObjectConns = $(thisConnection.concept.target);
   }
   
   // get the count of visual instances the target object has in the entire model
   var countTestObjects = testObjectConns.objectRefs().size();

   for (m = 0; m < countTestObjects; m++) {
   
      var testObject = testObjectConns.objectRefs().get(m);
      
      // get count of visual connections this visual object has in the entire model
      var countConnections = $(testObject).rels().size();
      
      for (n = 0; n < countConnections; n ++) {

         var testConnection = $(testObject).rels().get(n);
         
         // when we match connection id's, we've found the right visual instance on the right view
         if (testConnection.id == thisConnection.id) {
            returnVal.result = true;
            returnVal.object = testObject;
            m = n = Number.MAX_VALUE; // blast the loop variables
         }
      }
   }
   return returnVal;
}
Title: Re: jarchi: how to select object at the far end of a relationship?
Post by: Phil Beauvoir on May 12, 2019, 14:58:48 PM
If your aim is to just find the source or target model concept at the end of a connection on a view you can get these easily with:

sourceConcept = thisConnection.concept.source;

and

targetConcept = thisConnection.concept.target;

Don't understand what the rest of the function is doing?
Title: Re: jarchi: how to select object at the far end of a relationship?
Post by: TimHale on May 13, 2019, 15:32:43 PM
Sigh.
I missed the obvious answer.  I am doing some exploration in terms of moving objects and manipulating bendpoints.   
Based on earlier issues trying to select connections by id, I made an erroneous assumption that I couldn't select the connection target by id either.

Oh well,  I guess I just took the scenic route this weekend.  I learned some other aspects of working with jArchi along the way.

Thanks!