crb_agent/
runtime.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
use crate::agent::Agent;
use crate::context::AgentContext;
use crate::performers::{Transition, TransitionCommand};
use anyhow::{Error, Result};
use async_trait::async_trait;
use crb_runtime::kit::{
    Context, Failures, InteractiveRuntime, InteractiveTask, Interruptor, Runtime, Task,
};
use futures::stream::Abortable;

pub struct RunAgent<T: Agent> {
    pub agent: Option<T>,
    pub context: T::Context,
    pub failures: Failures,
}

impl<T: Agent> RunAgent<T> {
    pub fn new(agent: T) -> Self
    where
        T::Context: Default,
    {
        Self {
            agent: Some(agent),
            context: T::Context::default(),
            failures: Failures::default(),
        }
    }
}

impl<T: Agent> Task<T> for RunAgent<T> {}
impl<A: Agent> InteractiveTask<A> for RunAgent<A> {}

#[async_trait]
impl<A: Agent> InteractiveRuntime for RunAgent<A> {
    type Context = A::Context;

    fn address(&self) -> <Self::Context as Context>::Address {
        self.context.address().clone()
    }
}

impl<T: Agent> RunAgent<T> {
    async fn perform_routine(&mut self) -> Result<(), Error> {
        let reg = self.context.session().controller.take_registration()?;
        let fut = self.perform_task();
        let output = Abortable::new(fut, reg).await??;
        // TODO: Distribute outputs
        // TODO: Call finalizers to deliver the result
        // TODO: The default finalizer is = oneshot address self channel!!!!!
        self.context.session().joint.report(output)?;
        Ok(())
    }

    async fn perform_task(&mut self) -> Result<T::Output, Error> {
        if let Some(mut agent) = self.agent.take() {
            // let session = self.context.session();

            // Initialize
            let initial_state = agent.initialize(&mut self.context);
            let mut pair = (agent, Some(initial_state));

            // Events or States
            while self.context.session().controller.is_active() {
                let (mut agent, next_state) = pair;
                if let Some(mut next_state) = next_state {
                    let res = next_state
                        .transition
                        .perform(agent, &mut self.context)
                        .await;
                    match res {
                        Transition::Continue { mut agent, command } => match command {
                            TransitionCommand::Next(Ok(next_state)) => {
                                pair = (agent, Some(next_state));
                            }
                            TransitionCommand::Next(Err(err)) => {
                                let (agent, next_state) =
                                    next_state.transition.fallback(agent, err).await;
                                pair = (agent, Some(next_state));
                            }
                            TransitionCommand::Process => {
                                pair = (agent, None);
                            }
                            TransitionCommand::Interrupted => {
                                pair = (agent, None);
                                break;
                            }
                            TransitionCommand::InContext(envelope) => {
                                envelope
                                    .handle(&mut agent, &mut self.context)
                                    .await
                                    .expect("Agent's loopback should never fail");
                                let next_state = self.context.session().next_state.take();
                                pair = (agent, next_state);
                            }
                        },
                        Transition::Crashed(err) => {
                            return Err(err);
                        }
                    }
                } else {
                    let result = agent.event(&mut self.context).await;
                    self.failures.put(result);
                    let next_state = self.context.session().next_state.take();
                    pair = (agent, next_state);
                }
            }

            // Finalize
            let mut agent = pair.0;
            let output = agent.finalize(&mut self.context);
            self.agent = Some(agent);
            Ok(output)
        } else {
            Err(Error::msg("Agent's agent has consumed already."))
        }
    }
}

#[async_trait]
impl<T> Runtime for RunAgent<T>
where
    T: Agent,
{
    fn get_interruptor(&mut self) -> Interruptor {
        self.context.session().controller.interruptor.clone()
    }

    async fn routine(&mut self) {
        let result = self.perform_routine().await;
        self.failures.put(result);
    }
}