Skip to content

Commit

Permalink
minor changes related to random interaction generation and drawing of…
Browse files Browse the repository at this point in the history
… coregions
  • Loading branch information
erwanM974 committed Jul 7, 2024
1 parent 409ab93 commit ee793b1
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 41 deletions.
76 changes: 44 additions & 32 deletions src/experiments/interaction_random_gen/gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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<usize> = (0..signature.get_lf_num()).collect();
lifelines.shuffle(rng);
let orig_lf_id = lifelines.pop().unwrap();
let targets : Vec<EmissionTargetRef> = 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,
Expand All @@ -62,50 +86,28 @@ 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,
max_depth,
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))
Expand Down Expand Up @@ -134,6 +136,16 @@ pub fn generate_random_interaction(probas : &InteractionSymbolsProbabilities,
InteractionGenerationSymbol::Alt => {
Interaction::Alt(i1,i2)
},
InteractionGenerationSymbol::Coreg => {
let involved_in_both : Vec<usize> = 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!()
}
Expand Down
26 changes: 25 additions & 1 deletion src/experiments/interaction_random_gen/probas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,15 @@ pub enum InteractionGenerationSymbol {
Action,
Strict,
Seq,
Coreg,
Par,
LoopS,
LoopW,
LoopP,
Alt,
Basic
Basic,
Transmission,
Broadcast
}


Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
19 changes: 14 additions & 5 deletions src/io/output/to_hfiles/interaction/interaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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<String> = 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"));
Expand Down Expand Up @@ -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<String> = 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);
Expand Down
2 changes: 1 addition & 1 deletion src/io/textual_convention.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down
4 changes: 4 additions & 0 deletions src/ui/commands/cli_rng_gen_interactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ pub fn cli_rng_gen_interactions(matches : &ArgMatches) -> (Vec<String>,u32) {
probas_name = "conservative";
InteractionSymbolsProbabilities::conservative()
},
"protocols_with_coreg" => {
probas_name = "conservative";
InteractionSymbolsProbabilities::protocols_with_coreg()
},
"default" => {
InteractionSymbolsProbabilities::default_non_regular()
},
Expand Down

0 comments on commit ee793b1

Please sign in to comment.