|
| 1 | +module CFA.CFA where |
| 2 | + |
| 3 | + import Data.List |
| 4 | + import qualified DEA.DEA as DEA |
| 5 | + import DEA.Parsing |
| 6 | + import SMT.SMTLib2 |
| 7 | + import Data.List |
| 8 | + import Control.Monad hiding (guard) |
| 9 | + import Text.Parsec hiding (State, label) |
| 10 | + import Text.Parsec.String |
| 11 | + |
| 12 | + import Parseable |
| 13 | + |
| 14 | + data Event = Epsilon | DEAEvent DEA.Event deriving (Eq, Ord, Show) |
| 15 | +--------- |
| 16 | + instance Parseable Event where |
| 17 | + display Epsilon = "" |
| 18 | + display (DEAEvent ev) = display ev |
| 19 | + {-parser = do char 'ϵ' |
| 20 | + return Epsilon |
| 21 | + <||> do ev <- parser |
| 22 | + return DEAEvent ev-} |
| 23 | +--------------- |
| 24 | +--------------- |
| 25 | +-- --the Z3Asserts here represent the value associated with the parameters of the called CFA |
| 26 | + --data Call = Call State CFA [Z3Assert] | DelegateCall State CFA [Z3Assert] | DynamicCall State [Z3Assert] |
| 27 | + |
| 28 | + --TODO need to model input parameters for more precise model checking |
| 29 | + data Call = Call State CFAReference | DelegateCall State | DynamicCall State deriving (Eq, Ord, Show) |
| 30 | + |
| 31 | + |
| 32 | + data State = State String deriving (Eq, Ord, Show) |
| 33 | +-------- |
| 34 | + instance Parseable State where |
| 35 | + display (State s) = s |
| 36 | + {- parser = do s <- many alphaNum |
| 37 | + return State s-} |
| 38 | +----------------- |
| 39 | +----------------- |
| 40 | + type CFAReference = String |
| 41 | +----------------- |
| 42 | +----------------- |
| 43 | + type Z3Condition = [Z3Construct] |
| 44 | + type Z3Statement = [Z3Construct] |
| 45 | + |
| 46 | + data Transition = |
| 47 | + Transition { |
| 48 | + src, dst :: State, |
| 49 | + condition :: Z3Condition, |
| 50 | + stmt :: Z3Statement, |
| 51 | + event :: Event |
| 52 | + } deriving (Eq, Ord, Show) |
| 53 | +-------- |
| 54 | + instance Parseable Transition where |
| 55 | + {- parser = do src <- parser |
| 56 | + spaces |
| 57 | + string "->" |
| 58 | + dst <- parser |
| 59 | + spaces |
| 60 | + char '[' |
| 61 | + spaces |
| 62 | + string "label" |
| 63 | + spaces |
| 64 | + char '=' |
| 65 | + spaces |
| 66 | + char '"' |
| 67 | + cond <- parser |
| 68 | + spaces |
| 69 | + string ">>" |
| 70 | + spaces |
| 71 | + act <- parser |
| 72 | + spaces |
| 73 | + string ">>" |
| 74 | + spaces |
| 75 | + event <- parser |
| 76 | + char '"' |
| 77 | + spaces |
| 78 | + char ']' |
| 79 | + spaces |
| 80 | + char ';' |
| 81 | + spaces |
| 82 | + return (Transition (src) (dst) (cond) (act) (event))-} |
| 83 | + display (Transition src dst cond act event) = (display src) ++ " -> " ++ (display dst) ++ " [label = \"" ++ (foldr (++) "" $ map display cond) ++ " >> " ++ (foldr (++) "" $ map display act) ++ " >> " ++ (display event) ++ "\"];\n" |
| 84 | +----------------- |
| 85 | +----------------- |
| 86 | + |
| 87 | + data CFA = CFA{ |
| 88 | + name :: String, |
| 89 | + sortDecs :: [SortDeclaration], |
| 90 | + varDecs :: [VarDeclaration], |
| 91 | + transitions :: [Transition], |
| 92 | + states :: [State], |
| 93 | + initial :: State, |
| 94 | + end :: [State], |
| 95 | + calls :: [Call] |
| 96 | + } deriving (Eq, Ord, Show) |
| 97 | +-------- |
| 98 | + instance Parseable CFA where |
| 99 | +{- parser = do sortDecss <- many parser |
| 100 | + varDecss <- many parser |
| 101 | + string "digraph" |
| 102 | + spaces |
| 103 | + char '"' |
| 104 | + spaces |
| 105 | + cfaName <- many alphaNum |
| 106 | + char '"' |
| 107 | + spaces |
| 108 | + char '{' |
| 109 | + spaces |
| 110 | + string "initial" |
| 111 | + spaces |
| 112 | + string "->" |
| 113 | + spaces |
| 114 | + initialState <- parser |
| 115 | + spaces |
| 116 | + char ';' |
| 117 | + spaces |
| 118 | + endStates <- many (parser <* spaces <* string "->" <* spaces <* string "end" <* spaces <* char ';') |
| 119 | + transitionList <- many parser |
| 120 | + spaces |
| 121 | +-- try string "labelloc=\"t\";" |
| 122 | +-- string "label=\"" |
| 123 | +-- spaces |
| 124 | +-- signat <- parser |
| 125 | +-- spaces |
| 126 | +-- string "\";" |
| 127 | +-- spaces |
| 128 | + char '}' |
| 129 | + eof |
| 130 | + return CFA{name = cfaName, sortDecs = sortDecss, varDecs = varDecss, transitions = transitionList, states = statesFromTransitions transitionList [], initial = initialState, end = endStates}-} |
| 131 | + |
| 132 | + display cfa = foldr (++) "" (map display (sortDecs cfa)) ++ |
| 133 | + foldr (++) "" (map display (varDecs cfa)) ++ "\n\n" ++ |
| 134 | + "digraph \"" ++ display (name cfa) ++ "\"{\n" ++ |
| 135 | + foldr (++) "" (map display (transitions cfa)) ++ |
| 136 | + foldr (++) "" (nub [display s ++ "[style=filled, color=gray, label=\"call: " ++ name ++ "\"]" ++ ";\n" | (Call s name) <- calls cfa]) ++ |
| 137 | + foldr (++) "" (nub [display s ++ "[style=filled, color=gray, label=\"delegate call\"]" ++ ";\n" | (DelegateCall s) <- calls cfa]) ++ |
| 138 | + foldr (++) "" (nub [display s ++ "[style=filled, color=gray, label=\"dynamic call\"]" ++ ";\n" | (DynamicCall s) <- calls cfa]) ++ |
| 139 | + foldr (++) "" (nub [display (s) ++ "[style=filled, color=lightgreen]" ++ ";\n" | s <- (end cfa)]) |
| 140 | + ++ "\n}\n" |
| 141 | +----------------- |
| 142 | +----------------- |
| 143 | + |
| 144 | + emptyCFA :: CFA |
| 145 | + emptyCFA = CFA "" [] [] [] [State "0"] (State "0") [State "0"] [] |
0 commit comments