Skip to content

Commit

Permalink
Merge branch 'main' into dev-symbolic-domains
Browse files Browse the repository at this point in the history
  • Loading branch information
daemontus committed Jun 20, 2023
2 parents 9e2e3bb + 288fa60 commit cf49c3b
Show file tree
Hide file tree
Showing 10 changed files with 1,258 additions and 1 deletion.
22 changes: 22 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"editor.formatOnSave": true,
"files.associations": {
"*.sbml": "xml"
},
"[xml]": {
"editor.defaultFormatter": "redhat.vscode-xml",
"editor.formatOnSave": true
},
"[rust]": {
"editor.defaultFormatter": "rust-lang.rust-analyzer",
"editor.formatOnSave": true
},
"rust-analyzer.checkOnSave": true,
"rust-analyzer.check.overrideCommand": [
"cargo",
"clippy",
"--workspace",
"--message-format",
"json"
],
}
6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,8 @@ path = "src/lib.rs"

[dependencies]
biodivine-lib-bdd = "0.5.1"
dyn-clonable = "0.9.0"
dyn-clonable = "0.9.0"
serde = { version = "1.0", features = ["derive"] }
serde-xml-rs = "0.6.0"
thiserror = "1.0.40"
xml-rs = "0.8.14"
461 changes: 461 additions & 0 deletions data/dataset.sbml

Large diffs are not rendered by default.

60 changes: 60 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ pub fn add(x: i32, y: i32) -> i32 {
x + y
}

// expose the prototype module
mod prototype;
pub use prototype::*;

#[cfg(test)]
mod tests {
use super::add;
Expand All @@ -21,4 +25,60 @@ mod tests {
pub fn test() {
assert_eq!(5, add(2, 3));
}

#[test]
pub fn test_foo() {
super::foo();
}

#[test]
pub fn test_tutorial() {
super::tutorial();
}

// #[test]
// pub fn test_sol() {
// super::node_processing();
// }

#[test]
pub fn test_sbml_model() {
super::trying();
}

#[test]
pub fn test_sbml_xml_rs() {
let xml = r#"<apply>
<lt/>
<cn type="integer">5</cn>
<ci> x </ci>
</apply>
<apply>
<eq/>
<ci>x</ci>
<cn type="integer">6</cn>
</apply>
"#;
let mut xml = xml::reader::EventReader::new(xml.as_bytes());
loop {
match xml.next() {
Ok(xml::reader::XmlEvent::StartElement { name, .. }) => {
if name.to_string() == "apply" {
println!("parsed apply {:?}", super::parse_apply_element(&mut xml));
}
}
Ok(xml::reader::XmlEvent::EndDocument) => {
println!("end of document");
break;
}
Err(err) => {
println!("err: {:?}", err);
break;
}
_ => {}
}
}

// super::parse_apply_element(&mut xml::reader::EventReader::new(xml.as_bytes()));
}
}
81 changes: 81 additions & 0 deletions src/prototype/expression.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
use std::io::BufRead;
use xml::reader::EventReader;

use super::sbml_xml_rs::Proposition;

pub enum Expression {
Terminal(Proposition),
// Internal(Box<Expression>),
Not(Box<Expression>),
And(Box<Expression>, Box<Expression>),
Or(Box<Expression>, Box<Expression>),
Xor(Box<Expression>, Box<Expression>),
Implies(Box<Expression>, Box<Expression>),
}

impl Expression {
pub fn dflt() -> Self {
unimplemented!("default expression")
}

// todo consider iterative approach instead of recursive?
pub fn try_from_xml<T: BufRead>(
xml: &mut EventReader<T>,
) -> Result<Self, Box<dyn std::error::Error>> {
loop {
match xml.next() {
Ok(xml::reader::XmlEvent::StartElement { name, .. }) => {
match name.local_name.as_str() {
"not" => unimplemented!(),
"and" => unimplemented!(),
"or" => unimplemented!(),
"xor" => unimplemented!(),
"implies" => unimplemented!(),
must_be_cmp_op => {
// todo oh fck already consumed the operator; need to pass it to parse_apply_element
let proposition = super::parse_apply_element(xml)?; // pass the op somehow
drain(xml, "apply")?; // clean the xml iterator; will be used
return Ok(Expression::Terminal(proposition));
}
}
}
Ok(xml::reader::XmlEvent::EndElement { name, .. }) => {
if name.local_name == "apply" {
return Ok(Expression::dflt()); // todo not default ofc; build
}
}
Ok(xml::reader::XmlEvent::EndDocument) => {
return Err("unexpected end of document".into())
}
Err(e) => panic!("Error: {}", e),
_ => (),
}
}
}
}

/// consume the resut of the xml iterator, until the appropriate closing tag is found
fn drain<T: BufRead>(
xml: &mut EventReader<T>,
stop: &str,
) -> Result<(), Box<dyn std::error::Error>> {
loop {
match xml.next() {
Ok(xml::reader::XmlEvent::StartElement { name, .. }) => {
return Err(format!("unexpected start element: {}", name.local_name).into());
}
Ok(xml::reader::XmlEvent::EndElement { name, .. }) => {
if name.local_name == stop {
return Ok(());
}
}
Ok(xml::reader::XmlEvent::EndDocument) => {
return Err("unexpected end of document".into());
}
Err(e) => {
return Err(e.into());
}
_ => (),
}
}
}
14 changes: 14 additions & 0 deletions src/prototype/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
mod tutorial;
pub use tutorial::*;

mod sol;
pub use sol::*;

mod sbml_model;
pub use sbml_model::*;

mod sbml_xml_rs;
pub use sbml_xml_rs::*;

mod expression;
pub use expression::*;
59 changes: 59 additions & 0 deletions src/prototype/sbml_model.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
use serde::{Deserialize, Serialize};
use serde_xml_rs::{from_str, to_string}; // todo likely want to read from stream or smth

struct Apply {}

// this is kinda scuffed but i need to preserve the order of the elements while allowing
// different permutations of the elements
// since serde_xml_rs does not work well with different orderings of the children, this
// will have to do; the correct number & type of children will have to be checked manually elsewhere
#[derive(Debug, Deserialize, Serialize)]
struct Proposition {
events: Vec<PropositionEvent>,
}

#[derive(Debug, Deserialize, Serialize)]
enum PropositionEvent {
CmpOp(CmpOp),
Ci(Ci),
Cn(Cn),
}

/// represents variable name in apply terminal
#[derive(Debug, Deserialize, Serialize)]
struct Ci {
#[serde(rename = "$value")]
// renaming so that it is wrapped in the tag instead of being tags attribute
value: String,
}

#[derive(Debug, Deserialize, Serialize)]
struct Cn {
value: f64, // todo
}

#[derive(Debug, Deserialize, Serialize)]
#[serde(rename_all = "lowercase")]
enum CmpOp {
Eq,
Neq,
Lt,
Leq,
Gt,
Geq,
}

pub fn trying() {
let lol = Proposition {
events: vec![
PropositionEvent::CmpOp(CmpOp::Eq),
PropositionEvent::Ci(Ci {
value: "x".to_string(),
}),
PropositionEvent::Cn(Cn { value: 5.0 }),
],
};

let xml = to_string(&lol).unwrap();
println!("{}", xml);
}
Loading

0 comments on commit cf49c3b

Please sign in to comment.