import * as fs from "fs" ; import * as $N from "@/core/base/BaseNode" ; import * as $G from "@/core/base/BaseGraph" ; export interface ICSVOutConfig { separator ? : string ; explicit_direction ? : boolean ; direction_mode ? : boolean ; weighted ? : boolean ; } export interface ICSVOutput { _config : ICSVOutConfig ; writeToAdjacencyListFile ( filepath , graph ) : void ; writeToAdjacencyList ( graph ) : string ; writeToEdgeListFile ( filepath , graph , weighted ) : void ; writeToEdgeList ( graph , weighted ) : string ; } /**
 *
 */ class CSVOutput implements ICSVOutput { _config : ICSVOutConfig ; constructor ( config ? ) { this . _config = config || { separator : ( config && config . separator ) || "," , explicit_direction : ( config && config . explicit_direction ) || true , direction_mode : ( config && config . direction_mode ) || false , } ; } writeToAdjacencyListFile ( filepath , graph ) { if ( typeof window !== "undefined" && window !== null ) { throw new Error ( "cannot write to File inside of Browser" ) ; } fs . writeFileSync ( filepath , this . writeToAdjacencyList ( graph ) ) ; } writeToAdjacencyList ( graph ) { let graphString = "" ; let nodes = graph . getNodes ( ) , node = null , adj_nodes = null , adj_node = null ; for ( let node_key in nodes ) { node = nodes [ node_key ] ; graphString += node . getID ( ) ; adj_nodes = node . reachNodes ( this . mergeFunc ) ; for ( let adj_idx in adj_nodes ) { adj_node = adj_nodes [ adj_idx ] . node ; graphString += this . _config . separator + adj_node . getID ( ) ; } graphString += "\n" ; } return graphString ; } writeToEdgeListFile ( filepath , graph , weighted = false ) { if ( typeof window !== "undefined" && window !== null ) { throw new Error ( "cannot write to File inside of Browser" ) ; } fs . writeFileSync ( filepath , this . writeToEdgeList ( graph , weighted ) ) ; } param graph param weighted writeToEdgeList ( graph , weighted = false ) { let graphString = "" , nodes = graph . getNodes ( ) , node = null , adj_nodes = null , adj_entry , adj_node = null , weight_str ; for ( let node_key in nodes ) { node = nodes [ node_key ] ; adj_nodes = node . reachNodes ( this . mergeFunc ) ; for ( let adj_idx in adj_nodes ) { adj_entry = adj_nodes [ adj_idx ] ; adj_node = adj_entry . node ; weight_str = "" ; if ( weighted ) { weight_str = this . _config . separator ; weight_str += adj_entry . edge . isWeighted ( ) ? adj_entry . edge . getWeight ( ) : 1 ; } graphString += node . getID ( ) + this . _config . separator + adj_node . getID ( ) + weight_str + "\n" ; } } return graphString ; } private mergeFunc ( ne ) { return ne . node . getID ( ) ; } } export { CSVOutput } ;