Main.hs 11.8 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
import Data.List (partition)
Philipp Meyer's avatar
Philipp Meyer committed
12

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

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

27
28
data NetTransformation = TerminationToReachability

29
data ImplicitProperty = Termination
30
                      | NoDeadlock | NoDeadlockUnlessFinal
Philipp Meyer's avatar
Philipp Meyer committed
31
                      | ProperTermination
32
33
34
                      | Safe | Bounded Integer
                      deriving (Show,Read)

35
data Options = Options { inputFormat :: InputFormat
36
                       , optVerbosity :: Int
37
38
                       , optShowHelp :: Bool
                       , optShowVersion :: Bool
39
                       , optProperties :: [ImplicitProperty]
40
                       , optTransformations :: [NetTransformation]
41
                       , optRefine :: Bool
42
43
44
45
                       }

startOptions :: Options
startOptions = Options { inputFormat = PNET
46
                       , optVerbosity = 1
47
48
                       , optShowHelp = False
                       , optShowVersion = False
49
                       , optProperties = []
50
                       , optTransformations = []
51
                       , optRefine = True
52
53
54
55
                       }

options :: [ OptDescr (Options -> Either String Options) ]
options =
56
57
58
59
60
61
62
63
64
65
66
        [ 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"
67

68
69
70
71
72
73
        , Option "" ["termination-to-reachability"]
        (NoArg (\opt -> Right opt {
            optTransformations = TerminationToReachability : optTransformations opt
          }))
        "Reduce termination to reachability"

74
        , Option "" ["terminating"]
75
        (NoArg (\opt -> Right opt {
Philipp Meyer's avatar
Philipp Meyer committed
76
                   optProperties = Termination : optProperties opt
77
               }))
78
        "Prove that the net is terminating"
79

Philipp Meyer's avatar
Philipp Meyer committed
80
81
82
83
84
85
        , Option "" ["proper-termination"]
        (NoArg (\opt -> Right opt {
                   optProperties = ProperTermination : optProperties opt
               }))
        "Prove termination in the final marking"

86
        , Option "" ["deadlock-free"]
87
        (NoArg (\opt -> Right opt {
Philipp Meyer's avatar
Philipp Meyer committed
88
                   optProperties = NoDeadlock : optProperties opt
89
               }))
90
        "Prove that the net is deadlock-free"
91

92
        , Option "" ["deadlock-free-unless-final"]
Philipp Meyer's avatar
Philipp Meyer committed
93
94
95
        (NoArg (\opt -> Right opt {
                   optProperties = NoDeadlockUnlessFinal : optProperties opt
               }))
96
97
        ("Prove that the net is deadlock-free\n" ++
         "unless it is in the final marking")
Philipp Meyer's avatar
Philipp Meyer committed
98

99
100
        , Option "" ["safe"]
        (NoArg (\opt -> Right opt {
Philipp Meyer's avatar
Philipp Meyer committed
101
                   optProperties = Safe : optProperties opt
102
103
104
105
106
107
108
109
110
111
112
113
               }))
        "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"

114
115
116
117
        , Option "n" ["no-refinement"]
        (NoArg (\opt -> Right opt { optRefine = False }))
        "Don't use refinement"

118
        , Option "v" ["verbose"]
119
120
121
122
123
124
        (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)"
125
126
127
128
129
130
131
132
133
134

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

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

135
136
137
138
verbosePut :: Int -> Int -> String -> IO ()
verbosePut verbosity level str =
        when (verbosity >= level) (putStrLn str)

139
140
141
142
143
144
145
146
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

147
checkFile :: Parser (PetriNet,[Property]) -> Int -> Bool ->
148
149
            [ImplicitProperty] -> [NetTransformation] -> String -> IO Bool
checkFile parser verbosity refine implicitProperties transformations file = do
150
        verbosePut verbosity 0 $ "Reading \"" ++ file ++ "\""
151
        (net,props) <- parseFile parser file
152
153
154
155
        verbosePut verbosity 1 $ "Analyzing " ++ showNetName net
        verbosePut verbosity 2 $
                "Places: " ++ show (length  $ places net) ++ "\n" ++
                "Transitions: " ++ show (length $ transitions net)
156
157
158
        let props' = props ++ map (makeImplicitProperty net) implicitProperties
        let (net',props'') = foldl transformNet (net,props') transformations
        rs <- mapM (checkProperty verbosity net' refine) props''
159
        verbosePut verbosity 0 ""
160
161
        return $ and rs

162
placeOp :: Op -> (String, Integer) -> Formula
163
placeOp op (p,w) = Atom $ LinIneq (Var p) op (Const w)
164

165
166
167
168
transformNet :: (PetriNet, [Property]) -> NetTransformation ->
               (PetriNet, [Property])
transformNet (net, props) TerminationToReachability = (net, props)

169
makeImplicitProperty :: PetriNet -> ImplicitProperty -> Property
170
makeImplicitProperty _ Termination =
171
172
        Property "termination" Liveness FTrue
makeImplicitProperty net ProperTermination =
Philipp Meyer's avatar
Philipp Meyer committed
173
        let (finals, nonfinals) = partition (null . lpost net) (places net)
174
        in  Property "proper termination" Safety
Philipp Meyer's avatar
Philipp Meyer committed
175
176
            (foldl (:|:) FFalse (map (\p -> placeOp Gt (p,0)) finals) :&:
             foldl (:|:) FFalse (map (\p -> placeOp Gt (p,0)) nonfinals))
177
makeImplicitProperty net NoDeadlock =
178
        Property "no deadlock" Safety $
179
180
181
            foldl (:&:) FTrue
                (map (foldl (:|:) FFalse . map (placeOp Lt) . lpre net)
                     (transitions net))
182
183
184
185
makeImplicitProperty net NoDeadlockUnlessFinal =
        let nodeadlock = makeImplicitProperty net NoDeadlock
            (finals, nonfinals) = partition (null . lpost net) (places net)
        in  Property "no deadlock unless final" Safety $
Philipp Meyer's avatar
Philipp Meyer committed
186
187
188
            (foldl (:&:) FTrue  (map (\p -> placeOp Eq (p,0)) finals) :|:
             foldl (:|:) FFalse (map (\p -> placeOp Gt (p,0)) nonfinals)) :&:
            pformula nodeadlock
189
makeImplicitProperty net (Bounded k) =
190
        Property (show k ++ "-bounded") Safety $
191
192
            foldl (:|:) FFalse
                (map (\p -> placeOp Gt (p,k)) (places net))
193
194
195
makeImplicitProperty net Safe =
        let bounded = makeImplicitProperty net (Bounded 1)
        in  Property "safe" Safety $ pformula bounded
196

197
198
199
checkProperty :: Int -> PetriNet -> Bool -> Property -> IO Bool
checkProperty verbosity net refine p = do
        verbosePut verbosity 1 $ "\nChecking " ++ showPropertyName p
200
        r <- case ptype p of
201
202
203
            Safety -> checkSafetyProperty verbosity net refine (pformula p) []
            Liveness -> checkLivenessProperty verbosity net refine (pformula p) []
        verbosePut verbosity 0 $ showPropertyName p ++
204
                                    if r then " is satisfied."
205
                                    else " may not be satisfied."
206
207
        return r

208
checkSafetyProperty :: Int -> PetriNet -> Bool ->
209
        Formula -> [[String]] -> IO Bool
210
checkSafetyProperty verbosity net refine f traps = do
211
212
213
214
215
        r <- checkSat $ checkStateEquationSat net f traps
        case r of
            Nothing -> return True
            Just a -> do
                let assigned = markedPlacesFromAssignment net a
216
217
                verbosePut verbosity 1 "Assignment found"
                verbosePut verbosity 2 $ "Places marked: " ++ show assigned
218
219
220
221
222
223
224
225
                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
226
227
228
229
                            verbosePut verbosity 1 "Trap found"
                            verbosePut verbosity 2 $ "Places in trap: " ++
                                                      show trap
                            checkSafetyProperty verbosity net refine f
230
231
232
233
                                                (trap:traps)
                else
                    return False

234
checkLivenessProperty :: Int -> PetriNet -> Bool ->
235
        Formula -> [([String],[String])] -> IO Bool
236
checkLivenessProperty verbosity net refine f strans = do
237
        r <- checkSat $ checkTransitionInvariantSat net f strans
Philipp Meyer's avatar
Philipp Meyer committed
238
        case r of
239
            Nothing -> return True
240
241
            Just ax -> do
                let fired = firedTransitionsFromAssignment ax
242
243
                verbosePut verbosity 1 "Assignment found"
                verbosePut verbosity 2 $ "Transitions fired: " ++ show fired
244
245
246
247
                if refine then do
                    rt <- checkSat $ checkSComponentSat net fired ax
                    case rt of
                        Nothing -> do
248
                            verbosePut verbosity 1 "No S-component found"
249
250
251
                            return False
                        Just as -> do
                            let sOutIn = getSComponentOutIn net ax as
252
253
254
                            verbosePut verbosity 1 "S-component found"
                            verbosePut verbosity 2 $ "Out/In: " ++ show sOutIn
                            checkLivenessProperty verbosity net refine f
255
256
257
                                                  (sOutIn:strans)
                else
                    return False
258

259
main :: IO ()
Philipp Meyer's avatar
Philipp Meyer committed
260
main = do
261
        putStrLn "SLAPnet - Safety and Liveness Analysis of Petri Nets with SMT solvers\n"
262
263
264
265
266
267
268
269
        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")
270
271
                let verbosity = optVerbosity opts
                    refinement = optRefine opts
272
273
                let parser = case inputFormat opts of
                                 PNET -> PNET.parseContent
Philipp Meyer's avatar
Philipp Meyer committed
274
                                 LOLA -> LOLA.parseContent
275
                                 TPN -> TPN.parseContent
276
277
278
279
                let properties = reverse $ optProperties opts
                let transformations = reverse $ optTransformations opts
                rs <- mapM (checkFile parser verbosity refinement properties
                                transformations) files
280
281
282
283
284
285
286
287
288
289
290
291
292
                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
293
        exitWith $ ExitFailure 2
Philipp Meyer's avatar
Philipp Meyer committed
294

295
296
297
exitErrorWith :: String -> IO ()
exitErrorWith msg = do
        hPutStrLn stderr msg
298
        exitWith $ ExitFailure 3