uni2work.workflows.visualiser/app/Export.hs
David Mosbach 7ab262cd2b init
2023-03-11 00:53:26 +01:00

43 lines
1.9 KiB
Haskell

{-# Language OverloadedStrings #-}
module Export where
----------------Imports----------------
import Data.Aeson
import Data.Map hiding (fromList)
import Data.Vector hiding ((!))
import Workflow (NodeData(..), EdgeData(..), GraphData(..))
---------------------------------------
---------------Instances---------------
instance ToJSON NodeData where
toJSON (NData d) = Array (fromList $ foldrWithKey newObject [] d) where
newObject :: String -> Map String String -> [Value] -> [Value]
newObject ident values result = object [
"id" .= ident,
"name" .= values ! "name",
"val" .= show 5, -- Todo adjust to number of edges
"stateData" .= object [
"viewers" .= values ! "viewers",
"final" .= values ! "final"]] : result
-- toEncoding = genericToEncoding defaultOptions
instance ToJSON EdgeData where
toJSON (EData d) = Array (fromList $ foldrWithKey newObject [] d) where
newObject :: String -> Map String String -> [Value] -> [Value]
newObject ident values result = object [
"id" .= ident,
"name" .= values ! "name",
"source" .= values ! "source",
"target" .= values ! "target",
"actionData" .= object [
"mode" .= values ! "mode"]] : result
instance ToJSON GraphData where
toJSON (GData (nd, ed)) = object ["states" .= toJSON nd, "actions" .= toJSON ed]
---------------------------------------