Commit 8d36ccec authored by Bharat Garhewal's avatar Bharat Garhewal
Browse files

Added dynamic loading lib support as External sul

parent 98c8230a
Pipeline #60908 failed with stages
in 35 seconds
......@@ -17,7 +17,7 @@ use crate::{
},
membership::Oracle as OQOracle,
},
sul::simulator::Simulator,
sul::{external::External, simulator::Simulator},
util::{
learning_config::{EqOracle, LearnResult},
writers::overall as MealyWriter,
......@@ -54,7 +54,9 @@ pub fn learn_fsm<S: ::std::hash::BuildHasher + Default>(
let mealy_machine = Arc::new(sul.clone());
log::info!("Qsize : {}", mealy_machine.as_ref().states().len());
log::info!("Isize : {}", mealy_machine.as_ref().input_alphabet().len());
let mut trial = Simulator::new(&mealy_machine, input_map, output_map);
let oq_oracle: WrappedOracle = Rc::new(RefCell::new(OQOracle::new(
&mut trial,
options.rule2_mode.clone(),
......@@ -99,6 +101,7 @@ pub fn learn_fsm<S: ::std::hash::BuildHasher + Default>(
.collect::<FnvHashSet<_>>(),
options.use_ly_ads,
);
let mut idx = 1;
let success: bool;
let mut learn_inputs: usize = 0;
......
use super::SystemUnderLearning;
use crate::automatadefs::{
mealy::{InputSymbol, Mealy, OutputSymbol, State},
FiniteStateMachine,
};
use crate::automatadefs::mealy::{InputSymbol, OutputSymbol, State};
use bimap::BiHashMap;
use itertools::Itertools;
use std::{collections::HashMap, sync::Arc};
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<S> {
fsm: Arc<Mealy>,
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<S: std::hash::BuildHasher + std::default::Default> External<S> {
impl<'a, S: std::hash::BuildHasher + std::default::Default> External<'a, S> {
#[must_use]
pub fn new(fsm: Arc<Mealy>, input_map: &HashMap<String, InputSymbol, S>) -> Self {
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 {
fsm,
step_func,
reset_func,
cnt_inputs: 0,
cnt_resets: 0,
input_map: i_m,
......@@ -33,29 +37,48 @@ impl<S: std::hash::BuildHasher + std::default::Default> External<S> {
}
}
impl<S: std::hash::BuildHasher> SystemUnderLearning for External<S> {
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 word: String = Itertools::intersperse(i_s.into_iter(), String::from(" ")).collect();
let (_dest, out_seq) = self.fsm.trace_from(State::new(0), input_seq);
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, out_seq);
out_seq
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 (_dest, out_seq) = self.fsm.trace_from(State::new(0), input_seq);
self.cnt_inputs += input_seq.len();
let out_seq = self.step(input_seq);
log::trace!(" {:?} / {:?}", input_seq, out_seq);
out_seq
}
......
use crate::automatadefs::mealy::{InputSymbol, OutputSymbol};
use crate::automatadefs::{
mealy::{InputSymbol, OutputSymbol},
FiniteStateMachine,
};
use derive_builder::Builder;
use std::{collections::HashMap, default::Default, hash::BuildHasher, io::Write};
use crate::automatadefs::FiniteStateMachine;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MealyEncoding {
Soucha,
......
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