From aa0f795fae2f137b63279d15d401ea7adc545300 Mon Sep 17 00:00:00 2001 From: elvis Date: Tue, 2 Sep 2025 01:34:01 +0200 Subject: [PATCH] More examples --- Cargo.toml | 1 + src/main.rs | 9 +++++-- src/rsprocess/grammar.lalrpop | 1 - src/rsprocess/presets.rs | 30 +++++++++++----------- testing/examples/bisimilarity.system | 8 ++++++ testing/examples/bisimilarity2.system | 4 +++ testing/examples/deserialize.system | 3 +++ testing/examples/digraph.system | 16 ++++++++++++ testing/examples/fastfrequency.experiment | 2 ++ testing/examples/fastfrequency.system | 6 +++++ testing/examples/frequency.system | 6 +++++ testing/examples/limitfrequency.experiment | 2 ++ testing/examples/limitfrequency.system | 6 +++++ testing/examples/loop.system | 7 +++++ testing/examples/run.system | 6 +++++ testing/examples/serialize.system | 8 ++++++ testing/examples/stats.system | 6 +++++ testing/examples/target.system | 6 +++++ 18 files changed, 109 insertions(+), 18 deletions(-) create mode 100644 testing/examples/bisimilarity.system create mode 100644 testing/examples/bisimilarity2.system create mode 100644 testing/examples/deserialize.system create mode 100644 testing/examples/digraph.system create mode 100644 testing/examples/fastfrequency.experiment create mode 100644 testing/examples/fastfrequency.system create mode 100644 testing/examples/frequency.system create mode 100644 testing/examples/limitfrequency.experiment create mode 100644 testing/examples/limitfrequency.system create mode 100644 testing/examples/loop.system create mode 100644 testing/examples/run.system create mode 100644 testing/examples/serialize.system create mode 100644 testing/examples/stats.system create mode 100644 testing/examples/target.system diff --git a/Cargo.toml b/Cargo.toml index f3e5412..7ac97b9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,6 +2,7 @@ name = "reactionsystems" version = "0.1.0" edition = "2024" +exclude = ["*.system", "*.experiment", "/testing/", "*.serial"] [build-dependencies] lalrpop = "0.22" diff --git a/src/main.rs b/src/main.rs index 45adea1..8f62dbf 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,8 +1,13 @@ fn main() { + use reactionsystems::rsprocess::presets; + + let mut input = String::new(); + std::io::stdin().read_line(&mut input).unwrap(); + input = input.trim().into(); + let now = std::time::Instant::now(); - use reactionsystems::rsprocess::presets; - match presets::run("testing/medical.system".into()) { + match presets::run(input) { Ok(()) => {}, Err(e) => {println!("{e}")} } diff --git a/src/rsprocess/grammar.lalrpop b/src/rsprocess/grammar.lalrpop index 28d0d0f..f131f8e 100644 --- a/src/rsprocess/grammar.lalrpop +++ b/src/rsprocess/grammar.lalrpop @@ -845,7 +845,6 @@ Instruction: presets::Instruction = { presets::Instruction::FastFrequency { experiment: p, so }, "Digraph" ">" > => presets::Instruction::Digraph { gso }, - // "Bisimilarity" "(" ")" "relabel" ">" => presets::Instruction::Bisimilarity { diff --git a/src/rsprocess/presets.rs b/src/rsprocess/presets.rs index 31d256c..9909129 100644 --- a/src/rsprocess/presets.rs +++ b/src/rsprocess/presets.rs @@ -162,20 +162,20 @@ where // relative path let mut path = match env::current_dir() { Ok(p) => p, - Err(_) => return Err("Error getting current directory.".into()), + Err(e) => return Err(format!("Error getting current directory: {e}")), }; path = path.join(path_string); // we read the file with a buffer let f = match fs::File::open(path) { Ok(f) => f, - Err(_) => return Err("Error opening file.".into()), + Err(e) => return Err(format!("Error opening file: {e}.")), }; let mut buf_reader = io::BufReader::new(f); let mut contents = String::new(); match buf_reader.read_to_string(&mut contents) { Ok(_) => {} - Err(_) => return Err("Error reading file.".into()), + Err(e) => return Err(format!("Error reading file: {e}")), } // parse @@ -215,7 +215,7 @@ where let mut err = format!( "Unrecognized token {}{}{} \ - between positions {l} and {r}. ", + between positions {l} and {r}.", "\"".red(), t.to_string().red(), "\"".red(), @@ -252,7 +252,7 @@ where let line_pos_r = r - left_new_line; err.push_str( - &format!(".\nLine {} position {} to {}:\n{}{}{}{}", + &format!("\nLine {} position {} to {}:\n{}{}{}{}", line_number, line_pos_l, line_pos_r, @@ -334,7 +334,7 @@ pub fn stats(system: &EvaluatedSystem) -> Result { Ok(sys.statistics(translator)), EvaluatedSystem::Graph { graph, translator } => { let Some(sys) = graph.node_weights().next() else { - return Err("No node found in graph".into()); + return Err("No node found in graph.".into()); }; Ok(sys.statistics(translator)) } @@ -350,7 +350,7 @@ pub fn target(system: &EvaluatedSystem) -> Result { (sys.target()?, translator), EvaluatedSystem::Graph { graph, translator } => { let Some(sys) = graph.node_weights().next() else { - return Err("No node found in graph".into()); + return Err("No node found in graph.".into()); }; (sys.target()?, translator) } @@ -373,7 +373,7 @@ pub fn traversed(system: &EvaluatedSystem) -> Result { } EvaluatedSystem::Graph { graph, translator } => { let Some(sys) = graph.node_weights().next() else { - return Err("No node found in graph".into()); + return Err("No node found in graph.".into()); }; (sys.run_separated()?, translator) } @@ -381,7 +381,7 @@ pub fn traversed(system: &EvaluatedSystem) -> Result { let mut output = String::new(); - output.push_str("The trace is composed by the set of entities:"); + output.push_str("The trace is composed by the set of entities: "); for (e, _c, _t) in res { output.push_str(&format!( "{}", @@ -394,7 +394,7 @@ pub fn traversed(system: &EvaluatedSystem) -> Result { /// Finds the looping list of states in a reaction system with a perpetual /// context. IMPORTANT: for loops, we assume Delta defines the process constant /// x = Q.x and the context process is x . -/// equivalent to main_do(loop,Es) + /// equivalent to main_do(loop,Es) pub fn hoop( system: &EvaluatedSystem, symbol: String @@ -403,14 +403,14 @@ pub fn hoop( EvaluatedSystem::System { sys, translator } => (sys, translator), EvaluatedSystem::Graph { graph, translator } => { let Some(sys) = graph.node_weights().next() else { - return Err("No node found in graph".into()); + return Err("No node found in graph.".into()); }; (sys, translator) } }; // we retrieve the id for "x" and use it to find the corresponding loop let Some(id) = translator.encode_not_mut(&symbol) else { - return Err(format!("Symbol {symbol} not found")); + return Err(format!("Symbol {symbol} not found.")); }; let res = match res.lollipops_only_loop_named(id) { Some(o) => o, @@ -421,7 +421,7 @@ pub fn hoop( let mut output = String::new(); - output.push_str("The loop is composed by the sets:"); + output.push_str("The loop is composed by the sets: "); for e in res { output.push_str(&format!( "{}", @@ -440,7 +440,7 @@ pub fn freq(system: &EvaluatedSystem) -> Result { EvaluatedSystem::System { sys, translator } => (sys, translator), EvaluatedSystem::Graph { graph, translator } => { let Some(sys) = graph.node_weights().next() else { - return Err("No node found in graph".into()); + return Err("No node found in graph.".into()); }; (sys, translator) } @@ -465,7 +465,7 @@ pub fn limit_freq( EvaluatedSystem::System { sys, translator } => (sys, translator), EvaluatedSystem::Graph { graph, translator } => { let Some(sys) = graph.node_weights().next() else { - return Err("No node found in graph".into()); + return Err("No node found in graph.".into()); }; (sys, translator) } diff --git a/testing/examples/bisimilarity.system b/testing/examples/bisimilarity.system new file mode 100644 index 0000000..62eb75f --- /dev/null +++ b/testing/examples/bisimilarity.system @@ -0,0 +1,8 @@ +Environment: [x = {a}.y, y =({a}.x + {b}.y)] +Initial Entities: {a, b} +Context: [({a,b}.{a}.{a,c}.x + {a,b}.{a}.{a}.nill)] +Reactions: ([{a,b}, {c}, {b}]) + +Bisimilarity ("bisimilarity2.system") + relabel label { return true } + > Print, diff --git a/testing/examples/bisimilarity2.system b/testing/examples/bisimilarity2.system new file mode 100644 index 0000000..b3ddd79 --- /dev/null +++ b/testing/examples/bisimilarity2.system @@ -0,0 +1,4 @@ +Environment: [x = {a}.y, y =({a}.x + {b}.y)] +Initial Entities: {a} +Context: [{a,b}.{a}.{a,c}.nill] +Reactions: ([r: {a,b}, i: {c}, p: {b}]) diff --git a/testing/examples/deserialize.system b/testing/examples/deserialize.system new file mode 100644 index 0000000..6e14b22 --- /dev/null +++ b/testing/examples/deserialize.system @@ -0,0 +1,3 @@ +Deserialize ("deserialize.serial") + +Stats > Print diff --git a/testing/examples/digraph.system b/testing/examples/digraph.system new file mode 100644 index 0000000..6fc27b9 --- /dev/null +++ b/testing/examples/digraph.system @@ -0,0 +1,16 @@ +Environment: [x = {a}.y, y =({a}.x + {b}.y) ] +Initial Entities: {a, b} +Context: [({a,b}.{a}.{a,c}.x + {a,b}.{a}.{a}.nill)] +Reactions: ([{a,b}, {c}, {b}]) + +Digraph > Dot + | Entities " - " Context " - entities masked: " MaskEntities {a} + | Entities + | Entities < {a} ? "blue" ! "white" + | ! "black" + > Print, + +Digraph > GraphML + | Entities + | Entities + > Print diff --git a/testing/examples/fastfrequency.experiment b/testing/examples/fastfrequency.experiment new file mode 100644 index 0000000..81dd1dc --- /dev/null +++ b/testing/examples/fastfrequency.experiment @@ -0,0 +1,2 @@ +Weights: 10, 5 +Sets: {a,b}, {a} diff --git a/testing/examples/fastfrequency.system b/testing/examples/fastfrequency.system new file mode 100644 index 0000000..6784ed0 --- /dev/null +++ b/testing/examples/fastfrequency.system @@ -0,0 +1,6 @@ +Environment: [ x = {b}.{c}.x ] +Initial Entities: {a, b} +Context: [({a,b}.{a}.{a,c}.x + {a,b}.{a}.{a}.nill)] +Reactions: ([{a}, {b}, {b}]; [{b}, {a}, {a}]; [{c}, {b}, {c}]) + +FastFrequency("fastfrequency.experiment") > Print diff --git a/testing/examples/frequency.system b/testing/examples/frequency.system new file mode 100644 index 0000000..c03486a --- /dev/null +++ b/testing/examples/frequency.system @@ -0,0 +1,6 @@ +Environment: [x = {a}.y, y =({a}.{a, b}.nill + {b}.nill) ] +Initial Entities: {a, b} +Context: [({a,b}.{a}.{a,c}.x + {a,b}.{a}.{a}.nill)] +Reactions: ([{a,b}, {c}, {b}]) + +Frequency > Print diff --git a/testing/examples/limitfrequency.experiment b/testing/examples/limitfrequency.experiment new file mode 100644 index 0000000..81dd1dc --- /dev/null +++ b/testing/examples/limitfrequency.experiment @@ -0,0 +1,2 @@ +Weights: 10, 5 +Sets: {a,b}, {a} diff --git a/testing/examples/limitfrequency.system b/testing/examples/limitfrequency.system new file mode 100644 index 0000000..ed0dad5 --- /dev/null +++ b/testing/examples/limitfrequency.system @@ -0,0 +1,6 @@ +Environment: [ x = {b}.{c}.x ] +Initial Entities: {a, b} +Context: [({a,b}.{a}.{a,c}.x + {a,b}.{a}.{a}.nill)] +Reactions: ([{a}, {b}, {b}]; [{b}, {a}, {a}]; [{c}, {b}, {c}]) + +LimitFrequency("limitfrequency.experiment") > Print diff --git a/testing/examples/loop.system b/testing/examples/loop.system new file mode 100644 index 0000000..a3dabfb --- /dev/null +++ b/testing/examples/loop.system @@ -0,0 +1,7 @@ +Environment: [ x = {b}.x, y = {a, c}.y ] +Initial Entities: {a, b} +Context: [({a,b}.{a}.{a,c}.x + {a,b}.{a}.{a}.y)] +Reactions: ([{a}, {b}, {b}]; [{b}, {a}, {a}]; [{c}, {}, {d}]) + +Loop (x) > Print, +Loop (y) > Print diff --git a/testing/examples/run.system b/testing/examples/run.system new file mode 100644 index 0000000..c567ac5 --- /dev/null +++ b/testing/examples/run.system @@ -0,0 +1,6 @@ +Environment: [x = {a}.y, y =({a}.{a, b}.nill + {b}.nill) ] +Initial Entities: {a, b} +Context: [({a,b}.{a}.{a,c}.x + {a,b}.{a}.{a}.nill)] +Reactions: ([{a,b}, {c}, {b}]) + +Run > Print diff --git a/testing/examples/serialize.system b/testing/examples/serialize.system new file mode 100644 index 0000000..d65807a --- /dev/null +++ b/testing/examples/serialize.system @@ -0,0 +1,8 @@ +Environment: [x = {a}.y, y =({a}.x + {b}.y)] +Initial Entities: {a, b} +Context: [({a,b}.{a}.{a,c}.x + {a,b}.{a}.{a}.nill)] +Reactions: ([{a,b}, {c}, {b}]) + +Digraph > Serialize ("deserialize.serial"), + +Stats > Print diff --git a/testing/examples/stats.system b/testing/examples/stats.system new file mode 100644 index 0000000..88c1ff1 --- /dev/null +++ b/testing/examples/stats.system @@ -0,0 +1,6 @@ +Environment: [x = {a}.y, y =({a}.x + {b}.y) ] +Initial Entities: {a, b} +Context: [({a,b}.{a}.{a,c}.x + {a,b}.{a}.{a}.nill)] +Reactions: ([{a,b}, {c}, {b}]) + +Stats > Print diff --git a/testing/examples/target.system b/testing/examples/target.system new file mode 100644 index 0000000..a9d89d4 --- /dev/null +++ b/testing/examples/target.system @@ -0,0 +1,6 @@ +Environment: [x = {a}.y, y =({a}.{a, b}.nill + {b}.nill) ] +Initial Entities: {a, b} +Context: [({a,b}.{a}.{a,c}.x + {a,b}.{a}.{a}.nill)] +Reactions: ([{a,b}, {c}, {b}]) + +Target > Print