diff --git a/src/experiments/interaction_random_gen/gen.rs b/src/experiments/interaction_random_gen/gen.rs index e18ec63..30113be 100644 --- a/src/experiments/interaction_random_gen/gen.rs +++ b/src/experiments/interaction_random_gen/gen.rs @@ -21,7 +21,8 @@ use rand::prelude::{SliceRandom, StdRng}; use rand::Rng; use crate::core::general_context::GeneralContext; -use crate::core::language::syntax::action::{CommunicationSynchronicity, EmissionAction, ReceptionAction}; +use crate::core::language::involve::involves::InvolvesLifelines; +use crate::core::language::syntax::action::{CommunicationSynchronicity, EmissionAction, EmissionTargetRef, ReceptionAction}; use crate::core::language::syntax::interaction::{Interaction, LoopKind}; use crate::core::language::syntax::metrics::{InteractionMetrics, SymbolKind}; use crate::experiments::interaction_random_gen::probas::{InteractionGenerationSymbol, InteractionSymbolsProbabilities}; @@ -52,6 +53,29 @@ pub fn generate_random_action(signature : &GeneralContext,rng : &mut StdRng) -> } } +pub fn generate_random_pattern(signature : &GeneralContext, + is_broadcast : bool, + rng : &mut StdRng) -> Interaction { + let ms_id = rng.gen_range(0..signature.get_ms_num()); + let mut lifelines : Vec = (0..signature.get_lf_num()).collect(); + lifelines.shuffle(rng); + let orig_lf_id = lifelines.pop().unwrap(); + let targets : Vec = if is_broadcast { + let number_of_targets = rng.gen_range(0..lifelines.len()); + lifelines[0..number_of_targets].iter() + .map(|x| EmissionTargetRef::Lifeline(*x)).collect() + } else { + vec![EmissionTargetRef::Lifeline(lifelines.pop().unwrap())] + }; + let emission = EmissionAction::new( + orig_lf_id, + ms_id, + CommunicationSynchronicity::Asynchronous, + targets + ); + Interaction::Emission(emission) +} + pub fn generate_random_interaction(probas : &InteractionSymbolsProbabilities, //resolve_basic_at_low_depth : bool, depth : u32, @@ -62,36 +86,14 @@ pub fn generate_random_interaction(probas : &InteractionSymbolsProbabilities, return generate_random_action(signature,rng); } let mut symbol = probas.get_random_symbol(rng); - /*if !resolve_basic_at_low_depth && depth <= max_depth/2 { - while symbol == InteractionGenerationSymbol::Basic { - println!("should not resolve basic interaction at low depth"); - symbol = probas.get_random_symbol(rng); - } - }*/ match symbol { + InteractionGenerationSymbol::Transmission => { + generate_random_pattern(signature,false,rng) + }, + InteractionGenerationSymbol::Broadcast => { + generate_random_pattern(signature,true,rng) + }, InteractionGenerationSymbol::Basic => { - /*let alphabet = get_alphabet_from_gen_ctx(&signature); - let mut i = Interaction::Empty; - loop { - let got_i = generate_random_interaction( - &InteractionSymbolsProbabilities::default_basic(), - depth, - max_depth, - signature, - rng - ); - let (nfa, _) = get_nfa_from_interaction_exploration(signature, - &i, - alphabet.clone()); - if nfa.transitions.len() > 1000 { - println!("basic interaction has more than 1000 states, retrying..."); - continue; - } else { - i = got_i; - break; - } - }; - i*/ generate_random_interaction( &InteractionSymbolsProbabilities::default_basic(), depth, @@ -99,13 +101,13 @@ pub fn generate_random_interaction(probas : &InteractionSymbolsProbabilities, signature, rng ) - } + }, InteractionGenerationSymbol::Empty => { Interaction::Empty - } + }, InteractionGenerationSymbol::Action => { generate_random_action(signature,rng) - } + }, InteractionGenerationSymbol::LoopS => { let i1 = generate_random_interaction(probas,depth+1,max_depth,signature,rng); Interaction::Loop(LoopKind::SStrictSeq,Box::new(i1)) @@ -134,6 +136,16 @@ pub fn generate_random_interaction(probas : &InteractionSymbolsProbabilities, InteractionGenerationSymbol::Alt => { Interaction::Alt(i1,i2) }, + InteractionGenerationSymbol::Coreg => { + let involved_in_both : Vec = i1.involved_lifelines().intersection(&i2.involved_lifelines()) + .into_iter().cloned().collect(); + if involved_in_both.is_empty() { + Interaction::Seq(i1,i2) + } else { + let lf_id = involved_in_both.get(rng.gen_range(0..involved_in_both.len())).unwrap(); + Interaction::CoReg(vec![*lf_id],i1,i2) + } + }, _ => { panic!() } diff --git a/src/experiments/interaction_random_gen/probas.rs b/src/experiments/interaction_random_gen/probas.rs index 9d91b3c..98d022d 100644 --- a/src/experiments/interaction_random_gen/probas.rs +++ b/src/experiments/interaction_random_gen/probas.rs @@ -34,12 +34,15 @@ pub enum InteractionGenerationSymbol { Action, Strict, Seq, + Coreg, Par, LoopS, LoopW, LoopP, Alt, - Basic + Basic, + Transmission, + Broadcast } @@ -79,6 +82,27 @@ impl InteractionSymbolsProbabilities { Ok(Self{ordered_symbols,ordered_bounds}) } + pub fn protocols_with_coreg() -> InteractionSymbolsProbabilities { + let map = hashmap!{ + InteractionGenerationSymbol::Empty => 0.025, + InteractionGenerationSymbol::Transmission => 0.175, + // total 0.2 + InteractionGenerationSymbol::Broadcast => 0.1, + // total 0.3 + InteractionGenerationSymbol::Seq => 0.2, + // total 0.5 + InteractionGenerationSymbol::Coreg => 0.1, + // total 0.6 + InteractionGenerationSymbol::Alt => 0.2, + // total 0.8 + InteractionGenerationSymbol::LoopW => 0.15, + // total 0.95 + InteractionGenerationSymbol::Par => 0.05 + // total 1.0 + }; + Self::from_map(map).unwrap() + } + pub fn conservative() -> InteractionSymbolsProbabilities { let map = hashmap!{ InteractionGenerationSymbol::Empty => 0.025, diff --git a/src/io/output/draw_interactions/as_sd/interaction_repr/img_content.rs b/src/io/output/draw_interactions/as_sd/interaction_repr/img_content.rs index df559b6..2e5b6a9 100644 --- a/src/io/output/draw_interactions/as_sd/interaction_repr/img_content.rs +++ b/src/io/output/draw_interactions/as_sd/interaction_repr/img_content.rs @@ -282,8 +282,8 @@ fn draw_coregion_frame( image : &mut RgbImage, // *** let y_end : f32 = y_coords.pop().unwrap(); for lf_coord in x_coords { - let x_left = lf_coord.x_middle - lf_coord.x_span_inner/2.0; - let x_right = lf_coord.x_middle + lf_coord.x_span_inner/2.0; + let x_left = lf_coord.x_middle - lf_coord.x_span_outer/2.0 + (nest_shift as f32)*FRAGMENT_PADDING; + let x_right = lf_coord.x_middle + lf_coord.x_span_outer/2.0 - (nest_shift as f32)*FRAGMENT_PADDING; // *** draw_line_segment_mut(image, (x_left, y_start), diff --git a/src/io/output/to_hfiles/interaction/interaction.rs b/src/io/output/to_hfiles/interaction/interaction.rs index 7594164..3f5610d 100644 --- a/src/io/output/to_hfiles/interaction/interaction.rs +++ b/src/io/output/to_hfiles/interaction/interaction.rs @@ -17,9 +17,14 @@ limitations under the License. use crate::core::general_context::GeneralContext; use crate::core::language::syntax::interaction::{Interaction, LoopKind}; -use crate::core::language::syntax::util::get_recursive_frag::{get_recursive_alt_frags, get_recursive_par_frags, get_recursive_seq_frags, get_recursive_strict_frags}; +use crate::core::language::syntax::util::get_recursive_frag::{ + get_recursive_alt_frags, + get_recursive_par_frags, + get_recursive_seq_frags, + get_recursive_coreg_frags, + get_recursive_strict_frags}; use crate::io::output::to_hfiles::interaction::model_action::{emission_as_hif_encoding, reception_as_hif_encoding}; -use crate::io::textual_convention::{SYNTAX_ALT, SYNTAX_EMPTY, SYNTAX_LOOP_H, SYNTAX_LOOP_P, SYNTAX_LOOP_S, SYNTAX_LOOP_W, SYNTAX_PAR, SYNTAX_SEQ, SYNTAX_STRICT}; +use crate::io::textual_convention::{SYNTAX_ALT, SYNTAX_COREG, SYNTAX_EMPTY, SYNTAX_LOOP_H, SYNTAX_LOOP_P, SYNTAX_LOOP_S, SYNTAX_LOOP_W, SYNTAX_PAR, SYNTAX_SEQ, SYNTAX_STRICT}; pub fn interaction_as_hif_encoding(gen_ctx : &GeneralContext, @@ -29,7 +34,7 @@ pub fn interaction_as_hif_encoding(gen_ctx : &GeneralContext, fn op_as_hif_encoding(gen_ctx : &GeneralContext, depth : usize, - op_text : &'static str, + op_text : &str, sub_ints : Vec<&Interaction>) -> String { let ints_strs : Vec = sub_ints.iter().map(|i| interaction_as_hif_encoding_inner(gen_ctx,depth+1,i)).collect(); return format!("{0}{1}(\n{2}\n{0})", "\t".repeat(depth), op_text, ints_strs.join(",\n")); @@ -75,8 +80,12 @@ fn interaction_as_hif_encoding_inner(gen_ctx : &GeneralContext, seq_frags.extend_from_slice(&mut get_recursive_seq_frags(i2)); return op_as_hif_encoding(gen_ctx,depth,SYNTAX_SEQ,seq_frags); }, - &Interaction::CoReg(_, ref i1, ref i2) => { - panic!("todo"); + &Interaction::CoReg(ref cr, ref i1, ref i2) => { + let mut cr_frags = get_recursive_coreg_frags(cr,i1); + cr_frags.extend_from_slice(&mut get_recursive_coreg_frags(cr,i2)); + let cr_lfs : Vec = cr.iter().map(|lf_id| gen_ctx.get_lf_name(*lf_id).unwrap()).collect(); + let operator_name = format!("{:}({:})", SYNTAX_COREG, cr_lfs.join(",")); + return op_as_hif_encoding(gen_ctx,depth,&operator_name,cr_frags); }, &Interaction::Par(ref i1, ref i2) => { let mut par_frags = get_recursive_par_frags(i1); diff --git a/src/io/textual_convention.rs b/src/io/textual_convention.rs index d128edb..fff02e0 100644 --- a/src/io/textual_convention.rs +++ b/src/io/textual_convention.rs @@ -21,7 +21,7 @@ pub static SYNTAX_SEQ: &'static str = "seq"; pub static SYNTAX_PAR: &'static str = "par"; pub static SYNTAX_ALT: &'static str = "alt"; pub static SYNTAX_SYNC: &'static str = "sync"; -pub static SYNTAX_COREG: &'static str = "creg"; +pub static SYNTAX_COREG: &'static str = "coreg"; pub static SYNTAX_AND: &'static str = "and"; diff --git a/src/ui/commands/cli_rng_gen_interactions.rs b/src/ui/commands/cli_rng_gen_interactions.rs index 39aac87..cdfe401 100644 --- a/src/ui/commands/cli_rng_gen_interactions.rs +++ b/src/ui/commands/cli_rng_gen_interactions.rs @@ -119,6 +119,10 @@ pub fn cli_rng_gen_interactions(matches : &ArgMatches) -> (Vec,u32) { probas_name = "conservative"; InteractionSymbolsProbabilities::conservative() }, + "protocols_with_coreg" => { + probas_name = "conservative"; + InteractionSymbolsProbabilities::protocols_with_coreg() + }, "default" => { InteractionSymbolsProbabilities::default_non_regular() },