 processFile = function(filepath) {
   rel = vector()
   ren = vector()
   con = file(filepath, "r")
   while ( TRUE ) {
     line = readLines(con, n = 1)
     if ( length(line) == 0 ) {
       break
     }

     if ( grepl("\\[|\\]", line) ) {
       ren = append(ren, line)
       next
     }
     
     if ( grepl("->", line) ) {
       rel = append(rel, line)
       next
     }
     
     else {
       next
     }
     
   }
   
   close(con)
   
   resu = list("name" = ren, "str" = rel)
   return (resu)
 }
 
 TransDottoString = function(dotfile = "/home/zgong001/Documents/Drpark/huang/g.dot"){
   library(bnlearn)
   node = vector()
   nn = vector()
   nc = vector()
   re = processFile(dotfile)
   
   na = re$name
   for(j in 1:length(na)){
     nalist <- na[j] %>%
       gsub("\\s", "", .) %>%
       gsub(";", "", .) %>%
       strsplit(., "\\[|label=|\"|\\]") 
     nn = append(nn, nalist[[1]][1])
     nc = append(nc, nalist[[1]][4])
   }

   rs = re$str
   for (i in 1:length(rs)){
     stlist<- rs[i] %>%
       gsub("\\s", "", .) %>%
       gsub(";", "", .) %>%
       strsplit(., "->") 
     node = append(node, stlist[[1]])
   }
   
#   dag = empty.graph(nodes = unique(node))
#   arc.set <- matrix(node, byrow = TRUE, ncol = 2, dimnames = list(NULL, c("from", "to")))
#   arcs(dag) <- arc.set
#   S = modelstring(dag)
   
   node2 = as.character(factor(node, levels = nn, labels = nc))
   dag2 = empty.graph(nodes = unique(node2))
   arc.set2 <- matrix(node2, byrow = TRUE, ncol = 2, dimnames = list(NULL, c("from", "to")))
   arcs(dag2) <- arc.set2
   S2 = modelstring(dag2)
   
   print(S)
   print(S2)
   return(2)
 }
 

 
 