Skip to content

Commit

Permalink
[#328] [studio-common, stbx-protocol] check if transition is enabled …
Browse files Browse the repository at this point in the history
…before firing
  • Loading branch information
marcosh committed Feb 26, 2020
1 parent 398fcb2 commit 66e42fe
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
7 changes: 5 additions & 2 deletions stbx-protocol/src/Statebox/Protocol/Fire.purs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import Data.Either.Nested (type (\/))
import Data.Maybe (maybe)
import Data.NonEmpty (head)

import Data.Petrinet.Representation.Dict (fireAtMarking)
import Data.Petrinet.Representation.Marking (MarkingF)
import Data.Petrinet.Representation.Dict (fireEnabledAtMarking)
import Statebox.Core.Transition (gluedTokens)
import Statebox.Core.Types (Firing, Wiring, PID)
import Statebox.Core.WiringTree (LinearizationError, fromWiring, linearize)
Expand Down Expand Up @@ -38,7 +38,10 @@ fire wiring firing marking = maybe
in maybe
(Left FireTransitionIndexOutOfBounds)
(\gluedTransition ->
Right $ fireAtMarking marking $ gluedTokens gluedTransition)
maybe
(Left FireTransitionNotEnabled)
Right
(fireEnabledAtMarking marking $ gluedTokens gluedTransition))
(index gluedTransitions transitionIndex))
(linearize wiringTree))
(fromWiring wiring)
Expand Down
15 changes: 15 additions & 0 deletions studio-common/src/Data/Petrinet/Representation/Dict.purs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ module Data.Petrinet.Representation.Dict
, fire
, fireAtMarking
, isTransitionEnabled
, fireEnabledAtMarking

, preMarking
, postMarking
Expand Down Expand Up @@ -154,3 +155,17 @@ isTransitionEnabled marking t = isPlaceEnabled `all` t.pre
where
isPlaceEnabled :: PlaceMarkingF pid tok -> Boolean
isPlaceEnabled tp = fromMaybe false $ (>=) <$> marking `tokensAt` tp.place <*> Just tp.tokens

fireEnabledAtMarking
:: p tok
. Ord p
=> Ord tok
=> Semiring tok
=> Group (MarkingF p tok)
=> MarkingF p tok
-> TransitionF p tok
-> Maybe (MarkingF p tok)
fireEnabledAtMarking marking t =
if isTransitionEnabled marking t
then Just $ fireAtMarking marking t
else Nothing

0 comments on commit 66e42fe

Please sign in to comment.