module Data.Registry.Internal.Operations where
import Data.Registry.Internal.Types
import Data.Text as T
import Protolude
type Operations = [AppliedFunction]
data AppliedFunction = AppliedFunction {
_outputValue :: Value
, _inputValues ::[Value]
} deriving (Show)
makeEdges :: Operations -> [(Value, Value)]
makeEdges [] = []
makeEdges (AppliedFunction out ins : rest) =
((out,) <$> ins) <>
makeEdges rest
newtype Dot = Dot {
unDot :: Text
} deriving (Eq, Show)
toDot :: Operations -> Dot
toDot op = Dot $ T.unlines $
[ "strict digraph {"
, " node [shape=record]"
]
<> (toDotEdge <$> makeEdges op)
<> ["}"]
toDotEdge :: (Value, Value) -> Text
toDotEdge (v1, v2) =
adjust (nodeDescription . valDescription $ v1)
<> " -> "
<> adjust (nodeDescription . valDescription $ v2)
<> ";"
nodeDescription :: ValueDescription -> Text
nodeDescription (ValueDescription t Nothing) = t
nodeDescription (ValueDescription t (Just v)) = t <> "\n" <> v
adjust :: Text -> Text
adjust t = "\"" <> (escapeNewlines . removeQuotes) t <> "\""
removeQuotes :: Text -> Text
removeQuotes = T.replace "\"" ""
escapeNewlines :: Text -> Text
escapeNewlines = T.replace "\n" "\\n"