Positive Labels now with more fileds
This commit is contained in:
@ -5,8 +5,6 @@ fn main() {
|
|||||||
std::io::stdin().read_line(&mut input).unwrap();
|
std::io::stdin().read_line(&mut input).unwrap();
|
||||||
input = input.trim().into();
|
input = input.trim().into();
|
||||||
|
|
||||||
// let input = "testing/medical.system";
|
|
||||||
|
|
||||||
let now = std::time::Instant::now();
|
let now = std::time::Instant::now();
|
||||||
|
|
||||||
match presets::run(input) {
|
match presets::run(input) {
|
||||||
|
|||||||
@ -144,6 +144,8 @@ pub struct PositiveLabel {
|
|||||||
pub t: PositiveSet,
|
pub t: PositiveSet,
|
||||||
pub reactants: PositiveSet,
|
pub reactants: PositiveSet,
|
||||||
pub reactants_absent: PositiveSet,
|
pub reactants_absent: PositiveSet,
|
||||||
|
pub inhibitors: PositiveSet,
|
||||||
|
pub inhibitors_present: PositiveSet,
|
||||||
pub products: PositiveSet,
|
pub products: PositiveSet,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -157,13 +159,15 @@ impl BasicLabel for PositiveLabel {
|
|||||||
|
|
||||||
impl PartialEq for PositiveLabel {
|
impl PartialEq for PositiveLabel {
|
||||||
fn eq(&self, other: &Self) -> bool {
|
fn eq(&self, other: &Self) -> bool {
|
||||||
self.available_entities == other.available_entities &&
|
self.available_entities == other.available_entities
|
||||||
self.context == other.context &&
|
&& self.context == other.context
|
||||||
// self.t == other.t && // no need since its the union of the above
|
// && self.t == other.t // no need since its the union of the above
|
||||||
// // elements
|
// // elements
|
||||||
self.reactants == other.reactants &&
|
&& self.reactants == other.reactants
|
||||||
self.reactants_absent == other.reactants_absent &&
|
&& self.reactants_absent == other.reactants_absent
|
||||||
self.products == other.products
|
&& 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.t.hash(state);
|
||||||
self.reactants.hash(state);
|
self.reactants.hash(state);
|
||||||
self.reactants_absent.hash(state);
|
self.reactants_absent.hash(state);
|
||||||
|
self.inhibitors.hash(state);
|
||||||
|
self.inhibitors_present.hash(state);
|
||||||
self.products.hash(state);
|
self.products.hash(state);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -187,12 +193,16 @@ impl PrintableWithTranslator for PositiveLabel {
|
|||||||
t: {}, \
|
t: {}, \
|
||||||
reactants: {}, \
|
reactants: {}, \
|
||||||
reactantsi: {}, \
|
reactantsi: {}, \
|
||||||
|
inhibitors: {}, \
|
||||||
|
ireactants: {}, \
|
||||||
products: {}}}",
|
products: {}}}",
|
||||||
Formatter::from(translator, &self.available_entities),
|
Formatter::from(translator, &self.available_entities),
|
||||||
Formatter::from(translator, &self.context),
|
Formatter::from(translator, &self.context),
|
||||||
Formatter::from(translator, &self.t),
|
Formatter::from(translator, &self.t),
|
||||||
Formatter::from(translator, &self.reactants),
|
Formatter::from(translator, &self.reactants),
|
||||||
Formatter::from(translator, &self.reactants_absent),
|
Formatter::from(translator, &self.reactants_absent),
|
||||||
|
Formatter::from(translator, &self.inhibitors),
|
||||||
|
Formatter::from(translator, &self.inhibitors_present),
|
||||||
Formatter::from(translator, &self.products),
|
Formatter::from(translator, &self.products),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@ -206,15 +216,18 @@ impl PositiveLabel {
|
|||||||
t: PositiveSet,
|
t: PositiveSet,
|
||||||
reactants: PositiveSet,
|
reactants: PositiveSet,
|
||||||
reactants_absent: PositiveSet,
|
reactants_absent: PositiveSet,
|
||||||
|
inhibitors: PositiveSet,
|
||||||
|
inhibitors_present: PositiveSet,
|
||||||
products: PositiveSet,
|
products: PositiveSet,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
Self {
|
Self { available_entities,
|
||||||
available_entities,
|
context,
|
||||||
context,
|
t,
|
||||||
t,
|
reactants,
|
||||||
reactants,
|
reactants_absent,
|
||||||
reactants_absent,
|
inhibitors,
|
||||||
products,
|
inhibitors_present,
|
||||||
|
products
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -224,14 +237,18 @@ impl PositiveLabel {
|
|||||||
context: PositiveSet,
|
context: PositiveSet,
|
||||||
reactants: PositiveSet,
|
reactants: PositiveSet,
|
||||||
reactants_absent: PositiveSet,
|
reactants_absent: PositiveSet,
|
||||||
|
inhibitors: PositiveSet,
|
||||||
|
inhibitors_present: PositiveSet,
|
||||||
products: PositiveSet,
|
products: PositiveSet,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
Self {
|
Self {
|
||||||
available_entities: available_entities.clone(),
|
|
||||||
context: context.clone(),
|
|
||||||
t: available_entities.union(&context),
|
t: available_entities.union(&context),
|
||||||
|
available_entities,
|
||||||
|
context,
|
||||||
reactants,
|
reactants,
|
||||||
reactants_absent,
|
reactants_absent,
|
||||||
|
inhibitors,
|
||||||
|
inhibitors_present,
|
||||||
products,
|
products,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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 {
|
impl PositiveSet {
|
||||||
/// Returns the list of elements that are in both set with opposite state.
|
/// Returns the list of elements that are in both set with opposite state.
|
||||||
/// Example: [+1, +2, -3] ⩀ [-1, +2, +3] = [1, 3]
|
/// Example: [+1, +2, -3] ⩀ [-1, +2, +3] = [1, 3]
|
||||||
@ -614,4 +626,16 @@ impl PositiveSet {
|
|||||||
}
|
}
|
||||||
self_copy.is_empty()
|
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>()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -5,6 +5,7 @@ use std::fmt::Debug;
|
|||||||
use std::hash::Hash;
|
use std::hash::Hash;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
|
||||||
|
use super::choices::{BasicChoices, PositiveChoices};
|
||||||
use super::element::IdState;
|
use super::element::IdState;
|
||||||
use super::environment::{
|
use super::environment::{
|
||||||
BasicEnvironment, Environment, ExtensionsEnvironment, LoopEnvironment, PositiveEnvironment,
|
BasicEnvironment, Environment, ExtensionsEnvironment, LoopEnvironment, PositiveEnvironment,
|
||||||
@ -15,6 +16,7 @@ use super::reaction::{BasicReaction, ExtensionReaction, PositiveReaction, Reacti
|
|||||||
use super::set::{BasicSet, PositiveSet, Set};
|
use super::set::{BasicSet, PositiveSet, Set};
|
||||||
use super::transitions::TransitionsIterator;
|
use super::transitions::TransitionsIterator;
|
||||||
use super::translator::{Formatter, PrintableWithTranslator, Translator};
|
use super::translator::{Formatter, PrintableWithTranslator, Translator};
|
||||||
|
use super::choices::Choices;
|
||||||
|
|
||||||
pub trait BasicSystem
|
pub trait BasicSystem
|
||||||
where
|
where
|
||||||
@ -25,7 +27,10 @@ where
|
|||||||
type Reaction: BasicReaction<Set = Self::Set>;
|
type Reaction: BasicReaction<Set = Self::Set>;
|
||||||
type Label: BasicLabel<Set = Self::Set>;
|
type Label: BasicLabel<Set = Self::Set>;
|
||||||
type Process: BasicProcess<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>;
|
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>)>;
|
type Trace<L, S> = Vec<(Option<Rc<L>>, Rc<S>)>;
|
||||||
|
|
||||||
pub trait ExtensionsSystem: BasicSystem {
|
pub trait ExtensionsSystem: BasicSystem {
|
||||||
|
fn unfold(&self) -> Result<Self::Choices, String>;
|
||||||
|
|
||||||
fn one_transition(&self) -> Result<Option<(Self::Label, Self)>, String>;
|
fn one_transition(&self) -> Result<Option<(Self::Label, Self)>, String>;
|
||||||
|
|
||||||
fn nth_transition(&self, n: usize) -> 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 {
|
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
|
/// see oneTransition, transition, smartTransition, smartOneTransition
|
||||||
fn one_transition(&self) -> Result<Option<(Self::Label, Self)>, String> {
|
fn one_transition(&self) -> Result<Option<(Self::Label, Self)>, String> {
|
||||||
let mut tr = self.to_transitions_iterator()?;
|
let mut tr = self.to_transitions_iterator()?;
|
||||||
@ -315,6 +326,7 @@ impl BasicSystem for System {
|
|||||||
type Label = Label;
|
type Label = Label;
|
||||||
type Process = Process;
|
type Process = Process;
|
||||||
type Environment = Environment;
|
type Environment = Environment;
|
||||||
|
type Choices = Choices;
|
||||||
|
|
||||||
fn to_transitions_iterator(&self) -> Result<impl Iterator<Item = (Self::Label, Self)>, String> {
|
fn to_transitions_iterator(&self) -> Result<impl Iterator<Item = (Self::Label, Self)>, String> {
|
||||||
TransitionsIterator::<Self::Set, Self, Self::Process>::from(self)
|
TransitionsIterator::<Self::Set, Self, Self::Process>::from(self)
|
||||||
@ -561,6 +573,7 @@ impl BasicSystem for PositiveSystem {
|
|||||||
type Label = PositiveLabel;
|
type Label = PositiveLabel;
|
||||||
type Process = PositiveProcess;
|
type Process = PositiveProcess;
|
||||||
type Environment = PositiveEnvironment;
|
type Environment = PositiveEnvironment;
|
||||||
|
type Choices = PositiveChoices;
|
||||||
|
|
||||||
fn to_transitions_iterator(&self) -> Result<impl Iterator<Item = (Self::Label, Self)>, String> {
|
fn to_transitions_iterator(&self) -> Result<impl Iterator<Item = (Self::Label, Self)>, String> {
|
||||||
TransitionsIterator::<Self::Set, Self, Self::Process>::from(self)
|
TransitionsIterator::<Self::Set, Self, Self::Process>::from(self)
|
||||||
|
|||||||
@ -2,12 +2,11 @@
|
|||||||
|
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
|
||||||
use super::environment::BasicEnvironment;
|
|
||||||
use super::label::{Label, PositiveLabel};
|
use super::label::{Label, PositiveLabel};
|
||||||
use super::process::{BasicProcess, PositiveProcess, Process};
|
use super::process::{BasicProcess, PositiveProcess, Process};
|
||||||
use super::reaction::BasicReaction;
|
use super::reaction::BasicReaction;
|
||||||
use super::set::{BasicSet, PositiveSet, Set};
|
use super::set::{BasicSet, PositiveSet, Set};
|
||||||
use super::system::{BasicSystem, PositiveSystem, System};
|
use super::system::{BasicSystem, PositiveSystem, System, ExtensionsSystem};
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct TransitionsIterator<
|
pub struct TransitionsIterator<
|
||||||
@ -22,10 +21,7 @@ pub struct TransitionsIterator<
|
|||||||
|
|
||||||
impl<'a> TransitionsIterator<'a, Set, System, Process> {
|
impl<'a> TransitionsIterator<'a, Set, System, Process> {
|
||||||
pub fn from(system: &'a System) -> Result<Self, String> {
|
pub fn from(system: &'a System) -> Result<Self, String> {
|
||||||
match system
|
match system.unfold() {
|
||||||
.environment()
|
|
||||||
.unfold(system.context(), system.available_entities())
|
|
||||||
{
|
|
||||||
Ok(o) => Ok(TransitionsIterator {
|
Ok(o) => Ok(TransitionsIterator {
|
||||||
choices_iterator: o.into_iter(),
|
choices_iterator: o.into_iter(),
|
||||||
system,
|
system,
|
||||||
@ -96,10 +92,7 @@ impl<'a> Iterator for TransitionsIterator<'a, Set, System, Process> {
|
|||||||
|
|
||||||
impl<'a> TransitionsIterator<'a, PositiveSet, PositiveSystem, PositiveProcess> {
|
impl<'a> TransitionsIterator<'a, PositiveSet, PositiveSystem, PositiveProcess> {
|
||||||
pub fn from(system: &'a PositiveSystem) -> Result<Self, String> {
|
pub fn from(system: &'a PositiveSystem) -> Result<Self, String> {
|
||||||
match system
|
match system.unfold() {
|
||||||
.environment()
|
|
||||||
.unfold(system.context(), system.available_entities())
|
|
||||||
{
|
|
||||||
Ok(o) => Ok(TransitionsIterator {
|
Ok(o) => Ok(TransitionsIterator {
|
||||||
choices_iterator: o.into_iter(),
|
choices_iterator: o.into_iter(),
|
||||||
system,
|
system,
|
||||||
@ -116,29 +109,36 @@ impl<'a> Iterator for TransitionsIterator<'a, PositiveSet, PositiveSystem, Posit
|
|||||||
fn next(&mut self) -> Option<Self::Item> {
|
fn next(&mut self) -> Option<Self::Item> {
|
||||||
let (c, k) = self.choices_iterator.next()?;
|
let (c, k) = self.choices_iterator.next()?;
|
||||||
let t = self.system.available_entities.union(c.as_ref());
|
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()
|
||||||
PositiveSet::default(), // reactants
|
.fold(
|
||||||
PositiveSet::default(), // reactants_absent
|
(
|
||||||
PositiveSet::default(), // products
|
PositiveSet::default(), // reactants
|
||||||
),
|
PositiveSet::default(), // reactants_absent
|
||||||
|acc, reaction| {
|
PositiveSet::default(), // inhibitors
|
||||||
if reaction.enabled(&t) {
|
PositiveSet::default(), // inhibitors_present
|
||||||
(
|
PositiveSet::default(), // products
|
||||||
acc.0.union(&reaction.reactants),
|
),
|
||||||
acc.1,
|
|acc, reaction| {
|
||||||
acc.2.union(&reaction.products),
|
if reaction.enabled(&t) {
|
||||||
)
|
(
|
||||||
} else {
|
acc.0.union(&reaction.reactants.positives()),
|
||||||
(
|
acc.1,
|
||||||
acc.0,
|
acc.2.union(&reaction.reactants.negatives()),
|
||||||
// TODO is this right?
|
acc.3,
|
||||||
acc.1.union(&reaction.reactants.intersection(&t)),
|
acc.4.union(&reaction.products),
|
||||||
acc.2,
|
)
|
||||||
)
|
} else {
|
||||||
}
|
(
|
||||||
},
|
acc.0,
|
||||||
);
|
acc.1.union(&reaction.reactants.negatives().intersection(&t)),
|
||||||
|
acc.2,
|
||||||
|
acc.3.union(&reaction.reactants.positives().subtraction(&t)),
|
||||||
|
acc.4,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
let label = PositiveLabel::from(
|
let label = PositiveLabel::from(
|
||||||
self.system.available_entities.clone(),
|
self.system.available_entities.clone(),
|
||||||
@ -146,6 +146,8 @@ impl<'a> Iterator for TransitionsIterator<'a, PositiveSet, PositiveSystem, Posit
|
|||||||
t,
|
t,
|
||||||
reactants,
|
reactants,
|
||||||
reactants_absent,
|
reactants_absent,
|
||||||
|
inhibitors,
|
||||||
|
inhibitors_present,
|
||||||
products.clone(),
|
products.clone(),
|
||||||
);
|
);
|
||||||
let new_system = PositiveSystem::from(
|
let new_system = PositiveSystem::from(
|
||||||
|
|||||||
Reference in New Issue
Block a user