StateEquation.hs 2.26 KB
Newer Older
1
module Solver.StateEquation
Philipp Meyer's avatar
Philipp Meyer committed
2
    (checkStateEquationSat)
3
4
5
where

import Data.SBV
6
import Control.Monad
7
8
9
10
11
12

import PetriNet
import Property
import Solver
import Solver.Formula

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
51
52
53
54
placeConstraints :: PetriNet -> VarMap Place -> VarMap Transition -> IntConstraint
placeConstraints net m x =
            liftM bAnd $ mapM checkPlaceEquation $ places net
        where checkPlaceEquation p = do
                mp <- val m p
                incoming <- mapM addTransition $ lpre net p
                outgoing <- mapM addTransition $ lpost net p
                let pinit = literal $ initial net p
                return $ pinit + sum incoming - sum outgoing .== mp
              addTransition (t,w) = do
                  xt <- val x t
                  return $ literal w * xt

nonNegativityConstraints :: PetriNet -> VarMap Place -> VarMap Transition ->
        IntConstraint
nonNegativityConstraints net m x = do
            mnn <- mapM (checkVal m) (places net)
            xnn <- mapM (checkVal x) (transitions net)
            return $ bAnd mnn &&& bAnd xnn
        where checkVal mapping n = do
                mn <- val mapping n
                return $ mn .>= 0

checkTraps :: [Trap] -> VarMap Place -> IntConstraint
checkTraps traps m = do
            tc <- mapM checkTrapDelta traps
            return $ bAnd tc
        where checkTrapDelta trap = do
                mts <- mapM (val m) trap
                return $ sum mts .>= 1

checkStateEquation :: PetriNet -> Formula Place ->
        VarMap Place -> VarMap Transition -> [Trap] ->
        IntConstraint
checkStateEquation net f m x traps = do
        c1 <- placeConstraints net m x
        c2 <- nonNegativityConstraints net m x
        c3 <- checkTraps traps m
        c4 <- evaluateFormula f m
        return $ c1 &&& c2 &&& c3 &&& c4

checkStateEquationSat :: PetriNet -> Formula Place -> [Trap] ->
Philipp Meyer's avatar
Philipp Meyer committed
55
        ConstraintProblem Integer Marking
56
checkStateEquationSat net f traps =
57
58
        let m = makeVarMap $ places net
            x = makeVarMap $ transitions net
Philipp Meyer's avatar
Philipp Meyer committed
59
60
        in  ("state equation", "marking",
             getNames m ++ getNames x,
61
             checkStateEquation net f m x traps,
Philipp Meyer's avatar
Philipp Meyer committed
62
             markingFromAssignment m)
63

Philipp Meyer's avatar
Philipp Meyer committed
64
65
66
markingFromAssignment :: VarMap Place -> IntResult Marking
markingFromAssignment m =
        liftM makeMarking (vals m)
67