1 module Main (main, gain) where
5 import Graphics.UI.WXCore
11 import Data.IntMap (IntMap)
12 import qualified Data.IntMap as IntMap
13 import Data.List (nub)
14 import Data.Maybe (fromJust, fromMaybe)
15 import qualified PersistentDocument as PD
22 import Text.XML.HaXml.XmlContent (XmlContent)
32 do{ state <- State.empty
33 ; NetworkUI.create state () [] [] graphOps
36 instance InfoKind Int () where
38 check n _ i | i<0 = ["Number should not be negative in "++n]
40 instance InfoKind [Int] () where
44 -- A simple range of operations on a graph network.
45 graphOps :: GraphOps () [Int] [Int]
46 graphOps = GraphOps { pureOps = [ ]
47 , ioOps = [ ("Generate textual description"
49 , ("Compile through external compiler", compilePIN)
52 -- Every edge is augmented with the sum of the numbers in its from-node.
53 pushAlongEdge :: IntMap (Node [Int]) -> Edge [Int] -> Edge [Int]
54 nodemap `pushAlongEdge` edge = setEdgeInfo (nub (sum n: getEdgeInfo edge)) edge
55 where n = (getInfo . fromJust . flip IntMap.lookup nodemap . getEdgeFrom)
58 -- Every node is augmented with a list of all the numbers in its incoming edges.
59 accumulateIn :: IntMap (Edge [Int]) -> NodeNr -> Node [Int] -> Node [Int]
60 (edgemap `accumulateIn` nr) node = setInfo (nub (es++getInfo node)) node
61 where es = (concat . IntMap.elems
62 . IntMap.map getEdgeInfo
63 . IntMap.filter (\e-> getEdgeTo e == nr) )
69 compilePIN :: (InfoKind n g, InfoKind e g, XmlContent g, Show g) => IOOp g n e
70 compilePIN doc state =
72 logMessage "Compiling in PIN"
73 pDoc <- getDocument state
74 inName <- PD.getFileName pDoc
75 theFrame <- getNetworkFrame state
77 let fn = changeExt ".INblobs" "-PIN" $ fromMaybe "" inName
79 fnI = fn ++ ".INblobs"
81 writeFile fnI . showRepresentation PIN . simplify $ doc2net doc
82 comm <- textDialog theFrame "Command to invoque external compiler.\nThis special tokens can be used:\n#i for FILE.net\n#o for FILE.INblobs" "Compiling through external compiler" "java pin.compiler.Main #i"
83 when (not $ null comm) $
85 let command = subst fnN fnI comm
91 openNetworkFile fnI state (Just theFrame)
92 >> singleCheckOverIN iNCheck state
93 ExitFailure n -> errorDialog theFrame "Compiling through external compiler"
94 $ "Error compiling through external compiler:\n"
96 where subst inp out [] = []
97 subst inp out ('#':'i':str) = inp ++ subst inp out str
98 subst inp out ('#':'o':str) = out ++ subst inp out str
99 subst inp out (c:str) = c : subst inp out str