From cc14c6d8b204beb430d250d6c2a2d99db625d098 Mon Sep 17 00:00:00 2001 From: Mu-Woong Lee Date: Mon, 7 Aug 2017 13:43:07 +0900 Subject: [PATCH] trigger: set the lower/upper bound of int comparisons considering its logical type The current code is not exactly same to the previous version w.r.t. the handling of the attribute logical types, in cases of writing unrealistic comparisons. However, it would be OK with the currently supported attribute types. Change-Id: If2bf85bf0238f99a955f770e819248c0e51fb30c Signed-off-by: Mu-Woong Lee --- src/trigger/context_trigger.cpp | 70 +++++++++++++++++++++++++-------- 1 file changed, 53 insertions(+), 17 deletions(-) diff --git a/src/trigger/context_trigger.cpp b/src/trigger/context_trigger.cpp index eda8254..ec6ee5a 100644 --- a/src/trigger/context_trigger.cpp +++ b/src/trigger/context_trigger.cpp @@ -17,6 +17,9 @@ #include #include #include +#include +#include + #include #include #include @@ -88,8 +91,32 @@ typedef struct _context_trigger_rule_entry_s { int type; std::string uri; ctx_sched_job_context_h jobContext; - std::set conjunctionKeys; - std::set disjunctionKeys; + + struct Bound { + bool conjunction; + int gt; + int ge; + int lt; + int le; + + Bound(bool conj) : + conjunction(conj) + { + if (conj) { + gt = INT_MIN; + ge = INT_MIN; + lt = INT_MAX; + le = INT_MAX; + } else { + gt = INT_MAX; + ge = INT_MAX; + lt = INT_MIN; + le = INT_MIN; + } + } + }; + + std::map attributes; _context_trigger_rule_entry_s(int c, int t, const char* u) : category(c), type(t), uri(u), jobContext(NULL) @@ -749,8 +776,7 @@ EXPORT_API int context_trigger_rule_entry_add_option(context_trigger_rule_entry_ static bool __is_member_key(context_trigger_rule_entry_h entry, const char* key) { - if (entry->conjunctionKeys.find(key) != entry->conjunctionKeys.end() || - entry->disjunctionKeys.find(key) != entry->disjunctionKeys.end()) { + if (entry->attributes.find(key) != entry->attributes.end()) { _D("'%s' found", key); return true; } @@ -772,10 +798,7 @@ EXPORT_API int context_trigger_rule_entry_add_key(context_trigger_rule_entry_h e IF_FAIL_RETURN_TAG(valid, E_INV_RULE, _E, "Invalid parameter"); IF_FAIL_RETURN(!__is_member_key(entry, key), E_INV_RULE); - if (logical_type == CONTEXT_TRIGGER_LOGICAL_CONJUNCTION) - entry->conjunctionKeys.insert(key); - else - entry->disjunctionKeys.insert(key); + entry->attributes.emplace(key, logical_type == CONTEXT_TRIGGER_LOGICAL_CONJUNCTION); return E_NONE; } @@ -822,18 +845,31 @@ EXPORT_API int context_trigger_rule_entry_add_comparison_int(context_trigger_rul if (__getOpType(op) == OpType::NE) return ctx_sched_job_context_attribute_add_ne_int(entry->jobContext, key, value); - //TODO: Consider logical disjunction - if (__getOpType(op) == OpType::GT) - return ctx_sched_job_context_attribute_set_gt_int(entry->jobContext, key, value); + _context_trigger_rule_entry_s::Bound& bound = entry->attributes.find(key)->second; + + if (__getOpType(op) == OpType::GT) { + if (bound.conjunction) bound.gt = std::max(bound.gt, value); + else bound.gt = std::min(bound.gt, value); + return ctx_sched_job_context_attribute_set_gt_int(entry->jobContext, key, bound.gt); + } - if (__getOpType(op) == OpType::GE) - return ctx_sched_job_context_attribute_set_ge_int(entry->jobContext, key, value); + if (__getOpType(op) == OpType::GE) { + if (bound.conjunction) bound.ge = std::max(bound.ge, value); + else bound.ge = std::min(bound.ge, value); + return ctx_sched_job_context_attribute_set_ge_int(entry->jobContext, key, bound.ge); + } - if (__getOpType(op) == OpType::LT) - return ctx_sched_job_context_attribute_set_lt_int(entry->jobContext, key, value); + if (__getOpType(op) == OpType::LT) { + if (bound.conjunction) bound.lt = std::min(bound.lt, value); + else bound.lt = std::max(bound.lt, value); + return ctx_sched_job_context_attribute_set_lt_int(entry->jobContext, key, bound.lt); + } - if (__getOpType(op) == OpType::LE) - return ctx_sched_job_context_attribute_set_le_int(entry->jobContext, key, value); + if (__getOpType(op) == OpType::LE) { + if (bound.conjunction) bound.le = std::min(bound.le, value); + else bound.le = std::max(bound.le, value); + return ctx_sched_job_context_attribute_set_le_int(entry->jobContext, key, bound.le); + } _E("Invalid operator"); return E_INV_RULE; -- 2.34.1