From 268da71a52a4f4145e8f6ddf80ab33e55b853cd6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Chud=C3=AD=C4=8Dek?= Date: Sat, 7 Oct 2023 22:29:31 +0200 Subject: [PATCH] feat: capable of getting successor for given valuation under specific VariableUpdateFunction --- data/update_fn_test.sbml | 2 +- src/prototype/system_update_fn.rs | 76 ++++++++++++++++++++++++++----- src/prototype/update_fn_bdd.rs | 14 +++++- 3 files changed, 78 insertions(+), 14 deletions(-) diff --git a/data/update_fn_test.sbml b/data/update_fn_test.sbml index c4d39fa..8db0397 100644 --- a/data/update_fn_test.sbml +++ b/data/update_fn_test.sbml @@ -14,7 +14,7 @@ qual:sign="negative" qual:id="tr_p53_in_2" /> --> - diff --git a/src/prototype/system_update_fn.rs b/src/prototype/system_update_fn.rs index a255b7a..dab6f2a 100644 --- a/src/prototype/system_update_fn.rs +++ b/src/prototype/system_update_fn.rs @@ -2,7 +2,7 @@ use std::{collections::HashMap, io::BufRead}; -use biodivine_lib_bdd::BddVariableSetBuilder; +use biodivine_lib_bdd::{BddPartialValuation, BddValuation, BddVariableSetBuilder}; use xml::EventReader; use crate::{ @@ -13,9 +13,10 @@ struct SystemUpdateFn, T> { penis: std::marker::PhantomData, penis_the_second: std::marker::PhantomData, pub update_fns: HashMap>, + pub named_symbolic_domains: HashMap, } -impl> SystemUpdateFn { +impl SystemUpdateFn { /// expects the xml reader to be at the start of the element pub fn try_from_xml, BR: BufRead>( xml: &mut XR, @@ -23,6 +24,9 @@ impl> SystemUpdateFn { let var_names_and_upd_fns = load_all_update_fns(xml)?; let ctx = vars_and_their_max_values(&var_names_and_upd_fns); + // todo currently, we have no way of adding those variables, that do not have their VariableUpdateFn + // todo (ie their qual:transition in the xml) into the named_symbolic_domains, even tho they migh + // todo be used as inputs to some functions, causing panic let mut bdd_variable_set_builder = BddVariableSetBuilder::new(); let named_symbolic_domains = ctx .into_iter() @@ -54,8 +58,32 @@ impl> SystemUpdateFn { update_fns, penis: std::marker::PhantomData, penis_the_second: std::marker::PhantomData, + named_symbolic_domains, }) } + + /// returns valuation inicialized so that all the symbolic values are = 0 + pub fn get_default_partial_valuation(&self) -> BddPartialValuation { + self.named_symbolic_domains.values().fold( + BddPartialValuation::empty(), + |mut acc, domain| { + domain.encode_bits(&mut acc, &0); + acc + }, + ) + } + + /// panics if this system does not contain variable of `sym_var_name` name + pub fn get_result_bits_of_variable( + &self, + sym_var_name: &str, + valuation: &BddValuation, + ) -> Vec { + self.update_fns + .get(sym_var_name) + .unwrap() + .get_result_bits(valuation) + } } #[allow(dead_code)] @@ -105,17 +133,9 @@ fn vars_and_their_max_values( .collect() } -// todo also maybe just zip the two maps together -fn compile_update_fns( - _var_names_and_upd_fns: &HashMap>, - _ctx: &HashMap, -) -> HashMap> { - todo!() -} - #[cfg(test)] mod tests { - use crate::UnaryIntegerDomain; + use crate::{SymbolicDomain, UnaryIntegerDomain}; use super::SystemUpdateFn; @@ -128,7 +148,39 @@ mod tests { let mut reader = crate::LoudReader::new(reader); // uncomment to see how xml is loaded crate::find_start_of(&mut reader, "listOfTransitions").expect("cannot find start of list"); - let _system_update_fn: SystemUpdateFn = + let system_update_fn: SystemUpdateFn = super::SystemUpdateFn::try_from_xml(&mut reader).unwrap(); + + let mut valuation = system_update_fn.get_default_partial_valuation(); + let some_domain = system_update_fn + .named_symbolic_domains + .get("todo some existing name") + .unwrap(); + + some_domain.encode_bits(&mut valuation, &1); + } + + #[test] + fn test_on_test_data() { + let mut reader = xml::reader::EventReader::new(std::io::BufReader::new( + std::fs::File::open("data/update_fn_test.sbml").unwrap(), + )); + + crate::find_start_of(&mut reader, "listOfTransitions").expect("cannot find start of list"); + let system_update_fn: SystemUpdateFn = + super::SystemUpdateFn::try_from_xml(&mut reader).unwrap(); + + let mut valuation = system_update_fn.get_default_partial_valuation(); + let domain_renamed = system_update_fn + .named_symbolic_domains + .get("renamed") + .unwrap(); + domain_renamed.encode_bits(&mut valuation, &1); + + let res = system_update_fn + .get_result_bits_of_variable("renamed", &valuation.clone().try_into().unwrap()); + + println!("valuation: {:?}", valuation); + println!("result: {:?}", res); } } diff --git a/src/prototype/update_fn_bdd.rs b/src/prototype/update_fn_bdd.rs index 59a6dcd..6a9ab07 100644 --- a/src/prototype/update_fn_bdd.rs +++ b/src/prototype/update_fn_bdd.rs @@ -161,7 +161,19 @@ fn prop_to_bdd>( symbolic_domains: &HashMap, bdd_variable_set: &BddVariableSet, ) -> Bdd { - let var = symbolic_domains.get(&prop.ci).unwrap(); + // let var = symbolic_domains.get(&prop.ci).unwrap(); + + let var = match symbolic_domains.get(&prop.ci) { + None => { + panic!( + "looking for {:?} but only {:?} present", + prop.ci, + symbolic_domains.keys().collect::>() + ); + } + Some(var) => var, + }; + let val = prop.cn; match prop.cmp {