Last active
January 24, 2021 22:10
-
-
Save dertin/d11c50e957c41325131d3c45cee28f66 to your computer and use it in GitHub Desktop.
Revisions
-
dertin revised this gist
Jan 24, 2021 . 1 changed file with 17 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,6 @@ // This code is just a proof of concept of the code I need to generate in Rust, having as input a yaml file with process definitions of the style (bpmn) // Look at the process diagram: https://gist.github.com/dertin/d11c50e957c41325131d3c45cee28f66#gistcomment-3605790 #[allow(unused_variables)] #[macro_use] @@ -14,6 +17,7 @@ use rand::Rng; enum Value { I32(i32), String(String), VecString(Vec<String>) } fn dump<V, R>(v: V) -> R @@ -40,6 +44,15 @@ impl From<&Value> for String { } } impl From<&Value> for Vec<String> { fn from(v: &Value) -> Vec<String> { match v { Value::VecString(s) => s.clone(), _ => panic!(), } } } fn main() { // static CONTEXT: OnceCell<Arc<RwLock<HashMap<String, Value>>>> = OnceCell::new(); @@ -92,7 +105,7 @@ fn main() { println!("task_01 - call job_01"); let var01 = job_01(); map.insert("vector", Value::VecString(vec!["test".to_string()])); map.insert("var01", Value::I32(var01)); println!("task_01 - set context.var01 with value: {}", var01); @@ -112,10 +125,12 @@ fn main() { let mut map = CONTEXT.lock().unwrap(); let context_var03 = map.get("var03").unwrap(); let context_vector = map.get("vector").unwrap(); let context_var03: String = dump(context_var03); let context_vector: Vec<String> = dump(context_vector); println!("task_02 - get context.var03 with value: {}", context_var03); println!("task_02 - get context.vector with value: {:#?}", context_vector); println!("task_02 - call job_02"); let (var02_a, var02_b) = job_02(); -
dertin revised this gist
Jan 24, 2021 . 1 changed file with 76 additions and 42 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -5,23 +5,38 @@ extern crate lazy_static; use std::collections::HashMap; use std::sync::{Mutex}; // Arc, RwLock // use once_cell::sync::OnceCell; use rand::Rng; #[derive(Debug)] enum Value { I32(i32), String(String), } fn dump<V, R>(v: V) -> R where R: From<V> { v.into() } impl From<&Value> for i32 { fn from(v: &Value) -> i32 { match v { Value::I32(n) => *n, _ => panic!(), } } } impl From<&Value> for String { fn from(v: &Value) -> String { match v { Value::String(s) => s.clone(), _ => panic!(), } } } @@ -39,20 +54,21 @@ fn main() { // Context - is a group of mutable variables that are maintained throughout the task process. // Each job changed the context. fn job_01() -> i32 { let mut rng = rand::thread_rng(); let num = rng.gen_range(7..10); println!(" job_01 - generate a random number: {}", num); num } fn job_02() -> (String, String) { println!(" job_02 - it only returns the same two strings "); ("pass".to_string(), "other".to_string()) } fn job_03(input: i32) -> String { println!(" job_03({}) - receives a numeric parameter from context.var01", input); if input == 9 { "load".to_string() @@ -61,79 +77,96 @@ fn main() { } } fn job_04() -> i32 { println!(" job_04 - always returns the same number"); 200 } /// Tasks are job containers, they serve to orchestrate the call of other tasks after executing a main job that changed the context. fn task_01() { println!(""); println!("::task_01::"); let mut map = CONTEXT.lock().unwrap(); println!("task_01 - call job_01"); let var01 = job_01(); map.insert("var01", Value::I32(var01)); println!("task_01 - set context.var01 with value: {}", var01); std::mem::drop(map); if var01 == 0 { println!("task_01 - call task_02"); task_02(); }else{ println!("task_01 - call task_03"); task_03(); } } fn task_02() { println!(""); println!("::task_02::"); let mut map = CONTEXT.lock().unwrap(); let context_var03 = map.get("var03").unwrap(); let context_var03: String = dump(context_var03); println!("task_02 - get context.var03 with value: {}", context_var03); println!("task_02 - call job_02"); let (var02_a, var02_b) = job_02(); map.insert("var02_a", Value::String(var02_a.clone())); map.insert("var02_b", Value::String(var02_b.clone())); println!("task_02 - set context.var02_a -> {}", var02_a.clone()); println!("task_02 - set context.var02_b -> {}", var02_b.clone()); std::mem::drop(map); if var02_a == "pass".to_string() && context_var03 == "load".to_string() { println!("task_02 - call task_04"); task_04(); }else{ println!("task_02 - call task_01"); task_01(); } } fn task_03() { println!(""); println!("::task_03::"); let mut map = CONTEXT.lock().unwrap(); let context_var01 = map.get("var01").unwrap(); let var01_dump: i32 = dump(context_var01); println!("task_03 - get context.var01 with value: {}", var01_dump); println!("task_03 - call job_03({})", var01_dump); let var03 = job_03(var01_dump); map.insert("var03", Value::String(var03.clone())); println!("task_03 - set context.var03 with value: {}", var03.clone()); std::mem::drop(map); println!("task_03 - call task_02"); task_02(); } fn task_04() { println!(""); println!("::task_04::"); println!("task_04 - call job_04"); let var04 = job_04(); let mut map = CONTEXT.lock().unwrap(); map.insert("var04", Value::I32(var04)); println!("task_04 - set context.var04 with value: {}", var04); std::mem::drop(map); if var04 != 200 { @@ -144,9 +177,10 @@ fn main() { } fn task_end() { println!("::task_end::"); println!(""); let map = CONTEXT.lock().unwrap(); println!("task_end - result context -> {:#?}", map); std::mem::drop(map); } -
dertin revised this gist
Jan 24, 2021 . 1 changed file with 7 additions and 6 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -17,11 +17,11 @@ enum Value { // .. whatever else you need } fn dump (v: &Value) { use Value::*; match *v { I32(n) => println!("{}", n), String(ref s) => println!("{}", s), } } @@ -41,7 +41,7 @@ fn main() { fn job_01(input: String) -> i32 { println!("job_01: {:?}", input); //let mut rng = rand::thread_rng(); //rng.gen_range(0..9) 9 @@ -108,11 +108,12 @@ fn main() { let mut map = CONTEXT.lock().unwrap(); let context_var01 = map.get("var01").unwrap(); //println!("context_var01 -> {:?}", dump(context_var01)); /* TODO: if dump(context_var01) == "9" { println!("pepe"); }*/ let var03 = job_03(9); -
dertin revised this gist
Jan 24, 2021 . 1 changed file with 84 additions and 26 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,61 +1,102 @@ #[allow(unused_variables)] #[macro_use] extern crate lazy_static; use std::collections::HashMap; use std::sync::{Mutex}; // Arc, RwLock, //use once_cell::sync::OnceCell; use rand::Rng; #[derive(Debug)] enum Value { I32(i32), String(String), // .. whatever else you need } fn dump (v: &Value) -> T { use Value::*; match *v { I32(n) => format!("{}", n), String(ref s) => format!("{}", s), } } fn main() { // static CONTEXT: OnceCell<Arc<RwLock<HashMap<String, Value>>>> = OnceCell::new(); lazy_static! { static ref CONTEXT: Mutex<HashMap<&'static str, Value>> = { let m = HashMap::new(); Mutex::new(m) }; } // Context - is a group of mutable variables that are maintained throughout the task process. // Each job changed the context. fn job_01(input: String) -> i32 { println!("job_01: {:?}", input); let mut rng = rand::thread_rng(); //rng.gen_range(0..9) 9 } fn job_02(input: String) -> (String, String) { println!("job_02: {:?}", input); ("pass".to_string(), "other".to_string()) } fn job_03(input: i32) -> String { println!("job_03: {:?}", input); if input == 9 { "load".to_string() }else{ "continue".to_string() } } fn job_04(input: Vec<String>) -> i32 { println!("job_04: {:?}", input); 200 } /// Tasks are job containers, they serve to orchestrate the call of other tasks after executing a main job that changed the context. fn task_01() { let mut map = CONTEXT.lock().unwrap(); let var01 = job_01("anything".to_string()); map.insert("var01", Value::I32(var01)); println!("task_01 - context -> {:?}", map); std::mem::drop(map); if var01 == 0 { task_02(); }else{ task_03(); } } fn task_02() { let mut map = CONTEXT.lock().unwrap(); let (var02_a, var02_b) = job_02("info".to_string()); map.insert("var02_a", Value::String(var02_a.clone())); map.insert("var02_b", Value::String(var02_b.clone())); println!("task_02 - context -> {:?}", map); std::mem::drop(map); if var02_a == "pass".to_string() { task_04(); }else{ @@ -64,19 +105,36 @@ fn main() { } fn task_03() { let mut map = CONTEXT.lock().unwrap(); let context_var01 = map.get("var01").unwrap(); println!("context_var01 -> {:?}", dump(context_var01)); if dump(context_var01) == "9" { println!("pepe"); } let var03 = job_03(9); map.insert("var03", Value::String(var03.clone())); println!("task_03 - context -> {:?}", map); std::mem::drop(map); if var03 == "load" { task_02(); }else{ task_end(); } } fn task_04() { let var04 = job_04(vec!["Hello".to_string(), "World".to_string()]); // TODO: send context let mut map = CONTEXT.lock().unwrap(); map.insert("var04", Value::I32(var04)); println!("task_04 - context -> {:?}", map); std::mem::drop(map); if var04 != 200 { task_03(); }else{ @@ -86,9 +144,9 @@ fn main() { fn task_end() { println!(""); let map = CONTEXT.lock().unwrap(); println!("task_end - context -> {:?}", map); std::mem::drop(map); } // START -
dertin renamed this gist
Jan 23, 2021 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
dertin revised this gist
Jan 23, 2021 . 1 changed file with 9 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -4,6 +4,7 @@ use std::collections::HashMap; use once_cell::sync::OnceCell; use std::sync::RwLock; #[derive(Debug)] enum Value{ I32(i32), F32(f32), @@ -12,14 +13,17 @@ enum Value{ } fn main() { static CONTEXT: OnceCell<RwLock<HashMap<String, Value>>> = OnceCell::new(); // Context - is a group of mutable variables that are maintained throughout the task process. // Each job changed the context. fn job_01(input: String) -> i32 { println!("job_01: {:?}", input); let mut map: HashMap<String, Value> = HashMap::new(); map.insert("var01".to_string(), Value::I32(0)); CONTEXT.set(RwLock::new(map)); 0 } fn job_02(input: String) -> (String, String) { @@ -52,7 +56,7 @@ fn main() { let (var02_a, var02_b) = job_02("info".to_string()); // TODO: send context // TODO: context.var02_a = var02_a; // TODO: context.var02_b = var02_b; if var02_a == "pass".to_string() { task_04(); }else{ task_01(); @@ -82,7 +86,8 @@ fn main() { fn task_end() { println!(""); let context = CONTEXT.get(); println!("task_end - context -> {:?}", context); // TODO: print context } -
dertin revised this gist
Jan 23, 2021 . 1 changed file with 7 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -4,6 +4,13 @@ use std::collections::HashMap; use once_cell::sync::OnceCell; use std::sync::RwLock; enum Value{ I32(i32), F32(f32), String(String), // .. whatever else you need } fn main() { // Context - is a group of mutable variables that are maintained throughout the task process. -
dertin revised this gist
Jan 23, 2021 . 1 changed file with 6 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,8 +1,14 @@ #[allow(unused_variables)] use std::collections::HashMap; use once_cell::sync::OnceCell; use std::sync::RwLock; fn main() { // Context - is a group of mutable variables that are maintained throughout the task process. static CONTEXT: OnceCell<RwLock<HashMap<String, Value>>> = OnceCell::new(); // Each job changed the context. fn job_01(input: String) -> i32 { -
dertin revised this gist
Jan 23, 2021 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -39,7 +39,7 @@ fn main() { let (var02_a, var02_b) = job_02("info".to_string()); // TODO: send context // TODO: context.var02_a = var02_a; // TODO: context.var02_b = var02_b; if var02_a == "pass".to_string() { // TODO: Add ( and context.var03 == "load" ) task_04(); }else{ task_01(); -
dertin created this gist
Jan 23, 2021 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,78 @@ #[allow(unused_variables)] fn main() { // Context - is a group of mutable variables that are maintained throughout the task process. // Each job changed the context. fn job_01(input: String) -> i32 { println!("job_01: {:?}", input); 0 } fn job_02(input: String) -> (String, String) { println!("job_02: {:?}", input); ("pass".to_string(), "load".to_string()) } fn job_03(input: i32) -> String { println!("job_03: {:?}", input); "load".to_string() } fn job_04(input: Vec<String>) -> i32 { println!("job_04: {:?}", input); 200 } /// Tasks are job containers, they serve to orchestrate the call of other tasks after executing a main job that changed the context. fn task_01() { println!("task_01 - context -> {:?}", "???"); let var01 = job_01("anything".to_string()); // TODO: send context // TODO: context.var01 = var01; if var01 == 0 { task_02(); }else{ task_03(); } } fn task_02() { println!("task_02 - context -> {:?}", "???"); let (var02_a, var02_b) = job_02("info".to_string()); // TODO: send context // TODO: context.var02_a = var02_a; // TODO: context.var02_b = var02_b; if var02_a == "pass".to_string() { task_04(); }else{ task_01(); } } fn task_03() { println!("task_03 - context -> {:?}", "???"); let var03 = job_03(1234); // TODO: send context // TODO: context.var03 = var03; if var03 == "load" { task_02(); }else{ task_end(); } } fn task_04() { println!("task_04 - context -> {:?}", "???"); let var04 = job_04(vec!["Hello".to_string(), "World".to_string()]); // TODO: send context // TODO: context.var04 = var04; if var04 != 200 { task_03(); }else{ task_end(); } } fn task_end() { println!(""); println!("task_end - context -> {:?}", "???"); // TODO: print context } // START task_01(); }