Skip to content

Commit

Permalink
It is now possible to compare between two variables
Browse files Browse the repository at this point in the history
  • Loading branch information
dialogflowchatbot committed Jan 13, 2024
1 parent e7e2ca6 commit 248448b
Show file tree
Hide file tree
Showing 13 changed files with 739 additions and 498 deletions.
2 changes: 1 addition & 1 deletion src/flow/rt/collector.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use serde::{Deserialize, Serialize};
use {once_cell::sync::Lazy, regex::Regex};

static NUMBER_REGEX: Lazy<Regex> = Lazy::new(|| Regex::new(r"[1-9][\d]+(.[\d]+)?").unwrap());
static NUMBER_REGEX: Lazy<Regex> = Lazy::new(|| Regex::new(r"[1-9]([\d]+)?(.[\d]+)?").unwrap());

#[derive(Clone, Deserialize, Serialize, rkyv::Archive, rkyv::Deserialize, rkyv::Serialize)]
#[archive(compare(PartialEq), check_bytes)]
Expand Down
264 changes: 248 additions & 16 deletions src/flow/rt/condition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use serde::{Deserialize, Serialize};
use crate::flow::rt::context::Context;
use crate::flow::rt::dto::{Request, UserInputResult};
use crate::variable::crud as variable;
use crate::variable::dto::VariableType;

#[derive(
Clone, Copy, Deserialize, Serialize, rkyv::Archive, rkyv::Deserialize, rkyv::Serialize,
Expand All @@ -22,14 +23,21 @@ pub(crate) enum ConditionType {
)]
#[archive(compare(PartialEq), check_bytes)]
pub(crate) enum CompareType {
HasValue,
DoesNotHaveValue,
EmptyString,
Eq,
NotEq,
Contains,
NotContains,
NGT,
NGTE,
NLT,
NLTE,
Timeout,
}

