Better tests
This commit is contained in:
@ -9,7 +9,7 @@ use super::rsassert::*;
|
|||||||
type LocalAssert = RSassert<EdgeRelablerInput>;
|
type LocalAssert = RSassert<EdgeRelablerInput>;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn assert_tycheck_true() {
|
fn return_true() {
|
||||||
use translator::Translator;
|
use translator::Translator;
|
||||||
use structure::{RSsystem, RSlabel};
|
use structure::{RSsystem, RSlabel};
|
||||||
|
|
||||||
@ -29,7 +29,7 @@ fn assert_tycheck_true() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn assert_tycheck_concat_1() {
|
fn concat_1() {
|
||||||
use translator::Translator;
|
use translator::Translator;
|
||||||
use structure::{RSsystem, RSlabel};
|
use structure::{RSsystem, RSlabel};
|
||||||
|
|
||||||
@ -57,7 +57,7 @@ fn assert_tycheck_concat_1() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn assert_tycheck_concat_2() {
|
fn concat_2() {
|
||||||
use translator::Translator;
|
use translator::Translator;
|
||||||
use structure::{RSsystem, RSlabel};
|
use structure::{RSsystem, RSlabel};
|
||||||
|
|
||||||
@ -85,7 +85,7 @@ fn assert_tycheck_concat_2() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn assert_tycheck_return_1() {
|
fn return_1() {
|
||||||
use translator::Translator;
|
use translator::Translator;
|
||||||
use structure::{RSsystem, RSlabel};
|
use structure::{RSsystem, RSlabel};
|
||||||
|
|
||||||
@ -110,7 +110,7 @@ fn assert_tycheck_return_1() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn assert_tycheck_return_incompatible_1() {
|
fn return_incompatible_1() {
|
||||||
let tree = LocalAssert {
|
let tree = LocalAssert {
|
||||||
tree: Tree::Concat(
|
tree: Tree::Concat(
|
||||||
Box::new(Tree::Return(Box::new(Expression::True))),
|
Box::new(Tree::Return(Box::new(Expression::True))),
|
||||||
@ -121,7 +121,7 @@ fn assert_tycheck_return_incompatible_1() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn assert_tycheck_return_incompatible_2() {
|
fn return_incompatible_2() {
|
||||||
let tree = LocalAssert {
|
let tree = LocalAssert {
|
||||||
tree: Tree::Concat(
|
tree: Tree::Concat(
|
||||||
Box::new(Tree::Return(Box::new(Expression::True))),
|
Box::new(Tree::Return(Box::new(Expression::True))),
|
||||||
@ -141,7 +141,7 @@ fn assert_tycheck_return_incompatible_2() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn assert_tycheck_return_2() {
|
fn return_2() {
|
||||||
use translator::Translator;
|
use translator::Translator;
|
||||||
use structure::{RSsystem, RSlabel};
|
use structure::{RSsystem, RSlabel};
|
||||||
|
|
||||||
@ -176,7 +176,7 @@ fn assert_tycheck_return_2() {
|
|||||||
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn assert_tycheck_return_3() {
|
fn return_3() {
|
||||||
use translator::Translator;
|
use translator::Translator;
|
||||||
use structure::{RSsystem, RSlabel};
|
use structure::{RSsystem, RSlabel};
|
||||||
|
|
||||||
@ -210,7 +210,7 @@ fn assert_tycheck_return_3() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn assert_tycheck_if_1() {
|
fn if_1() {
|
||||||
use translator::Translator;
|
use translator::Translator;
|
||||||
use structure::{RSsystem, RSlabel};
|
use structure::{RSsystem, RSlabel};
|
||||||
|
|
||||||
@ -246,7 +246,7 @@ fn assert_tycheck_if_1() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn assert_tycheck_if_2() {
|
fn if_2() {
|
||||||
use translator::Translator;
|
use translator::Translator;
|
||||||
use structure::{RSsystem, RSlabel};
|
use structure::{RSsystem, RSlabel};
|
||||||
|
|
||||||
@ -281,7 +281,7 @@ fn assert_tycheck_if_2() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn assert_tycheck_if_3() {
|
fn if_3() {
|
||||||
use translator::Translator;
|
use translator::Translator;
|
||||||
use structure::{RSsystem, RSlabel};
|
use structure::{RSsystem, RSlabel};
|
||||||
|
|
||||||
@ -317,7 +317,7 @@ fn assert_tycheck_if_3() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn assert_tycheck_if_4() {
|
fn if_4() {
|
||||||
let tree = LocalAssert {
|
let tree = LocalAssert {
|
||||||
tree: Tree::Concat(
|
tree: Tree::Concat(
|
||||||
Box::new(
|
Box::new(
|
||||||
@ -339,7 +339,7 @@ fn assert_tycheck_if_4() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn assert_tycheck_if_else_1() {
|
fn if_else_1() {
|
||||||
use translator::Translator;
|
use translator::Translator;
|
||||||
use structure::{RSsystem, RSlabel};
|
use structure::{RSsystem, RSlabel};
|
||||||
|
|
||||||
@ -378,7 +378,7 @@ fn assert_tycheck_if_else_1() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn assert_tycheck_if_else_2() {
|
fn if_else_2() {
|
||||||
use translator::Translator;
|
use translator::Translator;
|
||||||
use structure::{RSsystem, RSlabel};
|
use structure::{RSsystem, RSlabel};
|
||||||
|
|
||||||
@ -417,7 +417,7 @@ fn assert_tycheck_if_else_2() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn assert_tycheck_if_else_3() {
|
fn if_else_3() {
|
||||||
let tree = LocalAssert {
|
let tree = LocalAssert {
|
||||||
tree: Tree::Concat(
|
tree: Tree::Concat(
|
||||||
Box::new(
|
Box::new(
|
||||||
@ -442,7 +442,7 @@ fn assert_tycheck_if_else_3() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn assert_tycheck_if_else_4() {
|
fn if_else_4() {
|
||||||
let tree = LocalAssert {
|
let tree = LocalAssert {
|
||||||
tree: Tree::Concat(
|
tree: Tree::Concat(
|
||||||
Box::new(
|
Box::new(
|
||||||
@ -467,7 +467,7 @@ fn assert_tycheck_if_else_4() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn assert_tycheck_assignment_1() {
|
fn assignment_1() {
|
||||||
let tree = LocalAssert {
|
let tree = LocalAssert {
|
||||||
tree: Tree::Assignment(
|
tree: Tree::Assignment(
|
||||||
Variable::Id("a".into()),
|
Variable::Id("a".into()),
|
||||||
@ -479,7 +479,7 @@ fn assert_tycheck_assignment_1() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn assert_tycheck_assignment_2() {
|
fn assignment_2() {
|
||||||
use translator::Translator;
|
use translator::Translator;
|
||||||
use structure::{RSsystem, RSlabel};
|
use structure::{RSsystem, RSlabel};
|
||||||
|
|
||||||
@ -516,7 +516,7 @@ fn assert_tycheck_assignment_2() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn assert_tycheck_assignment_3() {
|
fn assignment_3() {
|
||||||
use translator::Translator;
|
use translator::Translator;
|
||||||
use structure::{RSsystem, RSlabel};
|
use structure::{RSsystem, RSlabel};
|
||||||
|
|
||||||
@ -555,7 +555,7 @@ fn assert_tycheck_assignment_3() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn assert_tycheck_assignment_4() {
|
fn assignment_4() {
|
||||||
let tree = LocalAssert {
|
let tree = LocalAssert {
|
||||||
tree: Tree::Concat(
|
tree: Tree::Concat(
|
||||||
Box::new(
|
Box::new(
|
||||||
@ -580,7 +580,7 @@ fn assert_tycheck_assignment_4() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn assert_tycheck_assignment_5() {
|
fn assignment_5() {
|
||||||
use translator::Translator;
|
use translator::Translator;
|
||||||
use structure::{RSsystem, RSlabel};
|
use structure::{RSsystem, RSlabel};
|
||||||
|
|
||||||
@ -620,7 +620,7 @@ fn assert_tycheck_assignment_5() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn assert_tycheck_assignment_6() {
|
fn assignment_6() {
|
||||||
use translator::Translator;
|
use translator::Translator;
|
||||||
use structure::{RSsystem, RSlabel};
|
use structure::{RSsystem, RSlabel};
|
||||||
|
|
||||||
@ -667,7 +667,7 @@ fn assert_tycheck_assignment_6() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn assert_tycheck_assignment_7() {
|
fn assignment_7() {
|
||||||
use translator::Translator;
|
use translator::Translator;
|
||||||
use structure::{RSsystem, RSlabel};
|
use structure::{RSsystem, RSlabel};
|
||||||
|
|
||||||
@ -712,7 +712,7 @@ fn assert_tycheck_assignment_7() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn assert_tycheck_assignment_8() {
|
fn assignment_8() {
|
||||||
use translator::Translator;
|
use translator::Translator;
|
||||||
use structure::{RSsystem, RSlabel};
|
use structure::{RSsystem, RSlabel};
|
||||||
|
|
||||||
@ -761,7 +761,7 @@ fn assert_tycheck_assignment_8() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn assert_tycheck_assignment_9() {
|
fn assignment_9() {
|
||||||
use translator::Translator;
|
use translator::Translator;
|
||||||
use structure::{RSsystem, RSlabel};
|
use structure::{RSsystem, RSlabel};
|
||||||
|
|
||||||
@ -808,7 +808,7 @@ fn assert_tycheck_assignment_9() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn assert_tycheck_assignment_10() {
|
fn assignment_10() {
|
||||||
use translator::Translator;
|
use translator::Translator;
|
||||||
use structure::{RSsystem, RSlabel};
|
use structure::{RSsystem, RSlabel};
|
||||||
|
|
||||||
@ -858,7 +858,7 @@ fn assert_tycheck_assignment_10() {
|
|||||||
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn assert_tycheck_for_1() {
|
fn for_1() {
|
||||||
use translator::Translator;
|
use translator::Translator;
|
||||||
use structure::{RSsystem, RSlabel, RSset};
|
use structure::{RSsystem, RSlabel, RSset};
|
||||||
|
|
||||||
@ -906,7 +906,7 @@ fn assert_tycheck_for_1() {
|
|||||||
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn assert_tycheck_for_2() {
|
fn for_2() {
|
||||||
use translator::Translator;
|
use translator::Translator;
|
||||||
use structure::{RSsystem, RSlabel, RSset};
|
use structure::{RSsystem, RSlabel, RSset};
|
||||||
|
|
||||||
@ -958,7 +958,7 @@ fn assert_tycheck_for_2() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn assert_tycheck_for_3() {
|
fn for_3() {
|
||||||
use translator::Translator;
|
use translator::Translator;
|
||||||
use structure::{RSsystem, RSlabel, RSset};
|
use structure::{RSsystem, RSlabel, RSset};
|
||||||
|
|
||||||
@ -1024,7 +1024,7 @@ fn assert_tycheck_for_3() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn assert_tycheck_for_4() {
|
fn for_4() {
|
||||||
use translator::Translator;
|
use translator::Translator;
|
||||||
use structure::{RSsystem, RSlabel, RSset};
|
use structure::{RSsystem, RSlabel, RSset};
|
||||||
|
|
||||||
@ -1091,7 +1091,7 @@ fn assert_tycheck_for_4() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn assert_tycheck_for_5() {
|
fn for_5() {
|
||||||
use translator::Translator;
|
use translator::Translator;
|
||||||
use structure::{RSsystem, RSlabel, RSset};
|
use structure::{RSsystem, RSlabel, RSset};
|
||||||
|
|
||||||
@ -1171,7 +1171,7 @@ fn assert_tycheck_for_5() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn assert_tycheck_for_6() {
|
fn for_6() {
|
||||||
use translator::Translator;
|
use translator::Translator;
|
||||||
use structure::{RSsystem, RSlabel, RSset};
|
use structure::{RSsystem, RSlabel, RSset};
|
||||||
|
|
||||||
@ -1262,7 +1262,7 @@ fn assert_tycheck_for_6() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn assert_tycheck_for_7() {
|
fn for_7() {
|
||||||
use translator::Translator;
|
use translator::Translator;
|
||||||
use structure::{RSsystem, RSlabel};
|
use structure::{RSsystem, RSlabel};
|
||||||
|
|
||||||
@ -1314,7 +1314,7 @@ fn assert_tycheck_for_7() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn assert_tycheck_for_8() {
|
fn for_8() {
|
||||||
use translator::Translator;
|
use translator::Translator;
|
||||||
use structure::{RSsystem, RSlabel};
|
use structure::{RSsystem, RSlabel};
|
||||||
|
|
||||||
@ -1394,7 +1394,7 @@ fn assert_tycheck_for_8() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn assert_tycheck_system() {
|
fn nodes() {
|
||||||
use translator::Translator;
|
use translator::Translator;
|
||||||
use structure::{RSsystem, RSlabel, RSset, RSenvironment, RSprocess};
|
use structure::{RSsystem, RSlabel, RSset, RSenvironment, RSprocess};
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
|||||||
@ -1,11 +1,11 @@
|
|||||||
//! Definitions for simple simulation steps.
|
//! Definitions for simple simulation steps.
|
||||||
|
|
||||||
use super::structure::{RSchoices,
|
use super::structure::{RSchoices,
|
||||||
RSenvironment,
|
RSenvironment,
|
||||||
RSlabel,
|
RSlabel,
|
||||||
RSprocess,
|
RSprocess,
|
||||||
RSset,
|
RSset,
|
||||||
RSsystem};
|
RSsystem};
|
||||||
use super::support_structures::TransitionsIterator;
|
use super::support_structures::TransitionsIterator;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
|
||||||
@ -14,216 +14,331 @@ use std::rc::Rc;
|
|||||||
/// of entities and the continuation.
|
/// of entities and the continuation.
|
||||||
/// see unfold
|
/// see unfold
|
||||||
pub fn unfold(
|
pub fn unfold(
|
||||||
environment: &RSenvironment,
|
environment: &RSenvironment,
|
||||||
context_process: &RSprocess,
|
context_process: &RSprocess,
|
||||||
current_entities: &RSset,
|
current_entities: &RSset,
|
||||||
) -> Result<RSchoices, String> {
|
) -> Result<RSchoices, String> {
|
||||||
match context_process {
|
match context_process {
|
||||||
RSprocess::Nill => {
|
RSprocess::Nill => {
|
||||||
Ok(RSchoices::new())
|
Ok(RSchoices::new())
|
||||||
},
|
},
|
||||||
RSprocess::RecursiveIdentifier { identifier } => {
|
RSprocess::RecursiveIdentifier { identifier } => {
|
||||||
let newprocess = environment.get(*identifier);
|
let newprocess = environment.get(*identifier);
|
||||||
if let Some(newprocess) = newprocess {
|
if let Some(newprocess) = newprocess {
|
||||||
unfold(environment, newprocess, current_entities)
|
unfold(environment, newprocess, current_entities)
|
||||||
} else {
|
} else {
|
||||||
Err(format!("Missing symbol in context: {identifier}"))
|
Err(format!("Missing symbol in context: {identifier}"))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
RSprocess::EntitySet { entities, next_process, } => {
|
RSprocess::EntitySet { entities, next_process, } => {
|
||||||
Ok(RSchoices::from([(
|
Ok(RSchoices::from([(
|
||||||
Rc::new(entities.clone()),
|
Rc::new(entities.clone()),
|
||||||
Rc::clone(next_process),
|
Rc::clone(next_process),
|
||||||
)]))
|
)]))
|
||||||
},
|
},
|
||||||
RSprocess::Guarded { reaction, next_process } => {
|
RSprocess::Guarded { reaction, next_process } => {
|
||||||
if reaction.enabled(current_entities) {
|
if reaction.enabled(current_entities) {
|
||||||
Ok(RSchoices::from([(Rc::new(reaction.products.clone()),
|
Ok(RSchoices::from([(Rc::new(reaction.products.clone()),
|
||||||
Rc::clone(next_process))]))
|
Rc::clone(next_process))]))
|
||||||
} else {
|
} else {
|
||||||
Ok(RSchoices::new())
|
Ok(RSchoices::new())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
RSprocess::WaitEntity { repeat, repeated_process: _, next_process, }
|
RSprocess::WaitEntity { repeat, repeated_process: _, next_process, }
|
||||||
if *repeat <= 0 => {
|
if *repeat <= 0 => {
|
||||||
unfold(environment, next_process, current_entities)
|
unfold(environment, next_process, current_entities)
|
||||||
},
|
},
|
||||||
RSprocess::WaitEntity { repeat, repeated_process, next_process, }
|
RSprocess::WaitEntity { repeat, repeated_process, next_process, }
|
||||||
if *repeat == 1 => {
|
if *repeat == 1 => {
|
||||||
let mut choices1 = unfold(environment, repeated_process, current_entities)?;
|
let mut choices1 = unfold(environment,
|
||||||
choices1.replace(Rc::clone(next_process));
|
repeated_process,
|
||||||
Ok(choices1)
|
current_entities)?;
|
||||||
}
|
choices1.replace(Rc::clone(next_process));
|
||||||
RSprocess::WaitEntity { repeat, repeated_process, next_process, } => {
|
Ok(choices1)
|
||||||
let mut choices1 = unfold(environment, repeated_process, current_entities)?;
|
|
||||||
choices1.replace(Rc::new(RSprocess::WaitEntity {
|
|
||||||
repeat: (*repeat - 1),
|
|
||||||
repeated_process: Rc::clone(repeated_process),
|
|
||||||
next_process: Rc::clone(next_process),
|
|
||||||
}));
|
|
||||||
Ok(choices1)
|
|
||||||
}
|
|
||||||
RSprocess::Summation { children } => {
|
|
||||||
// short-circuits with try_fold.
|
|
||||||
children.iter().try_fold(RSchoices::new(), |mut acc, x| {
|
|
||||||
match unfold(environment, x, current_entities) {
|
|
||||||
Ok(mut choices) => {
|
|
||||||
acc.append(&mut choices);
|
|
||||||
Ok(acc)
|
|
||||||
}
|
|
||||||
Err(e) => Err(e),
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
RSprocess::NondeterministicChoice { children } => {
|
|
||||||
// short-circuits with try_fold.
|
|
||||||
if children.is_empty() {
|
|
||||||
Ok(RSchoices::from(vec![(
|
|
||||||
Rc::new(RSset::new()),
|
|
||||||
Rc::new(RSprocess::Nill),
|
|
||||||
)]))
|
|
||||||
} else {
|
|
||||||
children.iter().try_fold(RSchoices::new(), |mut acc, x| {
|
|
||||||
acc.shuffle(unfold(environment, x, current_entities)?);
|
|
||||||
Ok(acc)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
RSprocess::WaitEntity { repeat, repeated_process, next_process, } => {
|
||||||
|
let mut choices1 = unfold(environment,
|
||||||
|
repeated_process,
|
||||||
|
current_entities)?;
|
||||||
|
choices1.replace(Rc::new(RSprocess::WaitEntity {
|
||||||
|
repeat: (*repeat - 1),
|
||||||
|
repeated_process: Rc::clone(repeated_process),
|
||||||
|
next_process: Rc::clone(next_process),
|
||||||
|
}));
|
||||||
|
Ok(choices1)
|
||||||
|
}
|
||||||
|
RSprocess::Summation { children } => {
|
||||||
|
// short-circuits with try_fold.
|
||||||
|
children.iter().try_fold(RSchoices::new(), |mut acc, x| {
|
||||||
|
match unfold(environment, x, current_entities) {
|
||||||
|
Ok(mut choices) => {
|
||||||
|
acc.append(&mut choices);
|
||||||
|
Ok(acc)
|
||||||
|
}
|
||||||
|
Err(e) => Err(e),
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
RSprocess::NondeterministicChoice { children } => {
|
||||||
|
// short-circuits with try_fold.
|
||||||
|
if children.is_empty() {
|
||||||
|
Ok(RSchoices::from(vec![(
|
||||||
|
Rc::new(RSset::new()),
|
||||||
|
Rc::new(RSprocess::Nill),
|
||||||
|
)]))
|
||||||
|
} else {
|
||||||
|
children.iter().try_fold(RSchoices::new(), |mut acc, x| {
|
||||||
|
acc.shuffle(unfold(environment, x, current_entities)?);
|
||||||
|
Ok(acc)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn iterator_transitions<'a>(
|
pub fn iterator_transitions<'a>(
|
||||||
system: &'a RSsystem
|
system: &'a RSsystem
|
||||||
) -> Result<TransitionsIterator<'a>, String> {
|
) -> Result<TransitionsIterator<'a>, String> {
|
||||||
TransitionsIterator::from(system)
|
TransitionsIterator::from(system)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// see oneTransition, transition, smartTransition, smartOneTransition
|
/// see oneTransition, transition, smartTransition, smartOneTransition
|
||||||
pub fn one_transition(
|
pub fn one_transition(
|
||||||
system: &RSsystem
|
system: &RSsystem
|
||||||
) -> Result<Option<(RSlabel, RSsystem)>, String> {
|
) -> Result<Option<(RSlabel, RSsystem)>, String> {
|
||||||
let mut tr = TransitionsIterator::from(system)?;
|
let mut tr = TransitionsIterator::from(system)?;
|
||||||
Ok(tr.next())
|
Ok(tr.next())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// see allTransitions, smartAllTransitions
|
/// see allTransitions, smartAllTransitions
|
||||||
pub fn all_transitions(
|
pub fn all_transitions(
|
||||||
system: &RSsystem
|
system: &RSsystem
|
||||||
) -> Result<Vec<(RSlabel, RSsystem)>, String> {
|
) -> Result<Vec<(RSlabel, RSsystem)>, String> {
|
||||||
let tr = TransitionsIterator::from(system)?;
|
let tr = TransitionsIterator::from(system)?;
|
||||||
Ok(tr.collect::<Vec<_>>())
|
Ok(tr.collect::<Vec<_>>())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// see oneTarget, smartOneTarget, target, smartTarget
|
/// see oneTarget, smartOneTarget, target, smartTarget
|
||||||
pub fn target(
|
pub fn target(
|
||||||
system: &RSsystem
|
system: &RSsystem
|
||||||
) -> Result<(i64, RSset), String> {
|
) -> Result<(i64, RSset), String> {
|
||||||
let current = one_transition(system)?;
|
let current = one_transition(system)?;
|
||||||
if current.is_none() {
|
if current.is_none() {
|
||||||
return Ok((0, system.available_entities.clone()));
|
return Ok((0, system.available_entities.clone()));
|
||||||
}
|
}
|
||||||
let mut n = 1;
|
let mut n = 1;
|
||||||
let mut current = current.unwrap().1;
|
let mut current = current.unwrap().1;
|
||||||
while let Some((_, next)) = one_transition(¤t)? {
|
while let Some((_, next)) = one_transition(¤t)? {
|
||||||
current = next;
|
current = next;
|
||||||
n += 1;
|
n += 1;
|
||||||
}
|
}
|
||||||
Ok((n, current.available_entities.clone()))
|
Ok((n, current.available_entities.clone()))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// see oneRun, run, smartOneRunEK, smartRunEK
|
/// see oneRun, run, smartOneRunEK, smartRunEK
|
||||||
pub fn run(system: RSsystem) -> Result<Vec<Rc<RSsystem>>, String> {
|
pub fn run(system: RSsystem) -> Result<Vec<Rc<RSsystem>>, String> {
|
||||||
let mut res = vec![Rc::new(system)];
|
let mut res = vec![Rc::new(system)];
|
||||||
while let Some((_, next_sys)) = one_transition(res.last().unwrap())? {
|
while let Some((_, next_sys)) = one_transition(res.last().unwrap())? {
|
||||||
res.push(Rc::new(next_sys));
|
res.push(Rc::new(next_sys));
|
||||||
}
|
}
|
||||||
Ok(res)
|
Ok(res)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// see smartOneRunECT, smartRunECT
|
/// see smartOneRunECT, smartRunECT
|
||||||
pub fn run_separated(
|
pub fn run_separated(
|
||||||
system: &RSsystem
|
system: &RSsystem
|
||||||
) -> Result<Vec<(RSset, RSset, RSset)>, String> {
|
) -> Result<Vec<(RSset, RSset, RSset)>, String> {
|
||||||
let mut res = vec![];
|
let mut res = vec![];
|
||||||
let current = one_transition(system)?;
|
let current = one_transition(system)?;
|
||||||
if current.is_none() {
|
if current.is_none() {
|
||||||
return Ok(res);
|
return Ok(res);
|
||||||
}
|
}
|
||||||
let current = current.unwrap();
|
let current = current.unwrap();
|
||||||
let (available_entities, context, t) = current.0.get_context();
|
let (available_entities, context, t) = current.0.get_context();
|
||||||
res.push((available_entities.clone(), context.clone(), t.clone()));
|
res.push((available_entities.clone(), context.clone(), t.clone()));
|
||||||
let mut current = current.1;
|
let mut current = current.1;
|
||||||
while let Some((label, next)) = one_transition(¤t)? {
|
while let Some((label, next)) = one_transition(¤t)? {
|
||||||
current = next;
|
current = next;
|
||||||
let (available_entities, context, t) = label.get_context();
|
let (available_entities, context, t) = label.get_context();
|
||||||
res.push((available_entities.clone(), context.clone(), t.clone()));
|
res.push((available_entities.clone(), context.clone(), t.clone()));
|
||||||
}
|
}
|
||||||
Ok(res)
|
Ok(res)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
fn nth_transition(
|
fn nth_transition(
|
||||||
system: &RSsystem,
|
system: &RSsystem,
|
||||||
n: usize,
|
n: usize,
|
||||||
) -> Result<Option<(RSlabel, RSsystem)>, String> {
|
) -> Result<Option<(RSlabel, RSsystem)>, String> {
|
||||||
let mut tr = TransitionsIterator::from(system)?;
|
let mut tr = TransitionsIterator::from(system)?;
|
||||||
Ok(tr.nth(n))
|
Ok(tr.nth(n))
|
||||||
}
|
}
|
||||||
|
|
||||||
type Trace = Vec<(Option<Rc<RSlabel>>, Rc<RSsystem>)>;
|
type Trace = Vec<(Option<Rc<RSlabel>>, Rc<RSsystem>)>;
|
||||||
|
|
||||||
pub fn traces(
|
pub fn traces(
|
||||||
system: RSsystem,
|
system: RSsystem,
|
||||||
n: usize,
|
n: usize,
|
||||||
) -> Result<Vec<Trace>, String> {
|
) -> Result<Vec<Trace>, String> {
|
||||||
if n == 0 {
|
if n == 0 {
|
||||||
return Ok(vec![])
|
return Ok(vec![])
|
||||||
|
}
|
||||||
|
let mut n = n;
|
||||||
|
let mut res : Vec<Trace> = vec![];
|
||||||
|
let mut current_trace: Trace = vec![(None, Rc::new(system))];
|
||||||
|
let mut branch = vec![0];
|
||||||
|
let mut depth = 0;
|
||||||
|
let mut new_branch = true;
|
||||||
|
|
||||||
|
loop {
|
||||||
|
let next_sys = nth_transition(¤t_trace[depth].1,
|
||||||
|
branch[depth])?;
|
||||||
|
|
||||||
|
if let Some((current_label, next_sys)) = next_sys {
|
||||||
|
depth += 1;
|
||||||
|
if depth >= branch.len() {
|
||||||
|
branch.push(0);
|
||||||
|
current_trace.push((Some(Rc::new(current_label)),
|
||||||
|
Rc::new(next_sys)));
|
||||||
|
} else {
|
||||||
|
branch[depth] = 0;
|
||||||
|
current_trace[depth] = (Some(Rc::new(current_label)),
|
||||||
|
Rc::new(next_sys));
|
||||||
|
}
|
||||||
|
new_branch = true;
|
||||||
|
} else {
|
||||||
|
// at the bottom of a trace, we save to res, then backtrack until
|
||||||
|
// we find another possible path.
|
||||||
|
if new_branch {
|
||||||
|
res.push(current_trace[0..depth].to_vec());
|
||||||
|
new_branch = false;
|
||||||
|
n -= 1;
|
||||||
|
}
|
||||||
|
if n == 0 {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if depth == 0 {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
depth -= 1;
|
||||||
|
branch[depth] += 1;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let mut n = n;
|
Ok(res)
|
||||||
let mut res : Vec<Trace> = vec![];
|
}
|
||||||
let mut current_trace: Trace = vec![(None, Rc::new(system))];
|
|
||||||
let mut branch = vec![0];
|
#[test]
|
||||||
let mut depth = 0;
|
fn traces_1() {
|
||||||
let mut new_branch = true;
|
use super::structure::RSreaction;
|
||||||
|
|
||||||
loop {
|
let system = RSsystem {
|
||||||
let next_sys = nth_transition(¤t_trace[depth].1,
|
delta: Rc::new(RSenvironment::from([
|
||||||
branch[depth])?;
|
(100, RSprocess::WaitEntity {
|
||||||
|
repeat: 2,
|
||||||
if let Some((current_label, next_sys)) = next_sys {
|
repeated_process: Rc::new(RSprocess::EntitySet {
|
||||||
depth += 1;
|
entities: RSset::from([1]),
|
||||||
if depth >= branch.len() {
|
next_process: Rc::new(RSprocess::Nill)
|
||||||
branch.push(0);
|
}),
|
||||||
current_trace.push((Some(Rc::new(current_label)),
|
next_process: Rc::new(RSprocess::Nill) }),
|
||||||
Rc::new(next_sys)));
|
(102, RSprocess::WaitEntity {
|
||||||
} else {
|
repeat: 3,
|
||||||
branch[depth] = 0;
|
repeated_process: Rc::new(RSprocess::EntitySet {
|
||||||
current_trace[depth] = (Some(Rc::new(current_label)),
|
entities: RSset::from([2]),
|
||||||
Rc::new(next_sys));
|
next_process: Rc::new(RSprocess::Nill) }),
|
||||||
}
|
next_process: Rc::new(RSprocess::Nill) }),
|
||||||
new_branch = true;
|
(103, RSprocess::WaitEntity {
|
||||||
} else {
|
repeat: 4,
|
||||||
// at the bottom of a trace, we save to res, then backtrack until
|
repeated_process: Rc::new(RSprocess::EntitySet {
|
||||||
// we find another possible path.
|
entities: RSset::from([3]),
|
||||||
if new_branch {
|
next_process: Rc::new(RSprocess::Nill) }),
|
||||||
res.push(current_trace[0..depth].to_vec());
|
next_process: Rc::new(RSprocess::Nill) }),
|
||||||
new_branch = false;
|
(101, RSprocess::Summation { children: vec![
|
||||||
n -= 1;
|
Rc::new(RSprocess::EntitySet {
|
||||||
}
|
entities: RSset::from([10]),
|
||||||
if n == 0 {
|
next_process: Rc::new(RSprocess::RecursiveIdentifier {
|
||||||
break;
|
identifier: 100 }) }),
|
||||||
}
|
Rc::new(RSprocess::EntitySet {
|
||||||
|
entities: RSset::from([11]),
|
||||||
if depth == 0 {
|
next_process: Rc::new(RSprocess::RecursiveIdentifier {
|
||||||
break;
|
identifier: 102 }) }),
|
||||||
}
|
Rc::new(RSprocess::EntitySet {
|
||||||
|
entities: RSset::from([11]),
|
||||||
depth -= 1;
|
next_process: Rc::new(RSprocess::RecursiveIdentifier {
|
||||||
branch[depth] += 1;
|
identifier: 103 }) })
|
||||||
}
|
] }),
|
||||||
}
|
])),
|
||||||
|
available_entities: RSset::from([1, 2]),
|
||||||
Ok(res)
|
context_process: RSprocess::RecursiveIdentifier { identifier: 101 },
|
||||||
|
reaction_rules:
|
||||||
|
Rc::new(vec![RSreaction { reactants: RSset::from([1]),
|
||||||
|
inhibitors: RSset::from([3]),
|
||||||
|
products: RSset::from([3]), },
|
||||||
|
RSreaction { reactants: RSset::from([3]),
|
||||||
|
inhibitors: RSset::from([1]),
|
||||||
|
products: RSset::from([1]), },
|
||||||
|
RSreaction { reactants: RSset::from([2]),
|
||||||
|
inhibitors: RSset::new(),
|
||||||
|
products: RSset::from([4]), },
|
||||||
|
])
|
||||||
|
};
|
||||||
|
|
||||||
|
// for (pos, trace) in res.iter().enumerate() {
|
||||||
|
// println!("trace {}:", pos);
|
||||||
|
// for (_, sy) in trace {
|
||||||
|
// let ent = format!("{:?}", sy.available_entities);
|
||||||
|
// let con = format!("{:?}", sy.context_process);
|
||||||
|
// println!("\t({}, {})", ent, con);
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
let res = traces(system.clone(), 1).unwrap();
|
||||||
|
assert_eq!(res.len(), 1);
|
||||||
|
|
||||||
|
let res = traces(system.clone(), 2).unwrap();
|
||||||
|
assert_eq!(res.len(), 2);
|
||||||
|
assert_eq!(res[0].len() + 1, res[1].len());
|
||||||
|
|
||||||
|
let res = traces(system.clone(), 3).unwrap();
|
||||||
|
assert_eq!(res.len(), 3);
|
||||||
|
|
||||||
|
let res = traces(system.clone(), 4).unwrap();
|
||||||
|
assert_eq!(res.len(), 3);
|
||||||
|
|
||||||
|
let res = traces(system.clone(), 0).unwrap();
|
||||||
|
assert_eq!(res.len(), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn traces_empty_env() {
|
||||||
|
use super::structure::RSreaction;
|
||||||
|
|
||||||
|
let system = RSsystem {
|
||||||
|
delta: Rc::new(RSenvironment::from([])),
|
||||||
|
available_entities: RSset::from([1, 2]),
|
||||||
|
context_process: RSprocess::WaitEntity {
|
||||||
|
repeat: 10,
|
||||||
|
repeated_process: Rc::new(RSprocess::EntitySet {
|
||||||
|
entities: RSset::from([1, 2]),
|
||||||
|
next_process: Rc::new(RSprocess::Nill) }),
|
||||||
|
next_process: Rc::new(RSprocess::Nill) },
|
||||||
|
reaction_rules:
|
||||||
|
Rc::new(vec![RSreaction { reactants: RSset::from([1]),
|
||||||
|
inhibitors: RSset::from([3]),
|
||||||
|
products: RSset::from([3]), },
|
||||||
|
RSreaction { reactants: RSset::from([3]),
|
||||||
|
inhibitors: RSset::from([1]),
|
||||||
|
products: RSset::from([1]), },
|
||||||
|
RSreaction { reactants: RSset::from([2]),
|
||||||
|
inhibitors: RSset::new(),
|
||||||
|
products: RSset::from([4]), },
|
||||||
|
])
|
||||||
|
};
|
||||||
|
|
||||||
|
let res = traces(system.clone(), 10).unwrap();
|
||||||
|
assert_eq!(res.len(), 1);
|
||||||
|
assert_eq!(res[0].len(), 10);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user