Refactoring

This commit is contained in:
elvis
2025-08-16 21:52:36 +02:00
parent 1face6efda
commit a0a2f01c30
4 changed files with 2102 additions and 2020 deletions

File diff suppressed because it is too large Load Diff

212
src/rsprocess/assert_fmt.rs Normal file
View File

@ -0,0 +1,212 @@
// -----------------------------------------------------------------------------
// Display Implementation for all types
// -----------------------------------------------------------------------------
impl fmt::Display for RSassert {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "label {{\n{}\n}}", self.tree)
}
}
impl fmt::Display for Tree {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Concat(t1, t2) => {write!(f, "{t1};\n{t2}")},
Self::If(exp, t) => {write!(f, "if {exp} {{\n{t}\n}}")},
Self::IfElse(exp, t1, t2) => {
write!(f, "if {exp} {{\n{t1}\n}} else {{\n{t2}\n}}")
},
Self::Assignment(v, q, exp) => {
if let Some(q) = q {
write!(f, "{v}.{q} = {exp}")
} else {
write!(f, "{v} = {exp}")
}
},
Self::Return(exp) => {write!(f, "return {exp}")},
Self::For(v, r, t) => {write!(f, "for {v} in {r} {{\n{t}\n}}")},
}
}
}
impl fmt::Display for Variable {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Label => {write!(f, "label")},
Self::Edge => {write!(f, "edge")},
Self::Id(s) => {write!(f, "{s}")}
}
}
}
impl fmt::Display for Expression {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::True => {write!(f, "True")},
Self::False => {write!(f, "False")},
Self::Integer(i) => {write!(f, "{i}")},
Self::Label(rslabel) => {write!(f, "{{debug: {rslabel:?}}}")},
Self::Set(set) => {write!(f, "{{debug: {set:?}}}")},
Self::Element(el) => {write!(f, "'{{debug: {el:?}}}'")},
Self::String(s) => {write!(f, r#""{s}""#)},
Self::Var(v) => {write!(f, "{v}")},
Self::Unary(u, exp) => {
if u.is_prefix() {
write!(f, "{u}({exp})")
} else if u.is_suffix() {
write!(f, "{exp}{u}")
} else {
unreachable!()
}
},
Self::Binary(b, exp1, exp2) => {
if b.is_prefix() {
write!(f, "{b}({exp1}, {exp2})")
} else if b.is_suffix() {
write!(f, "({exp1}, {exp2}){b}")
} else if b.is_infix() {
write!(f, "({exp1} {b} {exp2})")
} else {
unreachable!()
}
},
}
}
}
impl fmt::Display for Range {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::IterateOverSet(exp) => {write!(f, "{{{exp}}}")},
Self::IterateInRange(exp1, exp2) => {write!(f, "{exp1}..{exp2}")}
}
}
}
impl fmt::Display for Unary {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Not => write!(f, "not"),
Self::Rand => write!(f, "rand"),
Self::Empty => write!(f, ".empty"),
Self::Length => write!(f, ".length"),
Self::ToStr => write!(f, ".tostr"),
Self::Qualifier(q) => write!(f, ".{q}"),
Self::ToEl => write!(f, ".toel"),
Self::Source => write!(f, ".source"),
Self::Target => write!(f, ".target"),
Self::Neighbours => write!(f, ".neighbours"),
Self::System => write!(f, ".system"),
}
}
}
impl fmt::Display for QualifierRestricted {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Entities => write!(f, "Entities"),
Self::Context => write!(f, "Context"),
Self::Reactants => write!(f, "Reactants"),
Self::ReactantsAbsent => write!(f, "ReactantsAbsent"),
Self::Inhibitors => write!(f, "Inhibitors"),
Self::InhibitorsPresent => write!(f, "InhibitorsPresent"),
Self::Products => write!(f, "Products"),
}
}
}
impl fmt::Display for QualifierLabel {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::AvailableEntities => write!(f, "AvailableEntities"),
Self::AllReactants => write!(f, "AllReactants"),
Self::AllInhibitors => write!(f, "AllInhibitors"),
}
}
}
impl fmt::Display for QualifierSystem {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Context => write!(f, "context"),
Self::Entities => write!(f, "entities"),
}
}
}
impl fmt::Display for Qualifier {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Label(q) => write!(f, "{q}"),
Self::Restricted(q) => write!(f, "{q}"),
Self::System(q) => write!(f, "{q}"),
}
}
}
impl fmt::Display for Binary {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::And => write!(f, "&&"),
Self::Or => write!(f, "||"),
Self::Xor => write!(f, "^^"),
Self::Less => write!(f, "<"),
Self::LessEq => write!(f, "<="),
Self::More => write!(f, ">"),
Self::MoreEq => write!(f, ">="),
Self::Eq => write!(f, "=="),
Self::NotEq => write!(f, "!="),
Self::Plus => write!(f, "+"),
Self::Minus => write!(f, "-"),
Self::Times => write!(f, "*"),
Self::Exponential => write!(f, "^"),
Self::Quotient => write!(f, "/"),
Self::Reminder => write!(f, "%"),
Self::Concat => write!(f, "::"),
Self::SubStr => write!(f, "substr"),
Self::Min => write!(f, "min"),
Self::Max => write!(f, "max"),
Self::CommonSubStr => write!(f, "commonsubstr"),
}
}
}
impl fmt::Display for AssertReturnValue {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Boolean(b) => write!(f, "{b}"),
Self::Integer(i) => write!(f, "{i}"),
Self::String(s) => write!(f, r#""{s}""#),
Self::Label(l) => write!(f, "{{debug: {l:?}}}"),
Self::Set(set) => write!(f, "{{debug: {set:?}}}"),
Self::Element(el) => write!(f, "{{debug: {el:?}}}"),
Self::Edge(edge) => write!(f, "{{debug: {edge:?}}}"),
Self::Node(node) => write!(f, "{{debug: {node:?}}}"),
Self::Neighbours(node) =>
write!(f, "{{debug: {node:?}}}.neighbours"),
Self::System(sys) => write!(f, "{{debug: {sys:?}}}"),
Self::Context(ctx) => write!(f, "{{debug: {ctx:?}}}"),
}
}
}
impl fmt::Display for AssertionTypes {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Boolean => write!(f, "boolean"),
Self::Integer => write!(f, "integer"),
Self::String => write!(f, "string"),
Self::Label => write!(f, "label"),
Self::Set => write!(f, "set"),
Self::Element => write!(f, "element"),
Self::System => write!(f, "system"),
Self::Context => write!(f, "context"),
Self::NoType => write!(f, "no type"),
Self::RangeInteger => write!(f, "range of integers"),
Self::RangeSet => write!(f, "range of set"),
Self::RangeNeighbours => write!(f, "range of edges"),
Self::Edge => write!(f, "edge"),
Self::Node => write!(f, "node"),
}
}
}

