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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
//! A web service for interactively exploring a model. Remember to import the [`Explorer`] trait to
//! enable `model.serve()`.
//!
//! ![Stateright Explorer screenshot](https://raw.githubusercontent.com/stateright/stateright/master/explorer.jpg)
//!
//! # Example
//!
//! ```no_run
//! use stateright::Model;
//! use stateright::explorer::Explorer; // IMPORTANT
//!
//! #[derive(Clone, Debug, Hash)]
//! enum FizzBuzzAction { Fizz, Buzz, FizzBuzz }
//! #[derive(Clone)]
//! struct FizzBuzzModel { max: usize }
//!
//! impl Model for FizzBuzzModel {
//!     type State = Vec<(usize, Option<FizzBuzzAction>)>;
//!     type Action = Option<FizzBuzzAction>;
//!     fn init_states(&self) -> Vec<Self::State> {
//!         vec![Vec::new()]
//!     }
//!     fn actions(&self, state: &Self::State, actions: &mut Vec<Self::Action>) {
//!         actions.push(
//!             if state.len() % 15 == 0 {
//!                 Some(FizzBuzzAction::FizzBuzz)
//!             } else if state.len() % 5 == 0 {
//!                 Some(FizzBuzzAction::Buzz)
//!             } else if state.len() % 3 == 0 {
//!                 Some(FizzBuzzAction::Fizz)
//!             } else {
//!                 None
//!             });
//!     }
//!     fn next_state(&self, state: &Self::State, action: Self::Action) -> Option<Self::State> {
//!         let mut state = state.clone();
//!         state.push((state.len(), action));
//!         Some(state)
//!     }
//!     fn within_boundary(&self, state: &Self::State) -> bool {
//!         state.len() <= self.max
//!     }
//! }
//!
//! let _ = FizzBuzzModel { max: 30 }.checker().serve("localhost:3000");
//! ```
//!
//! # API
//!
//! - `GET /` returns a web browser UI as HTML.
//! - `GET /.status` returns information about the model checker status.
//! - `GET /.states` returns available initial states and fingerprints.
//! - `GET /.states/{fingerprint1}/{fingerprint2}/...` follows the specified
//!    path of fingerprints and returns available actions with resulting
//!    states and fingerprints.
//! - `GET /.states/.../{invalid-fingerprint}` returns 404.

use actix_web::{*, web::Json};
use crate::*;
use crate::checker::PathName;
use serde::ser::{SerializeStruct, Serializer};
use serde::Serialize;
use std::net::ToSocketAddrs;
use std::sync::{Arc, Mutex};
use std::collections::HashMap;

#[derive(Debug, Eq, PartialEq)]
pub struct StateView<State, Action> {
    action: Option<Action>,
    outcome: Option<String>,
    state: State,
}

impl<Action, State> StateView<State, Action>
where State: Hash
{
    fn fingerprint(&self) -> Fingerprint {
        fingerprint(&self.state)
    }
}

impl<Action, State> serde::Serialize for StateView<State, Action>
where
    Action: Debug,
    State: Debug + Hash,
{
    fn serialize<S: Serializer>(&self, ser: S) -> Result<S::Ok, S::Error> {
        let mut out = ser.serialize_struct("StateView", 3)?;
        if let Some(ref action) = self.action {
            out.serialize_field("action", &format!("{:?}", action))?;
        }
        if let Some(ref outcome) = self.outcome {
            out.serialize_field("outcome", outcome)?;
        }
        out.serialize_field("state", &format!("{:#?}", self.state))?;
        out.serialize_field("fingerprint", &format!("{:?}", self.fingerprint()))?;
        out.end()
    }
}

pub type StateViewsJson<State, Action> = Json<Vec<StateView<State, Action>>>;

#[derive(Clone, Debug, Default, Eq, PartialEq, Serialize)]
pub struct StatusView {
    model: String,
    threads: usize,
    pending: usize,
    generated: usize,
    discoveries: HashMap<&'static str, PathName>,
}

pub struct Context<M> {
    model: M,
    status: Mutex<StatusView>,
}

impl<M: Model> Explorer<M> for Checker<M>
where M: 'static + Clone + Model + Send + Sync,
      M::Action: Debug + Send + Sync,
      M::State: Clone + Debug + Hash + Send + Sync,
{

    /// Begin serving requests on a specified address such as `"localhost:3000"`.
    fn serve<A: ToSocketAddrs>(mut self, addr: A) -> Result<()>
    {
        self.check(1_000); // small number just to expedite startup
        let data = Arc::new(Context {
            model: self.model.clone(),
            status: Mutex::new(self.status_view()),
        });

        {
            let data = Arc::clone(&data);
            std::thread::spawn(move || {
                let context = &*data;
                loop {
                    self.check(25_000);
                    let mut status = context.status.lock().unwrap();
                    *status = self.status_view();
                    if self.is_done() { break }
                }
            });
        }

        HttpServer::new(move || {
            macro_rules! get_ui_file {
                ($filename:literal) => {
                    web::get().to(|| HttpResponse::Ok().body({
                        if let Ok(content) = std::fs::read(concat!("./ui/", $filename)) {
                            log::info!("Explorer dev mode. Loading {} from disk.", $filename);
                            content
                        } else {
                            include_bytes!(concat!("../ui/", $filename)).to_vec()
                        }
                    }))
                }
            };

            App::new()
                .data(Arc::clone(&data))
                .route("/.status", web::get().to(Self::status))
                .route("/.states{fingerprints:.*}", web::get().to(Self::states))
                .route("/", get_ui_file!("index.htm"))
                .route("/app.css", get_ui_file!("app.css"))
                .route("/app.js", get_ui_file!("app.js"))
                .route("/knockout-3.5.0.js", get_ui_file!("knockout-3.5.0.js"))
        }).bind(addr)?.run()?;

        Ok(())
    }

    fn status_view(&self) -> StatusView {
        StatusView {
            model: std::any::type_name::<M>().to_string(),
            threads: self.thread_count,
            pending: self.pending.len(),
            generated: self.generated_count(),
            discoveries: self.discoveries.iter()
                .map(|e| (
                    *e.key(),
                    self.path(*e.value()).name(),
                )).collect(),
        }
    }
}

