module Language.HaLex.FaAsDiGraph (
ndfa2graphviz
, ndfa2graphviz2file
, dfa2graphviz
, dfa2graphviz2file
, tographviz
, tographvizIO
, dfa2DiGraphWithNoSyncSt
, dfaDiGraphWithNoSyncStIO
, genOneArrow
) where
import Language.HaLex.RegExp
import Language.HaLex.RegExp2Fa
import Language.HaLex.Ndfa
import Language.HaLex.Dfa
import Language.HaLex.FaOperations
import Language.HaLex.Minimize
ndfa2graphviz ndfa name = tographviz ndfa name "circle" "LR" (show . show)
ndfa2graphviz2file ndfa name = writeFile (name++".dot") (ndfa2graphviz ndfa name)
dfa2graphviz dfa name = tographviz (dfa2ndfa dfa) name "circle" "LR" (show . show)
dfa2graphviz2file dfa name = writeFile (name++".dot") (dfa2graphviz dfa name)
tographviz :: (Eq sy, Show sy, Ord st , Show st)
=> Ndfa st sy
-> [Char]
-> [Char]
-> [Char]
-> (st -> [Char])
-> [Char]
tographviz ndfa@(Ndfa v q s z delta) name shape orientation showState =
"digraph " ++ name ++ " {\n "
++ "rankdir = " ++ orientation ++ " ;\n "
++ (showElemsListPerLine (showStates q)) ++ "\n "
++ (showElemsListPerLine (showInitialStates s)) ++ "\n "
++ (showElemsListPerLine (showFinalStates' z))
++ (showElemsListPerLine (showNdfaArrows'' ndfa))
++ "node [shape=none, lavel=initialState, style = invis];\n"
++ (createInitialArrows (mirroredInitialStates s) s)
++ "\n}"
where
showElemsListPerLine :: [String] -> String
showElemsListPerLine [] = ""
showElemsListPerLine (h:t) = ((showString h) "\n ") ++
(showElemsListPerLine t)
showStates qs = [(showState q) ++
" [shape=" ++ shape ++" , label=" ++ (showState q) ++ " ,color=black];"
| q <- qs , not (ndfaIsStDead delta v z q ) ]
showInitialStates ss = map showInitialState ss
showInitialState s = (showState s)
++ " [shape=" ++ shape ++ " , label= " ++ (showState s)
++ ", color=green];\n "
showFinalStates' zs = [ (showState z)
++ " [shape=double" ++ shape ++" , color=red];" | z <- zs ]
showNdfaArrows' ndfa
= map (\ (o,l,d) -> genOneArrow (showState o) (show l) (showState d))
((groupMoves . transitionTableNdfa) ndfa)
showNdfaArrows'' ndfa@(Ndfa v q s z delta)
= map (\ (o,l,d) -> if (ndfaIsStDead delta v z o) || (ndfaIsStDead delta v z d) then ""
else genOneArrow (showState o) (showListMaybe l) (showState d))
((groupMoves . transitionTableNdfa) ndfa)
mirroredInitialStates = map (\state -> "_newState" ++ (show state))
createInitialArrows [] [] = " "
createInitialArrows (x:xs) (y:ys) = x ++ " -> " ++ (showState y) ++
"[color = green];\n" ++
createInitialArrows xs ys
showListMaybe [] = ""
showListMaybe (x:xs) = case x of
Just a -> (show a) ++ if (showListMaybe xs == "") then ""
else ("," ++ showListMaybe xs)
Nothing -> "Epsilon" ++ if (showListMaybe xs == "") then ""
else ("," ++ showListMaybe xs)
groupMoves [] = []
groupMoves ((o,l,d):rs) = res
where (l',rs') = groupMoves' (o,l,d) ((o,l,d):rs)
res = (o,l',d) : groupMoves rs'
groupMoves' :: (Eq st, Eq sy) => (st,Maybe sy,st) -> [(st,Maybe sy,st)]
-> ([Maybe sy],[(st,Maybe sy,st)])
groupMoves' _ [] = ([],[])
groupMoves' (o,l,d) ((o',l',d'):rs)
| o==o' && d==d' = (new_label,rs')
| otherwise = (l'', (o',l',d') : rs')
where (l'',rs') = groupMoves' (o,l,d) rs
new_label = if l'' == [] then [l']
else l' : l''
showNdfaArrows :: (Ord st,Show st,Show sy) => Ndfa st sy -> [String]
showNdfaArrows (Ndfa vs qs s z delta) = [ genOneArrow (show q) (show v) (show r)
| q <- qs , v <- vs
, r <- delta q (Just v)
, not (ndfaIsStDead delta vs z r )
, not (ndfaIsStDead delta vs z q )
] ++
[ genOneArrow (show q) "Epsilon" (show r)
| q <- qs
, r <- delta q Nothing
, not (ndfaIsStDead delta vs z r )
, not (ndfaIsStDead delta vs z q )
]
genOneArrow orin label dest = orin
++ " -> " ++ dest
++ " [label = " ++ (show label) ++ "];"
tographvizIO ndfa name shape orientation showState =
writeFile (name++".dot") (tographviz ndfa name shape orientation showState)
dfa2DiGraphWithNoSyncSt dfa name = dfa2graphviz dfa name
dfa2DiGraphIO dfa name fn = writeFile (fn++".gph") (dfa2graphviz dfa name )
dfaDiGraphWithNoSyncStIO dfa name fn = writeFile fn (dfa2graphviz dfa name)
dfa2DiGraphIO'' dfa name = dfa2DiGraphIO (beautifyDfa dfa) name name