diff --git a/scan_core/src/channel_system.rs b/scan_core/src/channel_system.rs index 62ea1d0..1c9f4b4 100644 --- a/scan_core/src/channel_system.rs +++ b/scan_core/src/channel_system.rs @@ -13,7 +13,7 @@ use thiserror::Error; use crate::grammar::*; -use crate::program_graph::*; +use crate::program_graph::{Action as PgAction, Location as PgLocation, Var as PgVar, *}; use std::{collections::HashMap, rc::Rc}; /// An indexing object for PGs in a CS. @@ -35,24 +35,24 @@ pub struct Channel(usize); /// These cannot be directly created or manipulated, /// but have to be generated and/or provided by a [`ChannelSystemBuilder`] or [`ChannelSystem`]. #[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)] -pub struct CsLocation(PgId, Location); +pub struct Location(PgId, PgLocation); /// An indexing object for actions in a CS. /// /// These cannot be directly created or manipulated, /// but have to be generated and/or provided by a [`ChannelSystemBuilder`] or [`ChannelSystem`]. #[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)] -pub struct CsAction(PgId, Action); +pub struct Action(PgId, PgAction); /// An indexing object for typed variables in a CS. /// /// These cannot be directly created or manipulated, /// but have to be generated and/or provided by a [`ChannelSystemBuilder`] or [`ChannelSystem`]. #[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)] -pub struct CsVar(PgId, Var); +pub struct Var(PgId, PgVar); /// An expression using CS's [`CsVar`] as variables. -pub type CsExpression = super::grammar::Expression; +pub type CsExpression = super::grammar::Expression; impl TryFrom<(PgId, CsExpression)> for PgExpression { type Error = CsError; @@ -135,7 +135,7 @@ pub enum Message { /// Sending the computed value of an expression to a channel. Send(CsExpression), /// Retrieving a value out of a channel and associating it to a variable. - Receive(CsVar), + Receive(Var), /// Checking whether a channel is empty. ProbeEmptyQueue, } @@ -157,16 +157,16 @@ pub enum CsError { Empty(Channel), /// There is no such communication action in the CS. #[error("communication {0:?} has not been defined")] - NoCommunication(CsAction), + NoCommunication(Action), /// There is no such action in the CS. #[error("action {0:?} does not belong to program graph {1:?}")] - ActionNotInPg(CsAction, PgId), + ActionNotInPg(Action, PgId), /// There is no such variable in the given PG. #[error("variable {0:?} does not belong to program graph {1:?}")] - VarNotInPg(CsVar, PgId), + VarNotInPg(Var, PgId), /// There is no such location in the given PG. #[error("location {0:?} does not belong to program graph {1:?}")] - LocationNotInPg(CsLocation, PgId), + LocationNotInPg(Location, PgId), /// The given PGs do not match. #[error("program graphs {0:?} and {1:?} do not match")] DifferentPgs(PgId, PgId), @@ -174,7 +174,7 @@ pub enum CsError { /// /// Is returned when trying to associate an effect to a communication action. #[error("action {0:?} is a communication")] - ActionIsCommunication(CsAction), + ActionIsCommunication(Action), /// There is no such channel in the CS. #[error("channel {0:?} does not exists")] MissingChannel(Channel), @@ -185,7 +185,7 @@ pub enum CsError { pub struct ChannelSystemBuilder { program_graphs: Vec, channels: Vec<(Type, Option)>, - communications: HashMap, + communications: HashMap, } impl ChannelSystemBuilder { @@ -206,33 +206,33 @@ impl ChannelSystemBuilder { /// Gets the initial location of the given PG. /// /// Fails if the CS contains no such PG. - pub fn initial_location(&mut self, pg_id: PgId) -> Result { + pub fn initial_location(&mut self, pg_id: PgId) -> Result { let pg = self .program_graphs .get(pg_id.0) .ok_or(CsError::MissingPg(pg_id))?; - let initial = CsLocation(pg_id, pg.initial_location()); + let initial = Location(pg_id, pg.initial_location()); Ok(initial) } /// Adds a new variable of the given type to the given PG. /// /// Fails if the CS contains no such PG. - pub fn new_var(&mut self, pg_id: PgId, var_type: Type) -> Result { + pub fn new_var(&mut self, pg_id: PgId, var_type: Type) -> Result { self.program_graphs .get_mut(pg_id.0) .ok_or(CsError::MissingPg(pg_id)) - .map(|pg| CsVar(pg_id, pg.new_var(var_type))) + .map(|pg| Var(pg_id, pg.new_var(var_type))) } /// Adds a new action to the given PG. /// /// Fails if the CS contains no such PG. - pub fn new_action(&mut self, pg_id: PgId) -> Result { + pub fn new_action(&mut self, pg_id: PgId) -> Result { self.program_graphs .get_mut(pg_id.0) .ok_or(CsError::MissingPg(pg_id)) - .map(|pg| CsAction(pg_id, pg.new_action())) + .map(|pg| Action(pg_id, pg.new_action())) } /// Adds an effect to the given action of the given PG. @@ -241,8 +241,8 @@ impl ChannelSystemBuilder { pub fn add_effect( &mut self, pg_id: PgId, - action: CsAction, - var: CsVar, + action: Action, + var: Var, effect: CsExpression, ) -> Result<(), CsError> { if action.0 != pg_id { @@ -265,11 +265,11 @@ impl ChannelSystemBuilder { } /// Adds a new location to the given PG. - pub fn new_location(&mut self, pg_id: PgId) -> Result { + pub fn new_location(&mut self, pg_id: PgId) -> Result { self.program_graphs .get_mut(pg_id.0) .ok_or(CsError::MissingPg(pg_id)) - .map(|pg| CsLocation(pg_id, pg.new_location())) + .map(|pg| Location(pg_id, pg.new_location())) } /// Adds a transition to the PG. @@ -278,9 +278,9 @@ impl ChannelSystemBuilder { pub fn add_transition( &mut self, pg_id: PgId, - pre: CsLocation, - action: CsAction, - post: CsLocation, + pre: Location, + action: Action, + post: Location, guard: Option, ) -> Result<(), CsError> { if action.0 != pg_id { @@ -322,7 +322,7 @@ impl ChannelSystemBuilder { pg_id: PgId, channel: Channel, message: Message, - ) -> Result { + ) -> Result { let channel_type = self .channels .get(channel.0) @@ -394,7 +394,7 @@ impl ChannelSystemBuilder { pub struct ChannelSystem { program_graphs: Vec, channels: Rc)>>, - communications: Rc>, + communications: Rc>, message_queue: Vec>, } @@ -404,15 +404,15 @@ impl ChannelSystem { /// An admittable transition is characterized by the PG it executes on, the required action and the post-state /// (the pre-state being necessarily the current state of the machine). /// The (eventual) guard is guaranteed to be satisfied. - pub fn possible_transitions(&self) -> impl Iterator + '_ { + pub fn possible_transitions(&self) -> impl Iterator + '_ { self.program_graphs .iter() .enumerate() .flat_map(move |(id, pg)| { let pg_id = PgId(id); pg.possible_transitions().filter_map(move |(action, post)| { - let action = CsAction(pg_id, action); - let post = CsLocation(pg_id, post); + let action = Action(pg_id, action); + let post = Location(pg_id, post); if self.communications.contains_key(&action) && self.check_communication(pg_id, action).is_err() { @@ -424,7 +424,7 @@ impl ChannelSystem { }) } - fn check_communication(&self, pg_id: PgId, action: CsAction) -> Result<(), CsError> { + fn check_communication(&self, pg_id: PgId, action: Action) -> Result<(), CsError> { if action.0 != pg_id { Err(CsError::ActionNotInPg(action, pg_id)) } else if let Some((channel, message)) = self.communications.get(&action) { @@ -451,8 +451,8 @@ impl ChannelSystem { pub fn transition( &mut self, pg_id: PgId, - action: CsAction, - post: CsLocation, + action: Action, + post: Location, ) -> Result<(), CsError> { // If action is a communication, check it is legal if self.communications.contains_key(&action) { diff --git a/scan_fmt_xml/src/builder.rs b/scan_fmt_xml/src/builder.rs index a437df0..f1169ff 100644 --- a/scan_fmt_xml/src/builder.rs +++ b/scan_fmt_xml/src/builder.rs @@ -285,9 +285,9 @@ impl ModelBuilder { event_received, step, loc_tick, - Some(Expression::Equal(Box::new(( - Expression::Component(0, Box::new(Expression::Var(ext_event_var))), - Expression::Integer(tick_idx), + Some(CsExpression::Equal(Box::new(( + CsExpression::Component(0, Box::new(CsExpression::Var(ext_event_var))), + CsExpression::Integer(tick_idx), )))), ) .expect("hope this works"); @@ -297,9 +297,9 @@ impl ModelBuilder { event_received, step, loc_halt, - Some(Expression::Equal(Box::new(( - Expression::Component(0, Box::new(Expression::Var(ext_event_var))), - Expression::Integer(halt_idx), + Some(CsExpression::Equal(Box::new(( + CsExpression::Component(0, Box::new(CsExpression::Var(ext_event_var))), + CsExpression::Integer(halt_idx), )))), ) .expect("hope this works"); @@ -320,13 +320,13 @@ impl ModelBuilder { fn build_bt_node( &mut self, pg_id: PgId, - pt_tick: CsLocation, - pt_success: CsLocation, - pt_running: CsLocation, - pt_failure: CsLocation, - pt_halt: CsLocation, - pt_ack: CsLocation, - step: CsAction, + pt_tick: Location, + pt_success: Location, + pt_running: Location, + pt_failure: Location, + pt_halt: Location, + pt_ack: Location, + step: Action, node: &BtNode, ) -> anyhow::Result<()> { match node { @@ -342,7 +342,7 @@ impl ModelBuilder { pg_id, halt_after_failure, halting_after_failure, - Expression::Boolean(true), + CsExpression::Boolean(true), ) .expect("hand-picked arguments"); let failure_after_halting = @@ -352,7 +352,7 @@ impl ModelBuilder { pg_id, failure_after_halting, halting_after_failure, - Expression::Boolean(false), + CsExpression::Boolean(false), ) .expect("hand-picked arguments"); // If receives tick from parent, tick first child. @@ -420,7 +420,7 @@ impl ModelBuilder { prev_ack, step, pt_ack, - Some(Expression::Not(Box::new(Expression::Var( + Some(CsExpression::Not(Box::new(CsExpression::Var( halting_after_failure, )))), ) @@ -432,7 +432,7 @@ impl ModelBuilder { prev_ack, failure_after_halting, pt_failure, - Some(Expression::Var(halting_after_failure)), + Some(CsExpression::Var(halting_after_failure)), ) .expect("hand-made args"); } else { @@ -451,7 +451,7 @@ impl ModelBuilder { pg_id, halt_after_success, halting_after_success, - Expression::Boolean(true), + CsExpression::Boolean(true), ) .expect("hand-picked arguments"); let success_after_halting = @@ -461,7 +461,7 @@ impl ModelBuilder { pg_id, success_after_halting, halting_after_success, - Expression::Boolean(false), + CsExpression::Boolean(false), ) .expect("hand-picked arguments"); let loc_tick = self.cs.new_location(pg_id)?; @@ -539,7 +539,7 @@ impl ModelBuilder { prev_ack, step, pt_ack, - Some(Expression::Not(Box::new(Expression::Var( + Some(CsExpression::Not(Box::new(CsExpression::Var( halting_after_success, )))), ) @@ -551,7 +551,7 @@ impl ModelBuilder { prev_ack, success_after_halting, pt_success, - Some(Expression::Var(halting_after_success)), + Some(CsExpression::Var(halting_after_success)), ) .expect("hand-made args"); } else { @@ -588,8 +588,8 @@ impl ModelBuilder { pg_id, target_ext_queue, Message::Send(CsExpression::Tuple(vec![ - Expression::Integer(event_idx as Integer), - Expression::Integer(pg_idx), + CsExpression::Integer(event_idx as Integer), + CsExpression::Integer(pg_idx), ])), )?; let tick_sent = self.cs.new_location(pg_id)?; @@ -645,9 +645,9 @@ impl ModelBuilder { got_tick_response_param, step, pt_success, - Some(Expression::Equal(Box::new(( - Expression::Var(tick_response_param), - Expression::Integer(*self.enums.get("SUCCESS").unwrap()), + Some(CsExpression::Equal(Box::new(( + CsExpression::Var(tick_response_param), + CsExpression::Integer(*self.enums.get("SUCCESS").unwrap()), )))), ) .expect("hope this works"); @@ -657,9 +657,9 @@ impl ModelBuilder { got_tick_response_param, step, pt_failure, - Some(Expression::Equal(Box::new(( - Expression::Var(tick_response_param), - Expression::Integer(*self.enums.get("FAILURE").unwrap()), + Some(CsExpression::Equal(Box::new(( + CsExpression::Var(tick_response_param), + CsExpression::Integer(*self.enums.get("FAILURE").unwrap()), )))), ) .expect("hope this works"); @@ -669,9 +669,9 @@ impl ModelBuilder { got_tick_response_param, step, pt_running, - Some(Expression::Equal(Box::new(( - Expression::Var(tick_response_param), - Expression::Integer(*self.enums.get("RUNNING").unwrap()), + Some(CsExpression::Equal(Box::new(( + CsExpression::Var(tick_response_param), + CsExpression::Integer(*self.enums.get("RUNNING").unwrap()), )))), ) .expect("hope this works"); @@ -684,8 +684,8 @@ impl ModelBuilder { pg_id, target_ext_queue, Message::Send(CsExpression::Tuple(vec![ - Expression::Integer(event_idx as Integer), - Expression::Integer(pg_idx), + CsExpression::Integer(event_idx as Integer), + CsExpression::Integer(pg_idx), ])), )?; let halt_sent = self.cs.new_location(pg_id)?; @@ -836,8 +836,8 @@ impl ModelBuilder { .expect("hand-coded args"); // Create variables and channels for the storage of the parameters sent by external events. - let mut param_vars: HashMap<(usize, String), (CsVar, Type)> = HashMap::new(); - let mut param_actions: HashMap<(PgId, usize, String), CsAction> = HashMap::new(); + let mut param_vars: HashMap<(usize, String), (Var, Type)> = HashMap::new(); + let mut param_actions: HashMap<(PgId, usize, String), Action> = HashMap::new(); for event_builder in self .events .iter() @@ -987,14 +987,14 @@ impl ModelBuilder { .expect("sender must exist") .1 .index; - let mut is_event_sender = Some(Expression::And(vec![ - Expression::Equal(Box::new(( - Expression::Integer(event_index as Integer), - Expression::Var(current_event_var), + let mut is_event_sender = Some(CsExpression::And(vec![ + CsExpression::Equal(Box::new(( + CsExpression::Integer(event_index as Integer), + CsExpression::Var(current_event_var), ))), - Expression::Equal(Box::new(( - Expression::Integer(sender_index as Integer), - Expression::Var(origin_var), + CsExpression::Equal(Box::new(( + CsExpression::Integer(sender_index as Integer), + CsExpression::Var(origin_var), ))), ])); let mut current_loc = ext_event_processing_param; @@ -1049,7 +1049,7 @@ impl ModelBuilder { .iter() .filter(|((ev_ix, _), _)| *ev_ix == event_index) .map(|((_, name), (var, tp))| (name.to_owned(), (*var, tp.to_owned()))) - .collect::>(); + .collect::>(); } else { exec_origin = None; exec_params = HashMap::new(); @@ -1165,12 +1165,12 @@ impl ModelBuilder { pg_id: PgId, pg_idx: Integer, int_queue: Channel, - loc: CsLocation, - vars: &HashMap, - origin: Option, - params: &HashMap, + loc: Location, + vars: &HashMap, + origin: Option, + params: &HashMap, interner: &boa_interner::Interner, - ) -> Result { + ) -> Result { match executable { Executable::Raise { event } => { // Create event, if it does not exist already. @@ -1206,8 +1206,8 @@ impl ModelBuilder { pg_id, target_builder.ext_queue, Message::Send(CsExpression::Tuple(vec![ - Expression::Integer(event_idx as Integer), - Expression::Integer(pg_idx), + CsExpression::Integer(event_idx as Integer), + CsExpression::Integer(pg_idx), ])), ) .expect("must work"); @@ -1252,8 +1252,8 @@ impl ModelBuilder { pg_id, target_ext_queue, Message::Send(CsExpression::Tuple(vec![ - Expression::Integer(event_idx as Integer), - Expression::Integer(pg_idx), + CsExpression::Integer(event_idx as Integer), + CsExpression::Integer(pg_idx), ])), ) .expect("params are hard-coded"); @@ -1310,12 +1310,12 @@ impl ModelBuilder { target_id: PgId, param: &Param, event_idx: usize, - param_loc: CsLocation, - vars: &HashMap, - origin: Option, - params: &HashMap, + param_loc: Location, + vars: &HashMap, + origin: Option, + params: &HashMap, interner: &boa_interner::Interner, - ) -> Result { + ) -> Result { // Get param type. let scan_type = self .scan_types @@ -1344,9 +1344,9 @@ impl ModelBuilder { &mut self, expr: &boa_ast::Expression, interner: &boa_interner::Interner, - vars: &HashMap, - origin: Option, - params: &HashMap, + vars: &HashMap, + origin: Option, + params: &HashMap, ) -> anyhow::Result { let expr = match expr { boa_ast::Expression::This => todo!(), @@ -1361,7 +1361,7 @@ impl ModelBuilder { ident => self .enums .get(ident) - .map(|i| Expression::Integer(*i)) + .map(|i| CsExpression::Integer(*i)) .or_else(|| vars.get(ident).map(|(var, _)| CsExpression::Var(*var))) .ok_or(anyhow!("unknown identifier"))?, } @@ -1371,9 +1371,9 @@ impl ModelBuilder { match lit { Literal::String(_) => todo!(), Literal::Num(_) => todo!(), - Literal::Int(i) => Expression::Integer(*i), + Literal::Int(i) => CsExpression::Integer(*i), Literal::BigInt(_) => todo!(), - Literal::Bool(b) => Expression::Boolean(*b), + Literal::Bool(b) => CsExpression::Boolean(*b), Literal::Null => todo!(), Literal::Undefined => todo!(), }