forked from jkff/minxmod
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathToDot.hs
42 lines (35 loc) · 1.73 KB
/
ToDot.hs
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
module ToDot where
import Types
import StateGraph
import qualified Data.Map as M
import Data.List
safeLookup :: (Ord k, Show k) => k -> M.Map k v -> String -> v
safeLookup k map mapName = case M.lookup k map of
Nothing -> error $ "Key " ++ show k ++ " not in " ++ mapName
Just v -> v
toDot :: StateGraph -> String
toDot g = "digraph g {\n" ++
concat [show i ++ " [label = \"" ++ label i ++ "\"" ++ style ++ "]\n"
| i <- M.keys (sg_index2node g),
let style = case safeLookup i (sg_node2out g) "sg_node2out" of
Nothing -> ", style=dashed"
_ -> ""] ++
concat [show i ++ " -> " ++ show j ++ attr ++ "\n"
| i <- M.keys (sg_index2node g),
j <- case safeLookup i (sg_node2out g) "sg_node2out" of {Just js -> js; Nothing -> []},
let attr = if M.findWithDefault (-1) j (sg_node2prev g) == i
then " [style=bold, color=red, weight=10]"
else " [constraint=false]"] ++
"}"
where
label n = labelToDot $ safeLookup n (sg_index2node g) "index2node"
labelToDot (ProgramState {st_procs=p, st_vars=v, st_mons=m}) =
"V: "++join [v ++ ":" ++ show val
| (v,val) <- M.toList v]++"\\n"++
"M: "++join [m ++ ":" ++ show pid ++ "/" ++ show depth
| (m, MonOccupied pid depth) <- M.toList m]++"\\n"++
"P: "++join [show pid ++ "=" ++ n ++ ":" ++ show ip ++ show stk ++
maybe "" ("?"++) wm
| (pid,(n,Running {proc_ip=ip, proc_stack=stk, proc_waitedMon=wm})) <- M.toList p ]
where
join = concat . intersperse ","