Zora 1.1.18 → 1.1.19
raw patch · 2 files changed
+13/−28 lines, 2 files
Files
- Zora.cabal +1/−1
- Zora/Graphing/DAGGraphing.hs +12/−27
Zora.cabal view
@@ -1,5 +1,5 @@ Name: Zora-Version: 1.1.18+Version: 1.1.19 Synopsis: Graphing library wrapper + assorted useful functions Description: A library of assorted useful functions for working with lists, doing mathematical operations and graphing custom data types. Category: Unclassified
Zora/Graphing/DAGGraphing.hs view
@@ -12,7 +12,7 @@ -- Maintainer : bgwines@cs.stanford.edu -- Stability : experimental -- Portability : portable--- +-- -- A typeclass with default implementation for graphing trees with <https://hackage.haskell.org/package/graphviz Haskell GraphViz>. It is intended to be extremely straightforward to graph your data type; you only need to define one simple function (example implementation below). -- @@ -24,12 +24,13 @@ , expand ) where -import Shelly+import Shelly (shelly, run_, setStdin) import System.Directory (removeFile, getDirectoryContents) import Control.Exception import System.IO.Error hiding (catch) import Data.Maybe+import Data.String (fromString) import Data.Tuple import Data.Monoid@@ -62,14 +63,14 @@ expand :: g -> Maybe (Maybe String, [(Maybe String, g)]) -- | Returns whether a node is empty. Sometimes, when declaring algebraic data types, it is desirable to have an \"Empty\" show_node constructor. If your data type does not have an \"Empty\" show_node constructor, just always return @False@.--- +-- -- > is_empty Empty = True -- > is_empty _ = False is_empty :: (DAGGraphable g) => g -> Bool is_empty = isNothing . expand -- | Gets the contents of a node as a string, if it exists. For example,--- +-- -- > show_node (Empty) = error "Empty nodes don't contain values." -- > show_node (Leaf x) = Just x -- > show_node (Node x _ _) =@@ -82,7 +83,7 @@ else fst . fromJust . expand $ node -- | Gets the children of the current node together with edge label information. For example,--- +-- -- > get_children (Empty) = error "Empty nodes don't have children." -- > get_children (Leaf _) = [] -- > get_children (Node _ childrenMap) = map (show . fst) . toList $ childrenMap@@ -127,7 +128,7 @@ . filter (not . is_empty . snd) . get_children $ node- where + where make_edge :: (Maybe String, g) -> LEdge Ly.Text make_edge (str, child) = ( get_label node@@ -171,28 +172,12 @@ , Height 0.1 ] ] -- | Graphs the given string as if it were the contents of a <http://en.wikipedia.org/wiki/DOT_(graph_description_language) DOT> file. Output is written to the specified file. The first parameter is the outfile name, and the second is the contents of the dotfile.-render_dotfile :: String -> String -> IO String-render_dotfile outfile_name dotfile = do- write_dot_file- run_dot_cmd- remove_dot_file- return ("Graphed data structure to " ++ outfile_name)- where- run_dot_cmd :: IO ()- run_dot_cmd = do- shelly $ do cmd "dot" "-Tpng" "graph.dot" (Tx.pack $ "-o" ++ outfile_name)+render_dotfile :: String -> String -> IO ()+render_dotfile outfile_name dotfile = shelly $ do+ setStdin (Tx.pack dotfile)+ Shelly.run_ "dot" ["-Tpng", "-o", fromString outfile_name] - write_dot_file :: IO ()- write_dot_file = do writeFile "graph.dot" $ dotfile - remove_dot_file :: IO ()- remove_dot_file = removeFile "graph.dot" `catch` handleExists- where- handleExists e = if isDoesNotExistError e- then return ()- else throwIO e - -- | Graphs the given @DAGGraphable@ data type to a PDF file. Output is written to the specified file. This is a convenience function that is the composition of @render_dotfile@ and @to_dotfile@.-render :: (Eq g, Show g, DAGGraphable g) => String -> g -> IO String+render :: (Eq g, Show g, DAGGraphable g) => String -> g -> IO () render outfile_name = render_dotfile outfile_name . to_dotfile-