fmt and parser for positive set in grammar_separated

also slice with limit, implemented slice for system, changed print for
slicing trace to not use unicode
This commit is contained in:
elvis
2025-10-20 17:04:00 +02:00
parent 4f3e57faed
commit 7d0345f246
8 changed files with 131 additions and 19 deletions

View File

@ -1,8 +1,8 @@
use std::collections::HashMap; use std::collections::HashMap;
use serde::{Serialize, Deserialize};
use rsprocess::set::BasicSet; use rsprocess::set::BasicSet;
use rsprocess::{element, graph, label, process, set, system, translator}; use rsprocess::{element, graph, label, process, set, system, translator};
use serde::{Deserialize, Serialize};
/// If changing IntegerType in assert.rs, also change from Num to another /// If changing IntegerType in assert.rs, also change from Num to another
/// similar parser with different return type in grammar.lalrpop in /// similar parser with different return type in grammar.lalrpop in
@ -196,7 +196,9 @@ pub enum AssertReturnValue {
impl<S> Default for Assert<S> { impl<S> Default for Assert<S> {
fn default() -> Self { fn default() -> Self {
Self { tree: Tree::Return(Box::new(Expression::True)) } Self {
tree: Tree::Return(Box::new(Expression::True)),
}
} }
} }

View File

@ -1,8 +1,8 @@
use std::collections::HashMap; use std::collections::HashMap;
use serde::{Serialize, Deserialize};
use rsprocess::translator::PrintableWithTranslator; use rsprocess::translator::PrintableWithTranslator;
use rsprocess::{graph, label, set, system, translator}; use rsprocess::{graph, label, set, system, translator};
use serde::{Deserialize, Serialize};
use super::dsl::*; use super::dsl::*;

View File

@ -737,9 +737,9 @@ where
.map_edges(edge_relabeler, &mut system_a.translator)?; .map_edges(edge_relabeler, &mut system_a.translator)?;
let b: Graph<system::System, AssertReturnValue> = let b: Graph<system::System, AssertReturnValue> =
system_b system_b
.graph .graph
.unwrap() .unwrap()
.map_edges(edge_relabeler, &mut system_b.translator)?; .map_edges(edge_relabeler, &mut system_b.translator)?;
Ok(format!( Ok(format!(
"{}", "{}",
// bisimilarity::bisimilarity_kanellakis_smolka::bisimilarity(&&a, &&b) // bisimilarity::bisimilarity_kanellakis_smolka::bisimilarity(&&a, &&b)

View File

@ -2,12 +2,9 @@ use std::rc::Rc;
use std::str::FromStr; use std::str::FromStr;
use lalrpop_util::ParseError; use lalrpop_util::ParseError;
// use assert::{relabel, grouping};
use rsprocess::{set, reaction, process, environment, system, label}; use rsprocess::{set, reaction, process, environment, system, label};
use rsprocess::element::IdType; use rsprocess::element::{self, IdType};
use rsprocess::translator::Translator; use rsprocess::translator::Translator;
// use execution::presets;
// use rsprocess::graph;
use crate::custom_error; use crate::custom_error;
grammar(translator: &mut Translator); grammar(translator: &mut Translator);
@ -197,6 +194,16 @@ pub Set: set::Set = {
.collect::<Vec<_>>()) .collect::<Vec<_>>())
}; };
pub PositiveSet: set::PositiveSet = {
<s: Separated_Empty<"{", PositiveLiteral, ",", "}">> =>
set::PositiveSet::from(s)
};
PositiveLiteral: (element::IdType, element::IdState) = {
"+" <t: Literal> => (translator.encode(t), element::IdState::Positive),
"-" <t: Literal> => (translator.encode(t), element::IdState::Negative),
}
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
// ReactionParser // ReactionParser

View File

@ -452,7 +452,6 @@ impl From<Vec<(IdType, Process)>> for Environment {
} }
} }
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
// Confluence // Confluence
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------

View File

