uni2work.workflows.visualiser/app/Export.hs

61 lines
2.8 KiB
Haskell

-- SPDX-FileCopyrightText: 2023 David Mosbach <david.mosbach@campus.lmu.de>
--
-- SPDX-License-Identifier: AGPL-3.0-or-later
{-# Language OverloadedStrings #-}
module Export where
----------------Imports----------------
import Data.Aeson
import Data.Map hiding (fromList)
import Data.Vector hiding ((!))
import Workflow (NodeData(..), EdgeData(..), GraphData(..), Entry(..))
import Data.Text (pack)
---------------------------------------
---------------Instances---------------
instance ToJSON Entry where
toJSON (Single s) = toJSON s
toJSON (Dict d) = toJSON d
toJSON (List l) = toJSON l
toJSON (Val v) = toJSON v
instance ToJSON NodeData where
toJSON (NData d) = Array (fromList $ foldrWithKey newObject [] d) where
newObject :: String -> Map String Entry -> [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",
"messages" .= values ! "messages",
"payload" .= values ! "payload"]] : result
-- toEncoding = genericToEncoding defaultOptions
instance ToJSON EdgeData where
toJSON (EData d) = Array (fromList $ foldrWithKey newObject [] d) where
newObject :: String -> Map String Entry -> [Value] -> [Value]
newObject ident values result = object [
"id" .= ident,
"name" .= values ! "name",
"source" .= values ! "source",
"target" .= values ! "target",
"actionData" .= object [
"mode" .= values ! "mode",
"actors" .= values ! "actors",
"viewers" .= values ! "viewers",
"actor Viewers" .= values ! "view-actor",
"messages" .= values ! "messages",
"form" .= values ! "form"]] : result
instance ToJSON GraphData where
toJSON (GData (nd, ed)) = object ["states" .= toJSON nd, "actions" .= toJSON ed]
---------------------------------------