Commit 7a40b4fd authored by Philipp Meyer's avatar Philipp Meyer
Browse files

Using map to store initial marking in Petri net

parent fa4712f6
...@@ -13,9 +13,12 @@ data PetriNet = PetriNet { ...@@ -13,9 +13,12 @@ data PetriNet = PetriNet {
places :: [String], places :: [String],
transitions :: [String], transitions :: [String],
adjacency :: M.Map String ([(String,Integer)], [(String,Integer)]), adjacency :: M.Map String ([(String,Integer)], [(String,Integer)]),
initial :: [(String,Integer)] initMap :: M.Map String Integer
} }
initial :: PetriNet -> String -> Integer
initial net p = M.findWithDefault 0 p (initMap net)
context :: PetriNet -> String -> ([(String, Integer)], [(String, Integer)]) context :: PetriNet -> String -> ([(String, Integer)], [(String, Integer)])
context net x = M.findWithDefault ([],[]) x (adjacency net) context net x = M.findWithDefault ([],[]) x (adjacency net)
...@@ -46,14 +49,15 @@ instance Show PetriNet where ...@@ -46,14 +49,15 @@ instance Show PetriNet where
"Initial: " ++ unwords "Initial: " ++ unwords
(map (\(n,i) -> n ++ (map (\(n,i) -> n ++
(if i /= 1 then "[" ++ show i ++ "]" else [])) (if i /= 1 then "[" ++ show i ++ "]" else []))
(initial net)) (M.toList (initMap net)))
makePetriNet :: String -> [String] -> [String] -> makePetriNet :: String -> [String] -> [String] ->
[(String, String, Integer)] -> [(String, Integer)] -> PetriNet [(String, String, Integer)] -> [(String, Integer)] -> PetriNet
makePetriNet name places transitions arcs initial = makePetriNet name places transitions arcs initial =
let adjacency = foldl buildMap M.empty arcs let adjacency = foldl buildMap M.empty arcs
initMap = M.fromList initial
in PetriNet { name=name, places=places, transitions=transitions, in PetriNet { name=name, places=places, transitions=transitions,
adjacency=adjacency, initial=initial } adjacency=adjacency, initMap=initMap }
where where
buildMap m (l,r,w) = buildMap m (l,r,w) =
let m' = M.insertWith addArc l ([],[(r,w)]) m let m' = M.insertWith addArc l ([],[(r,w)]) m
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment