From 29a939484ce03e6b44b64a2b5c84b47adf43d105 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Chud=C3=AD=C4=8Dek?= Date: Sat, 13 Jan 2024 19:25:01 +0100 Subject: [PATCH] refactor: removed useless/duplicated functions that remained after the restructure --- src/benchmarks/reachability.rs | 3 +- src/test_utils.rs | 8 +- src/utils.rs | 465 +-------------------------------- tests/some_test.rs | 6 +- 4 files changed, 10 insertions(+), 472 deletions(-) diff --git a/src/benchmarks/reachability.rs b/src/benchmarks/reachability.rs index ef9ff36..fc98505 100644 --- a/src/benchmarks/reachability.rs +++ b/src/benchmarks/reachability.rs @@ -8,7 +8,8 @@ use std::fmt::Debug; use crate::symbolic_domains::symbolic_domain::SymbolicDomainOrd; use crate::update::update_fn::SmartSystemUpdateFn; -use crate::utils::{count_states, find_start_of, log_percent, pick_state_bdd}; +use crate::utils::{count_states, log_percent, pick_state_bdd}; +use crate::xml_parsing::utils::find_start_of; pub fn reachability_benchmark + Debug>(sbml_path: &str) { let smart_system_update_fn = { diff --git a/src/test_utils.rs b/src/test_utils.rs index dafef21..1dec31a 100644 --- a/src/test_utils.rs +++ b/src/test_utils.rs @@ -2,11 +2,13 @@ use biodivine_lib_bdd::Bdd; use num_bigint::BigInt; use std::fmt::Debug; -use crate::utils::{count_states_exact, encode_state_map, find_start_of, pick_state_map}; +use crate::utils::{count_states_exact, encode_state_map, pick_state_map}; + +use crate::xml_parsing::utils::find_start_of; use crate::symbolic_domains::symbolic_domain::{ - BinaryIntegerDomain, GrayCodeIntegerDomain, PetriNetIntegerDomain, SymbolicDomain, - SymbolicDomainOrd, UnaryIntegerDomain, + BinaryIntegerDomain, GrayCodeIntegerDomain, PetriNetIntegerDomain, SymbolicDomainOrd, + UnaryIntegerDomain, }; use crate::update::update_fn::SmartSystemUpdateFn; diff --git a/src/utils.rs b/src/utils.rs index c5fa279..30f9b79 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,14 +1,8 @@ -use biodivine_lib_bdd::{Bdd, BddPartialValuation, BddVariable}; +use biodivine_lib_bdd::{Bdd, BddPartialValuation}; use num_bigint::BigInt; +use std::collections::HashMap; use std::fmt::Debug; use std::ops::Shr; -use std::{collections::HashMap, io::BufRead}; -use xml::{ - attribute::OwnedAttribute, - name::OwnedName, - namespace::Namespace, - reader::{EventReader, XmlEvent}, -}; // use crate::prototype::symbolic_domain::SymbolicDomain; @@ -19,461 +13,6 @@ use xml::{ use crate::symbolic_domains::symbolic_domain::SymbolicDomain; use crate::update::update_fn::SmartSystemUpdateFn; -pub fn expect_opening, BR: BufRead>( - xml: &mut XR, -) -> Result> { - loop { - match xml.next() { - Ok(XmlEvent::Whitespace(_)) => { /* whitespace is the reason we want to loop */ } - Ok(XmlEvent::StartElement { - name, - attributes, - namespace, - }) => return Ok(StartElementWrapper::new(name, attributes, namespace)), // til abt variable binding - other => return Err(format!("expected an opening, got {:?}", other).into()), - } - } -} - -pub fn expect_opening_of, BR: BufRead>( - expected: &str, - xml: &mut XR, -) -> Result> { - loop { - match xml.next() { - Ok(XmlEvent::Whitespace(_)) => { /* whitespace is the reason we want to loop */ } - Ok(XmlEvent::StartElement { - name, - attributes, - namespace, - }) => { - return if name.local_name == expected { - Ok(StartElementWrapper::new(name, attributes, namespace)) - } else { - Err(format!( - "expected opening element {}, got {}", - expected, name.local_name - ) - .into()) - } - } - other => { - return Err(format!("expected opening of {}, got {:?}", expected, other).into()) - } - } - } -} - -/// since XmlEvent::StartElement obviously cannot be as return type, this is used instead in cases -/// where only this version of the enum can be returned -pub struct StartElementWrapper { - pub name: OwnedName, - pub attributes: Vec, - pub namespace: Namespace, -} - -impl StartElementWrapper { - pub fn new(name: OwnedName, attributes: Vec, namespace: Namespace) -> Self { - Self { - name, - attributes, - namespace, - } - } -} - -/// todo maybe add return value as the whole end tag; so far no usecase -pub fn expect_closure_of, BR: BufRead>( - expected: &str, - xml: &mut XR, -) -> Result<(), Box> { - loop { - match xml.next() { - Ok(XmlEvent::Whitespace(_)) => { /* whitespace is the reason we want to loop */ } - Ok(XmlEvent::EndElement { name, .. }) => { - return if name.local_name == expected { - Ok(()) - } else { - Err(format!("expected closing of {}, got {}", expected, name.local_name).into()) - } - } - any => return Err(format!("expected closing of {}, got {:?}", expected, any).into()), - } - } -} - -// lmao nice type signature -/// takes care of processing xml lists into vector of given items. list_name is expected to be the -/// name of the tag wrapping the whole list. item_name is expected to be the name of the tag -/// wrapping each element. each time `item_name` is encountered, the `xml` is handed off to the -/// `processing_fn` function. if any of the calls to `processing_fn` fail, that error is returned -/// immediately (// todo append some extra info abt the fact it was from `process_list`?). -/// `processing_fn` is expected to return with the `xml` pointing to the last element of the item -/// (ie to ``). if any other element in the list other than `item_name` is -/// encountered, error is returned. once closing tag with `list_name` is encountered, Vec -/// containing all the processed items is returned (items in the correct order ofc) -/// since some functions for processing of items require access to the opening event of the item, -/// that shall be provided as the second argument to the `processing_fn` -pub fn process_list, BR: BufRead, Fun, Res>( - list_name: &str, - item_name: &str, - processing_fn: Fun, - xml: &mut XR, -) -> Result, Box> -where - Fun: Fn(&mut XR, StartElementWrapper) -> Result>, -{ - let mut acc = Vec::::new(); - - loop { - let elem = xml.next(); - - match elem { - Ok(XmlEvent::Whitespace(_)) => { /* ignore */ } - Ok(XmlEvent::StartElement { - name, - attributes, - namespace, - }) => { - if name.local_name == item_name { - acc.push(processing_fn( - xml, - StartElementWrapper::new(name, attributes, namespace), - )?); - continue; - } - - return Err(format!( - "expected opening of item {}, got {}", - item_name, name.local_name - ) - .into()); - } - Ok(XmlEvent::EndElement { name, .. }) => { - return if name.local_name == list_name { - Ok(acc) - } else { - Err(format!( - "expected closing element with name {}, got {}", - list_name, name.local_name - ) - .into()) - } - } - other => { - return Err(format!( - "expected either opening of {} or closing of {}, got {:?}", - item_name, list_name, other, - ) - .into()) - } - } - } -} - -// todo @prototype: this seems to be unused -// /// get the update fn from "data/update_fn_test.sbml" -// /// used in tests / to play around with the code -// #[allow(dead_code)] -// pub fn get_test_update_fn() -> SystemUpdateFn { -// use std::fs::File; -// use std::io::BufReader; - -// let file = File::open("data/update_fn_test.sbml").expect("cannot open file"); -// let file = BufReader::new(file); - -// let mut xml = xml::reader::EventReader::new(file); - -// loop { -// match xml.next() { -// Ok(xml::reader::XmlEvent::StartElement { name, .. }) => { -// if name.local_name == "transition" { -// let update_fn = SystemUpdateFn::try_from_xml(&mut xml); -// return update_fn.unwrap(); -// } -// } -// Ok(xml::reader::XmlEvent::EndElement { .. }) => continue, -// Ok(xml::reader::XmlEvent::EndDocument) => panic!(), -// Err(_) => panic!(), -// _ => continue, -// } -// } -// } - -/// iterates through the xml until it finds the first opening tag with the given name -/// (specifically, opening_element.name.local_name == expected_name) -pub fn find_start_of, BR: BufRead>( - xml: &mut XR, - expected_name: &str, -) -> Result<(), String> { - loop { - match xml.next() { - Ok(xml::reader::XmlEvent::StartElement { name: n, .. }) - if n.local_name == expected_name => - { - return Ok(()); - } - Ok(xml::reader::XmlEvent::EndElement { .. }) => continue, - Ok(xml::reader::XmlEvent::EndDocument) => return Err("end of document".to_string()), - Err(e) => return Err(format!("error: {:?}", e)), - _ => continue, // should be uninteresting - } - } -} - -pub trait XmlReader { - fn next(&mut self) -> Result; -} - -impl XmlReader
for EventReader
{ - fn next(&mut self) -> Result { - match self.next() { - Ok(e) => Ok(e), - Err(e) => Err(format!("error: {:?}", e)), - } - } -} - -pub struct LoudReader { - xml: EventReader
, - curr_indent: usize, -} - -impl LoudReader
{ - pub fn new(xml: EventReader
) -> Self { - Self { - xml, - curr_indent: 0, - } - } -} - -impl XmlReader
for LoudReader
{ - fn next(&mut self) -> Result { - match self.xml.next() { - Ok(e) => { - match e.clone() { - XmlEvent::StartElement { - name, - // attributes, - // namespace, - .. - } => { - println!( - "{}<{:?}>", - (0..self.curr_indent).map(|_| ' ').collect::(), - name - ); - - self.curr_indent += 2; - } - XmlEvent::EndElement { name, .. } => { - println!( - "{}", - (0..self.curr_indent).map(|_| ' ').collect::(), - name - ); - - self.curr_indent -= 2; - } - _ => {} - } - // println!("xddd next: {:?}", e); - Ok(e) - } - Err(e) => Err(format!("error: {:?}", e)), - } - } -} - -/// use another XMLReader implementation from this file to get the update functions - the thing you need to pass to DebuggingReader::new -/// this is used to go through the xml (the same one that was previously loaded into update_fns) and -/// print error messages wherever a variable is compared to a value higher than its update_fn allows it to be -/// and also if there is a variable name, which is not known (does not have an update fn) -pub struct DebuggingReader { - loud_xml: LoudReader
, - vars_and_their_max_values: HashMap, - complain_about_values_too_large: bool, - complain_about_unknown_variable_name: bool, - current_ci: Option, // this is just a retarded way of holding the context of what variable is ananlyzed/compared with value - expecting_variable_name: bool, - expecting_variable_value: bool, -} - -// impl DebuggingReader
{ -// #[allow(dead_code)] -// pub fn new( -// xml: EventReader
, -// update_fns: &HashMap>, -// complain_about_values_too_large: bool, -// complain_about_unknown_variable_name: bool, -// ) -> Self { -// let vars_and_their_max_values = update_fns -// .iter() -// .map(|(var_name, update_fn)| { -// let max_value_this_variable_can_get_according_to_its_update_fn = update_fn -// .terms -// .iter() -// .map(|(val, _condition)| val) -// .chain(std::iter::once(&update_fn.default)) -// .max() -// .unwrap() -// .to_owned(); - -// ( -// var_name.to_owned(), -// max_value_this_variable_can_get_according_to_its_update_fn, -// ) -// }) -// .collect::>(); - -// Self { -// loud_xml: LoudReader::new(xml), -// vars_and_their_max_values, -// complain_about_values_too_large, -// complain_about_unknown_variable_name, -// current_ci: None, -// expecting_variable_name: false, -// expecting_variable_value: false, -// } -// } -// } - -impl XmlReader
for DebuggingReader
{ - fn next(&mut self) -> Result { - match self.loud_xml.next() { - Ok(e) => { - match e.clone() { - XmlEvent::StartElement { - name, - // attributes, - // namespace, - .. - } => match name.local_name.as_str() { - "ci" => self.expecting_variable_name = true, - "cn" => self.expecting_variable_value = true, - _ => {} - }, - XmlEvent::Characters(content) => { - if self.expecting_variable_name { - self.expecting_variable_name = false; - self.current_ci = Some(content.to_string()); - } - - if self.expecting_variable_value { - self.expecting_variable_value = false; - let actual_value = content.trim().parse::().unwrap_or_else(|_| { - panic!( - "currently only allowing DebugReader parse u8 values; got {}", - content - ) - }); - - let associated_max_value = self.vars_and_their_max_values.get( - &self - .current_ci - .clone() - .expect("current_ci should be initialized"), - ); - - match associated_max_value { - None => { - if self.complain_about_unknown_variable_name { - eprintln!( - "[debug: UNKNOWN_VARIABLE] got variable with name {} in this proposition, but no such name known; known names are {:?}", - self.current_ci.clone().unwrap(), - self.vars_and_their_max_values.keys() - ) - } - } - Some(expected_max_value) => { - if *expected_max_value < actual_value - && self.complain_about_values_too_large - { - eprintln!( - "[debug: VALUE_TOO_BIG] comparing variable {} (whose domain is only [0, {}]) with value {}", - self.current_ci.clone().unwrap(), - expected_max_value, - actual_value - ) - } - } - } - } - } - _ => {} - } - // println!("xddd next: {:?}", e); - Ok(e) - } - Err(e) => Err(format!("error: {:?}", e)), - } - } -} - -// impl XmlReader
for DebuggingReader
{ -// fn next(&mut self) -> Result {} -// } - -pub struct CountingReader { - xml: EventReader
, - pub curr_line: usize, -} - -impl CountingReader
{ - #[allow(dead_code)] - pub fn new(xml: EventReader
) -> Self { - Self { xml, curr_line: 0 } - } -} - -impl XmlReader
for CountingReader
{ - fn next(&mut self) -> Result { - match self.xml.next() { - Ok(e) => { - match e { - XmlEvent::StartElement { .. } => { - self.curr_line += 1; - } - XmlEvent::EndElement { .. } => { - self.curr_line += 1; - } - _ => {} - } - // println!("xddd next: {:?}", e); - Ok(e) - } - Err(e) => Err(format!("error: {:?}", e)), - } - } -} - -pub fn find_bdd_variables_prime, T>( - target_variable: &BddVariable, - target_sym_dom: &D, - target_sym_dom_primed: &D, -) -> BddVariable { - target_sym_dom - .raw_bdd_variables() - .into_iter() - .zip(target_sym_dom_primed.raw_bdd_variables()) - .find_map(|(maybe_target_variable, its_primed)| { - if maybe_target_variable == *target_variable { - Some(its_primed) - } else { - None - } - }) - .unwrap_or_else(|| { - // this might happen if the supplied target_sym_domain does not contain target_variable - // or if the target_sym_dom_primed has less elements (but this fn should not be called with such) - panic!( - "did not find the variable {} in given target sym domain", - target_variable, - ) - }) -} - /// Compute a [Bdd] which represents a single (un-primed) state within the given symbolic `set`. pub fn pick_state_bdd + Debug>( system: &SmartSystemUpdateFn, diff --git a/tests/some_test.rs b/tests/some_test.rs index 0d5d551..e8d1d54 100644 --- a/tests/some_test.rs +++ b/tests/some_test.rs @@ -1,11 +1,7 @@ #![allow(dead_code)] use biodivine_lib_bdd::Bdd; -use biodivine_lib_logical_models::prelude::{ - self as bio, - // old_symbolic_domain::SymbolicDomain, - symbolic_domain::SymbolicDomain as _, -}; +use biodivine_lib_logical_models::prelude as bio; // type OldDomain = bio::old_symbolic_domain::BinaryIntegerDomain; type NewDomain = bio::symbolic_domain::BinaryIntegerDomain;