wrote wasm binding for eval_boolean function - let's see if it works
This commit is contained in:
parent
a1066fa416
commit
05ab0906d6
7 changed files with 251 additions and 34 deletions
133
src/lib.rs
133
src/lib.rs
|
|
@ -1,33 +1,134 @@
|
|||
extern crate wasm_bindgen;
|
||||
extern crate argus;
|
||||
extern crate serde_json;
|
||||
extern crate js_sys;
|
||||
extern crate serde;
|
||||
|
||||
use wasm_bindgen::prelude::*;
|
||||
use argus::expr::Expr;
|
||||
|
||||
// use chumsky::prelude::Rich;
|
||||
use argus::expr::{Expr};
|
||||
use argus::{Trace, Signal, AnySignal, BooleanSemantics};
|
||||
use argus::signals::interpolation;
|
||||
use std::collections::HashMap;
|
||||
use std::vec;
|
||||
use serde::{Serialize, Deserialize};
|
||||
use std::time::Duration;
|
||||
|
||||
// #[wasm_bindgen]
|
||||
// pub struct WrappedExpr {
|
||||
// expr: Expr,
|
||||
// }
|
||||
|
||||
// macro_rules! wasmbind_fn {
|
||||
// ($name:ident($($arg:ident: $t:ty),*) -> $ret:ty) => {
|
||||
// #[wasm_bindgen]
|
||||
// pub fn $name($($arg: $t),*) -> $ret {
|
||||
// my_lib::$name($($arg),*)
|
||||
// }
|
||||
// #[wasm_bindgen]
|
||||
// pub fn parse_str(s: &str) -> WrappedExpr {
|
||||
// let expr = argus::parse_str(s);
|
||||
// return WrappedExpr{
|
||||
// expr: expr.expect("fuck!!!!"),
|
||||
// };
|
||||
// }
|
||||
|
||||
// wasmbind_fn!(parse_str(s: &str) -> Result<expr::Expr, Vec<Rich<'_,String>>>);
|
||||
struct TraceMap<'a> {
|
||||
events: HashMap<&'a str, Signal<bool>>,
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub struct WrappedExpr {
|
||||
expr: Expr,
|
||||
impl<'a> Trace for TraceMap<'a> {
|
||||
fn signal_names(&self) -> Vec<&str> {
|
||||
self.events.keys().cloned().collect()
|
||||
}
|
||||
|
||||
fn get<T: 'static>(&self, name: &str) -> Option<&Signal<T>> {
|
||||
let sig: &dyn AnySignal = match self.events.get(name) {
|
||||
Some(signal) => signal,
|
||||
None => return None,
|
||||
};
|
||||
sig.as_any().downcast_ref::<Signal<T>>()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
// type MyTrace = Vec<TraceItem>
|
||||
// struct MyResult {
|
||||
// ok: ,
|
||||
// }
|
||||
|
||||
// pub struct DefaultFalse;
|
||||
|
||||
// use argus::signals::{InterpolationMethod, Sample};
|
||||
|
||||
// use argus::core::utils::Neighborhood;
|
||||
|
||||
// impl InterpolationMethod<bool> for DefaultFalse {
|
||||
// fn at(a: &super::Sample<bool>, b: &super::Sample<bool>, time: std::time::Duration) -> Option<bool> {
|
||||
// if time == a.time {
|
||||
// Some(a.value)
|
||||
// }
|
||||
// else if time == b.time {
|
||||
// Some(b.value)
|
||||
// }
|
||||
// else {
|
||||
// Some(false)
|
||||
// }
|
||||
// }
|
||||
|
||||
// fn find_intersection(_a: &Neighborhood<bool>, _b: &Neighborhood<bool>) -> Option<Sample<bool>> {
|
||||
// None
|
||||
// }
|
||||
// }
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct StateBuddyTraceEntry {
|
||||
simtime: f64,
|
||||
inputEvent: String,
|
||||
outputEvents: Vec<String>,
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub fn parse_str(s: &str) -> WrappedExpr {
|
||||
let expr = argus::parse_str(s);
|
||||
return WrappedExpr{
|
||||
expr: expr.expect("fuck!!!!"),
|
||||
pub struct StateBuddyTrace {
|
||||
entries: Vec<StateBuddyTraceEntry>,
|
||||
}
|
||||
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub fn eval_boolean(s: &str, trace: &StateBuddyTrace) -> StateBuddyEvalResult {
|
||||
let mut traceMap = HashMap::<&str, Signal<bool>>::new();
|
||||
for entry in &trace.entries {
|
||||
let value = traceMap.entry(entry.inputEvent.as_str()).or_insert_with(|| Signal::<bool>::Sampled {
|
||||
values: vec![],
|
||||
time_points: vec![],
|
||||
});
|
||||
if let Signal::<bool>::Sampled { values, time_points } = value {
|
||||
values.push(true);
|
||||
time_points.push(Duration::from_millis(entry.simtime as u64));
|
||||
}
|
||||
else {
|
||||
panic!("fuck!!!");
|
||||
}
|
||||
}
|
||||
|
||||
let parse_result = argus::parse_str(s);
|
||||
let expr = parse_result.expect("fuuuckk!!");
|
||||
let eval_result = match expr {
|
||||
Expr::Bool(bool_expr) => BooleanSemantics::eval::<interpolation::Constant>(&bool_expr, &TraceMap{events: traceMap}),
|
||||
_ => panic!("fuuuck"),
|
||||
};
|
||||
let mut result = Vec::<StateBuddyEvalResultEntry>::new();
|
||||
eval_result.expect("fuuuck").iter().for_each(|(timestamp, satisfied), | {
|
||||
result.push(StateBuddyEvalResultEntry{
|
||||
timestamp: timestamp.as_millis() as f64,
|
||||
satisfied: *satisfied,
|
||||
});
|
||||
});
|
||||
StateBuddyEvalResult { entries: result }
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub struct StateBuddyEvalResultEntry {
|
||||
timestamp: f64,
|
||||
satisfied: bool,
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub struct StateBuddyEvalResult {
|
||||
entries: Vec<StateBuddyEvalResultEntry>,
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue