diff options
-rw-r--r-- | events/events.cabal | 4 | ||||
-rw-r--r-- | events/src/Events/Eval.hs | 16 | ||||
-rw-r--r-- | events/src/Events/Types.hs | 44 | ||||
-rw-r--r-- | events/src/Main.hs | 1 |
4 files changed, 34 insertions, 31 deletions
diff --git a/events/events.cabal b/events/events.cabal index 22324cd..93a2daf 100644 --- a/events/events.cabal +++ b/events/events.cabal | |||
@@ -18,7 +18,9 @@ cabal-version: >=1.10 | |||
18 | 18 | ||
19 | executable events | 19 | executable events |
20 | main-is: Main.hs | 20 | main-is: Main.hs |
21 | -- other-modules: | 21 | other-modules: Events.Types |
22 | , Events.Types.NDT | ||
23 | , Events.Eval | ||
22 | -- other-extensions: | 24 | -- other-extensions: |
23 | build-depends: base >=4.8 && <5 | 25 | build-depends: base >=4.8 && <5 |
24 | , lens >=4.13 && <5 | 26 | , lens >=4.13 && <5 |
diff --git a/events/src/Events/Eval.hs b/events/src/Events/Eval.hs new file mode 100644 index 0000000..c5bc134 --- /dev/null +++ b/events/src/Events/Eval.hs | |||
@@ -0,0 +1,16 @@ | |||
1 | module Events.Eval | ||
2 | ( evaluate | ||
3 | ) where | ||
4 | |||
5 | import Control.Monad.Fix (MonadFix(mfix)) | ||
6 | |||
7 | import Events.Types | ||
8 | |||
9 | import Data.Maybe (catMaybes) | ||
10 | |||
11 | import Control.Lens | ||
12 | |||
13 | evaluate :: MonadFix m => (Maybe Object -> StateT EvalCtx m Bool) -> Eval m () -> m [Object] | ||
14 | evaluate predicate x = catMaybes <$> mfix x' | ||
15 | where | ||
16 | x' = evalStateT (foldNDT predicate (preview objCtx <$> execStateT x def)) . flip (set ctxEvents) def . catMaybes | ||
diff --git a/events/src/Events/Types.hs b/events/src/Events/Types.hs index 0eff7aa..711e200 100644 --- a/events/src/Events/Types.hs +++ b/events/src/Events/Types.hs | |||
@@ -4,32 +4,28 @@ module Events.Types | |||
4 | ( TimeRange(..), rangeStart, rangeDuration | 4 | ( TimeRange(..), rangeStart, rangeDuration |
5 | , Event(..), payload, occursWithin | 5 | , Event(..), payload, occursWithin |
6 | , EvalCtx(..), ctxEvents | 6 | , EvalCtx(..), ctxEvents |
7 | , ObjCtx(..), objVars, objOccurs, objPayload | 7 | , ObjCtx(..), objVars, objOccurs, objPayload, objCtx |
8 | , Eval, evaluate | 8 | , Eval |
9 | , module Data.Aeson | 9 | , module Data.Aeson |
10 | , module Data.Time.Clock | 10 | , module Data.Time.Clock |
11 | , module Data.Default.Class | 11 | , module Data.Default.Class |
12 | , module Events.Types.NDT | ||
13 | , module Control.Monad.State.Lazy | ||
12 | ) where | 14 | ) where |
13 | 15 | ||
14 | import Control.Lens.TH (makeLenses) | 16 | import Control.Lens.TH (makeLenses) |
17 | import Control.Lens | ||
15 | 18 | ||
16 | import Data.Aeson (Object) | 19 | import Data.Aeson hiding ((.=)) |
17 | 20 | ||
18 | import Data.Time.Clock (UTCTime, NominalDiffTime) | 21 | import Data.Time.Clock |
19 | 22 | ||
20 | import Control.Monad.State.Lazy (StateT, evalStateT, execStateT) | 23 | import Data.Default.Class |
21 | |||
22 | import Data.Default.Class (Default(def)) | ||
23 | |||
24 | -- import Data.Monoid | ||
25 | import Control.Monad.Fix (MonadFix(mfix)) | ||
26 | import Control.Lens ((^.), set) | ||
27 | import Data.Maybe (catMaybes) | ||
28 | |||
29 | -- import Debug.Trace | ||
30 | 24 | ||
31 | import Events.Types.NDT (NDT, foldNDT) | 25 | import Events.Types.NDT (NDT, foldNDT) |
32 | 26 | ||
27 | import Control.Monad.State.Lazy | ||
28 | |||
33 | data TimeRange = TimeRange | 29 | data TimeRange = TimeRange |
34 | { _rangeStart :: UTCTime | 30 | { _rangeStart :: UTCTime |
35 | , _rangeDuration :: NominalDiffTime | 31 | , _rangeDuration :: NominalDiffTime |
@@ -66,21 +62,9 @@ instance Default ObjCtx where | |||
66 | , _objVars = mempty | 62 | , _objVars = mempty |
67 | } | 63 | } |
68 | 64 | ||
69 | objCtx :: ObjCtx -> Maybe Object | 65 | objCtx :: Traversal' ObjCtx Object |
70 | objCtx ctx | 66 | objCtx fObj ctx |
71 | | ctx ^. objOccurs = ctx ^. objPayload | 67 | | _objOccurs ctx = traverseOf (objPayload . _Just) fObj ctx |
72 | | otherwise = Nothing | 68 | | otherwise = pure ctx |
73 | |||
74 | -- type Eval m a = StateT ObjCtx (ListT (StateT EvalCtx m)) a | ||
75 | |||
76 | -- evaluate :: MonadFix m => Eval m () -> m [Object] {- TODO: Switch to `ListT m Object` – `m [Object]` will turn out to be to strict. There is no instance for `MonadFix (ListT m)` – writing one seems to be possible. -} | ||
77 | -- evaluate x = catMaybes <$> mfix x' | ||
78 | -- where | ||
79 | -- x' = evalStateT (ListT.toList (objCtx <$> execStateT x def)) . flip (set ctxEvents) def . catMaybes | ||
80 | 69 | ||
81 | type Eval m a = StateT ObjCtx (NDT (StateT EvalCtx m)) a | 70 | type Eval m a = StateT ObjCtx (NDT (StateT EvalCtx m)) a |
82 | |||
83 | evaluate :: MonadFix m => (Maybe Object -> StateT EvalCtx m Bool) -> Eval m () -> m [Object] {- TODO: Switch to `ListT m Object` – `m [Object]` will turn out to be to strict. There is no instance for `MonadFix (ListT m)` – writing one seems to be possible. -} | ||
84 | evaluate predicate x = catMaybes <$> mfix x' | ||
85 | where | ||
86 | x' = evalStateT (foldNDT predicate (objCtx <$> execStateT x def)) . flip (set ctxEvents) def . catMaybes | ||
diff --git a/events/src/Main.hs b/events/src/Main.hs index a4ffa5a..9e732a5 100644 --- a/events/src/Main.hs +++ b/events/src/Main.hs | |||
@@ -3,6 +3,7 @@ | |||
3 | 3 | ||
4 | import Events.Types | 4 | import Events.Types |
5 | import Events.Spec | 5 | import Events.Spec |
6 | import Events.Eval | ||
6 | 7 | ||
7 | import qualified Data.Yaml as Yaml | 8 | import qualified Data.Yaml as Yaml |
8 | import qualified Data.ByteString.Char8 as CBS | 9 | import qualified Data.ByteString.Char8 as CBS |