Commit 379652fb authored by Bharat Garhewal's avatar Bharat Garhewal
Browse files

Hope that tests are fixed

parent fc861bb6
Pipeline #60968 failed with stages
in 4 minutes and 4 seconds
......@@ -333,6 +333,8 @@ impl PartialEq for AdsTree {
#[cfg(test)]
mod tests {
use std::collections::HashMap;
use super::{AdaptiveDistinguishingSequence, AdsNode, AdsTree};
use crate::{
automatadefs::mealy::{InputSymbol, OutputSymbol, State},
......@@ -406,7 +408,7 @@ mod tests {
tree,
curr_idx: s0_idx,
initial_idx: s0_idx,
seen: Default::default(),
seen: HashMap::default(),
}
};
assert_eq!(ref_ads, ads);
......@@ -499,7 +501,7 @@ mod tests {
tree,
curr_idx: s0_idx,
initial_idx: s0_idx,
seen: Default::default(),
seen: HashMap::default(),
}
};
assert_eq!(
......
......@@ -17,7 +17,7 @@ use crate::{
},
membership::Oracle as OQOracle,
},
sul::{external::External, simulator::Simulator},
sul::simulator::Simulator,
util::{
learning_config::{EqOracle, LearnResult},
writers::overall as MealyWriter,
......
......@@ -4,7 +4,7 @@ use crate::{
util::{
cli,
learning_config::EqOracle,
parsers::{logs::read_logs, machine::read_mealy_from_file},
parsers::{logs::read as read_logs, machine::read_mealy_from_file},
},
};
use fnv::FnvHashMap;
......
use crate::automatadefs::mealy::{InputSymbol, OutputSymbol};
use fnv::FnvHashMap;
use itertools::Itertools;
use std::{
collections::HashMap,
fs::{canonicalize, File},
io::{self, BufRead},
path::Path,
......@@ -13,24 +13,24 @@ pub type Logs = Vec<(Box<[InputSymbol]>, Box<[OutputSymbol]>)>;
///
/// # Errors
/// If file at `file_path` cannot be read.
pub fn read_logs(
pub fn read<S: std::hash::BuildHasher>(
file_path: &str,
input_map: &FnvHashMap<String, InputSymbol>,
output_map: &FnvHashMap<String, OutputSymbol>,
input_map: &HashMap<String, InputSymbol, S>,
output_map: &HashMap<String, OutputSymbol, S>,
) -> Result<Logs, Box<dyn std::error::Error>> {
let log_vec = read_lines(canonicalize(file_path)?)?
.flatten()
.skip(1) // The first entry contains the number of traces.
.into_iter()
.map(|line| parse_and_transform_trace(line, input_map, output_map))
.map(|line| parse_and_transform_trace(&line, input_map, output_map))
.collect_vec();
Ok(log_vec)
}
fn parse_and_transform_trace(
line: String,
input_map: &FnvHashMap<String, InputSymbol>,
output_map: &FnvHashMap<String, OutputSymbol>,
fn parse_and_transform_trace<S: std::hash::BuildHasher>(
line: &str,
input_map: &HashMap<String, InputSymbol, S>,
output_map: &HashMap<String, OutputSymbol, S>,
) -> (Box<[InputSymbol]>, Box<[OutputSymbol]>) {
let mut line_split_iter = line.split_whitespace();
let _ = line_split_iter.next(); // Skip length
......
......@@ -7,6 +7,9 @@ use std::{
path::Path,
};
#[must_use]
/// # Panics
/// If file is not found.
pub fn read_mealy_from_file(
file_path: &str,
) -> (
......
......@@ -25,6 +25,7 @@ impl RandomInfixGenerator {
}
}
pub fn generate(self) -> impl Iterator<Item = Vec<InputSymbol>> {
#[allow(clippy::cast_precision_loss)]
let prob = 1f64 / (self.expected_inputs as f64);
let geo = Geometric::new(prob).expect("Safe");
RandomInfixIterator::new(self.rng, self.num_inputs, geo, self.min_inputs)
......@@ -53,6 +54,7 @@ impl Iterator for RandomInfixIterator {
type Item = Vec<InputSymbol>;
fn next(&mut self) -> Option<Self::Item> {
#[allow(clippy::cast_possible_truncation)]
let lookahead = self.geo.sample(&mut self.rng) as usize;
let lookahead = lookahead + self.k_min;
let input_seq: Vec<_> = self
......
......@@ -8,13 +8,17 @@ use std::{collections::HashMap, hash::BuildHasher, io::Write};
/// Accepts an fsm and conversion maps and returns a vector of bytes.
///
/// If the input and output maps are empty, we print the bare natural numbers of the alphabets.
///
/// # Errors
/// If writing to the internal buffers fail or some state/input is undefined
/// in the maps.
pub fn write_to_dot<S: BuildHasher + Default, M: FiniteStateMachine>(
fsm: &M,
input_rev_map: &HashMap<InputSymbol, String, S>,
output_rev_map: &HashMap<OutputSymbol, String, S>,
) -> Result<Vec<u8>, std::io::Error> {
) -> Result<Vec<u8>, Box<dyn std::error::Error>> {
let mut vec = Vec::new();
writeln!(&mut vec, "digraph g {{\n").expect("Safe");
writeln!(&mut vec, "digraph g {{\n")?;
let mut state_info = HashMap::<_, _, S>::default();
for s in fsm.states() {
state_info.insert(s, vec![]);
......@@ -30,7 +34,7 @@ pub fn write_to_dot<S: BuildHasher + Default, M: FiniteStateMachine>(
let (dest, output) = fsm.step_from(src, input);
state_info
.entry(src)
.or_default()
.or_insert_with(|| Vec::with_capacity(fsm.input_alphabet().len()))
.push((input, output, dest));
}
......@@ -57,8 +61,8 @@ pub fn write_to_dot<S: BuildHasher + Default, M: FiniteStateMachine>(
"\t s{} -> s{} [label=\"{} / {}\"];",
src.raw(),
dest.raw(),
input_rev_map.get(&i_s).expect("Safe"),
output_rev_map.get(&o_s).expect("Safe")
input_rev_map.get(&i_s).ok_or("Input absent from map")?,
output_rev_map.get(&o_s).ok_or("Output absent from map")?
)?;
}
}
......
......@@ -6,11 +6,14 @@ use itertools::Itertools;
use std::{collections::HashMap, hash::BuildHasher, io::Write};
/// Convert a Mealy Machine to Soucha's textual format.
///
/// # Errors
/// If writing to the internal buffers fail.
pub fn write_to_soucha<S: BuildHasher + Default, Machine: FiniteStateMachine>(
fsm: &Machine,
_input_rev_map: &HashMap<InputSymbol, String, S>,
_output_rev_map: &HashMap<OutputSymbol, String, S>,
) -> Result<Vec<u8>, std::io::Error> {
) -> Result<Vec<u8>, Box<dyn std::error::Error>> {
let t = 2; // Always a mealy machine.
let r = 1; // Always reduced.
let num_states = fsm.states().len(); // Num states
......@@ -39,7 +42,7 @@ pub fn write_to_soucha<S: BuildHasher + Default, Machine: FiniteStateMachine>(
let output_symbol = fsm
.step_from(
*state_mapping.get(state_id).expect("Safe"),
InputSymbol::new(input.try_into().unwrap()),
InputSymbol::new(input.try_into().expect("Safe")),
)
.1;
// let output_raw = output_symbol.raw();
......@@ -58,7 +61,7 @@ pub fn write_to_soucha<S: BuildHasher + Default, Machine: FiniteStateMachine>(
let dest_state = fsm
.step_from(
*state_mapping.get(state_id).expect("Safe"),
InputSymbol::new(input.try_into().unwrap()),
InputSymbol::new(input.try_into().expect("Safe")),
)
.0;
let state_raw = state_mapping.binary_search(&dest_state).expect("Safe");
......
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