module Ideas.Encoding.Evaluator (Evaluator(..), evalService) where
import Ideas.Common.Library
import Ideas.Encoding.Encoder
import Ideas.Main.Logging
import Ideas.Service.Diagnose
import Ideas.Service.Types
data Evaluator a b c = Evaluator (TypedDecoder a b) (TypedEncoder a c)
data EvalResult a c = EvalResult
{ inputValues :: [TypedValue (Type a)]
, outputValue :: TypedValue (Type a)
, evalResult :: c
}
values :: EvalResult a c -> [TypedValue (Type a)]
values result = outputValue result : inputValues result
logType :: LogRef -> EvalResult a c -> Type a b -> (b -> Record -> Record) -> IO ()
logType logRef res tp f =
case concatMap (findValuesOfType tp) (values res) of
[] -> return ()
hd:_ -> changeLog logRef (f hd)
evalService :: LogRef -> Options a -> Evaluator a b c -> Service -> b -> IO c
evalService logRef opts f srv b = do
res <- eval opts f b (serviceFunction srv)
logType logRef res tState addState
logType logRef res tRule $ \rl r -> r {ruleid = showId rl}
logType logRef res tDiagnosis $ \d r -> r {serviceinfo = show d}
return (evalResult res)
eval :: Options a -> Evaluator a b c -> b -> TypedValue (Type a) -> IO (EvalResult a c)
eval opts (Evaluator dec enc) b = rec
where
rec tv@(val ::: tp) =
case tp of
Const String :|: t ->
either fail (\a -> rec (a ::: t)) val
t1 :-> t2 :-> t3 ->
rec (uncurry val ::: Pair t1 t2 :-> t3)
t1 :-> t2 -> do
a <- run (dec t1) opts b
res <- rec (val a ::: t2)
return res { inputValues = (a ::: t1) : inputValues res }
IO t -> do
a <- val
rec (a ::: t)
_ -> do
c <- run enc opts tv
return $ EvalResult [] tv c