-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#328] [stbx-core] split Transition module from WiringTree
- Loading branch information
Showing
3 changed files
with
77 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
module Statebox.Core.Transition where | ||
|
||
import Prelude | ||
import Data.Array ((:)) | ||
import Data.Foldable (foldr) | ||
import Data.FoldableWithIndex (foldrWithIndex) | ||
import Data.Map as Map | ||
import Data.Maybe (Maybe(..)) | ||
|
||
import Data.ArrayMultiset (ArrayMultiset) | ||
import Statebox.Core.Execution (Path) | ||
import Statebox.Core.Types (PID, TID) | ||
|
||
-- TODO: these types are currently duplicated from Data.Petrinet.Representation.Dict | ||
type TransitionF p tok = | ||
{ pre :: Array (PlaceMarkingF p tok) | ||
, post :: Array (PlaceMarkingF p tok) | ||
} | ||
|
||
type PlaceMarkingF p tok = | ||
{ place :: p | ||
, tokens :: tok | ||
} | ||
|
||
buildTokens :: ∀ a. Ord a => ArrayMultiset a -> ArrayMultiset a -> TransitionF a Int | ||
buildTokens pre post = | ||
{ pre : buildPlaceMarkings pre | ||
, post : buildPlaceMarkings post | ||
} | ||
|
||
buildPlaceMarkings :: ∀ a. Ord a => ArrayMultiset a -> Array (PlaceMarkingF a Int) | ||
buildPlaceMarkings multiset = | ||
let map = foldr (Map.update (Just <<< (_ + 1))) Map.empty multiset | ||
in foldrWithIndex (\place count -> (:) { place: place, tokens: count }) [] map | ||
|
||
type Tokens = Int | ||
|
||
type Transition = | ||
{ path :: Path | ||
, transition :: TID | ||
, name :: String | ||
, tokens :: TransitionF PID Tokens | ||
} | ||
|
||
data Glued a | ||
= Untouched a | ||
| Initial a | ||
| Final a | ||
| Glued a a | ||
|
||
isInitial :: ∀ a. Glued a -> Boolean | ||
isInitial = case _ of | ||
Initial a -> true | ||
_ -> false | ||
|
||
isFinal :: ∀ a. Glued a -> Boolean | ||
isFinal = case _ of | ||
Final a -> true | ||
_ -> false | ||
|
||
gluedTokens :: Glued Transition -> TransitionF PID Tokens | ||
gluedTokens = case _ of | ||
Untouched transition -> transition.tokens | ||
Initial transition -> transition.tokens | ||
Final transition -> transition.tokens | ||
Glued transition1 transition2 -> { pre : transition1.tokens.pre | ||
, post: transition2.tokens.post | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters