How to get, resolve and verify transitive/cyclic dependency

by Akhilesh Kumar   Last Updated October 16, 2019 08:26 AM - source

I need some custom way to resolve variable which might have transitive and verify cyclic/un-resolvable fields. e.g.

``````var v1 = 'asd';
var v2 = '@@{v1}@@';
var v3 = '@@{v2}@@';
var v4 = '@@{v3}@@';
var v5 = '@@{v4}@@';
var v6 = '@@{v5}@@[email protected]@{v3}@@';

var a1 = '@@{v1}@@[email protected]@{a3}@@';
var a2 = '@@{a1}@@';
var a3 = '@@{a2}@@';

var x1 = 'asd';
var x2 = '@@{x1}@@';
var x3 = '@@{x1}@@[email protected]@{x2}@@';

var p1 = '@@{v4}@@[email protected]@{xyz}@@';
var p2 = '@@{xyz}@@';

const getVariableNames = str => {
str += '';
const variables = str.match(/(@@{[^}{)(\]\[\-+*\/]+?}@@)+?/g);
return variables && variables.length ? variables.map(i => i.substring(3, i.length - 3)) : false;
};

const isCyclic = (myMap, node, target, [...visited] = []) => {
if (myMap[node]) {
if (node === target) {
return true;
}
if (visited.includes(node) || !myMap[node].dep.usedIns) {
return false;
}
visited.push(node);
return myMap[node].dep.usedIns.some(n => isCyclic(myMap, n, target, visited));
}
};

var allVars = [{name:'v1', val: v1}, {name:'v2', val: v2},
{name:'v3', val: v3}, {name:'v4', val: v4}, {name:'v5', val: v5}, {name:'v6', val: v6},
{name:'a1', val: a1}, {name:'a2', val: a2}, {name:'a3', val: a3}, {name:'x1', val: x1}, {name:'x2', val: x2}, {name:'x3', val: x3}];

function getValueMap(arr) {
// need to get implemented
const varNames = arr.map(i=>({...i, v: getVariableNames(i.val)}));
return varNames;
}

var retVar = getValueMap(allVars);

console.log(retVar);
// should console
/* {
v1: { val: 'asd', dependsOn: [], usedIn: ['v2', 'v3', 'v4', 'v5', 'v6', 'p1'], cyclic: [], unresolved: [] },
v2: { val: 'asd', dependsOn: ['v1'], usedIn: ['v3', 'v4', 'v5', 'v6'], cyclic: [], unresolved: [] },
v3: { val: 'asd', dependsOn: ['v2'], usedIn: ['v4', 'v5', 'v6'], cyclic: [], unresolved: [] },
v4: { val: 'asd', dependsOn: ['v3'], usedIn: ['v5', 'v6'], cyclic: [], unresolved: [] },
v5: { val: 'asd', dependsOn: ['v4'], usedIn: ['v6'], cyclic: [], unresolved: [] },
v6: { val: 'asd_asd', dependsOn: ['v5', 'v3'], usedIn: [], cyclic: [], unresolved: [] },

a1: { val: '', dependsOn: ['v1'], usedIn: [], cyclic: ['a1', 'a2', 'a3'], unresolved: [] },
a2: { val: '', dependsOn: [], usedIn: [], cyclic: ['a1', 'a2', 'a3'], unresolved: [] },
a3: { val: '', dependsOn: [], usedIn: [], cyclic: ['a1', 'a2', 'a3'], unresolved: [] },

x1: { val: 'asd', dependsOn: [], usedIn: [], cyclic: [], unresolved: [] },
x2: { val: 'asd', dependsOn: ['x1'], usedIn: ['x3'], cyclic: [], unresolved: [] },
x3: { val: 'asd_asd', dependsOn: ['x1', 'x2'], usedIn: [], cyclic: [], unresolved: [] },

p1: { val: '', dependsOn: ['v4'], usedIn: [], cyclic: [], unresolved: ['xyz'] },
p2: { val: '', dependsOn: [], usedIn: [], cyclic: [], unresolved: ['xyz'] },
};*/``````

My expected output is

``````{
v1: { val: 'asd', dependsOn: [], usedIn: ['v2', 'v3', 'v4', 'v5', 'v6', 'p1'], cyclic: [], unresolved: [] },
v2: { val: 'asd', dependsOn: ['v1'], usedIn: ['v3', 'v4', 'v5', 'v6'], cyclic: [], unresolved: [] },
v3: { val: 'asd', dependsOn: ['v2'], usedIn: ['v4', 'v5', 'v6'], cyclic: [], unresolved: [] },
v4: { val: 'asd', dependsOn: ['v3'], usedIn: ['v5', 'v6'], cyclic: [], unresolved: [] },
v5: { val: 'asd', dependsOn: ['v4'], usedIn: ['v6'], cyclic: [], unresolved: [] },
v6: { val: 'asd_asd', dependsOn: ['v5', 'v3'], usedIn: [], cyclic: [], unresolved: [] },

a1: { val: '', dependsOn: ['v1'], usedIn: [], cyclic: ['a1', 'a2', 'a3'], unresolved: [] },
a2: { val: '', dependsOn: [], usedIn: [], cyclic: ['a1', 'a2', 'a3'], unresolved: [] },
a3: { val: '', dependsOn: [], usedIn: [], cyclic: ['a1', 'a2', 'a3'], unresolved: [] },

x1: { val: 'asd', dependsOn: [], usedIn: [], cyclic: [], unresolved: [] },
x2: { val: 'asd', dependsOn: ['x1'], usedIn: ['x3'], cyclic: [], unresolved: [] },
x3: { val: 'asd_asd', dependsOn: ['x1', 'x2'], usedIn: [], cyclic: [], unresolved: [] },

p1: { val: '', dependsOn: ['v4'], usedIn: [], cyclic: [], unresolved: ['xyz'] },
p2: { val: '', dependsOn: [], usedIn: [], cyclic: [], unresolved: ['xyz'] },
}
``````
Tags :