#[derive(Clone, Deserialize, Serialize, rkyv::Archive, rkyv::Deserialize, rkyv::Serialize)]
#[derive(Copy, Clone, Debug, Deserialize, Serialize, rkyv::Archive, rkyv::Deserialize, rkyv::Serialize)]
#[archive(compare(PartialEq), check_bytes)]
pub(crate) enum TargetDataVariant {
Const,
Expand All @@ -47,38 +55,262 @@ pub(crate) struct ConditionData {
}

impl ConditionData {
pub(in crate::flow::rt) fn compare(&self, req: &Request, ctx: &mut Context) -> bool {
let target_data = match self.target_data_variant {
fn get_target_data(&self, req: &Request, ctx: &mut Context) -> String {
match self.target_data_variant {
TargetDataVariant::Const => self.target_data.clone(),
TargetDataVariant::Variable => variable::get_value(&self.target_data, req, ctx),
};
}
}
pub(in crate::flow::rt) fn compare(&self, req: &Request, ctx: &mut Context) -> bool {
// let target_data = match self.target_data_variant {
// TargetDataVariant::Const => self.target_data.clone(),
// TargetDataVariant::Variable => variable::get_value(&self.target_data, req, ctx),
// };
// println!("{} {}", &target_data, &req.user_input);
match self.condition_type {
ConditionType::UserInput => match self.compare_type {
CompareType::Eq => target_data.eq(&req.user_input),
CompareType::Contains => req.user_input.contains(&target_data),
CompareType::Eq => self.get_target_data(req, ctx).eq(&req.user_input),
CompareType::Contains => req.user_input.contains(&self.get_target_data(req, ctx)),
CompareType::Timeout => UserInputResult::Timeout == req.user_input_result,
_ => false,
},
ConditionType::UserIntent => {
// println!("{} {}", &target_data, req.user_input_intent.is_some());
req.user_input_intent.is_some()
&& target_data.eq(req.user_input_intent.as_ref().unwrap())
&& self.get_target_data(req, ctx).eq(req.user_input_intent.as_ref().unwrap())
}
ConditionType::FlowVariable => {
let mut n = false;
if let Ok(r) = variable::get(&self.ref_data) {
if let Some(ref_v) = r {
if let Some(val) = ref_v.get_value(req, ctx) {
n = val.val_to_string().eq(&target_data);
ConditionType::FlowVariable => match self.compare_type {
CompareType::HasValue => {
if let Ok(op) = variable::get(&self.ref_data) {
if let Some(v) = op {
v.get_value(req, ctx).is_some()
} else {
false
}
} else {
false
}
}
n
},
CompareType::DoesNotHaveValue => {
if let Ok(op) = variable::get(&self.ref_data) {
if let Some(v) = op {
v.get_value(req, ctx).is_none()
} else {
true
}
} else {
true
}
},
CompareType::EmptyString => {
if let Ok(op) = variable::get(&self.ref_data) {
if let Some(v) = op {
if v.var_type == VariableType::Num {
false
} else {
let val = v.get_value(req, ctx);
val.is_none() || val.as_ref().unwrap().val_to_string().is_empty()
}
} else {
false
}
} else {
false
}
},
CompareType::Eq => {
if let Ok(op) = variable::get(&self.ref_data) {
if let Some(v) = op {
if let Some(val) = v.get_value(req, ctx) {
val.val_to_string().eq(&self.get_target_data(req, ctx))
} else {
false
}
} else {
false
}
} else {
false
}
},
CompareType::NotEq => {
if let Ok(op) = variable::get(&self.ref_data) {
if let Some(v) = op {
if let Some(val) = v.get_value(req, ctx) {
!val.val_to_string().eq(&self.get_target_data(req, ctx))
} else {
true
}
} else {
true
}
} else {
true
}
},
CompareType::Contains => {
if let Ok(op) = variable::get(&self.ref_data) {
if let Some(v) = op {
if v.var_type == VariableType::Num {
false
} else {
if let Some(val) = v.get_value(req, ctx) {
val.val_to_string().find(&self.get_target_data(req, ctx)).is_some()
} else {
true
}
}
} else {
true
}
} else {
true
}
},
CompareType::NotContains => {
if let Ok(op) = variable::get(&self.ref_data) {
if let Some(v) = op {
if v.var_type == VariableType::Num {
false
} else {
if let Some(val) = v.get_value(req, ctx) {
val.val_to_string().find(&self.get_target_data(req, ctx)).is_none()
} else {
true
}
}
} else {
true
}
} else {
true
}
},
CompareType::NGT => {
if let Ok(op) = variable::get(&self.ref_data) {
if let Some(v) = op {
if v.var_type == VariableType::Str {
false
} else {
if let Some(val) = v.get_value(req, ctx) {
if let Ok(n1) = val.val_to_string().parse::<f64>() {
// println!("get_target_data {} {:?} |{}|", self.target_data, self.target_data_variant, self.get_target_data(req, ctx));
if let Ok(n2) = self.get_target_data(req, ctx).parse::<f64>() {
// println!("{} {}", n1, n2);
n1 > n2
} else {
false
}
} else {
false
}
} else {
false
}
}
} else {
false
}
} else {
false
}
},
CompareType::NGTE => {
if let Ok(op) = variable::get(&self.ref_data) {
if let Some(v) = op {
if v.var_type == VariableType::Str {
false
} else {
if let Some(val) = v.get_value(req, ctx) {
if let Ok(n1) = val.val_to_string().parse::<f64>() {
if let Ok(n2) = self.get_target_data(req, ctx).parse::<f64>() {
n1 >= n2
} else {
false
}
} else {
false
}
} else {
false
}
}
} else {
false
}
} else {
false
}
},
CompareType::NLT => {
if let Ok(op) = variable::get(&self.ref_data) {
if let Some(v) = op {
if v.var_type == VariableType::Str {
false
} else {
if let Some(val) = v.get_value(req, ctx) {
if let Ok(n1) = val.val_to_string().parse::<f64>() {
if let Ok(n2) = self.get_target_data(req, ctx).parse::<f64>() {
n1 < n2
} else {
false
}
} else {
false
}
} else {
false
}
}
} else {
false
}
} else {
false
}

},
CompareType::NLTE => {
if let Ok(op) = variable::get(&self.ref_data) {
if let Some(v) = op {
if v.var_type == VariableType::Str {
false
} else {
if let Some(val) = v.get_value(req, ctx) {
if let Ok(n1) = val.val_to_string().parse::<f64>() {
if let Ok(n2) = self.get_target_data(req, ctx).parse::<f64>() {
n1 <= n2
} else {
false
}
} else {
false
}
} else {
false
}
}
} else {
false
}
} else {
false
}

},
// let mut n = false;
// if let Ok(r) = variable::get(&self.ref_data) {
// if let Some(ref_v) = r {
// if let Some(val) = ref_v.get_value(req, ctx) {
// n = val.val_to_string().eq(&target_data);
// }
// }
// }
_ => false
}
ConditionType::CustomJavascript => todo!(),
ConditionType::CustomRegex => {
if let Ok(re) = Regex::new(&target_data) {
if let Ok(re) = Regex::new(&self.get_target_data(req, ctx)) {
return re.is_match(&req.user_input);
}
false
Expand Down
2 changes: 1 addition & 1 deletion src/flow/rt/convertor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ fn convert_node(main_flow_id: &str, node: &Node) -> Result<()> {
compare_type: cond.compare_type,
ref_data: cond.ref_choice.clone(),
target_data: cond.target_value.clone(),
target_data_variant: TargetDataVariant::Const,
target_data_variant: cond.target_value_variant,
};
and_conditions.push(c);
}
Expand Down
2 changes: 1 addition & 1 deletion src/flow/rt/facade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ pub(crate) async fn answer(Json(mut req): Json<Request>) -> impl IntoResponse {
let r = executor::process(&mut req);
// println!("exec used time:{:?}", now.elapsed());
let res = to_res(r);
log::info!("Total response used time:{:?}", now.elapsed());
log::info!("Response used time:{:?}", now.elapsed());
res
}
1 change: 1 addition & 0 deletions src/flow/rt/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ impl RuntimeNode for CollectNode {
fn exec(&self, req: &Request, ctx: &mut Context, response: &mut Response) -> bool {
// println!("Into CollectNode");
if let Some(r) = collector::collect(&req.user_input, &self.collect_type) {
// println!("{} {}", &self.var_name, r);
let v = VariableValue::new(r, &VariableType::Str);
ctx.vars.insert(self.var_name.clone(), v);
let collect_data = CollectData {
Expand Down
4 changes: 3 additions & 1 deletion src/flow/subflow/dto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use serde::{Deserialize, Serialize};
use serde_json::Value;

use crate::flow::rt::collector::CollectType;
use crate::flow::rt::condition::{CompareType, ConditionType};
use crate::flow::rt::condition::{CompareType, ConditionType, TargetDataVariant};
use crate::result::{Error, Result};

#[derive(Deserialize)]
Expand Down Expand Up @@ -243,6 +243,8 @@ pub(crate) struct BranchCondition {
pub(crate) compare_type: CompareType,
#[serde(rename = "targetValue")]
pub(crate) target_value: String,
#[serde(rename = "targetValueVariant")]
pub(crate) target_value_variant: TargetDataVariant,
}

#[derive(Deserialize)]
Expand Down
468 changes: 468 additions & 0 deletions src/resources/assets/assets/index-769b82be.js

Large diffs are not rendered by default.

468 changes: 0 additions & 468 deletions src/resources/assets/assets/index-cc9d124f.js

This file was deleted.

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion src/variable/crud.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,9 @@ pub(crate) fn get(name: &str) -> Result<Option<Variable>> {
pub(crate) fn get_value(name: &str, req: &Request, ctx: &mut Context) -> String {
if let Ok(r) = get(name) {
if let Some(v) = r {
v.get_value(req, ctx);
if let Some(val) = v.get_value(req, ctx) {
return val.val_to_string();
}
}
}
String::new()
Expand Down
5 changes: 3 additions & 2 deletions src/variable/dto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ impl Variable {
) -> Option<&'b VariableValue> {
match &self.var_val_source {
VariableValueSource::Collect | VariableValueSource::Import => {
// println!("{:?}", ctx.vars.get(&self.var_name));
ctx.vars.get(&self.var_name)
}
VariableValueSource::UserInput => {
Expand Down Expand Up @@ -195,7 +196,7 @@ impl Variable {
}
}

#[derive(Clone, Deserialize, Serialize, PartialEq)]
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
pub(crate) enum VariableValue {
Str(String),
Num(f64),
Expand Down Expand Up @@ -230,7 +231,7 @@ impl VariableValue {
}
}

#[derive(Clone, Deserialize, Serialize)]
#[derive(Clone, Deserialize, Serialize, PartialEq)]
pub(crate) enum VariableType {
Str,
Num,
Expand Down
Loading

0 comments on commit 248448b

Please sign in to comment.