11import { Device } from "./../devices/index" ; // Import the Device class
22import { Edge } from "./../edge" ;
3- import { DataGraph , DeviceId , isRouter } from "./datagraph" ;
3+ import { DataGraph , DeviceId , GraphNode , isRouter } from "./datagraph" ;
44import { Viewport } from "../../graphics/viewport" ;
55import { Layer } from "../devices/device" ;
6- import { createDevice } from "../devices/utils" ;
6+ import { createDevice , layerFromType , layerIncluded } from "../devices/utils" ;
77
88export type EdgeId = number ;
99
10+ function generateConnectionKey ( id1 : number , id2 : number ) : string {
11+ return [ id1 , id2 ] . sort ( ) . join ( "," ) ;
12+ }
13+
14+ function parseConnectionKey ( key : string ) : { id1 : number ; id2 : number } {
15+ const connection : number [ ] = key
16+ . split ( "," )
17+ . map ( ( value ) => parseInt ( value . trim ( ) ) ) ;
18+ return { id1 : connection [ 0 ] , id2 : connection [ 1 ] } ;
19+ }
20+
1021export class ViewGraph {
1122 private devices : Map < DeviceId , Device > = new Map < DeviceId , Device > ( ) ;
1223 private edges : Map < EdgeId , Edge > = new Map < EdgeId , Edge > ( ) ;
@@ -25,27 +36,35 @@ export class ViewGraph {
2536 private constructView ( ) {
2637 // TODO: Adjust construction based on the selected layer in the future
2738 console . log ( "Constructing ViewGraph from DataGraph" ) ;
28- const connections = new Set < { deviceId : DeviceId ; adyacentId : DeviceId } > ( ) ;
39+ const connections = new Set < string > ( ) ;
2940
30- this . datagraph . getDevices ( ) . forEach ( ( [ deviceId , graphNode ] ) => {
31- const deviceInfo = { ...graphNode , id : deviceId } ;
32- const device : Device = createDevice ( deviceInfo , this ) ;
41+ this . datagraph . getDevices ( ) . forEach ( ( graphNode , deviceId ) => {
42+ if ( layerIncluded ( layerFromType ( graphNode . type ) , this . layer ) ) {
43+ const deviceInfo = { ...graphNode , id : deviceId } ;
44+ const device : Device = createDevice ( deviceInfo , this ) ;
3345
34- this . viewport . addChild ( device ) ;
46+ this . viewport . addChild ( device ) ;
3547
36- this . addDevice ( device ) ;
37- graphNode . connections . forEach ( ( adyacentId ) => {
38- if ( ! connections . has ( { deviceId : adyacentId , adyacentId : deviceId } ) ) {
39- connections . add ( { deviceId, adyacentId } ) ;
40- }
41- } ) ;
48+ this . addDevice ( device ) ;
49+
50+ this . layer_dfs (
51+ this . datagraph . getDevices ( ) ,
52+ deviceId ,
53+ deviceId ,
54+ new Set ( [ deviceId ] ) ,
55+ connections ,
56+ ) ;
57+ }
4258 } ) ;
4359
4460 console . log ( "Finished creating devices in ViewGraph" ) ;
45- connections . forEach ( ( { deviceId, adyacentId } ) => {
46- const device1 = this . getDevice ( deviceId ) ;
47- const device2 = this . getDevice ( adyacentId ) ;
48- device1 . connectTo ( device2 . id ) ;
61+ connections . forEach ( ( key ) => {
62+ const connection = parseConnectionKey ( key ) ;
63+ const device1 = this . getDevice ( connection . id1 ) ;
64+ const device2 = this . getDevice ( connection . id2 ) ;
65+ const edge = this . drawEdge ( device1 , device2 ) ;
66+ device1 . addConnection ( edge . id , device2 . id ) ;
67+ device2 . addConnection ( edge . id , device1 . id ) ;
4968 } ) ;
5069 console . log ( "Finished constructing ViewGraph" ) ;
5170 }
@@ -60,6 +79,19 @@ export class ViewGraph {
6079 }
6180 }
6281
82+ drawEdge ( device1 : Device , device2 : Device ) : Edge {
83+ const edge = new Edge (
84+ this . idCounter ++ ,
85+ { n1 : device1 . id , n2 : device2 . id } ,
86+ device1 ,
87+ device2 ,
88+ this ,
89+ ) ;
90+ this . edges . set ( edge . id , edge ) ;
91+ this . viewport . addChild ( edge ) ;
92+ return edge ;
93+ }
94+
6395 // Add a connection between two devices
6496 addEdge ( device1Id : DeviceId , device2Id : DeviceId ) : EdgeId | null {
6597 if ( device1Id === device2Id ) {
@@ -92,17 +124,9 @@ export class ViewGraph {
92124 const device2 = this . devices . get ( device2Id ) ;
93125
94126 if ( device1 && device2 ) {
95- const edge = new Edge (
96- this . idCounter ++ ,
97- { n1 : device1Id , n2 : device2Id } ,
98- device1 ,
99- device2 ,
100- this ,
101- ) ;
102- this . edges . set ( edge . id , edge ) ;
127+ const edge = this . drawEdge ( device1 , device2 ) ;
103128
104129 this . datagraph . addEdge ( device1Id , device2Id ) ;
105- this . viewport . addChild ( edge ) ;
106130
107131 console . log (
108132 `Connection created between devices ID: ${ device1Id } and ID: ${ device2Id } ` ,
@@ -275,4 +299,33 @@ export class ViewGraph {
275299 }
276300 return path . reverse ( ) ;
277301 }
302+
303+ private layer_dfs (
304+ graph : Map < DeviceId , GraphNode > ,
305+ s : number , // source node
306+ v : number ,
307+ visited : Set < number > ,
308+ connections : Set < string > ,
309+ ) {
310+ graph . get ( v ) . connections . forEach ( ( w ) => {
311+ console . log ( `Se accede a ${ w } desde ${ v } ` ) ;
312+ if ( ! visited . has ( w ) ) {
313+ console . log ( `Se visita ${ w } ` ) ;
314+ const adyacent = this . datagraph . getDevice ( w ) ;
315+ // mark node as visited
316+ visited . add ( w ) ;
317+ if ( layerIncluded ( layerFromType ( adyacent . type ) , this . layer ) ) {
318+ // add connection between v and w
319+ const connectionKey : string = generateConnectionKey ( w , s ) ;
320+ if ( ! connections . has ( connectionKey ) ) {
321+ console . log ( `Dispositivos agregados a conexion` ) ;
322+ connections . add ( connectionKey ) ;
323+ }
324+ } else {
325+ // continue with recursive search
326+ this . layer_dfs ( graph , s , w , visited , connections ) ;
327+ }
328+ }
329+ } ) ;
330+ }
278331}
0 commit comments