@ -105,6 +105,11 @@ pub trait ExtensionsSystem: BasicSystem {
fn slice_trace( fn slice_trace(
&self, &self,
) -> Result<SlicingTrace<Self::Set, Self::Reaction, Self>, String>; ) -> Result<SlicingTrace<Self::Set, Self::Reaction, Self>, String>;
fn slice_trace_limit(
&self,
limit: usize,
) -> Result<SlicingTrace<Self::Set, Self::Reaction, Self>, String>;
} }
impl<T: BasicSystem> ExtensionsSystem for T { impl<T: BasicSystem> ExtensionsSystem for T {
@ -363,6 +368,62 @@ impl<T: BasicSystem> ExtensionsSystem for T {
// trace.enabled_reactions.pop(); // trace.enabled_reactions.pop();
Ok(trace) Ok(trace)
} }
#[allow(clippy::field_reassign_with_default)]
#[allow(clippy::type_complexity)]
fn slice_trace_limit(
&self,
limit: usize,
) -> Result<SlicingTrace<Self::Set, Self::Reaction, Self>, String> {
let mut trace = SlicingTrace::default();
trace.context_elements = Rc::new(self.context_elements());
trace.products_elements = Rc::new(self.products_elements());
trace.reactions = Rc::new(self.reactions().clone());
trace.systems.push(Rc::new(self.clone()));
trace.elements.push(SlicingElement::from((
Self::Set::default(),
self.available_entities().clone(),
)));
let current: Option<(Self::Set, Self::Set, Vec<usize>, Self)> =
self.to_slicing_iterator()?.next();
if current.is_none() {
return Ok(trace);
}
let current = current.unwrap();
let (context, products, enabled_reactions, mut current) = current;
trace
.elements
.push(SlicingElement::from((context, products)));
trace
.enabled_reactions
.push(EnabledReactions::from(enabled_reactions));
trace.systems.push(Rc::new(current.clone()));
let mut n = limit;
loop {
n -= 1;
let t = current.to_slicing_iterator()?.next();
if let Some((context, products, enabled_reactions, next_sys)) = t
&& n > 0
{
current = next_sys;
trace
.elements
.push(SlicingElement::from((context, products)));
trace
.enabled_reactions
.push(EnabledReactions::from(enabled_reactions));
trace.systems.push(Rc::new(current.clone()));
} else {
break;
}
}
// trace.enabled_reactions.pop();
Ok(trace)
}
} }
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
@ -508,7 +569,7 @@ impl BasicSystem for System {
fn to_slicing_iterator( fn to_slicing_iterator(
&self, &self,
) -> Result<TraceIterator<'_, Self::Set, Self, Self::Process>, String> { ) -> Result<TraceIterator<'_, Self::Set, Self, Self::Process>, String> {
unimplemented!() TraceIterator::<Self::Set, Self, Self::Process>::try_from(self)
} }
fn environment(&self) -> &Self::Environment { fn environment(&self) -> &Self::Environment {

View File

@ -192,10 +192,8 @@ impl EnabledReactions {
} }
} }
#[derive(Clone, Debug, Hash, Serialize, Deserialize)] #[derive(Clone, Debug, Hash, Serialize, Deserialize)]
pub struct SlicingTrace<S, R, Sys> pub struct SlicingTrace<S, R, Sys> {
{
pub elements: Vec<SlicingElement<S>>, pub elements: Vec<SlicingElement<S>>,
pub enabled_reactions: Vec<EnabledReactions>, pub enabled_reactions: Vec<EnabledReactions>,
@ -411,14 +409,14 @@ impl<
if elements.peek().is_some() { if elements.peek().is_some() {
writeln!( writeln!(
f, f,
"{}\n\t|\n{: ^17}\n\t|\n\t", "{}\n\t\t|\n{: ^17}\n\t\t|\n\t\tv",
Formatter::from(translator, el), Formatter::from(translator, el),
format!("({})", Formatter::from(translator, r)), format!("({})", Formatter::from(translator, r)),
)?; )?;
} else { } else {
writeln!( writeln!(
f, f,
"{}\n\t|\n{: ^17}\n\t|\n\t?", "{}\n\t\t|\n{: ^17}\n\t\t|\n\t\t?",
Formatter::from(translator, el), Formatter::from(translator, el),
format!("({})", Formatter::from(translator, r)), format!("({})", Formatter::from(translator, r)),
)?; )?;
@ -426,7 +424,7 @@ impl<
} else if elements.peek().is_some() { } else if elements.peek().is_some() {
writeln!( writeln!(
f, f,
"{}\n\t|\n\t|\n\t|\n\t", "{}\n\t\t|\n\t\t|\n\t\t|\n\t\tv",
Formatter::from(translator, el) Formatter::from(translator, el)
)?; )?;
} else { } else {

View File

@ -225,11 +225,56 @@ impl<'a> TryFrom<&'a PositiveSystem>
} }
} }
impl<'a> TryFrom<&'a System> for TraceIterator<'a, Set, System, Process> {
type Error = String;
fn try_from(value: &'a System) -> Result<Self, Self::Error> {
match value.unfold() {
| Ok(o) => Ok(Self {
choices_iterator: o.into_iter(),
system: value,
}),
| Err(e) => Err(e),
}
}
}
impl<'a> Iterator for TraceIterator<'a, Set, System, Process> { impl<'a> Iterator for TraceIterator<'a, Set, System, Process> {
type Item = (Set, Set, Vec<usize>, System); type Item = (Set, Set, Vec<usize>, System);
fn next(&mut self) -> Option<Self::Item> { fn next(&mut self) -> Option<Self::Item> {
unimplemented!() let (context, k) = self.choices_iterator.next()?;
let total_entities =
self.system.available_entities().union(context.as_ref());
let (enabled_reaction_positions, all_products) =
self.system.reactions().iter().enumerate().fold(
(vec![], Set::default()),
|mut acc, (pos, reaction)| {
if reaction.enabled(&total_entities) {
acc.0.push(pos);
(acc.0, acc.1.union(&reaction.products))
} else {
acc
}
},
);
let new_system = System::from(
Rc::clone(&self.system.delta),
// all_products.add_unique(&self.system.
// negated_products_elements()),
all_products.clone(),
(*k).clone(),
Rc::clone(&self.system.reaction_rules),
);
Some((
context.as_ref().clone(),
all_products,
enabled_reaction_positions,
new_system,
))
} }
} }