pub trait Explorer<M>: Sized + Send + 'static
where
    M: 'static + Model + Send + Sync,
    M::Action: Debug + Send + Sync,
    M::State: Clone + Debug + Hash + Send + Sync,
{
    /// Begin serving requests on a specified address such as `"localhost:3000"`.
    fn serve<A: ToSocketAddrs>(self, addr: A) -> Result<()>;

    fn status(_req: HttpRequest, data: web::Data<Arc<Context<M>>>) -> Result<Json<StatusView>> {
        let status = data.status.lock().unwrap();
        Ok(Json(status.clone()))
    }

    fn states(req: HttpRequest, data: web::Data<Arc<Context<M>>>) -> Result<StateViewsJson<M::State, M::Action>> {
        let model = &data.model;

        // extract fingerprints
        let mut fingerprints_str = req.match_info().get("fingerprints").expect("missing 'fingerprints' param").to_string();
        if fingerprints_str.ends_with('/') {
            let relevant_len = fingerprints_str.len() - 1;
            fingerprints_str.truncate(relevant_len);
        }
        let fingerprints: Vec<_> = fingerprints_str.split('/').filter_map(|fp| fp.parse::<Fingerprint>().ok()).collect();

        // ensure all but the first string (which is empty) were parsed
        if fingerprints.len() + 1 != fingerprints_str.split('/').count() {
            return Err(
                actix_web::error::ErrorNotFound(
                    format!("Unable to parse fingerprints {}", fingerprints_str)));
        }

        // now build up all the subsequent `StateView`s
        let mut results = Vec::new();
        if fingerprints.is_empty() {
            for state in model.init_states() {
                results.push(StateView { action: None, outcome: None, state });
            }
        } else if let Some(last_state) = model.follow_fingerprints(model.init_states(), fingerprints) {
            // Must generate the actions three times because they are consumed by `next_state`
            // and `display_outcome`.
            let mut actions1 = Vec::new();
            let mut actions2 = Vec::new();
            let mut actions3 = Vec::new();
            model.actions(&last_state, &mut actions1);
            model.actions(&last_state, &mut actions2);
            model.actions(&last_state, &mut actions3);
            for ((action, action2), action3) in actions1.into_iter().zip(actions2).zip(actions3) {
                let outcome = model.display_outcome(&last_state, action2);
                let state = model.next_state(&last_state, action3);
                if let (Some(outcome), Some(state)) = (outcome, state) {
                    results.push(StateView { action: Some(action), outcome: Some(outcome), state });
                }
            }
        } else {
            return Err(
                actix_web::error::ErrorNotFound(
                    format!("Unable to find state following fingerprints {}", fingerprints_str)));
        }

        Ok(Json(results))
    }

    fn status_view(&self) -> StatusView;
}

#[cfg(test)]
mod test {
    use super::*;
    use crate::test_util::binary_clock::*;

    #[test]
    fn can_init() {
        assert_eq!(states("/").unwrap(), vec![
            StateView { action: None, outcome: None, state: 0 },
            StateView { action: None, outcome: None, state: 1 },
        ]);
    }

    #[test]
    fn can_next() {
        // We need a static string for TestRequest, so this is precomputed, but you can recompute
        // the values if needed as follows:
        // ```
        // let first = fingerprint(&1_i8);
        // let second = fingerprint(&0_i8);
        // let path_name = format!("/{}/{}", first, second);
        // println!("New path name is: {}", path_name);
        // ```
        assert_eq!(states("/2716592049047647680/9080728272894440685").unwrap(), vec![
            StateView { action: Some(BinaryClockAction::GoHigh), outcome: Some("1".to_string()), state: 1 },
        ]);
    }

    #[test]
    fn err_for_invalid_fingerprint() {
        assert_eq!(format!("{}", states("/one/two/three").unwrap_err()),
            "Unable to parse fingerprints /one/two/three");
        assert_eq!(format!("{}", states("/1/2/3").unwrap_err()),
            "Unable to find state following fingerprints /1/2/3");
    }

    fn states(path_name: &'static str)
            -> Result<Vec<StateView<BinaryClockState, BinaryClockAction>>> {
        use actix_web::test::*;
        let req = TestRequest::get()
            .param("fingerprints", &path_name)
            .to_http_request();
        let data = web::Data::new(Arc::new(Context {
            model: BinaryClock,
            status: Mutex::new(Default::default())
        }));
        match Checker::states(req, data) {
            Ok(Json(view)) => Ok(view),
            Err(err) => Err(err),
        }
    }
}