Switch direction of (a) connection(s)

Started by JoCriSem, July 08, 2024, 07:55:03 AM

Previous topic - Next topic

JoCriSem

Anyone has a script to switch the direction of selected connections or can tell me how to do that?
Thanks.

Phil Beauvoir

#1
Assuming you have a connection selected in the UI:

var relation = selection.first().concept;
var target = relation.target;
relation.target = relation.source;
relation.source = target;

Phil
If you value and use Archi, please consider making a donation.
Ask your ArchiMate related questions to the ArchiMate Community's Discussion Board.

Jean-Baptiste Sarrodie

Hi,

Here's another one that also preserves bendpoints on views:

// Swap relationship ends.ajs
//
// Requires jArchi - https://www.archimatetool.com/blog/2018/07/02/jarchi/
//
// This script swaps relationship ends while preserving bendpoints on views
//
// (c) 2024 Jean-Baptiste Sarrodie

var response = window.confirm("This script will swap ends on selected relationships, and will update all views in which they appear. Continue?");

if (!response)
  exit();

selection.filter('relationship').each(function(o) {swapRelationshipEnds(concept(o))});

function swapRelationshipEnds(r) {
  // Check if allowed
  if ($.model.isAllowedRelationship(r.type, r.target.type, r.source.type)) {
    // Change type temporarily (some intermediate states are not valid otherwise)
    var origType = r.type;
    r.type = "association-relationship";
   
    // Swap source and target
    var origSource = r.source;
    var origTarget = r.target;
    r.source = origTarget;
    r.target = origSource;

    // Restore type
    r.type = origType;
   
    // Swap Bendpoints (if any)
    $(r).objectRefs().each(function(o) {
      bps = o.relativeBendpoints;
      o.deleteAllBendpoints();
      var i;
      for (i = 0; i < bps.length; i++) {
        bp = bps[bps.length-i-1];
        new_bp = {};
        new_bp.startX = bp.endX;
        new_bp.startY = bp.endY;
        new_bp.endX = bp.startX;
        new_bp.endY = bp.startY;
        o.addRelativeBendpoint(new_bp, i);
      }
    });
  }
}

function concept(o) {
  if(o.concept)
    return o.concept;
  else
    return o;
}

Regards,

JB
If you value and use Archi, please consider making a donation!
Ask your ArchiMate related questions to the ArchiMate Community's Discussion Board.

Xiaoqi

Hello,

Thanks for the sharing, both scripts are working.

However, I think swapping the relation should be carefully justified, since, firstly, like one triple statement, every [source]-(relation)->[target] should have certain meaning while you're modeling in that way, so from the ontology perspective, the inverse relation may not always providing the correct meaning, e.g. [Alice]-(eats)->[Bread].

So, my view is the needs to swap the relation is due to mistake / wrongly connection, with that it would be make sense to use this automation. Then you need to be noticed, secondly, the swapped relation should be valid, otherwise the script will not work.

When running the script to attached sample view, relation 1 is swapped, while relation 2 is kept since the inverse direction is not allowed.

Xiaoqi