Working-but-incomplete DOT output
Getty Ritter
9 years ago
| 1 | {-# LANGUAGE ParallelListComp #-} | |
| 2 | ||
| 1 | 3 | module Main where |
| 2 | 4 | |
| 3 | 5 | import Data.List (intercalate) |
| 95 | 97 | where go n (ReverseGraph t rs) = |
| 96 | 98 | indent n ++ stepName t ++ "\n" ++ concat (map (go (n+2)) rs) |
| 97 | 99 | indent n = replicate n ' ' |
| 98 | stepName (Right t) = T.unpack t | |
| 99 | stepName (Left (IngredientList is)) = | |
| 100 | intercalate "; " [ ingName i | i <- is ] | |
| 101 | ingName (Ingredient (Just amt) name) = | |
| 100 | ||
| 101 | stepName :: Either IngredientList Text -> String | |
| 102 | stepName (Right t) = T.unpack t | |
| 103 | stepName (Left (IngredientList is)) = | |
| 104 | intercalate "; " [ ingName i | i <- is ] | |
| 105 | where ingName (Ingredient (Just amt) name) = | |
| 102 | 106 | T.unpack amt ++ " " ++ T.unpack name |
| 103 | 107 | ingName (Ingredient Nothing name) = |
| 104 | 108 | T.unpack name |
| 105 | 109 | |
| 110 | stepMeta :: Either IngredientList Text -> String | |
| 111 | stepMeta (Right t) = " [label=\"" ++ T.unpack t ++ "\",color=red]" | |
| 112 | stepMeta (Left (IngredientList is)) = | |
| 113 | " [label=\"" ++ intercalate "; " [ ingName i | i <- is ] ++ "\"]" | |
| 114 | where ingName (Ingredient (Just amt) name) = | |
| 115 | T.unpack amt ++ " " ++ T.unpack name | |
| 116 | ingName (Ingredient Nothing name) = | |
| 117 | T.unpack name | |
| 118 | ||
| 119 | dotGraph :: Text -> ReverseGraph -> String | |
| 120 | dotGraph rname gr = ("digraph \"" ++ T.unpack rname ++ "\" {\n") ++ unlines (go "n" 0 gr) ++ "\n}" | |
| 121 | where go :: String -> Int -> ReverseGraph -> [String] | |
| 122 | go parent n (ReverseGraph t rs) = | |
| 123 | let name = parent ++ "_" ++ show n | |
| 124 | children = [ (i, name ++ "_" ++ show i, r) | |
| 125 | | i <- [0..] | |
| 126 | | r <- rs | |
| 127 | ] | |
| 128 | in [ " " ++ name ++ stepMeta t ] ++ | |
| 129 | [ " " ++ cname ++ " -> " ++ name ++ ";" | |
| 130 | | (_, cname, _) <- children | |
| 131 | ] ++ | |
| 132 | concat [ go name i r | |
| 133 | | (i, _, r) <- children | |
| 134 | ] | |
| 135 | ||
| 106 | 136 | showRecipeGraph :: Recipe -> String |
| 107 |
showRecipeGraph |
|
| 137 | showRecipeGraph r@(Recipe name _) = dotGraph name . buildReverseGraph . getChunks $ r | |