trigger: set the lower/upper bound of int comparisons considering its logical type 87/142687/1
authorMu-Woong Lee <muwoong.lee@samsung.com>
Mon, 7 Aug 2017 04:43:07 +0000 (13:43 +0900)
committerMu-Woong Lee <muwoong.lee@samsung.com>
Mon, 7 Aug 2017 04:43:07 +0000 (13:43 +0900)
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 <muwoong.lee@samsung.com>
src/trigger/context_trigger.cpp

index eda825435ce7a9e4383c5da4b5842e1090eb17a7..ec6ee5a29dabd610c4d4ae28b581fdc969598c44 100644 (file)
@@ -17,6 +17,9 @@
 #include <string>
 #include <vector>
 #include <set>
+#include <map>
+#include <algorithm>
+
 #include <app_control_internal.h>
 #include <aul.h>
 #include <pkgmgr-info.h>
@@ -88,8 +91,32 @@ typedef struct _context_trigger_rule_entry_s {
        int type;
        std::string uri;
        ctx_sched_job_context_h jobContext;
-       std::set<std::string> conjunctionKeys;
-       std::set<std::string> 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<std::string, Bound> 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;