Save graphs as images

This commit is contained in:
Joscha 2019-10-26 19:48:21 +00:00
parent 152335e267
commit 027ffa58ab

View file

@ -2,6 +2,7 @@
module Rextra.Visualize where module Rextra.Visualize where
import Control.Monad
import Data.Graph.Inductive import Data.Graph.Inductive
import Data.GraphViz import Data.GraphViz
import Data.List import Data.List
@ -12,6 +13,15 @@ import qualified Rextra.Dfa as Dfa
import qualified Rextra.Nfa as Nfa import qualified Rextra.Nfa as Nfa
import Rextra.Util import Rextra.Util
showDot :: DotGraph Node -> IO ()
showDot dg = runGraphvizCanvas' dg Gtk
saveDot :: GraphvizOutput -> String -> DotGraph Node -> IO ()
saveDot format path dg = void $ runGraphviz dg format path
saveDotAsPng :: String -> DotGraph Node -> IO ()
saveDotAsPng = saveDot Png
{- {-
- Visualizing DFAs - Visualizing DFAs
-} -}
@ -19,7 +29,7 @@ import Rextra.Util
convertDfaState :: (Int, Dfa.State Int Char) -> [LEdge String] convertDfaState :: (Int, Dfa.State Int Char) -> [LEdge String]
convertDfaState (from, state) = convertDfaState (from, state) =
let normalEdges = map (\(t, to) -> (from, to, [t])) . Map.assocs $ Dfa.transitions state let normalEdges = map (\(t, to) -> (from, to, [t])) . Map.assocs $ Dfa.transitions state
defaultEdge = (from, Dfa.defaultTransition state, "default") defaultEdge = (from, Dfa.defaultTransition state, "**")
in defaultEdge : normalEdges in defaultEdge : normalEdges
dfaToGraph :: Dfa.Dfa Int Char -> Gr () String dfaToGraph :: Dfa.Dfa Int Char -> Gr () String
@ -38,8 +48,8 @@ dfaAttributes dfa =
fmtEdge (n1, n2, l) = [toLabel l] fmtEdge (n1, n2, l) = [toLabel l]
in nonClusteredParams { fmtNode = fmtNode, fmtEdge = fmtEdge } in nonClusteredParams { fmtNode = fmtNode, fmtEdge = fmtEdge }
showDfa :: Dfa.Dfa Int Char -> IO () dfaToDot :: Dfa.Dfa Int Char -> DotGraph Node
showDfa dfa = runGraphvizCanvas' (graphToDot (dfaAttributes dfa) (dfaToGraph dfa)) Gtk dfaToDot dfa = graphToDot (dfaAttributes dfa) (dfaToGraph dfa)
{- {-
- Visualizing NFAs - Visualizing NFAs
@ -81,5 +91,5 @@ nfaAttributes nfa =
fmtEdge (n1, n2, l) = [toLabel l] fmtEdge (n1, n2, l) = [toLabel l]
in nonClusteredParams { fmtNode = fmtNode, fmtEdge = fmtEdge } in nonClusteredParams { fmtNode = fmtNode, fmtEdge = fmtEdge }
showNfa :: Nfa.Nfa Int Char -> IO () nfaToDot :: Nfa.Nfa Int Char -> DotGraph Node
showNfa nfa = runGraphvizCanvas' (graphToDot (nfaAttributes nfa) (nfaToGraph nfa)) Gtk nfaToDot nfa = graphToDot (nfaAttributes nfa) (nfaToGraph nfa)