Positive Labels now with more fileds

This commit is contained in:
elvis
2025-09-08 20:35:37 +02:00
parent 2571148e74
commit 355857abff
5 changed files with 105 additions and 51 deletions

View File

@ -5,8 +5,6 @@ fn main() {
std::io::stdin().read_line(&mut input).unwrap();
input = input.trim().into();
// let input = "testing/medical.system";
let now = std::time::Instant::now();
match presets::run(input) {

View File

@ -144,6 +144,8 @@ pub struct PositiveLabel {
pub t: PositiveSet,
pub reactants: PositiveSet,
pub reactants_absent: PositiveSet,
pub inhibitors: PositiveSet,
pub inhibitors_present: PositiveSet,
pub products: PositiveSet,
}
@ -157,13 +159,15 @@ impl BasicLabel for PositiveLabel {
impl PartialEq for PositiveLabel {
fn eq(&self, other: &Self) -> bool {
self.available_entities == other.available_entities &&
self.context == other.context &&
// self.t == other.t && // no need since its the union of the above
self.available_entities == other.available_entities
&& self.context == other.context
// && self.t == other.t // no need since its the union of the above
// // elements
self.reactants == other.reactants &&
self.reactants_absent == other.reactants_absent &&
self.products == other.products
&& self.reactants == other.reactants
&& self.reactants_absent == other.reactants_absent
&& self.inhibitors == other.inhibitors
&& self.inhibitors_present == other.inhibitors_present
&& self.products == other.products
}
}
@ -174,6 +178,8 @@ impl Hash for PositiveLabel {
// self.t.hash(state);
self.reactants.hash(state);
self.reactants_absent.hash(state);
self.inhibitors.hash(state);
self.inhibitors_present.hash(state);
self.products.hash(state);
}
}
@ -187,12 +193,16 @@ impl PrintableWithTranslator for PositiveLabel {
t: {}, \
reactants: {}, \
reactantsi: {}, \
inhibitors: {}, \
ireactants: {}, \
products: {}}}",
Formatter::from(translator, &self.available_entities),
Formatter::from(translator, &self.context),
Formatter::from(translator, &self.t),
Formatter::from(translator, &self.reactants),
Formatter::from(translator, &self.reactants_absent),
Formatter::from(translator, &self.inhibitors),
Formatter::from(translator, &self.inhibitors_present),
Formatter::from(translator, &self.products),
)
}
@ -206,15 +216,18 @@ impl PositiveLabel {
t: PositiveSet,
reactants: PositiveSet,
reactants_absent: PositiveSet,
inhibitors: PositiveSet,
inhibitors_present: PositiveSet,
products: PositiveSet,
) -> Self {
Self {
available_entities,
Self { available_entities,
context,
t,
reactants,
reactants_absent,
products,
inhibitors,
inhibitors_present,
products
}
}
@ -224,14 +237,18 @@ impl PositiveLabel {
context: PositiveSet,
reactants: PositiveSet,
reactants_absent: PositiveSet,
inhibitors: PositiveSet,
inhibitors_present: PositiveSet,
products: PositiveSet,
) -> Self {
Self {
available_entities: available_entities.clone(),
context: context.clone(),
t: available_entities.union(&context),
available_entities,
context,
reactants,
reactants_absent,
inhibitors,
inhibitors_present,
products,
}
}

View File

@ -579,6 +579,18 @@ impl From<Vec<PositiveType>> for PositiveSet {
}
}
impl FromIterator<PositiveType> for PositiveSet {
fn from_iter<T: IntoIterator<Item = PositiveType>>(iter: T) -> Self {
Self { identifiers: iter.into_iter().map(|el| (el.id, el.state)).collect() }
}
}
impl FromIterator<(IdType, IdState)> for PositiveSet {
fn from_iter<T: IntoIterator<Item = (IdType, IdState)>>(iter: T) -> Self {
Self { identifiers: iter.into_iter().collect() }
}
}
impl PositiveSet {
/// Returns the list of elements that are in both set with opposite state.
/// Example: [+1, +2, -3] ⩀ [-1, +2, +3] = [1, 3]
@ -614,4 +626,16 @@ impl PositiveSet {
}
self_copy.is_empty()
}
pub fn positives(&self) -> Self {
self.iter().filter(|el| *el.1 == IdState::Positive)
.map(|el| (*el.0, *el.1))
.collect::<PositiveSet>()
}
pub fn negatives(&self) -> Self {
self.iter().filter(|el| *el.1 == IdState::Negative)
.map(|el| (*el.0, *el.1))
.collect::<PositiveSet>()
}
}

View File

