use super::documents::{declare_document, DocTree, TagRule, Vocabulary};
use super::element::{attributes, elements, fn_name};
use super::{Declared, Family};
pub use super::{fragment, json, El, Json, Value};
elements! {
study: children;
material: leaf;
fixture: leaf;
load_case = "loadCase": children;
force: leaf;
pressure: leaf;
mesh: leaf;
solver: leaf;
criterion: leaf;
revise: leaf;
fluid: leaf;
inlet: leaf;
outlet: leaf;
flow_mesh = "flowMesh": leaf;
step: leaf;
gains: leaf;
tune: leaf;
response: leaf;
vary: leaf;
workspace: leaf;
payload: leaf;
obstacle: leaf;
objective: leaf;
}
attributes! {
pub trait StudyAttrs {
kind;
part;
units;
title;
grade;
body;
poisson;
id;
type_;
stiffness;
axis;
faces;
total;
value;
order;
size;
refine;
analysis;
timeout;
quantity;
limit;
region;
load_case;
nu;
velocity;
cells;
levels;
iterations;
wall_model;
note;
parameter;
min;
max;
joint;
from;
to;
kp;
kv;
duration;
dt;
band;
step;
field;
values;
down;
mass;
gravity;
goal;
wall;
}
}
pub(crate) const FAMILY: Family = Family { module: "study", roots: &["study"], declare };
pub const STUDY_UNITS: &str = "mm-N-MPa-t";
pub const STUDY_ROOT_FIELDS: &[&str] = &["kind", "part", "units", "title"];
pub const STUDY_KINDS: &[&str] = &["structural", "flow", "step-response", "arm-design"];
pub const STEP_RESPONSE_UNITS: &str = "rad-s";
pub const ARM_DESIGN_UNITS: &str = "mm-kg-s";
pub struct StudyTag {
pub tag: &'static str,
pub field: &'static str,
pub list: bool,
pub typed: bool,
pub kind: Option<&'static str>,
}
const fn tag(tag: &'static str, field: &'static str, list: bool, typed: bool, kind: Option<&'static str>) -> StudyTag {
StudyTag { tag, field, list, typed, kind }
}
const S: Option<&str> = Some("structural");
const F: Option<&str> = Some("flow");
const R: Option<&str> = Some("step-response");
const A: Option<&str> = Some("arm-design");
pub const STUDY_TAGS: &[StudyTag] = &[
tag("criterion", "criteria", true, false, None),
tag("revise", "revise", true, false, None),
tag("material", "material", false, false, S),
tag("fixture", "fixtures", true, false, S),
tag("loadCase", "loadCases", true, false, S),
tag("force", "loads", true, true, S),
tag("pressure", "loads", true, true, S),
tag("mesh", "mesh", false, false, S),
tag("solver", "solver", false, false, S),
tag("fluid", "fluid", false, false, F),
tag("inlet", "inlets", true, false, F),
tag("outlet", "outlets", true, false, F),
tag("flowMesh", "flowMesh", false, false, F),
tag("step", "steps", true, false, R),
tag("gains", "gains", true, false, R),
tag("tune", "tune", true, false, R),
tag("response", "response", false, false, R),
tag("vary", "vary", true, false, A),
tag("workspace", "workspace", false, false, A),
tag("payload", "payload", false, false, A),
tag("obstacle", "obstacles", true, false, A),
tag("objective", "objectives", true, false, A),
];
const IN_STUDY: &[&str] = &["study"];
const FACES_LOAD: &[&str] = &["id", "faces", "total"];
const PRESSURE: &[&str] = &["id", "faces", "value"];
const FIXTURE: &[&str] = &["id", "type", "faces"];
const STEP: &[&str] = &["id", "joint", "from", "to"];
pub const STUDY: Vocabulary = Vocabulary {
format: "study",
noun: "a study",
root: "study",
tags: &[
TagRule::new("study", &[]).required(&["kind", "part"]).attrs(STUDY_ROOT_FIELDS),
TagRule::new("criterion", IN_STUDY).key("id").required(&["id", "quantity", "limit"]).attrs(&["id", "quantity", "limit", "region", "loadCase", "step", "note"]),
TagRule::new("revise", IN_STUDY).key("parameter").required(&["parameter", "min", "max"]).attrs(&["parameter", "min", "max"]),
TagRule::new("material", IN_STUDY).single().required(&["grade"]).attrs(&["grade", "body", "poisson"]),
TagRule::new("fixture", IN_STUDY).key("id").required(FIXTURE).attrs(&["id", "type", "faces", "region", "stiffness", "axis"]),
TagRule::new("loadCase", IN_STUDY).key("id").required(&["id"]).attrs(&["id"]),
TagRule::new("force", &["loadCase"]).key("id").required(FACES_LOAD).attrs(FACES_LOAD),
TagRule::new("pressure", &["loadCase"]).key("id").required(PRESSURE).attrs(PRESSURE),
TagRule::new("mesh", IN_STUDY).single().attrs(&["order", "size", "refine"]),
TagRule::new("solver", IN_STUDY).single().attrs(&["kind", "analysis", "timeout"]),
TagRule::new("fluid", IN_STUDY).single().required(&["nu"]).attrs(&["nu", "body"]),
TagRule::new("inlet", IN_STUDY).key("id").required(&["id", "faces", "velocity"]).attrs(&["id", "faces", "velocity"]),
TagRule::new("outlet", IN_STUDY).key("id").required(&["id", "faces"]).attrs(&["id", "faces"]),
TagRule::new("flowMesh", IN_STUDY).single().required(&["cells"]).attrs(&["cells", "levels", "iterations", "wallModel"]),
TagRule::new("step", IN_STUDY).key("id").required(STEP).attrs(STEP),
TagRule::new("gains", IN_STUDY).key("joint").required(&["joint"]).attrs(&["joint", "kp", "kv"]),
TagRule::new("tune", IN_STUDY).key("joint").required(&["joint"]).attrs(&["joint", "kp", "kv"]),
TagRule::new("response", IN_STUDY).single().required(&["duration"]).attrs(&["duration", "dt", "band"]),
TagRule::new("vary", IN_STUDY).key("id").required(&["id", "axis", "values"]).attrs(&["id", "field", "joint", "axis", "values"]),
TagRule::new("workspace", IN_STUDY).single().required(&["min", "max", "step"]).attrs(&["min", "max", "step", "down"]),
TagRule::new("payload", IN_STUDY).single().required(&["mass", "gravity"]).attrs(&["mass", "gravity"]),
TagRule::new("obstacle", IN_STUDY).key("id").required(&["id", "min", "max"]).attrs(&["id", "min", "max", "wall"]),
TagRule::new("objective", IN_STUDY).key("quantity").required(&["quantity", "goal"]).attrs(&["quantity", "goal"]),
],
from_tree: study_from_tree,
};
fn study_tag(tag: &str) -> Option<&'static StudyTag> {
STUDY_TAGS.iter().find(|t| t.tag == tag)
}
fn records_of(children: &[DocTree], into: &mut Json) {
for c in children {
let Some(t) = study_tag(c.tag) else { continue };
let mut rec = if t.typed { Json::obj().with("type", c.tag) } else { Json::obj() };
for (k, v) in &c.attrs {
rec.set(k, v.clone());
}
records_of(&c.children, &mut rec);
if t.list {
match into.get(t.field) {
Some(Json::Arr(_)) => {}
_ => {
into.set(t.field, Json::Arr(Vec::new()));
}
}
if let Json::Obj(entries) = into {
if let Some((_, Json::Arr(items))) = entries.iter_mut().find(|(k, _)| k == t.field) {
items.push(rec);
}
}
} else {
into.set(t.field, rec);
}
}
}
fn study_from_tree(t: &DocTree) -> Result<Json, String> {
let mut doc = t.record();
let kind = doc.get("kind").cloned();
let Some(kind) = kind.as_ref().and_then(Json::as_str).filter(|k| STUDY_KINDS.contains(k)) else {
return Err(format!("study() kind is one of {}, not {}", STUDY_KINDS.join(", "), kind.map(|k| k.text()).unwrap_or_else(|| "undefined".into())));
};
for c in &t.children {
if let Some(k) = study_tag(c.tag).and_then(|s| s.kind) {
if k != kind {
return Err(format!("{}() belongs to a {k} study, not to a {kind} one", fn_name(c.tag)));
}
}
}
records_of(&t.children, &mut doc);
Ok(doc)
}
fn declare(root: El) -> Result<Declared, String> {
declare_document(&root, &STUDY)
}