Main.hs 10.2 KB
Newer Older
1
2
{-# LANGUAGE TupleSections #-}

3
4
module Main where

Philipp Meyer's avatar
Philipp Meyer committed
5
import System.Environment (getArgs)
6
import System.Exit
7
8
9
import System.IO
import System.Console.GetOpt
import Control.Monad
10
import Control.Applicative ((<$>))
Philipp Meyer's avatar
Philipp Meyer committed
11

12
13
import Parser
import qualified Parser.PNET as PNET
Philipp Meyer's avatar
Philipp Meyer committed
14
import qualified Parser.LOLA as LOLA
15
import qualified Parser.TPN as TPN
16
17
import PetriNet
import Property
18
import Solver
19
20
import Solver.StateEquation
import Solver.TrapConstraints
21
22
import Solver.TransitionInvariant
import Solver.SComponent
23

24
25
data InputFormat = PNET | LOLA | TPN deriving (Show,Read)

26
27
28
29
30
data ImplicitProperty = Termination
                      | NoDeadlock | NoDeadlockOutOf String
                      | Safe | Bounded Integer
                      deriving (Show,Read)

31
data Options = Options { inputFormat :: InputFormat
32
                       , optVerbosity :: Int
33
34
                       , optShowHelp :: Bool
                       , optShowVersion :: Bool
35
                       , optProperties :: [ImplicitProperty]
36
                       , optRefine :: Bool
37
38
39
40
                       }

startOptions :: Options
startOptions = Options { inputFormat = PNET
41
                       , optVerbosity = 1
42
43
                       , optShowHelp = False
                       , optShowVersion = False
44
                       , optProperties = []
45
                       , optRefine = True
46
47
48
49
                       }

options :: [ OptDescr (Options -> Either String Options) ]
options =
50
51
52
53
54
55
56
57
58
59
60
        [ Option "" ["pnet"]
        (NoArg (\opt -> Right opt { inputFormat = PNET }))
        "Use the pnet input format"

        , Option "" ["lola"]
        (NoArg (\opt -> Right opt { inputFormat = LOLA }))
        "Use the lola input format"

        , Option "" ["tpn"]
        (NoArg (\opt -> Right opt { inputFormat = TPN }))
        "Use the tpn input format"
61

62
        , Option "" ["termination"]
63
64
65
        (NoArg (\opt -> Right opt {
                           optProperties = Termination : optProperties opt
               }))
66
67
        "Prove termination"

68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
        , Option "" ["no-deadlock"]
        (NoArg (\opt -> Right opt {
                           optProperties = NoDeadlock : optProperties opt
               }))
        "Prove that there is no deadlock"

        , Option "" ["no-deadlock-out-of"]
        (ReqArg (\arg opt -> Right opt {
                           optProperties = NoDeadlockOutOf arg : optProperties opt
                })
                "PLACE")
        "Prove that there is no deadlock unless PLACE is marked"

        , Option "" ["safe"]
        (NoArg (\opt -> Right opt {
                           optProperties = Safe : optProperties opt
               }))
        "Prove that the net is safe, i.e. 1-bounded"

        , Option "" ["bounded"]
        (ReqArg (\arg opt -> case reads arg of
                    [(k, "")] -> Right opt {
                           optProperties = Bounded k : optProperties opt }
                    _ -> Left ("invalid argument for k-bounded option: " ++ arg)
                )
                "K")
        "Prove that the net is K-bounded"

96
97
98
99
        , Option "n" ["no-refinement"]
        (NoArg (\opt -> Right opt { optRefine = False }))
        "Don't use refinement"

100
        , Option "v" ["verbose"]
101
102
103
104
105
106
        (NoArg (\opt -> Right opt { optVerbosity = optVerbosity opt + 1 }))
        "Increase verbosity (may be specified more than once)"

        , Option "q" ["quiet"]
        (NoArg (\opt -> Right opt { optVerbosity = optVerbosity opt - 1 }))
        "Decrease verbosity (may be specified more than once)"
107
108
109
110
111
112
113
114
115
116

        , Option "V" ["version"]
        (NoArg (\opt -> Right opt { optShowVersion = True }))
        "Show version"

        , Option "h" ["help"]
        (NoArg (\opt -> Right opt { optShowHelp = True }))
        "Show help"
        ]

117
118
119
120
verbosePut :: Int -> Int -> String -> IO ()
verbosePut verbosity level str =
        when (verbosity >= level) (putStrLn str)

121
122
123
124
125
126
127
128
parseArgs :: IO (Either String (Options, [String]))
parseArgs = do
        args <- getArgs
        case getOpt Permute options args of
            (actions, files, []) ->
                return $ (,files) <$> foldl (>>=) (return startOptions) actions
            (_, _, errs) -> return $ Left $ concat errs

129
130
131
checkFile :: Parser (PetriNet,[Property]) -> Int -> Bool ->
            [ImplicitProperty] -> String -> IO Bool
checkFile parser verbosity refine implicitProperties file = do
132
        verbosePut verbosity 0 $ "Reading \"" ++ file ++ "\""
133
        (net,properties) <- parseFile parser file
134
135
136
137
        verbosePut verbosity 1 $ "Analyzing " ++ showNetName net
        verbosePut verbosity 2 $
                "Places: " ++ show (length  $ places net) ++ "\n" ++
                "Transitions: " ++ show (length $ transitions net)
138
        let addedProperties = map (makeImplicitProperty net) implicitProperties
139
        rs <- mapM (checkProperty verbosity net refine)
140
                  (addedProperties ++ properties)
141
        verbosePut verbosity 0 ""
142
143
        return $ and rs

144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
placeOp :: Op -> (String, Integer) -> Formula
placeOp op (p,w) = Atom $ LinIneq (Term [Var 1 p]) op (Term [Const w])

makeImplicitProperty :: PetriNet -> ImplicitProperty -> Property
makeImplicitProperty _ Termination = Property "termination" Liveness FTrue
makeImplicitProperty net NoDeadlock =
        Property "no deadlock" Safety $
            foldl (:&:) FTrue
                (map (foldl (:|:) FFalse . map (placeOp Lt) . lpre net)
                     (transitions net))
makeImplicitProperty net (NoDeadlockOutOf pl) =
        Property ("no deadlock out of " ++ pl) Safety $
            placeOp Lt (pl,1) :&: pformula (makeImplicitProperty net NoDeadlock)
makeImplicitProperty net (Bounded k) =
        Property (show k ++ "-bounded") Safety $
            foldl (:|:) FFalse
                (map (\p -> placeOp Gt (p,k)) (places net))
makeImplicitProperty net Safe =
        Property "safe" Safety $ pformula (makeImplicitProperty net (Bounded 1))

164
165
166
checkProperty :: Int -> PetriNet -> Bool -> Property -> IO Bool
checkProperty verbosity net refine p = do
        verbosePut verbosity 1 $ "\nChecking " ++ showPropertyName p
167
        r <- case ptype p of
168
169
170
            Safety -> checkSafetyProperty verbosity net refine (pformula p) []
            Liveness -> checkLivenessProperty verbosity net refine (pformula p) []
        verbosePut verbosity 0 $ showPropertyName p ++
171
                                    if r then " is satisfied."
172
                                    else " may not be satisfied."
173
174
        return r

175
checkSafetyProperty :: Int -> PetriNet -> Bool ->
176
        Formula -> [[String]] -> IO Bool
177
checkSafetyProperty verbosity net refine f traps = do
178
179
180
181
182
        r <- checkSat $ checkStateEquationSat net f traps
        case r of
            Nothing -> return True
            Just a -> do
                let assigned = markedPlacesFromAssignment net a
183
184
                verbosePut verbosity 1 "Assignment found"
                verbosePut verbosity 2 $ "Places marked: " ++ show assigned
185
186
187
188
189
190
191
192
                if refine then do
                    rt <- checkSat $ checkTrapSat net assigned
                    case rt of
                        Nothing -> do
                            putStrLn "No trap found."
                            return False
                        Just at -> do
                            let trap = trapFromAssignment at
193
194
195
196
                            verbosePut verbosity 1 "Trap found"
                            verbosePut verbosity 2 $ "Places in trap: " ++
                                                      show trap
                            checkSafetyProperty verbosity net refine f
197
198
199
200
                                                (trap:traps)
                else
                    return False

201
checkLivenessProperty :: Int -> PetriNet -> Bool ->
202
        Formula -> [([String],[String])] -> IO Bool
203
checkLivenessProperty verbosity net refine f strans = do
204
        r <- checkSat $ checkTransitionInvariantSat net f strans
Philipp Meyer's avatar
Philipp Meyer committed
205
        case r of
206
            Nothing -> return True
207
208
            Just ax -> do
                let fired = firedTransitionsFromAssignment ax
209
210
                verbosePut verbosity 1 "Assignment found"
                verbosePut verbosity 2 $ "Transitions fired: " ++ show fired
211
212
213
214
                if refine then do
                    rt <- checkSat $ checkSComponentSat net fired ax
                    case rt of
                        Nothing -> do
215
                            verbosePut verbosity 1 "No S-component found"
216
217
218
                            return False
                        Just as -> do
                            let sOutIn = getSComponentOutIn net ax as
219
220
221
                            verbosePut verbosity 1 "S-component found"
                            verbosePut verbosity 2 $ "Out/In: " ++ show sOutIn
                            checkLivenessProperty verbosity net refine f
222
223
224
                                                  (sOutIn:strans)
                else
                    return False
225

226
main :: IO ()
Philipp Meyer's avatar
Philipp Meyer committed
227
main = do
228
        putStrLn "SLAPnet - Safety and Liveness Analysis of Petri Nets with SMT solvers\n"
229
230
231
232
233
234
235
236
        args <- parseArgs
        case args of
            Left err -> exitErrorWith err
            Right (opts, files) -> do
                when (optShowVersion opts) (exitSuccessWith "Version 0.01")
                when (optShowHelp opts) (exitSuccessWith
                     (usageInfo "SLAPnet" options))
                when (null files) (exitErrorWith "No input file given")
237
238
                let verbosity = optVerbosity opts
                    refinement = optRefine opts
239
240
                let parser = case inputFormat opts of
                                 PNET -> PNET.parseContent
Philipp Meyer's avatar
Philipp Meyer committed
241
                                 LOLA -> LOLA.parseContent
242
                                 TPN -> TPN.parseContent
243
                let properties = optProperties opts
244
245
                rs <- mapM (checkFile parser verbosity refinement properties)
                          files
246
247
248
249
250
251
252
253
254
255
256
257
258
259
                if and rs then
                    exitSuccessWith "All properties satisfied."
                else
                    exitFailureWith "Some properties may not be satisfied."

exitSuccessWith :: String -> IO ()
exitSuccessWith msg = do
        putStrLn msg
        exitSuccess

exitFailureWith :: String -> IO ()
exitFailureWith msg = do
        putStrLn msg
        exitWith $ ExitFailure 1
Philipp Meyer's avatar
Philipp Meyer committed
260

261
262
263
264
exitErrorWith :: String -> IO ()
exitErrorWith msg = do
        hPutStrLn stderr msg
        exitWith $ ExitFailure 2