Skip to content

Commit

Permalink
feat: better environment to play around with input (file update_fn_te…
Browse files Browse the repository at this point in the history
…st.sbml) & see outputs (update_fn_compiled::tests)
  • Loading branch information
Lukáš Chudíček committed Sep 30, 2023
1 parent d63e226 commit e9b3eea
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 6 deletions.
56 changes: 56 additions & 0 deletions data/update_fn_test.sbml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?xml version='1.0' encoding='UTF-8' standalone='no'?>
<sbml xmlns="http://www.sbml.org/sbml/level3/version1/core" layout:required="false" level="3"
qual:required="true" xmlns:layout="http://www.sbml.org/sbml/level3/version1/layout/version1"
version="1" xmlns:qual="http://www.sbml.org/sbml/level3/version1/qual/version1">
<model metaid="_174907b7-8e1c-47f3-9a50-bb8e4c6ebd0d" id="model_id">
<qual:listOfTransitions xmlns:qual="http://www.sbml.org/sbml/level3/version1/qual/version1">
<qual:transition qual:id="tr_p53">
<qual:listOfInputs>
<qual:input qual:qualitativeSpecies="renamed" qual:transitionEffect="none"
qual:sign="negative" qual:id="tr_p53_in_2" />
<!-- <qual:input qual:qualitativeSpecies="renamed" qual:transitionEffect="none"
qual:sign="negative" qual:id="tr_p53_in_2" />
<qual:input qual:qualitativeSpecies="renamed" qual:transitionEffect="none"
qual:sign="negative" qual:id="tr_p53_in_2" /> -->
</qual:listOfInputs>
<qual:listOfOutputs>
<qual:output qual:qualitativeSpecies="new_output_name"
qual:transitionEffect="assignmentLevel"
qual:id="tr_p53_out" />
</qual:listOfOutputs>
<qual:listOfFunctionTerms>
<!-- 255 is the maximum accepted value; limit of u8 -->
<qual:defaultTerm qual:resultLevel="10">
</qual:defaultTerm>
<qual:functionTerm qual:resultLevel="0">
<math xmlns="http://www.w3.org/1998/Math/MathML">
<apply>
<eq />
<ci> renamed </ci>
<cn type="integer"> 0 </cn>
</apply>
</math>
</qual:functionTerm>
<qual:functionTerm qual:resultLevel="1">
<math xmlns="http://www.w3.org/1998/Math/MathML">
<apply>
<eq />
<ci> renamed </ci>
<cn type="integer"> 1 </cn>
</apply>
</math>
</qual:functionTerm>
<qual:functionTerm qual:resultLevel="2">
<math xmlns="http://www.w3.org/1998/Math/MathML">
<apply>
<eq />
<ci> renamed </ci>
<cn type="integer"> 2 </cn>
</apply>
</math>
</qual:functionTerm>
</qual:listOfFunctionTerms>
</qual:transition>
</qual:listOfTransitions>
</model>
</sbml>
7 changes: 6 additions & 1 deletion src/prototype/update_fn_bdd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use biodivine_lib_bdd::{
use super::expression::Proposition;

// todo currently do not know how to determine the max value of a variable; hardcoding it for now; should be extracted from the xml/UpdateFn
const HARD_CODED_MAX_VAR_VALUE: u8 = 2;
const HARD_CODED_MAX_VAR_VALUE: u8 = 10;

/// describes, how single variable is updated
/// set of UpdateFnBdds is used to describe the dynamics of the whole system
Expand Down Expand Up @@ -415,6 +415,7 @@ mod tests {
match xml.next() {
Ok(xml::reader::XmlEvent::StartElement { name, .. }) => {
if name.local_name == "transition" {
// println!("transition found: {:?}", lol.clone());
let update_fn = super::UpdateFn::try_from_xml(&mut xml);
return update_fn.unwrap();
}
Expand All @@ -431,6 +432,10 @@ mod tests {
fn test_compiled() {
let update_fn = get_update_fn();
let update_fn_bdd: UpdateFnBdd = update_fn.into();
println!(
"update fn bdd target var name: {:?}",
update_fn_bdd.target_var_name
);
let compiled: UpdateFnCompiled = update_fn_bdd.clone().into();

let mut partial = update_fn_bdd.get_default_valuation_but_partial();
Expand Down
83 changes: 78 additions & 5 deletions src/prototype/update_fn_compiled.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use biodivine_lib_bdd::{Bdd, BddPartialValuation, BddValuation, BddVariableSetBuilder};
use std::collections::HashMap;

use crate::{SymbolicDomain, UpdateFnBdd};
use biodivine_lib_bdd::{Bdd, BddPartialValuation, BddValuation};

use crate::{SymbolicDomain, UnaryIntegerDomain, UpdateFnBdd};

// todo want to have a trait abstracting functions of this over different domains
// todo also this should have the domain type as a type parameter
Expand All @@ -10,11 +12,16 @@ pub struct UpdateFnCompiled {
// for this (given) valuation, what is the i-th "bit" of the output?
// i-th bdd answers this for the i-th bit
pub bit_answering_bdds: Vec<Bdd>,
pub named_symbolic_domains: HashMap<String, UnaryIntegerDomain>,
}

impl From<UpdateFnBdd> for UpdateFnCompiled {
fn from(update_fn: UpdateFnBdd) -> Self {
// todo test they are mutually exclusive -> more motivation to move it into a function
// todo there is a possibility to play with the fact that ther might be some terms unreachable
// todo those would be indicated by the first bit_answering_bdd being const false
// todo could use this to uptimize this (but that is lost once we convert it to bit_answering_bdds)
// todo or could use this to give feedback to the user that some cases are unreachable
let mutually_exclusive_terms = to_mutually_exclusive_and_default(
update_fn
.terms
Expand Down Expand Up @@ -86,7 +93,11 @@ impl From<UpdateFnBdd> for UpdateFnCompiled {

let output_max_value = matrix[0].len() as u8; // todo get this more elegantly

Self::new(output_max_value, bit_answering_bdds)
Self::new(
output_max_value,
bit_answering_bdds,
update_fn.named_symbolic_domains,
)
}
}

Expand Down Expand Up @@ -116,10 +127,15 @@ fn to_mutually_exclusive_and_default(bdd_succession: Vec<Bdd>) -> Vec<Bdd> {

impl UpdateFnCompiled {
// intentionaly private; should only be instantiated through From<UpdateFn>
fn new(output_max_value: u8, bit_answering_bdds: Vec<Bdd>) -> Self {
fn new(
output_max_value: u8,
bit_answering_bdds: Vec<Bdd>,
named_symbolic_domains: HashMap<String, UnaryIntegerDomain>,
) -> Self {
Self {
output_max_value,
bit_answering_bdds,
named_symbolic_domains,
}
}

Expand All @@ -137,8 +153,65 @@ impl UpdateFnCompiled {

#[cfg(test)]
mod tests {
use crate::{SymbolicDomain, UpdateFn, UpdateFnBdd, UpdateFnCompiled};

#[test]
fn test_update_fn_compiled() {
todo!()
let update_fn = get_update_fn();
let bdd_update_fn: UpdateFnBdd = update_fn.into();
// todo yeah this should be accessible from compiled as well
let mut valuation = bdd_update_fn.get_default_valuation_but_partial();
let bdd_update_fn_compiled: UpdateFnCompiled = bdd_update_fn.into();

let var_domain = bdd_update_fn_compiled
.named_symbolic_domains
.get("renamed")
.unwrap();

var_domain.encode_bits(&mut valuation, &1);
println!("valuation: {:?}", valuation);
println!(
"result: {:?}",
bdd_update_fn_compiled.get_result_bits(&valuation.clone().try_into().unwrap())
);

var_domain.encode_bits(&mut valuation, &2);
println!("valuation: {:?}", valuation);
println!(
"result: {:?}",
bdd_update_fn_compiled.get_result_bits(&valuation.clone().try_into().unwrap())
);

var_domain.encode_bits(&mut valuation, &3);
println!("valuation: {:?}", valuation);
println!(
"result: {:?}",
bdd_update_fn_compiled.get_result_bits(&valuation.clone().try_into().unwrap())
);
}

fn get_update_fn() -> UpdateFn {
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 = UpdateFn::try_from_xml(&mut xml);
return update_fn.unwrap();
}
}
Ok(xml::reader::XmlEvent::EndElement { .. }) => continue,
Ok(xml::reader::XmlEvent::EndDocument) => panic!(),
Err(_) => panic!(),
_ => continue,
}
}
}
}

0 comments on commit e9b3eea

Please sign in to comment.