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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
use xfstruct::*;
use xfstate::*;
use dispatcher::*;

#[derive(Debug)]
pub enum XFlowStatus {
    Uninitialized,
    Initialized,
    Running,
    Finished,
    Aborted,
    TimedOut,
    InvalidState,
}

pub struct XFlowRunner<'a> {
    status: XFlowStatus,
    xflow: &'a XFlowStruct,
    dispatcher: &'a Dispatcher<'a>,
    state: XFState,
    current_node: Option<&'a XFlowNode>,
}

impl<'a> XFlowRunner<'a> {
    pub fn new(xflow: &'a XFlowStruct, dispatcher: &'a Dispatcher<'a>) -> XFlowRunner<'a> {

        let mut state = XFState::new();

        for xvar in &xflow.variables.input {
            state.add(&xvar);
        }

        for xvar in &xflow.variables.local {
            state.add(&xvar);
        }

        match xflow.get_entry_node() {
            Ok(node) => {
                XFlowRunner {
                    status: XFlowStatus::Initialized,
                    xflow: xflow,
                    dispatcher: dispatcher,
                    state: state,
                    current_node: Some(node),
                }
            }
            _ => {
                XFlowRunner {
                    status: XFlowStatus::Uninitialized,
                    xflow: xflow,
                    dispatcher: dispatcher,
                    state: state,
                    current_node: None,
                }
            }
        }
    }

    pub fn can_run(&self) -> bool {
        match self.status {
            XFlowStatus::Initialized | XFlowStatus::Running => true,
            _ => false,
        }
    }

    pub fn is_initialized(&self) -> bool {
        match self.status {
            XFlowStatus::Initialized => true,
            _ => false,
        }
    }

    pub fn is_completed_ok(&self) -> bool {
        match self.status {
            XFlowStatus::Finished => true,
            _ => false,
        }
    }

    pub fn run(&mut self) -> () {
        while self.can_run() {
            self.step();
        }
    }

    pub fn step(&mut self) -> bool {
        self.next_node();
        self.run_node()
    }

    fn run_node(&mut self) -> bool {
        let st = &mut self.state;
        if let Some(node) = self.current_node {
            self.status = XFlowStatus::Running;
            self.dispatcher.dispatch(node, st);
            true
        } else {
            self.status = XFlowStatus::Finished;
            false
        }
    }

    fn next_node(&mut self) -> () {
        if let Some(current_node) = self.current_node {
            let edges = self.xflow.get_out_edges(current_node);
            match edges.len() {
                0 => {
                    self.status = XFlowStatus::InvalidState;
                    self.current_node = None;
                }
                1 => {
                    if let Some(edge) = edges.first() {
                        self.current_node = self.xflow.get_node_id(edge.1);
                    } else {
                        self.status = XFlowStatus::InvalidState;
                        self.current_node = None;
                    }
                }
                _ => {

                    // XXX This branch matching is sloppy - it should happen on the node.parameters
                    // settings
                    //

                    let branches: Vec<&XFlowBranch> = self.xflow
                        .get_out_branches(current_node.id)
                        .iter()
                        .filter({
                            |branch| {
                                let xv = self.state
                                    .get(&branch.xvar
                                        .name);
                                if let Some(xvar) = xv {
                                    *xvar == branch.xvar
                                } else {
                                    false
                                }
                            }
                        })
                        .cloned()
                        .collect();
                    match branches.len() {
                        0 => {
                            self.status = XFlowStatus::InvalidState;
                            self.current_node = None;
                        }
                        1 => {
                            if let Some(branch) = branches.first() {
                                self.current_node = self.xflow.get_node_id(branch.edge.1);
                            } else {
                                self.status = XFlowStatus::InvalidState;
                                self.current_node = None;
                            }
                        }
                        _ => {
                            self.status = XFlowStatus::InvalidState;
                            self.current_node = None;
                        }
                    }
                }
            }
        } else {
            self.status = XFlowStatus::InvalidState;
            self.current_node = None;
        }
    }
}