PetriNet.hs 1.31 KB
Newer Older
Philipp Meyer's avatar
Philipp Meyer committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
{-# OPTIONS_GHC -fno-warn-name-shadowing #-}

module PetriNet
    (PetriNet,name,places,transitions,
     makePetriNet)
where

data PetriNet = PetriNet {
        name :: String,
        places :: [String],
        transitions :: [String],
        arcs :: [(String,String,Integer)],
        initial :: [(String,Integer)]
}

instance Show PetriNet where
17 18 19 20 21
        show net = "Petri net" ++
                    (if null (name net) then "" else " " ++ show (name net)) ++
                   "\nPlaces: " ++ unwords (places net) ++
                   "\nTransitions: " ++ unwords (transitions net) ++
                   "\nArcs:\n" ++ unlines
Philipp Meyer's avatar
Philipp Meyer committed
22 23 24 25 26 27 28 29 30 31 32 33 34 35
                        (map (\(l,r,w) -> l ++ " ->" ++
                            (if w /= 1 then "[" ++ show w ++ "]" else []) ++
                            " " ++ r)
                        (arcs net)) ++
                   "Initial: " ++ unwords
                        (map (\(n,i) -> n ++
                            (if i /= 1 then "[" ++ show i ++ "]" else []))
                        (initial net))

makePetriNet :: String -> [String] -> [String] ->
        [(String, String,Integer)] -> [(String, Integer)] -> PetriNet
makePetriNet name places transitions arcs initial =
        PetriNet { name=name, places=places, transitions=transitions,
                   arcs=arcs, initial=initial }