Check for duplicate relationships

Started by malcolm_t_evans, September 29, 2020, 08:42:55 AM

Previous topic - Next topic

malcolm_t_evans

I've written a couple of scripts to import data in relation to technology elements and associated relationships using combination of model.createElement and model.createRelationship (using assignment-relationship, with name of "Assign to", source and targets previously created elements.

Example pseudo data is along the lines of:

Virtual host, Physical host, Virtual host OS, Physical host OS, Virtual host IP, Virtual host FQDN, Virtual host Directory Domain

So from that there are the following relationships:
Virtual host OS ->(assigned to) Virtual host
Physical host OS ->(assigned to) Physical host
Physical host ->(assigned to) Virtual host

So whilst you can check for an existing element via $(".hostname).first() and therefore avoid creating duplicate hosts.  However there are duplicate physical hosts, given that a physical host can host multiple virtual hosts.  However the problem I have is trying to avoid duplicating an existing relationship (predominately between Physical host OS and Physical host).  I've included a code snippet below.  Any guidance or suggestions welcome

Thanks, regards,

Malcolm

// Now create relationship between Operating System and Node (Assignment)
   // First check to see if relationship exists, if not create it
   
   console.log("> At: ",myHypervisor.name," and ",myNode.name);

   var assignRelations = $(".Assigned to");
   var assignedtoHypervisor = assignRelations.rels(myHypervisor);
   console.log(assignedtoHypervisor.size());
   if (assignedtoHypervisor.size() > 0) {
      console.log("> Have Hypervisor relationship: ");
      // Check to see if Node is target end
      console.log(assignedtoHypervisor.targetEnds(myNode).size());s
      if (myNode.inRels("assignment-relationship").size() == 0) {
         var relationship = model.createRelationship("assignment-relationship", "Assigned to",myHypervisor,myNode);
         console.log("> Assign: ",myHypervisor.name," ->",myNode.name);
      }
   } else {
      console.log("> Don't have Hypervisor relationship: ");
      var relationship = model.createRelationship("assignment-relationship", "Assigned to",myHypervisor,myNode);
      console.log("> Assign: ",myHypervisor.name," ->",myNode.name);
   }


Jean-Baptiste Sarrodie

Hi,

Quote from: malcolm_t_evans on September 29, 2020, 08:42:55 AM
However the problem I have is trying to avoid duplicating an existing relationship
[...]
Any guidance or suggestions welcome

I think the easiest way to do this is:

// Assume 'source' is the source element, 'target' the target element, and 'type' is a string matching the relationship type (all this is quite obvious I guess)

relationship = $(source).outRels(type).filter(function(r) {return r.target.equals(target)}).first();


This will avoid creating duplicate by returning only the first relationship matching your criterias.

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.

malcolm_t_evans

Thanks Jean-Baptiste for the direction.  I did manage to come up with the following test code, which I've confirmed works, though not quite aligned to your pseudo code.

Thanks, again, regards,

Malcolm


console.show();
console.clear();

console.log("> Test for Duplicate relationships (create only relationships which don't exist)");

//  Every esx host has an assigned hypervisor (ie hypervisor ->(assigned to) host)
//
//  Though esx host may be duplicated by the export from vCentre, need to ensure that only unique relationships are created during
//  import process.
//
//  Have dummy array to simulate file import, containing duplicate hosts
//  Will create single hypervisor OS element

var hosts = new Array ("f01-esx201","f01-esx101","f01-esx301","f01-esx101","f01-esx301","f01-esx101");

var myOS = model.createElement("system-software","VMware ESXi 5.5.0");



var osHyper = $(".VMware ESXi 5.5.0").first();


hosts.forEach(relationships);

function relationships(host) {

if (!$("."+host).first()) {
var myNode = model.createElement("node",host);
} else {
var myNode =$("."+host).first();
}

console.log("Host: ",host);
console.log("Relationships: ",$(osHyper).outRels("assignment-relationship").size()," ",host);


if ($(osHyper).outRels("assignment-relationship").size() > 0) {
console.log("Have N: ",$(osHyper).outRels("assignment-relationship").targetEnds("node").size(), " relationships");
var matchrelations = 0;

var relations = $(osHyper).outRels("assignment-relationship").each(function(r) {
if (r.target.type === "node") {
console.log("Existing relationship: ",r.name," ",r.target.name);
if (r.target.name == host) {
console.log("Confirmed host match, no relationship created: ",host);
matchrelations++;
}
}

})
if (matchrelations == 0) {
console.log("creating relationship with: ",host);
var relationship = model.createRelationship("assignment-relationship", "Assigned to",osHyper,myNode);
}
} else {
console.log("Don't have relationship with: ",host);
var relationship = model.createRelationship("assignment-relationship", "Assigned to",osHyper,myNode);
}

}



console.log(" == ");

console.log("> Test Relationships");

Jean-Baptiste Sarrodie

Hi,

Quote from: malcolm_t_evans link=topic=919.msg5043#msg5043
though not quite aligned to your pseudo code.

That was not pseudocode at all but real code fragment from an existing script. You don't need more.

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