cargo fmt, additional operations for dsl on context

This commit is contained in:
elvis
2025-12-15 14:53:34 +01:00
parent ca6f6e7b39
commit bbd482e2ce
12 changed files with 7360 additions and 176 deletions

View File

@ -1,4 +1,9 @@
use crate::{graph::{ContextColorConditional, EdgeColor, EdgeColorConditional, EdgeDisplay, EdgeDisplayBase, NodeColor, NodeColorConditional, NodeDisplay, NodeDisplayBase, OperationType}, translator::{self, Formatter, PrintableWithTranslator}};
use crate::graph::{
ContextColorConditional, EdgeColor, EdgeColorConditional, EdgeDisplay,
EdgeDisplayBase, NodeColor, NodeColorConditional, NodeDisplay,
NodeDisplayBase, OperationType,
};
use crate::translator::{self, Formatter, PrintableWithTranslator};
impl PrintableWithTranslator for NodeDisplayBase {
fn print(
@ -7,21 +12,16 @@ impl PrintableWithTranslator for NodeDisplayBase {
translator: &translator::Translator,
) -> std::fmt::Result {
match self {
Self::String { string } =>
write!(f, "\"{string}\""),
Self::Hide =>
write!(f, "Hide"),
Self::Entities =>
write!(f, "Entities"),
Self::MaskEntities { mask } =>
| Self::String { string } => write!(f, "\"{string}\""),
| Self::Hide => write!(f, "Hide"),
| Self::Entities => write!(f, "Entities"),
| Self::MaskEntities { mask } =>
write!(f, "{}", Formatter::from(translator, mask)),
Self::ExcludeEntities { mask } =>
| Self::ExcludeEntities { mask } =>
write!(f, "{}", Formatter::from(translator, mask)),
Self::Context =>
write!(f, "Context"),
Self::UncommonEntities =>
write!(f, "UncommonEntities"),
Self::MaskUncommonEntities { mask } =>
| Self::Context => write!(f, "Context"),
| Self::UncommonEntities => write!(f, "UncommonEntities"),
| Self::MaskUncommonEntities { mask } =>
write!(f, "{}", Formatter::from(translator, mask)),
}
}
@ -47,7 +47,6 @@ impl PrintableWithTranslator for NodeDisplay {
}
}
impl PrintableWithTranslator for EdgeDisplayBase {
fn print(
&self,
@ -55,87 +54,124 @@ impl PrintableWithTranslator for EdgeDisplayBase {
translator: &translator::Translator,
) -> std::fmt::Result {
match self {
Self::String { string } =>
write!(f, "\"{string}\""),
Self::Hide =>
write!(f, "Hide"),
Self::Products { mask, filter_common } =>
match (mask, filter_common) {
(Some(mask), true) =>
write!(f, "UncommonMaskProducts {}", Formatter::from(translator, mask)),
(Some(mask), false) =>
write!(f, "MaskProducts {}", Formatter::from(translator, mask)),
(None, true) =>
write!(f, "UncommonProducts"),
(None, false) =>
write!(f, "Products"),
},
Self::Entities { mask, filter_common } =>
match (mask, filter_common) {
(Some(mask), true) =>
write!(f, "UncommonMaskEntities {}", Formatter::from(translator, mask)),
(Some(mask), false) =>
write!(f, "MaskEntities {}", Formatter::from(translator, mask)),
(None, true) =>
write!(f, "UncommonEntities"),
(None, false) =>
write!(f, "Entities"),
},
Self::Context { mask, filter_common } =>
match (mask, filter_common) {
(Some(mask), true) =>
write!(f, "UncommonMaskContext {}", Formatter::from(translator, mask)),
(Some(mask), false) =>
write!(f, "MaskContext {}", Formatter::from(translator, mask)),
(None, true) =>
write!(f, "UncommonContext"),
(None, false) =>
write!(f, "Context"),
},
Self::Union { mask, filter_common } =>
match (mask, filter_common) {
(Some(mask), true) =>
write!(f, "UncommonMaskUnion {}", Formatter::from(translator, mask)),
(Some(mask), false) =>
write!(f, "MaskUnion {}", Formatter::from(translator, mask)),
(None, true) =>
write!(f, "UncommonUnion"),
(None, false) =>
write!(f, "Union"),
},
Self::Difference { mask, filter_common } =>
match (mask, filter_common) {
(Some(mask), true) =>
write!(f, "UncommonMaskDifference {}", Formatter::from(translator, mask)),
(Some(mask), false) =>
write!(f, "MaskDifference {}", Formatter::from(translator, mask)),
(None, true) =>
write!(f, "UncommonDifference"),
(None, false) =>
write!(f, "Difference"),
},
Self::EntitiesDeleted { mask, filter_common } =>
match (mask, filter_common) {
(Some(mask), true) =>
write!(f, "UncommonMaskEntitiesDeleted {}", Formatter::from(translator, mask)),
(Some(mask), false) =>
write!(f, "MaskEntitiesDeleted {}", Formatter::from(translator, mask)),
(None, true) =>
write!(f, "UncommonEntitiesDeleted"),
(None, false) =>
write!(f, "EntitiesDeleted"),
},
Self::EntitiesAdded { mask, filter_common } =>
match (mask, filter_common) {
(Some(mask), true) =>
write!(f, "UncommonMaskEntitiesAdded {}", Formatter::from(translator, mask)),
(Some(mask), false) =>
write!(f, "MaskEntitiesAdded {}", Formatter::from(translator, mask)),
(None, true) =>
write!(f, "UncommonEntitiesAdded"),
(None, false) =>
write!(f, "EntitiesAdded"),
},
| Self::String { string } => write!(f, "\"{string}\""),
| Self::Hide => write!(f, "Hide"),
| Self::Products {
mask,
filter_common,
} => match (mask, filter_common) {
| (Some(mask), true) => write!(
f,
"UncommonMaskProducts {}",
Formatter::from(translator, mask)
),
| (Some(mask), false) => write!(
f,
"MaskProducts {}",
Formatter::from(translator, mask)
),
| (None, true) => write!(f, "UncommonProducts"),
| (None, false) => write!(f, "Products"),
},
| Self::Entities {
mask,
filter_common,
} => match (mask, filter_common) {
| (Some(mask), true) => write!(
f,
"UncommonMaskEntities {}",
Formatter::from(translator, mask)
),
| (Some(mask), false) => write!(
f,
"MaskEntities {}",
Formatter::from(translator, mask)
),
| (None, true) => write!(f, "UncommonEntities"),
| (None, false) => write!(f, "Entities"),
},
| Self::Context {
mask,
filter_common,
} => match (mask, filter_common) {
| (Some(mask), true) => write!(
f,
"UncommonMaskContext {}",
Formatter::from(translator, mask)
),
| (Some(mask), false) => write!(
f,
"MaskContext {}",
Formatter::from(translator, mask)
),
| (None, true) => write!(f, "UncommonContext"),
| (None, false) => write!(f, "Context"),
},
| Self::Union {
mask,
filter_common,
} => match (mask, filter_common) {
| (Some(mask), true) => write!(
f,
"UncommonMaskUnion {}",
Formatter::from(translator, mask)
),
| (Some(mask), false) =>
write!(f, "MaskUnion {}", Formatter::from(translator, mask)),
| (None, true) => write!(f, "UncommonUnion"),
| (None, false) => write!(f, "Union"),
},
| Self::Difference {
mask,
filter_common,
} => match (mask, filter_common) {
| (Some(mask), true) => write!(
f,
"UncommonMaskDifference {}",
Formatter::from(translator, mask)
),
| (Some(mask), false) => write!(
f,
"MaskDifference {}",
Formatter::from(translator, mask)
),
| (None, true) => write!(f, "UncommonDifference"),
| (None, false) => write!(f, "Difference"),
},
| Self::EntitiesDeleted {
mask,
filter_common,
} => match (mask, filter_common) {
| (Some(mask), true) => write!(
f,
"UncommonMaskEntitiesDeleted {}",
Formatter::from(translator, mask)
),
| (Some(mask), false) => write!(
f,
"MaskEntitiesDeleted {}",
Formatter::from(translator, mask)
),
| (None, true) => write!(f, "UncommonEntitiesDeleted"),
| (None, false) => write!(f, "EntitiesDeleted"),
},
| Self::EntitiesAdded {
mask,
filter_common,
} => match (mask, filter_common) {
| (Some(mask), true) => write!(
f,
"UncommonMaskEntitiesAdded {}",
Formatter::from(translator, mask)
),
| (Some(mask), false) => write!(
f,
"MaskEntitiesAdded {}",
Formatter::from(translator, mask)
),
| (None, true) => write!(f, "UncommonEntitiesAdded"),
| (None, false) => write!(f, "EntitiesAdded"),
},
}
}
}
@ -160,7 +196,6 @@ impl PrintableWithTranslator for EdgeDisplay {
}
}
impl PrintableWithTranslator for OperationType {
fn print(
&self,
@ -168,11 +203,11 @@ impl PrintableWithTranslator for OperationType {
_translator: &translator::Translator,
) -> std::fmt::Result {
match self {
Self::Equals => write!(f, "=="),
Self::Subset => write!(f, ""),
Self::SubsetEqual => write!(f, ""),
Self::Superset => write!(f, ""),
Self::SupersetEqual => write!(f, ""),
| Self::Equals => write!(f, "=="),
| Self::Subset => write!(f, ""),
| Self::SubsetEqual => write!(f, ""),
| Self::Superset => write!(f, ""),
| Self::SupersetEqual => write!(f, ""),
}
}
}
@ -184,12 +219,22 @@ impl PrintableWithTranslator for ContextColorConditional {
translator: &translator::Translator,
) -> std::fmt::Result {
match self {
Self::Nill => write!(f, "Context.Nill"),
Self::RecursiveIdentifier(id) => write!(f, "Context.RecursiveIdentifier ({})", Formatter::from(translator, id)),
Self::EntitySet(ot, s) => write!(f, "Context.EntitySet {} {}", Formatter::from(translator, ot), Formatter::from(translator, s)),
Self::NonDeterministicChoice => write!(f, "Context.NonDeterministicChoice"),
Self::Summation => write!(f, "Context.Summation"),
Self::WaitEntity => write!(f, "Context.WaitEntity"),
| Self::Nill => write!(f, "Context.Nill"),
| Self::RecursiveIdentifier(id) => write!(
f,
"Context.RecursiveIdentifier ({})",
Formatter::from(translator, id)
),
| Self::EntitySet(ot, s) => write!(
f,
"Context.EntitySet {} {}",
Formatter::from(translator, ot),
Formatter::from(translator, s)
),
| Self::NonDeterministicChoice =>
write!(f, "Context.NonDeterministicChoice"),
| Self::Summation => write!(f, "Context.Summation"),
| Self::WaitEntity => write!(f, "Context.WaitEntity"),
}
}
}
@ -201,13 +246,16 @@ impl PrintableWithTranslator for NodeColorConditional {
translator: &translator::Translator,
) -> std::fmt::Result {
match self {
Self::ContextConditional(ccc) => {
| Self::ContextConditional(ccc) => {
write!(f, "{}", Formatter::from(translator, ccc))
},
Self::EntitiesConditional(ot, s) => {
write!(f, "Entities {} {}",
Formatter::from(translator, ot),
Formatter::from(translator, s))
| Self::EntitiesConditional(ot, s) => {
write!(
f,
"Entities {} {}",
Formatter::from(translator, ot),
Formatter::from(translator, s)
)
},
}
}
@ -233,7 +281,6 @@ impl PrintableWithTranslator for NodeColor {
}
}
impl PrintableWithTranslator for EdgeColorConditional {
fn print(
&self,
@ -241,14 +288,54 @@ impl PrintableWithTranslator for EdgeColorConditional {
translator: &translator::Translator,
) -> std::fmt::Result {
match self {
Self::Entities(ot, s) => write!(f, "Entities {} {}", Formatter::from(translator, ot), Formatter::from(translator, s)),
Self::Context(ot, s) => write!(f, "Entities {} {}", Formatter::from(translator, ot), Formatter::from(translator, s)),
Self::T(ot, s) => write!(f, "Entities {} {}", Formatter::from(translator, ot), Formatter::from(translator, s)),
Self::Reactants(ot, s) => write!(f, "Entities {} {}", Formatter::from(translator, ot), Formatter::from(translator, s)),
Self::ReactantsAbsent(ot, s) => write!(f, "Entities {} {}", Formatter::from(translator, ot), Formatter::from(translator, s)),
Self::Inhibitors(ot, s) => write!(f, "Entities {} {}", Formatter::from(translator, ot), Formatter::from(translator, s)),
Self::InhibitorsPresent(ot, s) => write!(f, "Entities {} {}", Formatter::from(translator, ot), Formatter::from(translator, s)),
Self::Products(ot, s) => write!(f, "Entities {} {}", Formatter::from(translator, ot), Formatter::from(translator, s)),
| Self::Entities(ot, s) => write!(
f,
"Entities {} {}",
Formatter::from(translator, ot),
Formatter::from(translator, s)
),
| Self::Context(ot, s) => write!(
f,
"Entities {} {}",
Formatter::from(translator, ot),
Formatter::from(translator, s)
),
| Self::T(ot, s) => write!(
f,
"Entities {} {}",
Formatter::from(translator, ot),
Formatter::from(translator, s)
),
| Self::Reactants(ot, s) => write!(
f,
"Entities {} {}",
Formatter::from(translator, ot),
Formatter::from(translator, s)
),
| Self::ReactantsAbsent(ot, s) => write!(
f,
"Entities {} {}",
Formatter::from(translator, ot),
Formatter::from(translator, s)
),
| Self::Inhibitors(ot, s) => write!(
f,
"Entities {} {}",
Formatter::from(translator, ot),
Formatter::from(translator, s)
),
| Self::InhibitorsPresent(ot, s) => write!(
f,
"Entities {} {}",
Formatter::from(translator, ot),
Formatter::from(translator, s)
),
| Self::Products(ot, s) => write!(
f,
"Entities {} {}",
Formatter::from(translator, ot),
Formatter::from(translator, s)
),
}
}
}

View File

@ -692,11 +692,10 @@ impl PositiveSet {
.filter(|el| {
!other.contains(&PositiveType::from((
*el.0,
IdState::Positive
))
)
IdState::Positive,
)))
})
.map(|el| (*el.0, *el.1))
.map(|el| (*el.0, *el.1)),
)
}
@ -752,14 +751,13 @@ impl PositiveSet {
}
pub fn has_positives(&self, state: &Self) -> bool {
self.iter()
.all(|a| {
if *a.1 == IdState::Positive {
state.contains(&PositiveType::from(a))
} else {
!state.contains(&PositiveType::from((*a.0, IdState::Positive)))
}
})
self.iter().all(|a| {
if *a.1 == IdState::Positive {
state.contains(&PositiveType::from(a))
} else {
!state.contains(&PositiveType::from((*a.0, IdState::Positive)))
}
})
}
fn has_element(&self, el: &PositiveType) -> bool {
@ -768,9 +766,12 @@ impl PositiveSet {
pub fn push_unique(&self, other: &Self) -> Self {
self.union(
&other.iter().filter(|el| {
!self.has_element(&PositiveType::from(*el))
}).map(|el| (*el.0, *el.1)).collect::<Vec<_>>().into()
&other
.iter()
.filter(|el| !self.has_element(&PositiveType::from(*el)))
.map(|el| (*el.0, *el.1))
.collect::<Vec<_>>()
.into(),
)
}
}

View File

@ -716,13 +716,19 @@ impl System {
Arc::new(Mutex::new(Some(new_product_elements)));
}
pub fn precomputed_context_elements(&mut self, new_context_elements: Option<Set>) {
pub fn precomputed_context_elements(
&mut self,
new_context_elements: Option<Set>,
) {
if let Some(nce) = new_context_elements {
self.overwrite_context_elements(nce);
}
}
pub fn precomputed_product_elements(&mut self, new_product_elements: Option<Set>) {
pub fn precomputed_product_elements(
&mut self,
new_product_elements: Option<Set>,
) {
if let Some(npe) = new_product_elements {
self.overwrite_product_elements(npe);
}
@ -1058,8 +1064,7 @@ impl From<System> for PositiveSystem {
// .subtraction(&value.available_entities)
// .to_positive_set(IdState::Negative);
let negative_entities =
value.products_elements()
.to_positive_set(IdState::Negative);
value.products_elements().to_positive_set(IdState::Negative);
let new_available_entities =
positive_entities.add_unique(&negative_entities);
@ -1115,13 +1120,19 @@ impl PositiveSystem {
Arc::new(Mutex::new(Some(new_product_elements)));
}
pub fn precomputed_context_elements(&mut self, new_context_elements: Option<PositiveSet>) {
pub fn precomputed_context_elements(
&mut self,
new_context_elements: Option<PositiveSet>,
) {
if let Some(nce) = new_context_elements {
self.overwrite_context_elements(nce);
}
}
pub fn precomputed_product_elements(&mut self, new_product_elements: Option<PositiveSet>) {
pub fn precomputed_product_elements(
&mut self,
new_product_elements: Option<PositiveSet>,
) {
if let Some(npe) = new_product_elements {
self.overwrite_product_elements(npe);
}

View File

@ -325,11 +325,7 @@ fn conversion_entities() {
assert_eq!(
entities,
PositiveSet::from([
(1, Positive),
(2, Positive),
(5, Negative)
])
PositiveSet::from([(1, Positive), (2, Positive), (5, Negative)])
);
}

View File

@ -100,8 +100,12 @@ impl<'a> Iterator for TransitionsIterator<'a, Set, System, Process> {
Arc::clone(&self.system.reaction_rules),
);
new_system.precomputed_context_elements(self.system.direct_get_context_elements());
new_system.precomputed_product_elements(self.system.direct_get_product_elements());
new_system.precomputed_context_elements(
self.system.direct_get_context_elements(),
);
new_system.precomputed_product_elements(
self.system.direct_get_product_elements(),
);
Some((label, new_system))
}
@ -196,8 +200,12 @@ impl<'a> Iterator
Arc::clone(&self.system.reaction_rules),
);
new_system.precomputed_context_elements(self.system.direct_get_context_elements());
new_system.precomputed_product_elements(self.system.direct_get_product_elements());
new_system.precomputed_context_elements(
self.system.direct_get_context_elements(),
);
new_system.precomputed_product_elements(
self.system.direct_get_product_elements(),
);
Some((label, new_system))
}
@ -276,8 +284,12 @@ impl<'a> Iterator for TraceIterator<'a, Set, System, Process> {
Arc::clone(&self.system.reaction_rules),
);
new_system.precomputed_context_elements(self.system.direct_get_context_elements());
new_system.precomputed_product_elements(self.system.direct_get_product_elements());
new_system.precomputed_context_elements(
self.system.direct_get_context_elements(),
);
new_system.precomputed_product_elements(
self.system.direct_get_product_elements(),
);
Some((
context.as_ref().clone(),
@ -295,7 +307,8 @@ impl<'a> Iterator
fn next(&mut self) -> Option<Self::Item> {
let (context, k) = self.choices_iterator.next()?;
let total_entities = self.system.available_entities().union(context.as_ref());
let total_entities =
self.system.available_entities().union(context.as_ref());
let (enabled_reaction_positions, all_products) =
self.system.reactions().iter().enumerate().fold(
@ -318,8 +331,12 @@ impl<'a> Iterator
Arc::clone(&self.system.reaction_rules),
);
new_system.precomputed_context_elements(self.system.direct_get_context_elements());
new_system.precomputed_product_elements(self.system.direct_get_product_elements());
new_system.precomputed_context_elements(
self.system.direct_get_context_elements(),
);
new_system.precomputed_product_elements(
self.system.direct_get_product_elements(),
);
Some((
context.as_ref().clone(),