PetriNet.hs 3.06 KB
Newer Older
Philipp Meyer's avatar
Philipp Meyer committed
1
2
3
{-# OPTIONS_GHC -fno-warn-name-shadowing #-}

module PetriNet
4
    (PetriNet,name,showNetName,places,transitions,initial,
5
     pre,lpre,post,lpost,initials,context,arcs,
Philipp Meyer's avatar
Philipp Meyer committed
6
     makePetriNet,makePetriNetWithTrans)
Philipp Meyer's avatar
Philipp Meyer committed
7
8
where

9
10
import qualified Data.Map as M

Philipp Meyer's avatar
Philipp Meyer committed
11
12
13
14
data PetriNet = PetriNet {
        name :: String,
        places :: [String],
        transitions :: [String],
15
        adjacency :: M.Map String ([(String,Integer)], [(String,Integer)]),
16
        initMap :: M.Map String Integer
Philipp Meyer's avatar
Philipp Meyer committed
17
18
}

19
20
21
initial :: PetriNet -> String -> Integer
initial net p = M.findWithDefault 0 p (initMap net)

22
23
24
25
arcs :: PetriNet -> [(String, String, Integer)]
arcs net = concatMap (\(a,(_,bs)) -> map (\(b,w) -> (a,b,w)) bs)
                           (M.toList (adjacency net))

26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
context :: PetriNet -> String -> ([(String, Integer)], [(String, Integer)])
context net x = M.findWithDefault ([],[]) x (adjacency net)

pre :: PetriNet -> String -> [String]
pre net = map fst . fst . context net

lpre :: PetriNet -> String -> [(String, Integer)]
lpre net = fst . context net

post :: PetriNet -> String -> [String]
post net = map fst . snd . context net

lpost :: PetriNet -> String -> [(String, Integer)]
lpost net = snd . context net

41
42
43
initials :: PetriNet -> [(String,Integer)]
initials net = M.toList (initMap net)

44
45
showNetName :: PetriNet -> String
showNetName net = "Petri net" ++
46
47
               (if null (name net) then "" else " " ++ show (name net))

Philipp Meyer's avatar
Philipp Meyer committed
48
instance Show PetriNet where
49
        show net = showNetName net ++
50
51
52
                   "\nPlaces: " ++ unwords (places net) ++
                   "\nTransitions: " ++ unwords (transitions net) ++
                   "\nArcs:\n" ++ unlines
53
54
55
                        (map (\(s,(l,r)) -> show l ++ " -> " ++
                            s ++ " -> " ++ show r)
                        (M.toList (adjacency net))) ++
Philipp Meyer's avatar
Philipp Meyer committed
56
57
58
                   "Initial: " ++ unwords
                        (map (\(n,i) -> n ++
                            (if i /= 1 then "[" ++ show i ++ "]" else []))
59
                        (M.toList (initMap net)))
Philipp Meyer's avatar
Philipp Meyer committed
60
61

makePetriNet :: String -> [String] -> [String] ->
62
        [(String, String, Integer)] -> [(String, Integer)] -> PetriNet
Philipp Meyer's avatar
Philipp Meyer committed
63
makePetriNet name places transitions arcs initial =
64
        let adjacency = foldl buildMap M.empty arcs
65
            initMap = M.fromList initial
66
        in  PetriNet { name=name, places=places, transitions=transitions,
67
                   adjacency=adjacency, initMap=initMap }
68
69
70
71
72
73
74
        where
            buildMap m (l,r,w) =
              let m'  = M.insertWith addArc l ([],[(r,w)]) m
                  m'' = M.insertWith addArc r ([(l,w)],[]) m'
              in  m''
            addArc (lNew,rNew) (lOld,rOld) = (lNew ++ lOld,rNew ++ rOld)

Philipp Meyer's avatar
Philipp Meyer committed
75
76
77
78
79
80
81
82
makePetriNetWithTrans :: String -> [String] ->
        [(String, [(String, Integer)], [(String, Integer)])] ->
        [(String, Integer)] -> PetriNet
makePetriNetWithTrans name places ts initial =
        let transitions = [ t | (t,_,_) <- ts ]
            arcs = [ (i,t,w) | (t,is,_) <- ts, (i,w) <- is ] ++
                   [ (t,o,w) | (t,_,os) <- ts, (o,w) <- os ]
        in  makePetriNet name places transitions arcs initial