2025-09-11 02:49:14 +02:00
|
|
|
use std::collections::HashMap;
|
2025-08-24 03:35:32 +02:00
|
|
|
|
2025-08-17 03:19:50 +02:00
|
|
|
use super::dsl::*;
|
2025-09-12 16:34:58 +02:00
|
|
|
use rsprocess::{graph, label, set, system, translator};
|
|
|
|
|
use rsprocess::translator::PrintableWithTranslator;
|
2025-08-17 03:19:50 +02:00
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
// Specific Assert Implementation
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
2025-09-10 22:41:40 +02:00
|
|
|
/// Module that has all types and structures for bisimilarity relabeler.
|
2025-08-18 20:39:43 +02:00
|
|
|
pub mod useful_types_edge_relabeler {
|
|
|
|
|
macro_rules! export_types {
|
|
|
|
|
( $( $x:ident ),* ) => {
|
|
|
|
|
$(
|
|
|
|
|
pub type $x = super::super::dsl::$x<super::EdgeRelablerInput>;
|
|
|
|
|
)*
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
macro_rules! export_types_no_parameter {
|
|
|
|
|
( $( $x:ident ),* ) => {
|
|
|
|
|
$(
|
|
|
|
|
pub type $x = super::super::dsl::$x;
|
|
|
|
|
)*
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-24 03:35:32 +02:00
|
|
|
export_types!(Assert, Tree, Variable, Expression, Range);
|
2025-08-18 20:39:43 +02:00
|
|
|
|
2025-09-07 17:55:53 +02:00
|
|
|
export_types_no_parameter!(
|
|
|
|
|
Unary,
|
|
|
|
|
QualifierRestricted,
|
|
|
|
|
QualifierLabel,
|
|
|
|
|
QualifierSystem,
|
|
|
|
|
QualifierEdge,
|
|
|
|
|
QualifierNode,
|
|
|
|
|
Qualifier,
|
|
|
|
|
Binary,
|
|
|
|
|
AssertReturnValue
|
|
|
|
|
);
|
2025-08-18 20:39:43 +02:00
|
|
|
|
|
|
|
|
pub type Special = super::EdgeRelablerInput;
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-17 03:19:50 +02:00
|
|
|
// Implementation for graph labeling in bisimulation.
|
|
|
|
|
|
2025-08-24 03:35:32 +02:00
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
|
2025-08-17 03:19:50 +02:00
|
|
|
pub enum EdgeRelablerInput {
|
|
|
|
|
Label,
|
|
|
|
|
Edge,
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-24 03:35:32 +02:00
|
|
|
#[derive(Clone)]
|
2025-08-18 20:39:43 +02:00
|
|
|
enum EdgeRelablerInputValues {
|
2025-08-24 02:01:24 +02:00
|
|
|
Label(label::Label),
|
2025-08-17 03:19:50 +02:00
|
|
|
Edge(petgraph::graph::EdgeIndex),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl SpecialVariables<EdgeRelablerInputValues> for EdgeRelablerInput {
|
|
|
|
|
fn type_of(&self) -> AssertionTypes {
|
2025-09-07 17:55:53 +02:00
|
|
|
match self {
|
2025-09-11 02:49:14 +02:00
|
|
|
| Self::Edge => AssertionTypes::Edge,
|
|
|
|
|
| Self::Label => AssertionTypes::Label,
|
2025-09-07 17:55:53 +02:00
|
|
|
}
|
2025-08-17 03:19:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn type_qualified(&self, q: &Qualifier) -> Result<AssertionTypes, String> {
|
2025-09-07 17:55:53 +02:00
|
|
|
match (self, q) {
|
2025-09-11 02:49:14 +02:00
|
|
|
| (Self::Label, Qualifier::Label(_))
|
|
|
|
|
| (Self::Label, Qualifier::Restricted(_)) =>
|
|
|
|
|
Ok(AssertionTypes::Set),
|
|
|
|
|
| (s, q) =>
|
|
|
|
|
Err(format!("Wrong use of qualifier {q:?} on variable {s:?}.")),
|
2025-09-07 17:55:53 +02:00
|
|
|
}
|
2025-08-17 03:19:50 +02:00
|
|
|
}
|
|
|
|
|
|
2025-09-07 17:55:53 +02:00
|
|
|
fn new_context(
|
|
|
|
|
input: HashMap<Self, EdgeRelablerInputValues>,
|
|
|
|
|
) -> HashMap<Self, AssertReturnValue> {
|
|
|
|
|
input
|
|
|
|
|
.iter()
|
|
|
|
|
.map(|(key, value)| match value {
|
2025-09-11 02:49:14 +02:00
|
|
|
| EdgeRelablerInputValues::Edge(e) =>
|
|
|
|
|
(*key, AssertReturnValue::Edge(*e)),
|
|
|
|
|
| EdgeRelablerInputValues::Label(l) =>
|
|
|
|
|
(*key, AssertReturnValue::Label(l.clone())),
|
2025-09-07 17:55:53 +02:00
|
|
|
})
|
|
|
|
|
.collect::<HashMap<Self, AssertReturnValue>>()
|
2025-08-17 03:19:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn correct_type(&self, other: &AssertReturnValue) -> bool {
|
2025-09-07 17:55:53 +02:00
|
|
|
match (self, other) {
|
2025-09-11 02:49:14 +02:00
|
|
|
| (Self::Edge, AssertReturnValue::Edge(_))
|
2025-09-07 17:55:53 +02:00
|
|
|
| (Self::Label, AssertReturnValue::Label(_)) => true,
|
2025-09-11 02:49:14 +02:00
|
|
|
| (_, _) => false,
|
2025-09-07 17:55:53 +02:00
|
|
|
}
|
2025-08-17 03:19:50 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-24 03:35:32 +02:00
|
|
|
impl std::fmt::Debug for EdgeRelablerInput {
|
2025-08-17 03:19:50 +02:00
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
2025-09-07 17:55:53 +02:00
|
|
|
match self {
|
2025-09-11 02:49:14 +02:00
|
|
|
| Self::Label => write!(f, "label"),
|
|
|
|
|
| Self::Edge => write!(f, "edge"),
|
2025-09-07 17:55:53 +02:00
|
|
|
}
|
2025-08-17 03:19:50 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-24 03:35:32 +02:00
|
|
|
impl PrintableWithTranslator for EdgeRelablerInput {
|
2025-09-07 17:55:53 +02:00
|
|
|
fn print(
|
|
|
|
|
&self,
|
|
|
|
|
f: &mut std::fmt::Formatter,
|
|
|
|
|
_translator: &translator::Translator,
|
|
|
|
|
) -> std::fmt::Result {
|
|
|
|
|
write!(f, "{self:?}")
|
2025-08-24 03:35:32 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Assert<EdgeRelablerInput> {
|
2025-08-17 03:19:50 +02:00
|
|
|
pub fn typecheck(&self) -> Result<(), String> {
|
2025-09-07 17:55:53 +02:00
|
|
|
let mut context = TypeContext::new();
|
|
|
|
|
let ty = typecheck(&self.tree, &mut context)?;
|
|
|
|
|
match ty {
|
2025-09-11 02:49:14 +02:00
|
|
|
| AssertionTypes::Boolean
|
2025-09-07 17:55:53 +02:00
|
|
|
| AssertionTypes::Integer
|
|
|
|
|
| AssertionTypes::String
|
|
|
|
|
| AssertionTypes::Label
|
|
|
|
|
| AssertionTypes::Set
|
|
|
|
|
| AssertionTypes::Element
|
|
|
|
|
| AssertionTypes::Edge
|
|
|
|
|
| AssertionTypes::Node
|
|
|
|
|
| AssertionTypes::System
|
|
|
|
|
| AssertionTypes::Context => Ok(()),
|
2025-09-11 02:49:14 +02:00
|
|
|
| AssertionTypes::NoType =>
|
2025-09-10 22:41:40 +02:00
|
|
|
Err("No return type, at least one return statement required."
|
2025-09-11 02:49:14 +02:00
|
|
|
.into()),
|
|
|
|
|
| AssertionTypes::RangeInteger
|
2025-09-07 17:55:53 +02:00
|
|
|
| AssertionTypes::RangeSet
|
2025-09-11 02:49:14 +02:00
|
|
|
| AssertionTypes::RangeNeighbours =>
|
|
|
|
|
Err(format!("Returned type {ty:?} is not a valid return type.")),
|
2025-09-07 17:55:53 +02:00
|
|
|
}
|
2025-08-17 03:19:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn execute(
|
2025-09-07 17:55:53 +02:00
|
|
|
&self,
|
|
|
|
|
graph: &graph::SystemGraph,
|
|
|
|
|
edge: &<graph::SystemGraph as petgraph::visit::GraphBase>::EdgeId,
|
|
|
|
|
translator: &mut translator::Translator,
|
2025-08-17 03:19:50 +02:00
|
|
|
) -> Result<AssertReturnValue, String> {
|
2025-09-07 17:55:53 +02:00
|
|
|
let label = graph
|
|
|
|
|
.edge_weight(*edge)
|
|
|
|
|
.ok_or("Missing edge {{debug: {edge:?}}}")?;
|
|
|
|
|
|
|
|
|
|
let mut input_vals = HashMap::new();
|
|
|
|
|
input_vals.insert(
|
|
|
|
|
EdgeRelablerInput::Edge,
|
|
|
|
|
EdgeRelablerInputValues::Edge(*edge),
|
|
|
|
|
);
|
|
|
|
|
input_vals.insert(
|
|
|
|
|
EdgeRelablerInput::Label,
|
|
|
|
|
EdgeRelablerInputValues::Label(label.clone()),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
let mut context = Context::new(input_vals);
|
|
|
|
|
if let Some(v) = execute(&self.tree, &mut context, translator, graph)? {
|
|
|
|
|
Ok(v)
|
|
|
|
|
} else {
|
|
|
|
|
Err("No value returned.".into())
|
|
|
|
|
}
|
2025-08-17 03:19:50 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-18 20:39:43 +02:00
|
|
|
// -----------------------------------------------------------------------------
|
2025-08-17 03:19:50 +02:00
|
|
|
// Implementation for node grouping.
|
2025-08-18 20:39:43 +02:00
|
|
|
// -----------------------------------------------------------------------------
|
2025-08-17 03:19:50 +02:00
|
|
|
|
2025-09-10 22:41:40 +02:00
|
|
|
/// Module that has all types and structures for bisimilarity relabeler.
|
|
|
|
|
pub mod useful_types_node_relabeler {
|
|
|
|
|
macro_rules! export_types {
|
|
|
|
|
( $( $x:ident ),* ) => {
|
|
|
|
|
$(
|
|
|
|
|
pub type $x = super::super::dsl::$x<super::NodeRelablerInput>;
|
|
|
|
|
)*
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
macro_rules! export_types_no_parameter {
|
|
|
|
|
( $( $x:ident ),* ) => {
|
|
|
|
|
$(
|
|
|
|
|
pub type $x = super::super::dsl::$x;
|
|
|
|
|
)*
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export_types!(Assert, Tree, Variable, Expression, Range);
|
|
|
|
|
|
|
|
|
|
export_types_no_parameter!(
|
|
|
|
|
Unary,
|
|
|
|
|
QualifierRestricted,
|
|
|
|
|
QualifierLabel,
|
|
|
|
|
QualifierSystem,
|
|
|
|
|
QualifierEdge,
|
|
|
|
|
QualifierNode,
|
|
|
|
|
Qualifier,
|
|
|
|
|
Binary,
|
|
|
|
|
AssertReturnValue
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
pub type Special = super::NodeRelablerInput;
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-24 03:35:32 +02:00
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
|
2025-08-17 03:19:50 +02:00
|
|
|
pub enum NodeRelablerInput {
|
|
|
|
|
Entities,
|
|
|
|
|
Node,
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-24 03:35:32 +02:00
|
|
|
#[derive(Clone)]
|
2025-08-18 20:39:43 +02:00
|
|
|
enum NodeRelablerInputValues {
|
2025-08-24 02:01:24 +02:00
|
|
|
Entities(set::Set),
|
2025-08-17 03:19:50 +02:00
|
|
|
Node(petgraph::graph::NodeIndex),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl SpecialVariables<NodeRelablerInputValues> for NodeRelablerInput {
|
|
|
|
|
fn type_of(&self) -> AssertionTypes {
|
2025-09-07 17:55:53 +02:00
|
|
|
match self {
|
2025-09-11 02:49:14 +02:00
|
|
|
| Self::Entities => AssertionTypes::Set,
|
|
|
|
|
| Self::Node => AssertionTypes::Node,
|
2025-09-07 17:55:53 +02:00
|
|
|
}
|
2025-08-17 03:19:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn type_qualified(&self, q: &Qualifier) -> Result<AssertionTypes, String> {
|
2025-09-07 17:55:53 +02:00
|
|
|
match (self, q) {
|
2025-09-11 02:49:14 +02:00
|
|
|
| (Self::Node, Qualifier::Node(QualifierNode::System)) =>
|
|
|
|
|
Ok(AssertionTypes::System),
|
|
|
|
|
| (Self::Node, Qualifier::Node(QualifierNode::Neighbours)) =>
|
|
|
|
|
Ok(AssertionTypes::RangeNeighbours),
|
|
|
|
|
| (s, q) =>
|
|
|
|
|
Err(format!("Wrong use of qualifier {q:?} on variable {s:?}.")),
|
2025-09-07 17:55:53 +02:00
|
|
|
}
|
2025-08-17 03:19:50 +02:00
|
|
|
}
|
|
|
|
|
|
2025-09-07 17:55:53 +02:00
|
|
|
fn new_context(
|
|
|
|
|
input: HashMap<Self, NodeRelablerInputValues>,
|
|
|
|
|
) -> HashMap<Self, AssertReturnValue> {
|
|
|
|
|
input
|
|
|
|
|
.iter()
|
|
|
|
|
.map(|(key, value)| match value {
|
2025-09-11 02:49:14 +02:00
|
|
|
| NodeRelablerInputValues::Entities(e) =>
|
|
|
|
|
(*key, AssertReturnValue::Set(e.clone())),
|
|
|
|
|
| NodeRelablerInputValues::Node(n) =>
|
|
|
|
|
(*key, AssertReturnValue::Node(*n)),
|
2025-09-07 17:55:53 +02:00
|
|
|
})
|
|
|
|
|
.collect::<HashMap<Self, AssertReturnValue>>()
|
2025-08-17 03:19:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn correct_type(&self, other: &AssertReturnValue) -> bool {
|
2025-09-07 17:55:53 +02:00
|
|
|
match (self, other) {
|
2025-09-11 02:49:14 +02:00
|
|
|
| (Self::Entities, AssertReturnValue::Set(_))
|
2025-09-07 17:55:53 +02:00
|
|
|
| (Self::Node, AssertReturnValue::Node(_)) => true,
|
2025-09-11 02:49:14 +02:00
|
|
|
| (_, _) => false,
|
2025-09-07 17:55:53 +02:00
|
|
|
}
|
2025-08-17 03:19:50 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-24 03:35:32 +02:00
|
|
|
impl std::fmt::Debug for NodeRelablerInput {
|
2025-08-17 03:19:50 +02:00
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
2025-09-07 17:55:53 +02:00
|
|
|
match self {
|
2025-09-11 02:49:14 +02:00
|
|
|
| Self::Entities => write!(f, "entities"),
|
|
|
|
|
| Self::Node => write!(f, "node"),
|
2025-09-07 17:55:53 +02:00
|
|
|
}
|
2025-08-17 03:19:50 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-24 03:35:32 +02:00
|
|
|
impl PrintableWithTranslator for NodeRelablerInput {
|
2025-09-07 17:55:53 +02:00
|
|
|
fn print(
|
|
|
|
|
&self,
|
|
|
|
|
f: &mut std::fmt::Formatter,
|
|
|
|
|
_translator: &translator::Translator,
|
|
|
|
|
) -> std::fmt::Result {
|
|
|
|
|
write!(f, "{self:?}")
|
2025-08-24 03:35:32 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Assert<NodeRelablerInput> {
|
2025-08-17 03:19:50 +02:00
|
|
|
pub fn typecheck(&self) -> Result<(), String> {
|
2025-09-07 17:55:53 +02:00
|
|
|
let mut context = TypeContext::new();
|
|
|
|
|
let ty = typecheck(&self.tree, &mut context)?;
|
|
|
|
|
match ty {
|
2025-09-11 02:49:14 +02:00
|
|
|
| AssertionTypes::Boolean
|
2025-09-07 17:55:53 +02:00
|
|
|
| AssertionTypes::Integer
|
|
|
|
|
| AssertionTypes::String
|
|
|
|
|
| AssertionTypes::Label
|
|
|
|
|
| AssertionTypes::Set
|
|
|
|
|
| AssertionTypes::Element
|
|
|
|
|
| AssertionTypes::Edge
|
|
|
|
|
| AssertionTypes::Node
|
|
|
|
|
| AssertionTypes::System
|
|
|
|
|
| AssertionTypes::Context => Ok(()),
|
2025-09-11 02:49:14 +02:00
|
|
|
| AssertionTypes::NoType =>
|
2025-09-10 22:41:40 +02:00
|
|
|
Err("No return type, at least one return statement required."
|
2025-09-11 02:49:14 +02:00
|
|
|
.into()),
|
|
|
|
|
| AssertionTypes::RangeInteger
|
2025-09-07 17:55:53 +02:00
|
|
|
| AssertionTypes::RangeSet
|
2025-09-11 02:49:14 +02:00
|
|
|
| AssertionTypes::RangeNeighbours =>
|
|
|
|
|
Err(format!("Returned type {ty:?} is not a valid return type.")),
|
2025-09-07 17:55:53 +02:00
|
|
|
}
|
2025-08-17 03:19:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn execute(
|
2025-09-07 17:55:53 +02:00
|
|
|
&self,
|
|
|
|
|
graph: &graph::SystemGraph,
|
|
|
|
|
node: &<graph::SystemGraph as petgraph::visit::GraphBase>::NodeId,
|
|
|
|
|
translator: &mut translator::Translator,
|
2025-08-17 03:19:50 +02:00
|
|
|
) -> Result<AssertReturnValue, String> {
|
2025-09-07 17:55:53 +02:00
|
|
|
let system::System {
|
|
|
|
|
available_entities: entities,
|
|
|
|
|
..
|
|
|
|
|
} = graph
|
|
|
|
|
.node_weight(*node)
|
|
|
|
|
.ok_or("Missing node {{debug: {node:?}}}")?;
|
|
|
|
|
|
|
|
|
|
let mut input_vals = HashMap::new();
|
|
|
|
|
input_vals.insert(
|
|
|
|
|
NodeRelablerInput::Entities,
|
|
|
|
|
NodeRelablerInputValues::Entities(entities.clone()),
|
|
|
|
|
);
|
|
|
|
|
input_vals.insert(
|
|
|
|
|
NodeRelablerInput::Node,
|
|
|
|
|
NodeRelablerInputValues::Node(*node),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
let mut context = Context::new(input_vals);
|
|
|
|
|
if let Some(v) = execute(&self.tree, &mut context, translator, graph)? {
|
|
|
|
|
Ok(v)
|
|
|
|
|
} else {
|
|
|
|
|
Err("No value returned.".into())
|
|
|
|
|
}
|
2025-08-17 03:19:50 +02:00
|
|
|
}
|
|
|
|
|
}
|