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

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

View File

@ -105,6 +105,11 @@ pub trait ExtensionsSystem: BasicSystem {
fn slice_trace(
&self,
) -> 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 {
@ -363,6 +368,62 @@ impl<T: BasicSystem> ExtensionsSystem for T {
// trace.enabled_reactions.pop();
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(
&self,
) -> Result<TraceIterator<'_, Self::Set, Self, Self::Process>, String> {
unimplemented!()
TraceIterator::<Self::Set, Self, Self::Process>::try_from(self)
}
fn environment(&self) -> &Self::Environment {

View File

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