forked from KarolinDuerr/CNA-ModelingApp
-
Notifications
You must be signed in to change notification settings - Fork 1
/
system.ts
254 lines (218 loc) · 9.61 KB
/
system.ts
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
import { BackingData } from "./backingData.js";
import { BackingService } from "./backingService.js";
import { BrokerBackingService } from "./brokerBackingService.js";
import { Component } from "./component.js";
import { DataAggregate } from "./dataAggregate.js";
import { DeploymentMapping } from "./deploymentMapping.js";
import { Infrastructure } from "./infrastructure.js";
import { Link } from "./link.js";
import { Network } from "./network.js";
import { ProxyBackingService } from "./proxyBackingService.js";
import { RequestTrace } from "./requestTrace.js";
import { Service } from "./service.js";
import { StorageBackingService } from "./storageBackingService.js";
/**
* The module for aspects related to a Component quality model Entity.
* @module entities/system
*/
/**
* Class representing a System entity.
* @class
*/
class System { // TODO use ID's as keys instead of name?
#id: string;
#systemName: string;
#componentEntities: Map<string, Component> = new Map();
#linkEntities: Map<string, Link> = new Map();
#infrastructureEntities: Map<string, Infrastructure> = new Map();
#deploymentMappingEntities: Map<string, DeploymentMapping> = new Map();
#requestTraceEntities: Map<string, RequestTrace> = new Map();
#dataAggregateEntities: Map<string, DataAggregate> = new Map();
#backingDataEntities: Map<string, BackingData> = new Map();
#networkEntities: Map<string, Network> = new Map();
/**
* Create a System entity.
* @param {string} applicationName The name of the application, which the System entity represents.
*/
constructor(id: string, applicationName: string) {
this.#id = id;
this.#systemName = applicationName;
}
/**
* Add a list of quality model entities ({@link Component}, {@link Service}, {@link BackingService}, {@link StorageBackingService}, {@link Link}, {@link Infrastructure},
* {@link DeploymentMapping}, {@link RequestTrace}, {@link DataAggregate} or {@link BackingData}) to the System.
* @param {Array} listOfEntitiesToAdd The list of entities, which should be added to the System entity.
* @throws {TypeError} If a wrong entity type is being provided.
*/
addEntities(listOfEntitiesToAdd: Component[] | Service[] | BackingService[] | StorageBackingService[] | ProxyBackingService[] | BrokerBackingService[] | Link[] | Infrastructure[] | DeploymentMapping[] | RequestTrace[] | DataAggregate[] | BackingData[] | Network[] ) {
for (const newEntity of listOfEntitiesToAdd) {
this.addEntity(newEntity);
}
}
/**
* Add a quality model entity ({@link Component}, {@link Service}, {@link BackingService}, {@link StorageBackingService}, {@link Link}, {@link Infrastructure},
* {@link DeploymentMapping}, {@link RequestTrace}, {@link DataAggregate} or {@link BackingData}) to the System.
* @param {Component|Link|Infrastructure|DeploymentMapping|RequestTrace|DataAggregate|BackingData} entityToAdd The entity that is part of this System entity.
* @throws {TypeError} If a wrong entity type is being provided.
*/
addEntity(entityToAdd: Component | Link | Infrastructure | DeploymentMapping | RequestTrace | DataAggregate | BackingData | Network) {
switch (entityToAdd.constructor) {
case Component:
case Service:
case BackingService:
case StorageBackingService:
case ProxyBackingService:
case BrokerBackingService:
this.#componentEntities.set(entityToAdd.getId, entityToAdd as Component);
break;
case Link:
this.#linkEntities.set(entityToAdd.getId, entityToAdd as Link);
break;
case Infrastructure:
this.#infrastructureEntities.set(entityToAdd.getId, entityToAdd as Infrastructure);
break;
case DeploymentMapping:
this.#deploymentMappingEntities.set(entityToAdd.getId, entityToAdd as DeploymentMapping);
break;
case RequestTrace:
this.#requestTraceEntities.set(entityToAdd.getId, entityToAdd as RequestTrace);
break;
case DataAggregate:
this.#dataAggregateEntities.set(entityToAdd.getId, entityToAdd as DataAggregate);
break;
case BackingData:
this.#backingDataEntities.set(entityToAdd.getId, entityToAdd as BackingData);
break;
case Network:
this.#networkEntities.set(entityToAdd.getId, entityToAdd as Network);
break;
default:
const errorMessage = "Wrong entity type provided. The provided entity was: " + Object.getPrototypeOf(entityToAdd) + JSON.stringify(entityToAdd);
throw new TypeError(errorMessage);
}
}
resetAllIncludedSystemEntities() {
this.#componentEntities = new Map();
this.#linkEntities = new Map();
this.#infrastructureEntities = new Map();
this.#deploymentMappingEntities = new Map();
this.#requestTraceEntities = new Map();
this.#dataAggregateEntities = new Map();
this.#backingDataEntities = new Map();
this.#networkEntities = new Map();
}
get getId() {
return this.#id;
}
set setSystemName(updatedSystemName: string) {
if (!updatedSystemName || !(updatedSystemName.trim())) {
return;
}
this.#systemName = updatedSystemName;
}
get getSystemName() {
return this.#systemName;
}
/**
* Returns the {@link Component} entities included in this System entity.
* @returns {Component}
*/
get getComponentEntities() {
return this.#componentEntities;
}
/**
* Returns the {@link Link} entities included in this System entity.
* @returns {Link}
*/
get getLinkEntities() {
return this.#linkEntities;
}
/**
* Returns the {@link Infrastructure} entities included in this System entity.
* @returns {Infrastructure}
*/
get getInfrastructureEntities() {
return this.#infrastructureEntities;
}
/**
* Returns the {@link DeploymentMapping} entities included in this System entity.
* @returns {DeploymentMapping}
*/
get getDeploymentMappingEntities() {
return this.#deploymentMappingEntities;
}
/**
* Returns the {@link RequestTrace} entities included in this System entity.
* @returns {RequestTrace}
*/
get getRequestTraceEntities() {
return this.#requestTraceEntities;
}
/**
* Returns the {@link DataAggregate} entities included in this System entity.
* @returns {DataAggregate}
*/
get getDataAggregateEntities() {
return this.#dataAggregateEntities;
}
/**
* Returns the {@link BackingData} entities included in this System entity.
* @returns {BackingData}
*/
get getBackingDataEntities() {
return this.#backingDataEntities;
}
get getNetworkEntities() {
return this.#networkEntities;
}
searchComponentOfEndpoint(endpointId: string): Component | undefined {
return Array.from(this.#componentEntities.values()).find(component => {
return !!component.getEndpointEntities.concat(component.getExternalEndpointEntities).find(endpoint => {
return endpoint.getId === endpointId;
})
})
}
getOutgoingLinksOfComponent(componentId: string): Link[] {
return [...this.#linkEntities.values()].filter(link => link.getSourceEntity.getId === componentId);
}
getIncomingLinksOfComponent(componentId: string): Link[] {
let endpointIds = this.#componentEntities.get(componentId).getEndpointEntities.map(endpoint => endpoint.getId);
return [...this.#linkEntities.values()].filter(link => endpointIds.includes(link.getTargetEndpoint.getId));
}
getShortestPathLength(fromComponentId: string, toComponentId: string) {
let alreadyVisited: string[] = [fromComponentId]; // track visited nodes to handle potential circular paths
let nextNodes: {component: Component, currentPath: number}[] = this.getOutgoingLinksOfComponent(fromComponentId)
.map(link => {
return {component: this.searchComponentOfEndpoint(link.getTargetEndpoint.getId), currentPath: 1};
})
.filter(next => !alreadyVisited.includes(next.component.getId));
let shortestPath = this.getComponentEntities.size - 1; //assume longest possible path length if no path can be found
while(nextNodes.length > 0) {
let next = nextNodes[0];
if (next.component.getId === toComponentId) {
// found path :)
shortestPath = next.currentPath;
break;
} else {
// continue search
let nextNextNodes: {component: Component, currentPath: number}[] = this.getOutgoingLinksOfComponent(next.component.getId)
.map(link => {
return {component: this.searchComponentOfEndpoint(link.getTargetEndpoint.getId), currentPath: next.currentPath + 1};
})
.filter(next => !alreadyVisited.includes(next.component.getId));
nextNodes.push(...nextNextNodes);
alreadyVisited.push(next.component.getId);
nextNodes.splice(0, 1);
}
}
return shortestPath;
}
/**
* Transforms the System object into a String.
* @returns {string}
*/
toString() {
return "System " + JSON.stringify(this);
}
}
export { System };