2025-07-10 15:02:14 +02:00
|
|
|
//! Definitions for finding loops in simulation.
|
|
|
|
|
|
2025-07-09 19:34:15 +02:00
|
|
|
use super::classical::compute_all;
|
2025-06-22 16:09:31 +02:00
|
|
|
use super::structure::{RSenvironment, RSprocess, RSreaction, RSset, RSsystem};
|
2025-07-01 19:22:50 +02:00
|
|
|
use super::translator::IdType;
|
2025-06-20 17:11:00 +02:00
|
|
|
|
2025-07-10 15:02:14 +02:00
|
|
|
/// Returns the prefix and the loop from a trace.
|
2025-06-22 16:09:31 +02:00
|
|
|
fn split<'a>(
|
|
|
|
|
set: &'a RSset,
|
|
|
|
|
trace: &'a [RSset]
|
2025-07-01 19:22:50 +02:00
|
|
|
) -> Option<(&'a [RSset], &'a [RSset])> {
|
2025-06-19 23:48:16 +02:00
|
|
|
let position = trace.iter().rposition(|x| x == set);
|
|
|
|
|
position.map(|pos| trace.split_at(pos))
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-10 15:02:14 +02:00
|
|
|
/// Finds the loops by simulating the system.
|
2025-06-22 16:09:31 +02:00
|
|
|
fn find_loop(
|
|
|
|
|
rs: &[RSreaction],
|
|
|
|
|
entities: RSset,
|
|
|
|
|
q: &RSset
|
|
|
|
|
) -> (Vec<RSset>, Vec<RSset>) {
|
2025-06-20 17:11:00 +02:00
|
|
|
let mut entities = entities;
|
2025-06-19 23:48:16 +02:00
|
|
|
let mut trace = vec![];
|
|
|
|
|
loop {
|
2025-07-01 19:22:50 +02:00
|
|
|
if let Some((prefix, hoop)) = split(&entities, &trace) {
|
|
|
|
|
return (prefix.to_vec(), hoop.to_vec());
|
|
|
|
|
} else {
|
|
|
|
|
let t = entities.union(q);
|
2025-07-09 19:34:15 +02:00
|
|
|
let products = compute_all(&t, rs);
|
2025-07-01 19:22:50 +02:00
|
|
|
trace.push(entities.clone());
|
|
|
|
|
entities = products;
|
|
|
|
|
}
|
2025-06-19 23:48:16 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-10 15:02:14 +02:00
|
|
|
/// Finds the loops by simulating the system.
|
2025-07-01 19:22:50 +02:00
|
|
|
fn find_only_loop(
|
|
|
|
|
rs: &[RSreaction],
|
|
|
|
|
entities: RSset,
|
|
|
|
|
q: &RSset
|
|
|
|
|
) -> Vec<RSset> {
|
2025-06-20 17:11:00 +02:00
|
|
|
let mut entities = entities;
|
|
|
|
|
let mut trace = vec![];
|
|
|
|
|
loop {
|
2025-07-01 19:22:50 +02:00
|
|
|
if let Some((_prefix, hoop)) = split(&entities, &trace) {
|
|
|
|
|
return hoop.to_vec();
|
|
|
|
|
} else {
|
|
|
|
|
let t = entities.union(q);
|
2025-07-09 19:34:15 +02:00
|
|
|
let products = compute_all(&t, rs);
|
2025-07-01 19:22:50 +02:00
|
|
|
trace.push(entities.clone());
|
|
|
|
|
entities = products;
|
|
|
|
|
}
|
2025-06-19 23:48:16 +02:00
|
|
|
}
|
2025-06-20 17:11:00 +02:00
|
|
|
}
|
2025-06-19 23:48:16 +02:00
|
|
|
|
2025-07-10 15:02:14 +02:00
|
|
|
/// Finds the loops and the length of the prefix by simulating the system.
|
2025-06-22 16:09:31 +02:00
|
|
|
fn find_prefix_len_loop(
|
|
|
|
|
rs: &[RSreaction],
|
|
|
|
|
entities: RSset,
|
|
|
|
|
q: &RSset
|
|
|
|
|
) -> (usize, Vec<RSset>) {
|
|
|
|
|
let mut entities = entities;
|
|
|
|
|
let mut trace = vec![];
|
|
|
|
|
loop {
|
2025-07-01 19:22:50 +02:00
|
|
|
if let Some((prefix, hoop)) = split(&entities, &trace) {
|
|
|
|
|
return (prefix.len(), hoop.to_vec());
|
|
|
|
|
} else {
|
|
|
|
|
let t = entities.union(q);
|
2025-07-09 19:34:15 +02:00
|
|
|
let products = compute_all(&t, rs);
|
2025-07-01 19:22:50 +02:00
|
|
|
trace.push(entities.clone());
|
|
|
|
|
entities = products;
|
|
|
|
|
}
|
2025-06-22 16:09:31 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
|
2025-07-10 15:02:14 +02:00
|
|
|
/// Finds only the rules X = pre(Q, rec(X)), but not only x = pre(Q, rec(x))
|
|
|
|
|
/// to use in filter_map.
|
2025-06-22 16:09:31 +02:00
|
|
|
fn filter_delta<'a>(x: (&IdType, &'a RSprocess)) -> Option<&'a RSset> {
|
|
|
|
|
use super::structure::RSprocess::*;
|
|
|
|
|
let (id, rest) = x;
|
2025-06-24 18:56:04 +02:00
|
|
|
|
2025-08-14 02:47:12 +02:00
|
|
|
if let EntitySet { entities, next_process } = rest
|
|
|
|
|
&& let RecursiveIdentifier { identifier } = &**next_process
|
|
|
|
|
&& identifier == id
|
2025-07-01 19:22:50 +02:00
|
|
|
{
|
2025-08-14 02:47:12 +02:00
|
|
|
return Some(entities);
|
2025-06-22 16:09:31 +02:00
|
|
|
}
|
2025-08-14 02:47:12 +02:00
|
|
|
|
2025-06-24 18:56:04 +02:00
|
|
|
None
|
2025-06-22 16:09:31 +02:00
|
|
|
}
|
|
|
|
|
|
2025-07-09 19:34:15 +02:00
|
|
|
/// A special case of systems is when the context recursively provides always
|
|
|
|
|
/// the same set of entities. The corresponding computation is infinite. It
|
|
|
|
|
/// consists of a finite sequence of states followed by a looping sequence.
|
|
|
|
|
/// IMPORTANT: We return all loops for all X = Q.X, by varing X. The set of
|
|
|
|
|
/// reactions Rs and the context x are constant. Each state of the computation
|
|
|
|
|
/// is distinguished by the current entities E. Under these assumptions, the
|
|
|
|
|
/// predicate lollipop finds the Prefixes and the Loops sequences of entities.
|
|
|
|
|
/// see lollipop
|
2025-06-22 16:09:31 +02:00
|
|
|
pub fn lollipops_decomposed(
|
|
|
|
|
delta: &RSenvironment,
|
|
|
|
|
reaction_rules: &[RSreaction],
|
2025-07-01 19:22:50 +02:00
|
|
|
available_entities: &RSset,
|
2025-06-22 16:09:31 +02:00
|
|
|
) -> Vec<(Vec<RSset>, Vec<RSset>)> {
|
2025-06-20 17:11:00 +02:00
|
|
|
// FIXME: i think we are only interested in "x", not all symbols that
|
2025-06-19 23:48:16 +02:00
|
|
|
// satisfy X = pre(Q, rec(X))
|
2025-06-22 16:09:31 +02:00
|
|
|
let filtered = delta.iter().filter_map(filter_delta);
|
2025-06-19 23:48:16 +02:00
|
|
|
|
2025-06-22 16:09:31 +02:00
|
|
|
let find_loop_fn = |q| find_loop(reaction_rules,
|
|
|
|
|
available_entities.clone(),
|
2025-06-20 17:11:00 +02:00
|
|
|
q);
|
|
|
|
|
|
|
|
|
|
filtered.map(find_loop_fn).collect::<Vec<_>>()
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-09 19:34:15 +02:00
|
|
|
/// A special case of systems is when the context recursively provides always
|
|
|
|
|
/// the same set of entities. The corresponding computation is infinite. It
|
|
|
|
|
/// consists of a finite sequence of states followed by a looping sequence.
|
|
|
|
|
/// IMPORTANT: We return all loops for all X = Q.X, by varing X. The set of
|
|
|
|
|
/// reactions Rs and the context x are constant. Each state of the computation
|
|
|
|
|
/// is distinguished by the current entities E. Under these assumptions, the
|
|
|
|
|
/// predicate lollipop finds the Prefixes and the Loops sequences of entities.
|
|
|
|
|
/// see lollipop
|
2025-06-22 16:09:31 +02:00
|
|
|
pub fn lollipops(system: RSsystem) -> Vec<(Vec<RSset>, Vec<RSset>)> {
|
2025-07-01 19:22:50 +02:00
|
|
|
lollipops_decomposed(
|
2025-07-09 16:12:22 +02:00
|
|
|
&system.delta,
|
|
|
|
|
&system.reaction_rules,
|
|
|
|
|
&system.available_entities,
|
2025-07-01 19:22:50 +02:00
|
|
|
)
|
2025-06-22 16:09:31 +02:00
|
|
|
}
|
|
|
|
|
|
2025-07-09 19:34:15 +02:00
|
|
|
/// Only returns the loop part of the lollipop, returns for all X, where X = Q.X
|
|
|
|
|
/// see loop
|
2025-06-20 17:11:00 +02:00
|
|
|
pub fn lollipops_only_loop(system: RSsystem) -> Vec<Vec<RSset>> {
|
2025-07-09 16:12:22 +02:00
|
|
|
let filtered = system.delta.iter().filter_map(filter_delta);
|
2025-06-20 17:11:00 +02:00
|
|
|
|
2025-07-01 19:22:50 +02:00
|
|
|
let find_loop_fn = |q| {
|
|
|
|
|
find_only_loop(
|
2025-07-09 16:12:22 +02:00
|
|
|
&system.reaction_rules,
|
|
|
|
|
system.available_entities.clone(),
|
2025-07-01 19:22:50 +02:00
|
|
|
q,
|
|
|
|
|
)
|
|
|
|
|
};
|
2025-06-20 17:11:00 +02:00
|
|
|
|
|
|
|
|
filtered.map(find_loop_fn).collect::<Vec<_>>()
|
2025-06-19 23:48:16 +02:00
|
|
|
}
|
2025-06-22 16:09:31 +02:00
|
|
|
|
|
|
|
|
pub fn lollipops_prefix_len_loop_decomposed(
|
|
|
|
|
delta: &RSenvironment,
|
|
|
|
|
reaction_rules: &[RSreaction],
|
2025-07-01 19:22:50 +02:00
|
|
|
available_entities: &RSset,
|
2025-06-22 16:09:31 +02:00
|
|
|
) -> Vec<(usize, Vec<RSset>)> {
|
|
|
|
|
let filtered = delta.iter().filter_map(filter_delta);
|
|
|
|
|
|
|
|
|
|
let find_loop_fn = |q| find_prefix_len_loop(reaction_rules,
|
|
|
|
|
available_entities.clone(),
|
|
|
|
|
q);
|
|
|
|
|
|
|
|
|
|
filtered.map(find_loop_fn).collect::<Vec<_>>()
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-09 19:34:15 +02:00
|
|
|
/// see loop
|
2025-06-22 16:09:31 +02:00
|
|
|
pub fn lollipops_only_loop_decomposed(
|
|
|
|
|
delta: &RSenvironment,
|
|
|
|
|
reaction_rules: &[RSreaction],
|
2025-07-01 19:22:50 +02:00
|
|
|
available_entities: &RSset,
|
2025-06-22 16:09:31 +02:00
|
|
|
) -> Vec<Vec<RSset>> {
|
|
|
|
|
let filtered = delta.iter().filter_map(filter_delta);
|
|
|
|
|
|
|
|
|
|
let find_loop_fn = |q| find_only_loop(reaction_rules,
|
2025-07-01 19:22:50 +02:00
|
|
|
available_entities.clone(),
|
|
|
|
|
q);
|
2025-06-22 16:09:31 +02:00
|
|
|
|
|
|
|
|
filtered.map(find_loop_fn).collect::<Vec<_>>()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
// Named versions
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
|
2025-07-10 15:02:14 +02:00
|
|
|
/// Finds only the rules symb = pre(Q, rec(symb)), get symb from a translator
|
|
|
|
|
/// to use in filter_map.
|
2025-06-22 16:09:31 +02:00
|
|
|
fn filter_delta_named<'a>(
|
|
|
|
|
x: (&IdType, &'a RSprocess),
|
|
|
|
|
symb: &IdType
|
|
|
|
|
) -> Option<&'a RSset> {
|
|
|
|
|
use super::structure::RSprocess::*;
|
|
|
|
|
let (id, rest) = x;
|
|
|
|
|
if id != symb {
|
2025-07-01 19:22:50 +02:00
|
|
|
return None;
|
2025-06-22 16:09:31 +02:00
|
|
|
}
|
2025-06-24 18:56:04 +02:00
|
|
|
|
2025-08-14 02:47:12 +02:00
|
|
|
if let EntitySet { entities, next_process } = rest
|
|
|
|
|
&& let RecursiveIdentifier { identifier } = &**next_process
|
|
|
|
|
&& identifier == id
|
2025-07-01 19:22:50 +02:00
|
|
|
{
|
2025-08-14 02:47:12 +02:00
|
|
|
return Some(entities);
|
2025-06-22 16:09:31 +02:00
|
|
|
}
|
2025-06-24 18:56:04 +02:00
|
|
|
None
|
2025-06-22 16:09:31 +02:00
|
|
|
}
|
|
|
|
|
|
2025-07-09 19:34:15 +02:00
|
|
|
/// A special case of systems is when the context recursively provides always
|
|
|
|
|
/// the same set of entities. The corresponding computation is infinite. It
|
|
|
|
|
/// consists of a finite sequence of states followed by a looping sequence.
|
|
|
|
|
/// IMPORTANT: We return all loops for all X = Q.X, by varing X. The set of
|
|
|
|
|
/// reactions Rs and the context x are constant. Each state of the computation
|
|
|
|
|
/// is distinguished by the current entities E. Under these assumptions, the
|
|
|
|
|
/// predicate lollipop finds the Prefixes and the Loops sequences of entities.
|
|
|
|
|
/// see lollipop
|
2025-06-22 16:09:31 +02:00
|
|
|
pub fn lollipops_decomposed_named(
|
|
|
|
|
delta: &RSenvironment,
|
|
|
|
|
reaction_rules: &[RSreaction],
|
|
|
|
|
available_entities: &RSset,
|
2025-07-01 19:22:50 +02:00
|
|
|
symb: IdType,
|
2025-06-22 16:09:31 +02:00
|
|
|
) -> Option<(Vec<RSset>, Vec<RSset>)> {
|
2025-07-01 19:22:50 +02:00
|
|
|
let filtered = delta
|
|
|
|
|
.iter()
|
|
|
|
|
.filter_map(|x| filter_delta_named(x, &symb))
|
|
|
|
|
.next();
|
2025-06-22 16:09:31 +02:00
|
|
|
|
|
|
|
|
let find_loop_fn = |q| find_loop(reaction_rules,
|
|
|
|
|
available_entities.clone(),
|
|
|
|
|
q);
|
|
|
|
|
|
|
|
|
|
filtered.map(find_loop_fn)
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-09 19:34:15 +02:00
|
|
|
/// A special case of systems is when the context recursively provides always
|
|
|
|
|
/// the same set of entities. The corresponding computation is infinite. It
|
|
|
|
|
/// consists of a finite sequence of states followed by a looping sequence.
|
|
|
|
|
/// IMPORTANT: We return all loops for all X = Q.X, by varing X. The set of
|
|
|
|
|
/// reactions Rs and the context x are constant. Each state of the computation
|
|
|
|
|
/// is distinguished by the current entities E. Under these assumptions, the
|
|
|
|
|
/// predicate lollipop finds the Prefixes and the Loops sequences of entities.
|
|
|
|
|
/// see lollipop
|
2025-06-22 16:09:31 +02:00
|
|
|
pub fn lollipops_named(
|
2025-07-12 02:42:28 +02:00
|
|
|
system: &RSsystem,
|
2025-06-22 16:09:31 +02:00
|
|
|
symb: IdType
|
|
|
|
|
) -> Option<(Vec<RSset>, Vec<RSset>)> {
|
2025-07-01 19:22:50 +02:00
|
|
|
lollipops_decomposed_named(
|
2025-07-09 16:12:22 +02:00
|
|
|
&system.delta,
|
|
|
|
|
&system.reaction_rules,
|
|
|
|
|
&system.available_entities,
|
2025-07-01 19:22:50 +02:00
|
|
|
symb,
|
|
|
|
|
)
|
2025-06-22 16:09:31 +02:00
|
|
|
}
|
|
|
|
|
|
2025-07-09 19:34:15 +02:00
|
|
|
/// Only returns the loop part of the lollipop, returns for all X, where X = Q.X
|
|
|
|
|
/// see loop
|
2025-06-22 16:09:31 +02:00
|
|
|
pub fn lollipops_only_loop_named(
|
2025-07-12 02:42:28 +02:00
|
|
|
system: &RSsystem,
|
2025-06-22 16:09:31 +02:00
|
|
|
symb: IdType
|
|
|
|
|
) -> Option<Vec<RSset>> {
|
2025-07-01 19:22:50 +02:00
|
|
|
let filtered = system
|
2025-07-09 16:12:22 +02:00
|
|
|
.delta
|
2025-07-01 19:22:50 +02:00
|
|
|
.iter()
|
|
|
|
|
.filter_map(|x| filter_delta_named(x, &symb))
|
|
|
|
|
.next();
|
|
|
|
|
|
|
|
|
|
let find_loop_fn = |q| {
|
|
|
|
|
find_only_loop(
|
2025-07-09 16:12:22 +02:00
|
|
|
&system.reaction_rules,
|
|
|
|
|
system.available_entities.clone(),
|
2025-07-01 19:22:50 +02:00
|
|
|
q,
|
|
|
|
|
)
|
|
|
|
|
};
|
2025-06-22 16:09:31 +02:00
|
|
|
|
|
|
|
|
filtered.map(find_loop_fn)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn lollipops_prefix_len_loop_decomposed_named(
|
|
|
|
|
delta: &RSenvironment,
|
|
|
|
|
reaction_rules: &[RSreaction],
|
|
|
|
|
available_entities: &RSset,
|
2025-07-01 19:22:50 +02:00
|
|
|
symb: IdType,
|
2025-06-22 16:09:31 +02:00
|
|
|
) -> Option<(usize, Vec<RSset>)> {
|
2025-07-01 19:22:50 +02:00
|
|
|
let filtered = delta
|
|
|
|
|
.iter()
|
|
|
|
|
.filter_map(|x| filter_delta_named(x, &symb))
|
|
|
|
|
.next();
|
2025-06-22 16:09:31 +02:00
|
|
|
|
|
|
|
|
let find_loop_fn = |q| find_prefix_len_loop(reaction_rules,
|
|
|
|
|
available_entities.clone(),
|
|
|
|
|
q);
|
|
|
|
|
|
|
|
|
|
filtered.map(find_loop_fn)
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-09 19:34:15 +02:00
|
|
|
/// see loop
|
2025-06-22 16:09:31 +02:00
|
|
|
pub fn lollipops_only_loop_decomposed_named(
|
|
|
|
|
delta: &RSenvironment,
|
|
|
|
|
reaction_rules: &[RSreaction],
|
|
|
|
|
available_entities: &RSset,
|
2025-07-01 19:22:50 +02:00
|
|
|
symb: IdType,
|
2025-06-22 16:09:31 +02:00
|
|
|
) -> Option<Vec<RSset>> {
|
2025-07-01 19:22:50 +02:00
|
|
|
let filtered = delta
|
|
|
|
|
.iter()
|
|
|
|
|
.filter_map(|x| filter_delta_named(x, &symb))
|
|
|
|
|
.next();
|
2025-06-22 16:09:31 +02:00
|
|
|
|
|
|
|
|
let find_loop_fn = |q| find_only_loop(reaction_rules,
|
|
|
|
|
available_entities.clone(),
|
|
|
|
|
q);
|
|
|
|
|
|
|
|
|
|
filtered.map(find_loop_fn)
|
|
|
|
|
}
|
2025-06-24 18:56:04 +02:00
|
|
|
|
2025-07-09 19:34:15 +02:00
|
|
|
/// see loop/5
|
2025-06-24 18:56:04 +02:00
|
|
|
pub fn lollipops_only_loop_decomposed_q(
|
|
|
|
|
q: &RSset,
|
|
|
|
|
reaction_rules: &[RSreaction],
|
|
|
|
|
available_entities: &RSset,
|
|
|
|
|
) -> Vec<RSset> {
|
|
|
|
|
let find_loop_fn = |q| find_only_loop(reaction_rules,
|
|
|
|
|
available_entities.clone(),
|
|
|
|
|
q);
|
|
|
|
|
|
|
|
|
|
find_loop_fn(q)
|
|
|
|
|
}
|