@ -5,6 +5,7 @@ use std::fmt::Debug;
use std::hash::Hash;
use std::rc::Rc;
use super::choices::{BasicChoices, PositiveChoices};
use super::element::IdState;
use super::environment::{
BasicEnvironment, Environment, ExtensionsEnvironment, LoopEnvironment, PositiveEnvironment,
@ -15,6 +16,7 @@ use super::reaction::{BasicReaction, ExtensionReaction, PositiveReaction, Reacti
use super::set::{BasicSet, PositiveSet, Set};
use super::transitions::TransitionsIterator;
use super::translator::{Formatter, PrintableWithTranslator, Translator};
use super::choices::Choices;
pub trait BasicSystem
where
@ -25,7 +27,10 @@ where
type Reaction: BasicReaction<Set = Self::Set>;
type Label: BasicLabel<Set = Self::Set>;
type Process: BasicProcess<Set = Self::Set>;
type Environment: BasicEnvironment<Set = Self::Set, Process = Self::Process>;
type Environment: BasicEnvironment<Set = Self::Set,
Process = Self::Process,
Choices = Self::Choices>;
type Choices: BasicChoices;
fn to_transitions_iterator(&self) -> Result<impl Iterator<Item = (Self::Label, Self)>, String>;
@ -38,6 +43,8 @@ where
type Trace<L, S> = Vec<(Option<Rc<L>>, Rc<S>)>;
pub trait ExtensionsSystem: BasicSystem {
fn unfold(&self) -> Result<Self::Choices, String>;
fn one_transition(&self) -> Result<Option<(Self::Label, Self)>, String>;
fn nth_transition(&self, n: usize) -> Result<Option<(Self::Label, Self)>, String>;
@ -57,6 +64,10 @@ pub trait ExtensionsSystem: BasicSystem {
}
impl<T: BasicSystem> ExtensionsSystem for T {
fn unfold(&self) -> Result<Self::Choices, String> {
self.environment().unfold(self.context(), self.available_entities())
}
/// see oneTransition, transition, smartTransition, smartOneTransition
fn one_transition(&self) -> Result<Option<(Self::Label, Self)>, String> {
let mut tr = self.to_transitions_iterator()?;
@ -315,6 +326,7 @@ impl BasicSystem for System {
type Label = Label;
type Process = Process;
type Environment = Environment;
type Choices = Choices;
fn to_transitions_iterator(&self) -> Result<impl Iterator<Item = (Self::Label, Self)>, String> {
TransitionsIterator::<Self::Set, Self, Self::Process>::from(self)
@ -561,6 +573,7 @@ impl BasicSystem for PositiveSystem {
type Label = PositiveLabel;
type Process = PositiveProcess;
type Environment = PositiveEnvironment;
type Choices = PositiveChoices;
fn to_transitions_iterator(&self) -> Result<impl Iterator<Item = (Self::Label, Self)>, String> {
TransitionsIterator::<Self::Set, Self, Self::Process>::from(self)

View File

@ -2,12 +2,11 @@
use std::rc::Rc;
use super::environment::BasicEnvironment;
use super::label::{Label, PositiveLabel};
use super::process::{BasicProcess, PositiveProcess, Process};
use super::reaction::BasicReaction;
use super::set::{BasicSet, PositiveSet, Set};
use super::system::{BasicSystem, PositiveSystem, System};
use super::system::{BasicSystem, PositiveSystem, System, ExtensionsSystem};
#[derive(Clone, Debug)]
pub struct TransitionsIterator<
@ -22,10 +21,7 @@ pub struct TransitionsIterator<
impl<'a> TransitionsIterator<'a, Set, System, Process> {
pub fn from(system: &'a System) -> Result<Self, String> {
match system
.environment()
.unfold(system.context(), system.available_entities())
{
match system.unfold() {
Ok(o) => Ok(TransitionsIterator {
choices_iterator: o.into_iter(),
system,
@ -96,10 +92,7 @@ impl<'a> Iterator for TransitionsIterator<'a, Set, System, Process> {
impl<'a> TransitionsIterator<'a, PositiveSet, PositiveSystem, PositiveProcess> {
pub fn from(system: &'a PositiveSystem) -> Result<Self, String> {
match system
.environment()
.unfold(system.context(), system.available_entities())
{
match system.unfold() {
Ok(o) => Ok(TransitionsIterator {
choices_iterator: o.into_iter(),
system,
@ -116,25 +109,32 @@ impl<'a> Iterator for TransitionsIterator<'a, PositiveSet, PositiveSystem, Posit
fn next(&mut self) -> Option<Self::Item> {
let (c, k) = self.choices_iterator.next()?;
let t = self.system.available_entities.union(c.as_ref());
let (reactants, reactants_absent, products) = self.system.reaction_rules.iter().fold(
let (reactants, reactants_absent, inhibitors, inhibitors_present, products)
= self.system.reaction_rules.iter()
.fold(
(
PositiveSet::default(), // reactants
PositiveSet::default(), // reactants_absent
PositiveSet::default(), // inhibitors
PositiveSet::default(), // inhibitors_present
PositiveSet::default(), // products
),
|acc, reaction| {
if reaction.enabled(&t) {
(
acc.0.union(&reaction.reactants),
acc.0.union(&reaction.reactants.positives()),
acc.1,
acc.2.union(&reaction.products),
acc.2.union(&reaction.reactants.negatives()),
acc.3,
acc.4.union(&reaction.products),
)
} else {
(
acc.0,
// TODO is this right?
acc.1.union(&reaction.reactants.intersection(&t)),
acc.1.union(&reaction.reactants.negatives().intersection(&t)),
acc.2,
acc.3.union(&reaction.reactants.positives().subtraction(&t)),
acc.4,
)
}
},
@ -146,6 +146,8 @@ impl<'a> Iterator for TransitionsIterator<'a, PositiveSet, PositiveSystem, Posit
t,
reactants,
reactants_absent,
inhibitors,
inhibitors_present,
products.clone(),
);
let new_system = PositiveSystem::from(