Advertisement
Guest User

Untitled

a guest
May 29th, 2015
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.94 KB | None | 0 0
  1. -- Naive implementation of alpha algorithm for descovering petri-nets from event logs
  2.  
  3. import Control.Monad
  4. import Data.Set (Set)
  5. import qualified Data.Set as Set
  6.  
  7. data Arc p t
  8. = P2T p t
  9. | T2P t p
  10. deriving (Eq, Ord, Show)
  11.  
  12. data Place a = I | O | P (Set a) (Set a)
  13. deriving (Eq, Ord, Show)
  14.  
  15. alpha :: Ord t => Set [t] -> (Set (Place t), Set t, Set (Arc (Place t) t))
  16. alpha ts = (p_l, t_l, f_l)
  17. where
  18. directSuccessions = Set.fromList [(x,y) | sigma <- Set.toList ts, t <- sigma, not (null sigma), (x,y) <- zip sigma (tail sigma)]
  19. x > y = (x,y) `Set.member` directSuccessions
  20. x .->. y = x > y && not (y > x) -- causality
  21. x .||. y = x > y && y > x -- parallel
  22. x # y = not (x > y) && not (y > x) -- choice
  23.  
  24. t_l = Set.fromList [t | sigma <- Set.toList ts, t <- sigma]
  25. t_i = Set.fromList [t | t:_ <- Set.toList ts]
  26. t_o = Set.fromList [t | t:_ <- map reverse (Set.toList ts)]
  27. x_l = Set.fromList $ do
  28. as <- subsets t_l
  29. guard $ not $ Set.null as
  30. bs <- subsets t_l
  31. guard $ not $ Set.null bs
  32. guard $ and [a1 # a2 | a1 <- Set.toList as, a2 <- Set.toList as]
  33. guard $ and [b1 # b2 | b1 <- Set.toList bs, b2 <- Set.toList bs]
  34. guard $ and [a .->. b | a <- Set.toList as, b <- Set.toList bs]
  35. return (as, bs)
  36. y_l = [(as,bs) | (as,bs) <- Set.toList x_l, and [not (as `Set.isSubsetOf` as' && bs `Set.isSubsetOf` bs') || (as==as' && bs==bs') | (as',bs') <- Set.toList x_l]]
  37. p_l = Set.fromList [I, O] `Set.union` Set.fromList [P as bs | (as,bs) <- y_l]
  38. f_l = Set.unions
  39. [ Set.fromList [T2P a (P as bs) | (as,bs) <- y_l, a <- Set.toList as]
  40. , Set.fromList [P2T (P as bs) b | (as,bs) <- y_l, b <- Set.toList bs]
  41. , Set.fromList [P2T I t | t <- Set.toList t_i]
  42. , Set.fromList [T2P t O | t <- Set.toList t_o]
  43. ]
  44.  
  45. subsets :: Ord a => Set a -> [Set a]
  46. subsets xs = foldM f Set.empty (Set.toList xs)
  47. where
  48. f ys x = return ys `mplus` return (Set.insert x ys)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement