-
Notifications
You must be signed in to change notification settings - Fork 0
/
packagetree.js
167 lines (158 loc) · 4.88 KB
/
packagetree.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// define role colors
var roles = {
"Controller": {color: "#6D00FF"},
"Coordinator": {color: "#36C95E"},
"Interfacer": {color: "#C8E11E"},
"Information Holder": {color: "#D04C2F"},
"Service Provider": {color: "#23B9DC"},
"Structurer": {color: "#B64991"}
};
var packagetree = {children: {}, name: 'root', expanded: true};
/**
* Add all classes to packagetree and give packages an id.
*/
function buildPackageTree() {
for (let i = 0; i < classes.length; i++) {
addKeyToTree(classes[i]);
}
giveAllPackagesAnId(packagetree, classes.length + 1);
placement();
//console.log(packagetree);
}
// var reverse_dependencies = [];
// function computeReverseDependencies() {
// for (var i = 0; i < dependencies.length; i++) {
// reverse_dependencies.push({});
// }
// for (var i = 0; i < dependencies.length; i++) {
// for (const [key, value] of Object.entries(dependencies[i])) {
// reverse_dependencies[key][i] = value;
// }
// }
// }
/**
* Adds a class to the packagetree.
* @param {object} cla - class to add to the packagetree.
*/
function addKeyToTree(cla) {
let path = cla['dot_file_ext'].split('.');
var currentelement = packagetree;
for (let i = 0; i < path.length; i++) {
if(!Object.keys(currentelement.children).includes(path[i])) {
if (i < path.length - 1) {
currentelement.children[path[i]] = {name: path[i], children: {}, expanded: true};
} else {
currentelement.children[path[i]] = {name: path[i], children: {}, id: cla.index, label: cla.label, classtype: cla.classtype};
}
} else {
if (i == path.length - 1) {
currentelement.children[path[i]].id = cla.index;
currentelement.children[path[i]].label = cla.label;
currentelement.children[path[i]].classtype = cla.classtype;
}
}
currentelement = currentelement.children[path[i]];
}
}
/**
* Find id in tree.
* @param {object} pkg - package to search through (haystack).
* @param {int} id - needle.
* @returns {(null|object)} null if id not found or package where id == needle.
*/
function findIdInTree(pkg, id) {
if (pkg.id == id) {
return pkg;
}
let foundpkg = null;
let i = 0;
let keys = Object.keys(pkg.children);
while (foundpkg == null && i < keys.length) {
foundpkg = findIdInTree(pkg.children[keys[i]], id);
i++;
}
return foundpkg;
}
/**
* returns list of children ids.
* @param {object} pkg - package.
* @returns {Array} list of children ids including package id.
*/
function getListOfChildren(pkg) {
let childrenlist = [pkg.id];
Object.keys(pkg.children).forEach((key, index) => {
childrenlist = getListOfChildren(pkg.children[key]).concat(childrenlist);
});
return childrenlist;
}
/**
* Finds parent of package or class with passed id.
* @param {object} pkg - package to search in (haystack).
* @param {int} id - needle.
* @param {int} maxdepth - if parent of found id is lower than this depth, return anscestor at this depth.
* @param {int} depth - current depth.
* @returns {(null|int)} null if id not found or -1 if parent should be returned or id >= 0 of parent class.
*/
function findClassParent(pkg, id, maxdepth, depth) {
if (pkg.id == id) {
return -1;
}
let foundpkg = null;
let i = 0;
let keys = Object.keys(pkg.children);
while (foundpkg == null && i < keys.length) {
foundpkg = findClassParent(pkg.children[keys[i]], id, maxdepth, depth+1);
i++;
}
if (foundpkg == null) {
return null;
}
if (Object.keys(pkg).includes('expanded') && pkg.expanded == true && depth <= maxdepth - 1) {
if (foundpkg == -1) {
return pkg.id;
}
return foundpkg;
}
return -1;
}
/**
* Finds package which is the destination of the dependencie in the visualization.
* @param {object} pkg - package to search through (haystack).
* @param {int} id - needle.
* @param {int} maxdepth - if found id is lower than this depth, parent at this depth must be returned.
* @param {int} depth - current depth.
* @returns {(null|object)} null if id is not found or the destination package.
*/
function findDependencieDestination(pkg, id, maxdepth, depth) {
if (pkg.id == id) {
return pkg;
}
let foundpkg = null;
let i = 0;
let keys = Object.keys(pkg.children);
while (foundpkg == null && i < keys.length) {
foundpkg = findDependencieDestination(pkg.children[keys[i]], id, maxdepth, depth+1);
i++;
}
if (foundpkg == null) {
return null;
}
if (Object.keys(pkg).includes('expanded') && pkg.expanded == true && depth <= maxdepth - 1) {
return foundpkg;
}
return pkg;
}
/**
* @param {object} pkg - package.
* @param {int} nextid - int for which no int larger than nextid is already an id.
*/
function giveAllPackagesAnId(pkg, nextid) {
if (!Object.keys(pkg).includes('id')) {
pkg.id = nextid;
nextid++;
}
Object.keys(pkg.children).forEach(key => {
nextid = giveAllPackagesAnId(pkg.children[key], nextid);
});
return nextid;
}