File diff suppressed because it is too large Load Diff

View File

@ -5,7 +5,7 @@ use crate::rsprocess::structure::{RSset,
RSprocess,
RSenvironment,
RSsystem,
RSlabel,
// RSlabel,
RSreaction};
use crate::rsprocess::structure::assert;
use crate::rsprocess::translator::{Translator, IdType};
@ -180,164 +180,174 @@ Env_term: (IdType, RSprocess) = {
// -----------------------------------------------------------------------------
// AssertParser
// -----------------------------------------------------------------------------
pub Assert: Box<assert::RSassert> = {
"label" "{" <f: AssertTree> "}" =>
Box::new(assert::RSassert{tree: f}),
};
// pub Assert: Box<assert::RSassert> = {
// "label" "{" <f: AssertTree> "}" =>
// Box::new(assert::RSassert{tree: f}),
// };
AssertTree: assert::Tree = {
<t1: AssertTree2> ";" <t2: AssertTree> =>
assert::Tree::Concat(Box::new(t1), Box::new(t2)),
<t: AssertTree2> => t,
}
// AssertTree: assert::Tree = {
// <t1: AssertTree2> ";" <t2: AssertTree> =>
// assert::Tree::Concat(Box::new(t1), Box::new(t2)),
// <t: AssertTree2> => t,
// }
AssertTree2: assert::Tree = {
"if" <e: AssertExpression>
"then" "{" <t: AssertTree> "}" =>
assert::Tree::If(Box::new(e), Box::new(t)),
// AssertTree2: assert::Tree = {
// "if" <e: AssertExpression>
// "then" "{" <t: AssertTree> "}" =>
// assert::Tree::If(Box::new(e), Box::new(t)),
"if" <e: AssertExpression>
"then" "{" <t1: AssertTree> "}"
"else" "{" <t2: AssertTree> "}" =>
assert::Tree::IfElse(Box::new(e), Box::new(t1), Box::new(t2)),
// "if" <e: AssertExpression>
// "then" "{" <t1: AssertTree> "}"
// "else" "{" <t2: AssertTree> "}" =>
// assert::Tree::IfElse(Box::new(e), Box::new(t1), Box::new(t2)),
"let" <v: AssertAssignmentVar> "=" <e: AssertExpression> =>
assert::Tree::Assignment(v, Box::new(e)),
// "let" <v: AssertVariable> <q: AssertQualifierLet?> "=" <e: AssertExpression> =>
// assert::Tree::Assignment(v, q, Box::new(e)),
"return" <e: AssertExpression> =>
assert::Tree::Return(Box::new(e)),
// "return" <e: AssertExpression> =>
// assert::Tree::Return(Box::new(e)),
"for" <v: AssertVariable> "in" <r: AssertRange> "{" <t: AssertTree> "}" =>
assert::Tree::For(v, r, Box::new(t)),
}
// "for" <v: AssertVariable> "in" <r: AssertRange> "{" <t: AssertTree> "}" =>
// assert::Tree::For(v, r, Box::new(t)),
// }
AssertAssignmentVar: assert::AssignmentVariable = {
<v: AssertVariable> => assert::AssignmentVariable::Var(v),
<v: AssertVariable> "." <q: AssertQualifierRestricted> =>
assert::AssignmentVariable::QualifiedVar(v, q),
}
// AssertQualifierLet: assert::Qualifier {
// "Entities" => assert::QualifierRestricted::Entities,
// "Context" => assert::QualifierRestricted::Context,
// "Reactants" => assert::QualifierRestricted::Reactants,
// "ReactantsAbsent" => assert::QualifierRestricted::ReactantsAbsent,
// "Inhibitors" => assert::QualifierRestricted::Inhibitors,
// "InhibitorsPresent" => assert::QualifierRestricted::InhibitorsPresent,
// "Products" => assert::QualifierRestricted::Products,
// }
AssertVariable: assert::Variable = {
"label" => assert::Variable::Label,
"edge" => assert::Variable::Edge,
<v: Literal> => assert::Variable::Id(v),
}
// AssertAssignmentVar: assert::AssignmentVariable = {
// <v: AssertVariable> => assert::AssignmentVariable::Var(v),
// <v: AssertVariable> "." <q: AssertQualifierRestricted> =>
// assert::AssignmentVariable::QualifiedVar(v, q),
// }
AssertExpression: assert::Expression = {
<unp: AssertUnaryPrefix> <e: AssertExpression> =>
assert::Expression::Unary(unp, Box::new(e)),
"(" <e: AssertExpression> ")" <uns: AssertUnarySuffix> =>
assert::Expression::Unary(uns, Box::new(e)),
// AssertVariable: assert::Variable = {
// "label" => assert::Variable::Label,
// "edge" => assert::Variable::Edge,
// <v: Literal> => assert::Variable::Id(v),
// }
"(" <e1: AssertExpression> <b: AssertBinary> <e2: AssertExpression> ")" =>
assert::Expression::Binary(b, Box::new(e1), Box::new(e2)),
<b: AssertBinaryPrefix>
"(" <e1: AssertExpression> "," <e2: AssertExpression> ")" =>
assert::Expression::Binary(b, Box::new(e1), Box::new(e2)),
// AssertExpression: assert::Expression = {
// <unp: AssertUnaryPrefix> <e: AssertExpression> =>
// assert::Expression::Unary(unp, Box::new(e)),
// "(" <e: AssertExpression> ")" <uns: AssertUnarySuffix> =>
// assert::Expression::Unary(uns, Box::new(e)),
"(" <e: AssertExpression> ")" => e,
"true" => assert::Expression::True,
"false" => assert::Expression::False,
// "(" <e1: AssertExpression> <b: AssertBinary> <e2: AssertExpression> ")" =>
// assert::Expression::Binary(b, Box::new(e1), Box::new(e2)),
// <b: AssertBinaryPrefix>
// "(" <e1: AssertExpression> "," <e2: AssertExpression> ")" =>
// assert::Expression::Binary(b, Box::new(e1), Box::new(e2)),
<v: AssertAssignmentVar> => assert::Expression::Var(v),
// "(" <e: AssertExpression> ")" => e,
// "true" => assert::Expression::True,
// "false" => assert::Expression::False,
// If changing IntegerType in assert.rs, also change from Num to another
// similar parser with different return type
<i: Num> => assert::Expression::Integer(i),
// <v: AssertAssignmentVar> => assert::Expression::Var(v),
<lab: AssertLabel> => assert::Expression::Label(Box::new(lab)),
<set: Set_of_entities> => assert::Expression::Set(set),
"'" <el: Literal> "'" => assert::Expression::Element(translator.encode(el)),
// // If changing IntegerType in assert.rs, also change from Num to another
// // similar parser with different return type
// <i: Num> => assert::Expression::Integer(i),
// strings
PATH => assert::Expression::String(<>.trim_end_matches("\"")
.trim_start_matches("\"")
.to_string()),
}
// <lab: AssertLabel> => assert::Expression::Label(Box::new(lab)),
// <set: Set_of_entities> => assert::Expression::Set(set),
// "'" <el: Literal> "'" => assert::Expression::Element(translator.encode(el)),
AssertUnaryPrefix: assert::Unary = {
"not" => assert::Unary::Not,
"rand" => assert::Unary::Rand,
}
// // strings
// PATH => assert::Expression::String(<>.trim_end_matches("\"")
// .trim_start_matches("\"")
// .to_string()),
// }
AssertUnarySuffix: assert::Unary = {
".empty" => assert::Unary::Empty,
".length" => assert::Unary::Length,
".tostr" => assert::Unary::ToStr,
".toel" => assert::Unary::ToEl,
".source" => assert::Unary::Source,
".target" => assert::Unary::Target,
".neighbours" => assert::Unary::Neighbours,
".system" => assert::Unary::System,
"." <q: AssertQualifierLabel> => assert::Unary::QualifierLabel(q),
"." <q: AssertQualifierSystem> => assert::Unary::QualifierSystem(q),
}
// AssertUnaryPrefix: assert::Unary = {
// "not" => assert::Unary::Not,
// "rand" => assert::Unary::Rand,
// }
AssertBinary: assert::Binary = {
"&&" => assert::Binary::And,
"||" => assert::Binary::Or,
"^^" => assert::Binary::Xor,
"<" => assert::Binary::Less,
"<=" => assert::Binary::LessEq,
">" => assert::Binary::More,
">=" => assert::Binary::MoreEq,
"==" => assert::Binary::Eq,
"!=" => assert::Binary::NotEq,
"+" => assert::Binary::Plus,
"-" => assert::Binary::Minus,
"*" => assert::Binary::Times,
"^" => assert::Binary::Exponential,
"/" => assert::Binary::Quotient,
"%" => assert::Binary::Reminder,
"::" => assert::Binary::Concat,
}
// AssertUnarySuffix: assert::Unary = {
// ".empty" => assert::Unary::Empty,
// ".length" => assert::Unary::Length,
// ".tostr" => assert::Unary::ToStr,
// ".toel" => assert::Unary::ToEl,
// ".source" => assert::Unary::Source,
// ".target" => assert::Unary::Target,
// ".neighbours" => assert::Unary::Neighbours,
// ".system" => assert::Unary::System,
// "." <q: AssertQualifierLabel> => assert::Unary::QualifierLabel(q),
// "." <q: AssertQualifierSystem> => assert::Unary::QualifierSystem(q),
// }
AssertBinaryPrefix: assert::Binary = {
"substr" => assert::Binary::SubStr,
"min" => assert::Binary::Min,
"max" => assert::Binary::Max,
"commonsubstr" => assert::Binary::CommonSubStr,
}
// AssertBinary: assert::Binary = {
// "&&" => assert::Binary::And,
// "||" => assert::Binary::Or,
// "^^" => assert::Binary::Xor,
// "<" => assert::Binary::Less,
// "<=" => assert::Binary::LessEq,
// ">" => assert::Binary::More,
// ">=" => assert::Binary::MoreEq,
// "==" => assert::Binary::Eq,
// "!=" => assert::Binary::NotEq,
// "+" => assert::Binary::Plus,
// "-" => assert::Binary::Minus,
// "*" => assert::Binary::Times,
// "^" => assert::Binary::Exponential,
// "/" => assert::Binary::Quotient,
// "%" => assert::Binary::Reminder,
// "::" => assert::Binary::Concat,
// }
AssertQualifierSystem: assert::QualifierSystem = {
"SystemEntities" => assert::QualifierSystem::Entities,
"SystemContext" => assert::QualifierSystem::Context,
}
// AssertBinaryPrefix: assert::Binary = {
// "substr" => assert::Binary::SubStr,
// "min" => assert::Binary::Min,
// "max" => assert::Binary::Max,
// "commonsubstr" => assert::Binary::CommonSubStr,
// }
AssertQualifierLabel: assert::QualifierLabel = {
"AvailableEntities" => assert::QualifierLabel::AvailableEntities,
"AllReactants" => assert::QualifierLabel::AllReactants,
"AllInhibitors" => assert::QualifierLabel::AllInhibitors,
<q: AssertQualifierRestricted> => assert::QualifierLabel::Restricted(q),
}
// AssertQualifierSystem: assert::QualifierSystem = {
// "SystemEntities" => assert::QualifierSystem::Entities,
// "SystemContext" => assert::QualifierSystem::Context,
// }
AssertQualifierRestricted: assert::QualifierRestricted = {
"Entities" => assert::QualifierRestricted::Entities,
"Context" => assert::QualifierRestricted::Context,
"Reactants" => assert::QualifierRestricted::Reactants,
"ReactantsAbsent" => assert::QualifierRestricted::ReactantsAbsent,
"Inhibitors" => assert::QualifierRestricted::Inhibitors,
"InhibitorsPresent" => assert::QualifierRestricted::InhibitorsPresent,
"Products" => assert::QualifierRestricted::Products,
}
// AssertQualifierLabel: assert::QualifierLabel = {
// "AvailableEntities" => assert::QualifierLabel::AvailableEntities,
// "AllReactants" => assert::QualifierLabel::AllReactants,
// "AllInhibitors" => assert::QualifierLabel::AllInhibitors,
// <q: AssertQualifierRestricted> => assert::QualifierLabel::Restricted(q),
// }
AssertLabel: RSlabel = {
"["
"Entities" ":" <e: Set_of_entities> ","
"Context" ":" <c: Set_of_entities> ","
"Reactants" ":" <r: Set_of_entities> ","
"ReactantsAbsent" ":" <r_a: Set_of_entities> ","
"Inhibitors" ":" <i: Set_of_entities> ","
"InhibitorsPresent" ":" <i_p: Set_of_entities> ","
"Products" ":" <p: Set_of_entities> ","
"]" => RSlabel::create(e, c, r, r_a, i, i_p, p)
}
// AssertQualifierRestricted: assert::QualifierRestricted = {
// "Entities" => assert::QualifierRestricted::Entities,
// "Context" => assert::QualifierRestricted::Context,
// "Reactants" => assert::QualifierRestricted::Reactants,
// "ReactantsAbsent" => assert::QualifierRestricted::ReactantsAbsent,
// "Inhibitors" => assert::QualifierRestricted::Inhibitors,
// "InhibitorsPresent" => assert::QualifierRestricted::InhibitorsPresent,
// "Products" => assert::QualifierRestricted::Products,
// }
AssertRange: assert::Range = {
"{" <e: AssertExpression> "}" => assert::Range::IterateOverSet(Box::new(e)),
"{" <e1: AssertExpression> ".." <e2: AssertExpression> "}" =>
assert::Range::IterateInRange(Box::new(e1), Box::new(e2)),
}
// AssertLabel: RSlabel = {
// "["
// "Entities" ":" <e: Set_of_entities> ","
// "Context" ":" <c: Set_of_entities> ","
// "Reactants" ":" <r: Set_of_entities> ","
// "ReactantsAbsent" ":" <r_a: Set_of_entities> ","
// "Inhibitors" ":" <i: Set_of_entities> ","
// "InhibitorsPresent" ":" <i_p: Set_of_entities> ","
// "Products" ":" <p: Set_of_entities> ","
// "]" => RSlabel::create(e, c, r, r_a, i, i_p, p)
// }
// AssertRange: assert::Range = {
// "{" <e: AssertExpression> "}" => assert::Range::IterateOverSet(Box::new(e)),
// "{" <e1: AssertExpression> ".." <e2: AssertExpression> "}" =>
// assert::Range::IterateInRange(Box::new(e1), Box::new(e2)),
// }
// -----------------------------------------------------------------------------
@ -727,9 +737,16 @@ Instruction: presets::Instruction = {
presets::Instruction::FastFrequency { experiment: p, so },
"Digraph" ">" <gso: Separated_Or<GraphSaveOptions, "|">> =>
presets::Instruction::Digraph { gso },
"Bisimilarity" "(" <p: Path> ")" "relabel" <edge_relabeler: Assert>
// <edge_relabeler: Assert>
"Bisimilarity" "(" <p: Path> ")" "relabel"
">" <so: SaveOptions> =>
presets::Instruction::Bisimilarity { system_b: p, edge_relabeler, so },
presets::Instruction::Bisimilarity {
system_b: p,
edge_relabeler: Box::new(assert::RSassert {
tree: assert::Tree::Return(Box::new(assert::Expression::True))
}),
so
},
}
pub Run: presets::Instructions = {