Skip to content

Commit

Permalink
Decapodes endpoint (#604)
Browse files Browse the repository at this point in the history
This should add an endpoint that takes in a list of MathML strings in
and returns a DecapodeCollection, which is mainly a vector of Decapodes,
one for each input MathML string.

---------

Co-authored-by: Justin <[email protected]>
  • Loading branch information
Free-Quarks and Justin authored Oct 31, 2023
1 parent df3e789 commit e5ed30d
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 12 deletions.
5 changes: 5 additions & 0 deletions skema/rest/morae_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,8 @@ async def get_models() -> List[str]:
@router.get("/ping", summary="Status of MORAE service")
async def healthcheck() -> int:
return requests.get(f"{SKEMA_RS_ADDESS}/ping").status_code


@router.get("/mathml/decapodes", summary="Gets Decapodes from a list of MathML strings")
async def get_decapodes(mathml: List[str]) -> Dict[Text, Any]:
return requests.get(f"{SKEMA_RS_ADDESS}/mathml/decapodes", json=mathml).json()
25 changes: 15 additions & 10 deletions skema/skema-rs/mathml/src/parsers/decapodes_serialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ use crate::ast::{
MathExpression,
};
use crate::parsers::math_expression_tree::MathExpressionTree;

use utoipa::ToSchema;
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Serialize, Deserialize)]
#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
enum Type {
Form0,
Form1,
Expand All @@ -15,47 +15,47 @@ enum Type {
Literal,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
pub struct Variable {
r#type: Type,
pub name: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
pub struct UnaryOperator {
pub src: usize,
pub tgt: usize,
pub op1: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
pub struct ProjectionOperator {
pub proj1: usize,
pub proj2: usize,
pub res: usize,
pub op2: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
pub struct Sum {
pub sum: usize,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
pub struct Summation {
pub summand: usize,
pub summation: usize,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
pub struct TableCounts {
pub variable_count: usize,
pub operation_count: usize,
pub sum_count: usize,
pub multi_count: usize,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
pub struct Tables {
pub variables: Vec<Variable>,
pub projection_operators: Vec<ProjectionOperator>,
Expand All @@ -64,7 +64,7 @@ pub struct Tables {
pub summand_op: Vec<Summation>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
pub struct WiringDiagram {
pub Var: Vec<Variable>,
pub Op1: Vec<UnaryOperator>,
Expand All @@ -73,6 +73,11 @@ pub struct WiringDiagram {
pub Summand: Vec<Summation>,
}

#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
pub struct DecapodesCollection {
pub decapodes: Vec<WiringDiagram>
}

pub fn to_decapodes_serialization(
input: &MathExpressionTree,
tables: &mut Tables,
Expand Down
28 changes: 26 additions & 2 deletions skema/skema-rs/skema/src/bin/morae.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ pub use mathml::mml2pn::{ACSet, Term};
// new imports
use mathml::acset::{PetriNet, RegNet};
use mathml::parsers::first_order_ode::get_FirstOrderODE_vec_from_file;
use mathml::parsers::math_expression_tree::MathExpressionTree;
use mathml::parsers::decapodes_serialization::{to_wiring_diagram, WiringDiagram, DecapodesCollection};
use skema::model_extraction::{module_id2mathml_MET_ast, subgraph2_core_dyn_ast};
use std::io::{BufRead, BufReader};
use std::fs::File;


#[derive(Parser, Debug)]
struct Cli {
Expand Down Expand Up @@ -38,13 +43,32 @@ fn main() {

let host = "localhost";

let math_content = module_id2mathml_MET_ast(module_id, host);
//let math_content = module_id2mathml_MET_ast(module_id, host);

let input_src = "../../data/mml2pn_inputs/testing_eqns/sidarthe_mml.txt";

// This does get a panic with a message, so need to figure out how to forward it
//let _mathml_ast = get_mathml_asts_from_file(input_src.clone());

let f = File::open(input_src.clone()).unwrap();
let lines = BufReader::new(f).lines();
let mut deca_vec = Vec::<MathExpressionTree>::new();
let mut wiring_vec = Vec::<WiringDiagram>::new();

for line in lines.flatten() {
let mut deca = line
.parse::<MathExpressionTree>()
.unwrap_or_else(|_| panic!("Unable to parse line {}!", line));
wiring_vec.push(to_wiring_diagram(&deca))
}

let decapodescollection = DecapodesCollection {
decapodes: wiring_vec.clone()
};

println!("{:?}", wiring_vec.clone());
println!("decapode collection: {:?}", decapodescollection.clone());

let odes = get_FirstOrderODE_vec_from_file(input_src.clone());

//println!("\nmath_content: {:?}", math_content);
Expand All @@ -54,7 +78,7 @@ fn main() {
"\nAMR from mathml: {}\n",
serde_json::to_string(&PetriNet::from(odes)).unwrap()
);
println!("\nAMR from code: {:?}", PetriNet::from(math_content));
//println!("\nAMR from code: {:?}", PetriNet::from(math_content));
}
// This is the graph id for the top level function for the core dynamics for our test case.
else if new_args.arg == *"manual" {
Expand Down
4 changes: 4 additions & 0 deletions skema/skema-rs/skema/src/bin/skema_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ async fn main() -> std::io::Result<()> {
skema::services::mathml::get_content_mathml,
skema::services::mathml::get_regnet,
skema::services::mathml::get_amr,
skema::services::mathml::get_decapodes,
gromet::get_model_ids,
gromet::post_model,
gromet::delete_model,
Expand All @@ -78,6 +79,8 @@ async fn main() -> std::io::Result<()> {
),
components(
schemas(
mathml::parsers::decapodes_serialization::DecapodesCollection,
mathml::parsers::decapodes_serialization::WiringDiagram,
mathml::acset::AMRmathml,
mathml::acset::RegNet,
mathml::acset::ModelRegNet,
Expand Down Expand Up @@ -143,6 +146,7 @@ async fn main() -> std::io::Result<()> {
.service(skema::services::mathml::get_acset)
.service(skema::services::mathml::get_regnet)
.service(skema::services::mathml::get_amr)
.service(skema::services::mathml::get_decapodes)
.service(gromet::get_model_RN)
.service(gromet::model2PN)
.service(gromet::model2RN)
Expand Down
26 changes: 26 additions & 0 deletions skema/skema-rs/skema/src/services/mathml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ use mathml::{
expression::{preprocess_content, wrap_math},
parsers::first_order_ode::{first_order_ode, FirstOrderODE},
};
use mathml::parsers::math_expression_tree::MathExpressionTree;
use mathml::parsers::decapodes_serialization::{to_wiring_diagram, WiringDiagram, DecapodesCollection};
use petgraph::dot::{Config, Dot};
use utoipa;

Expand Down Expand Up @@ -75,6 +77,30 @@ pub async fn get_content_mathml(payload: String) -> String {
ode.to_cmml()
}

/// Return a JSON representation of a DecapodeCollection, which should be the foundation of a DecapodeCollection AMR, from
/// an array of MathML strings.
#[utoipa::path(
request_body = Vec<String>,
responses(
(
status = 200,
body = DecapodeCollection
)
)
)]
#[put("/mathml/decapodes")]
pub async fn get_decapodes(payload: web::Json<Vec<String>>) -> HttpResponse {
let met_vec: Vec<MathExpressionTree> = payload.iter().map(|x| x.parse::<MathExpressionTree>().unwrap()).collect();
let mut deca_vec = Vec::<WiringDiagram>::new();
for term in met_vec.iter() {
deca_vec.push(to_wiring_diagram(term));
}
let decapodes_collection = DecapodesCollection {
decapodes: deca_vec.clone()
};
HttpResponse::Ok().json(web::Json(decapodes_collection))
}

/// Return a JSON representation of a PetriNet ModelRep constructed from an array of MathML strings.
#[utoipa::path(
request_body = Vec<String>,
Expand Down

0 comments on commit e5ed30d

Please sign in to comment.