Press n or j to go to the next uncovered block, b, p or k for the previous block.
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 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 | 6x 6x 6x 6x 6x 6x 23x 47x 47x 24x 66x 64x 64x 35x 24x 60x 41x 41x 41x 41x 6x 23x 47x 47x 47x 83x 64x 64x 64x 134x 64x 24x 24x 24x 24x 40x 40x 9x 18x 18x 8x 1x 124x 8x 116x 106x 106x 8x 108x 4x 4x 28x 4x 1x 1x 1x 7x 2x 6x 4x 1x 1x 1x 1x 7x 4x 4x 2x 7x 2x 1x 1x 1x 1x 5x 5x 18x 5x 164x 164x 146x 1x 1x 3x 3x 1x 2x 2x 2x 2x 4x 74x 10x 10x 122x 2x 2x 10x 8x 8x 8x 8x 8x 8x 2x 5x 164x 1x 1x 1x 1x 8x 8x 4x 4x 11x 11x 8x 14x 14x 11x 11x 11x 14x 1x 1x 4x 1x 36x 36x 36x 32x 6x | import * as Interface from '../interface'; import * as Types from '../types'; /** * Класс, представляющий граф элементов. Он строит и обрабатывает граф на основе заданного элемента. * Граф элементов может быть использован для обхода, поиска элементов и анализа соединений. */ class ElementGraph implements Interface.ElementGraph { /** * Массив нод, представляющий граф элементов. */ tree: Types.ElementGraphNode[]; /** * Конструктор, инициализирующий граф и генерирующий его на основе начального элемента. * @param pointElement Элемент, с которого начинается генерация графа. */ constructor(pointElement: Interface.Element) { this.tree = []; this.genGraph(pointElement); } /** * Генерирует граф элементов, начиная с указанного элемента. * @param pointElement Элемент, с которого начинается генерация графа. */ genGraph(pointElement: Interface.Element): void { const set = new Set<Interface.Element>(); this.findGenerators(pointElement, set); const arregn: Types.ElementGraphNode[] = []; for (let i = 0; i < this.tree.length; i++) { this.genGraphNode(this.tree[i], arregn); } } /** * Рекурсивно ищет генераторы (начальные элементы) в графе и строит дерево элементов. * @param pointElement Элемент, с которого начинается поиск. * @param set Множество элементов, уже обработанных для предотвращения циклов. * @returns Массив нод графа. */ private findGenerators( pointElement: Interface.Element, set: Set<Interface.Element> ): Types.ElementGraphNode[] { set.add(pointElement); if (pointElement.in_connections !== undefined) { for (let i = 0; i < pointElement.in_connections.length; i++) { if (typeof pointElement.in_connections[i] !== 'string') { const elem = (pointElement.in_connections[i] as Interface.Connection).out .element; if (!set.has(elem)) { this.findGenerators(elem, set); } } } for (let i = 0; i < pointElement.out_connections.length; i++) { if (pointElement.out_connections[i].in) { for ( let j = 0; j < (pointElement.out_connections[i].in as Types.SourcesArray).length; j++ ) { const elem = (pointElement.out_connections[i].in as Types.SourcesArray)[j] .element; if (!set.has(elem)) { this.findGenerators(elem, set); } } } } } else { this.tree.push({element: pointElement, connection: [], out: []}); } return this.tree; } /** * Генерирует ноды графа и их связи. * @param node Текущая нода для обработки. * @param arregn Массив уже обработанных нод для предотвращения повторов. */ private genGraphNode(node: Types.ElementGraphNode, arregn: Types.ElementGraphNode[]) { const pointElement = node.element; for (let j = 0; j < pointElement.out_connections.length; j++) { if (pointElement.out_connections[j].in) { for ( let k = 0; k < (pointElement.out_connections[j].in as Types.SourcesArray).length; k++ ) { const elem = (pointElement.out_connections[j].in as Types.SourcesArray)[k] .element; const egn = arregn.find((el) => el.element === elem); if (!egn) { const newNode = { element: elem, connection: [pointElement.out_connections[j]], out: [] }; node.out.push(newNode); arregn.push(newNode); this.genGraphNode(newNode, arregn); } else { egn.connection.push(pointElement.out_connections[j]); node.out.push(egn); } } } } } /** * Находит элемент в графе. * @param pointElement Элемент для поиска. * @returns Нода графа с указанным элементом или `false`, если элемент не найден. */ findElement(pointElement: Interface.Element): Types.ElementGraphNode | false { for (let i = 0; i < this.tree.length; i++) { const e = this.findElementNode(pointElement, this.tree[i]); if (e) { return e; } } return false; } /** * Рекурсивно находит элемент в графе. * @param pointElement Элемент для поиска. * @param firstNode Нода, с которой начинается поиск. * @returns Нода графа с указанным элементом или `false`, если элемент не найден. */ private findElementNode( pointElement: Interface.Element, firstNode: Types.ElementGraphNode ): Types.ElementGraphNode | false { if (pointElement === firstNode.element) { return firstNode; } for (let j = 0; j < firstNode.out.length; j++) { const e = this.findElementNode(pointElement, firstNode.out[j]); if (e) { return e; } } return false; } /** * Получаем массив элементов из элементов нод * @param na элементы нод * @returns массив элементов */ private getElementArrayFromNodeArray(na: Types.ElementGraphNode[]): Interface.Element[] { const ret: Interface.Element[] = []; for (let i = 0; i < na.length; i++) { ret.push(na[i].element); } return ret; } /** * Возвращает массив выходов, которые ни к чему не подключены. * @returns Массив соединений. */ getOutputs(): Interface.Connection[] { const it = this.getSetNodeDFS(); const ret: Interface.Connection[] = []; for (let i = 0; i < it.length; i++) { if (it[i].element.out_connections.length !== it[i].out.length) { for (let j = 0; j < it[i].element.out_connections.length; j++) { if (!it[i].element.out_connections[j].in) { ret.push(it[i].element.out_connections[j]); } } } } return ret; } /** * Возвращает массив входов, которые ни к чему не подключены. * @returns Массив соединений. */ getInputs(): Types.SourcesArray { const it = this.getSetNodeDFS(); const ret: Types.SourcesArray = []; for (let i = 0; i < it.length; i++) { if ('in_connections' in it[i].element) { const nuo = it[i].element.in_connections as (string | Interface.Connection)[]; if (nuo.length !== it[i].out.length) { for (let j = 0; j < nuo.length; j++) { if (typeof nuo[j] === 'string') { ret.push({name: nuo[j] as string, element: it[i].element}); } } } } } return ret; } /** * Возвращает массив генераторов графа. * @returns Массив генераторов (начальных элементов). */ getGenerators(): Interface.Element[] { return this.getElementArrayFromNodeArray(this.tree); } /** * Возвращает обход графа в глубину. * @returns Массив элементов графа. */ getAllElementsDFS(): Interface.Element[] { return this.getElementArrayFromNodeArray(this.getSetNodeDFS()); } /** * Возвращает обход графа в ширину. * @returns Массив элементов графа. */ getAllElementsBFS(): Interface.Element[] { return this.getElementArrayFromNodeArray(this.getAllNodeBFS()); } /** * Возвращает массив всех нод в глубину. * @returns Массив нод графа. */ getAllNodeDFS(): Types.ElementGraphNode[] { const ret: Types.ElementGraphNode[] = []; for (let i = 0; i < this.tree.length; i++) { this.DFSrec(this.tree[i], ret); } return ret; } /** * Рекурсивно обходит граф в глубину. * @param node Нода для обработки. * @param ret Массив результата обхода. */ private DFSrec(node: Types.ElementGraphNode, ret: Types.ElementGraphNode[]): void { ret.push(node); for (let j = 0; j < node.out.length; j++) { this.DFSrec(node.out[j], ret); } } /** * Находит все соединения, к которым подключена данная нода * @param node Данная нода * @returns Массив соединений */ getConnectionsNode(node: Types.ElementGraphNode): Interface.Connection[] { const ret: Interface.Connection[] = []; for (let i = 0; i < node.element.out_connections.length; i++) { Eif (node.element.out_connections[i].in) { ret.push(node.element.out_connections[i]); } } return ret; } /** * Возвращает массив всех нод в ширину. * @returns Массив нод графа. */ getAllNodeBFS(): Types.ElementGraphNode[] { const ret = [...this.tree]; const allel = this.getSetNodeDFS(); const conn: Interface.Connection[] = []; while (allel.length > 0) { for (let i = 0; i < allel.length; i++) { if (!ret.find((el) => el === allel[i])) { let fl = true; for (let j = 0; j < allel[i].connection.length; j++) { if (!ret.find((el) => el.element === allel[i].connection[j].out.element)) { fl = false; break; } } if (fl) { ret.push(allel[i]); conn.push(...allel[i].connection); allel.splice(i, 1); i--; } } else { allel.splice(i, 1); i--; } } } return ret; } /** * Возвращает уникальные ноды графа после обхода в глубину. * @returns Массив уникальных нод графа. */ getSetNodeDFS(): Types.ElementGraphNode[] { const arr = this.getAllNodeDFS(); return arr.filter((value, index, self) => self.indexOf(value) === index); } /** * Экспортирует граф для фронта * @returns Экспортированный граф */ getDataElementGraph(): Types.dataElementGraph { const nodes = this.getAllNodeBFS(); const elems = this.getElementArrayFromNodeArray(nodes); const elements: Types.exportElements = []; for (let i = 0; i < nodes.length; i++) { elements.push({ name: nodes[i].element.name as string, id: i, connections_in: [] as {conn_name: string; id: number}[], connections_out: [] as {conn_name: string; id: number[]}[] }); if (nodes[i].element.in_connections) { const thisConns = nodes[i].element.in_connections as ( | string | Interface.Connection )[]; for (let j = 0; j < thisConns.length; j++) { Iif (typeof thisConns[j] === 'string') { elements[i].connections_in.push({ conn_name: thisConns[j] as string, id: -1 }); } else { elements[i].connections_in.push({ conn_name: (thisConns[j] as Interface.Connection).findInString( nodes[i].element ), id: elems.indexOf((thisConns[j] as Interface.Connection).out.element) }); } } } for (let j = 0; j < nodes[i].element.out_connections.length; j++) { const ids = []; if (nodes[i].element.out_connections[j].in) { for ( let k = 0; k < (nodes[i].element.out_connections[j].in as Types.SourcesArray).length; k++ ) { ids.push( elems.indexOf( (nodes[i].element.out_connections[j].in as Types.SourcesArray)[k] .element ) ); } } elements[i].connections_out.push({ conn_name: nodes[i].element.out_connections[j].out.name, id: ids }); } } const eeg: Types.exportElementGraph = []; for (let i = 0; i < this.tree.length; i++) { this.recExportElementGraph(this.tree[i], eeg, elems); } return { elements: elements, elementGraph: eeg }; } /** * Рекурсивно обходим граф в глубину * @param node * @param ret * @param elems */ private recExportElementGraph( node: Types.ElementGraphNode, ret: Types.exportElementGraph, elems: Interface.Element[] ) { const id = elems.indexOf(node.element); ret.push({ id: id, out: [] }); for (let j = 0; j < node.out.length; j++) { this.recExportElementGraph(node.out[j], ret[ret.length - 1].out, elems); } } } export {ElementGraph}; |