-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFantasi.hs
50 lines (43 loc) · 1.82 KB
/
Fantasi.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
{-# LANGUAGE OverloadedStrings #-}
module Main where
import Data.List
import qualified Data.ByteString.Char8 as C (readFile, pack, unpack, ByteString)
import System.Environment
import System.Random
import Poets.Critic
import Poets.Critic.Format
import Poets.Critic.Trees
main :: IO ()
main = do
args <- getArgs
if length args /= 1
then putStrLn "Error: Please provide one file path only"
else prepareFile $ head args
prepareFile :: String -> IO ()
prepareFile path = do
graph <- parseFile path
case getGraph graph of
Nothing -> putStrLn "Error: Graph cannot be parsed"
Just g -> do
--Generate tree
let dis = getDeviceInstances g
treeEdges = buildRandomTree dis
-- putStrLn $ deviceStrings $ snd treeEdges
-- putStrLn $ treeStrings $ fst treeEdges
putStrLn $ show $ dis
putStrLn "Complete"
deviceStrings :: [DeviceInstance] -> String
deviceStrings [] = ""
deviceStrings (d:ds)
| props /= [] = str ++ ">\n\t\t\t<P>" ++ propStr ++ "</P>\n" ++ "\t\t</DevI>" ++ "\n" ++ deviceStrings ds
| otherwise = str ++ "/>" ++ "\n" ++ deviceStrings ds
where
props = deviceProperties d
propStr = unwords $ intersperse ", " $ map (\p -> "\"" ++ deviceProperty p ++ "\": " ++ value p) props
str = "\t\t<DevI type=\"" ++ deviceType d ++ "\" id=\"" ++ deviceInstanceID d ++ "\""
edgeStrings :: [EdgeInstance] -> String
edgeStrings [] = ""
edgeStrings (e:es) = "\t\t<EdgeI path=\"" ++ (deviceInstanceID $ inNode e) ++ ":in-" ++ (deviceInstanceID $ outNode e) ++ ":out\"/>\n" ++ edgeStrings es
treeStrings :: [EdgeInstance] -> String
treeStrings [] = ""
treeStrings (e:es) = "\t\t<EdgeI path=\"" ++ (deviceInstanceID $ inNode e) ++ ":tree_in-" ++ (deviceInstanceID $ outNode e) ++ ":tree_out\"/>\n" ++ treeStrings es