Commit 8ef9a0aa authored by Bharat's avatar Bharat
Browse files

Removed external CXX SUL

parent 51cab375
Pipeline #61319 failed with stages
in 4 minutes
......@@ -34,8 +34,6 @@ strum = "0.24.1"
strum_macros = "0.24"
num-format = "0.4.0"
priority-queue = "1.2.1"
libloading = "0.7"
cxx = "1.0"
[dev-dependencies]
assert_cmd = "2.0"
......
use super::SystemUnderLearning;
use crate::automatadefs::mealy::{InputSymbol, OutputSymbol};
use bimap::BiHashMap;
use itertools::Itertools;
use libloading::Symbol;
use std::collections::HashMap;
/// `Simulator` is the System Under Learning/Testing (SUL/SUT)
/// that we want to learn. This struct uses an FSM to "simulate"
/// the behaviour of a real SUL.
#[derive(Debug)]
pub struct External<'a, S> {
cnt_inputs: usize,
step_func: Symbol<'a, unsafe extern "C" fn(String) -> String>,
reset_func: Symbol<'a, unsafe extern "C" fn()>,
cnt_resets: usize,
input_map: BiHashMap<String, InputSymbol, S, S>,
output_map: BiHashMap<String, OutputSymbol, S, S>,
}
impl<'a, S: std::hash::BuildHasher + std::default::Default> External<'a, S> {
#[must_use]
pub fn new(
step_func: Symbol<'a, unsafe extern "C" fn(String) -> String>,
reset_func: Symbol<'a, unsafe extern "C" fn()>,
input_map: &HashMap<String, InputSymbol, S>,
) -> Self {
let i_m = input_map.iter().map(|(s, i)| (s.clone(), *i)).collect();
Self {
step_func,
reset_func,
cnt_inputs: 0,
cnt_resets: 0,
input_map: i_m,
output_map: BiHashMap::default(),
}
}
}
impl<S: std::hash::BuildHasher> SystemUnderLearning for External<'_, S> {
fn step(&mut self, input_seq: &[InputSymbol]) -> Box<[OutputSymbol]> {
let i_s = input_seq
.iter()
.map(|i| self.input_map.get_by_right(i).expect("Safe"))
.cloned()
.collect_vec();
let sf = self.step_func.clone();
let o_s = i_s
.iter()
.map(|i| unsafe { sf(i.to_string()) })
.collect_vec();
let mut out_seq = Vec::with_capacity(input_seq.len());
for o in &o_s {
let mut osymb = self.output_map.get_by_left(o).copied();
if osymb.is_none() {
#[allow(clippy::cast_possible_truncation)]
self.output_map.insert(
o.to_string(),
OutputSymbol::new(self.output_map.len() as u16),
);
osymb = self.output_map.get_by_left(o).copied();
}
out_seq.push(osymb.expect("Safe"));
}
self.cnt_inputs += input_seq.len();
log::trace!(" {:?} / {:?}", input_seq, o_s);
out_seq.into_boxed_slice()
}
fn reset(&mut self) {
let rf = self.reset_func.clone();
unsafe {
rf();
}
self.cnt_resets += 1;
log::trace!("\tRESET");
}
fn trace(&mut self, input_seq: &[InputSymbol]) -> Box<[OutputSymbol]> {
self.reset();
let out_seq = self.step(input_seq);
log::trace!(" {:?} / {:?}", input_seq, out_seq);
out_seq
}
fn get_counts(&mut self) -> (usize, usize) {
let ret = (self.cnt_inputs, self.cnt_resets);
self.cnt_inputs = 0;
self.cnt_resets = 0;
ret
}
fn input_map(&self) -> Vec<(String, InputSymbol)> {
self.input_map
.iter()
.map(|(s, i)| (s.clone(), *i))
.collect()
}
fn output_map(&self) -> Vec<(String, OutputSymbol)> {
self.output_map
.iter()
.map(|(s, o)| (s.clone(), *o))
.collect()
}
}
#[cxx::bridge(namespace = "std")]
mod ffi {
unsafe extern "C++" {
include!("cxx-demo/include/blobstore.h");
}
}
......@@ -6,8 +6,6 @@
use crate::automatadefs::mealy::{InputSymbol, OutputSymbol};
/// External SUL, i.e., we interface with something else to connect to the SUL.
pub mod external;
/// Simulate an SUL.
pub mod simulator;
/// External SUL which communicates over stdio.
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment