Arranged operator related. 84/56584/1
authorSomin Kim <somin926.kim@samsung.com>
Mon, 11 Jan 2016 09:07:31 +0000 (18:07 +0900)
committerSomin Kim <somin926.kim@samsung.com>
Mon, 11 Jan 2016 09:07:31 +0000 (18:07 +0900)
- ==, != won't be converted to eq, neq
- operator validity check function added

Change-Id: I183127c71249a720e493b47bb04a668f4b0185de
Signed-off-by: Somin Kim <somin926.kim@samsung.com>
src/context_trigger.cpp
src/rule_validator.cpp
src/rule_validator.h

index 87df89d..ea61c6e 100644 (file)
@@ -38,7 +38,6 @@
 static std::string convert_event_to_string(context_trigger_event_e item);
 static std::string convert_condition_to_string(context_trigger_condition_e item);
 static std::string convert_logical_type_to_string(context_trigger_logical_type_e logical_type);
-static int convert_operator(std::string type, std::string op, std::string* converted_op);
 //static std::string int_to_string(int value);
 //static std::string double_to_string(int value);
 
@@ -794,18 +793,13 @@ EXTAPI int context_trigger_rule_entry_add_comparison(context_trigger_rule_entry_
        bool ret = ctx::rule_validator::check_valid_key(TYPE_ATTR, name, key);
        IF_FAIL_RETURN(ret, CONTEXT_TRIGGER_ERROR_INVALID_RULE);
 
+       // Err: Invalid operator
        std::string type = ctx::rule_validator::get_data_type(TYPE_ATTR, name, key);
+       ret = ctx::rule_validator::is_valid_operator(type, op);
+       IF_FAIL_RETURN(ret, CONTEXT_TRIGGER_ERROR_INVALID_RULE);
 
-       int error;
-       std::string converted_op;
-       error = convert_operator(type, op, &converted_op);      // Err: Invalid operator
-       if (error != ERR_NONE){
-               return error;
-       }
-
-       error = context_trigger_rule_entry_add_comparison_string_internal(entry, key, converted_op, EVENT_DATA_KEY_PREFIX_STR + std::string(event_data_key));
-       if (error != ERR_NONE)
-               return error;
+       int error = context_trigger_rule_entry_add_comparison_string_internal(entry, key, op, EVENT_DATA_KEY_PREFIX_STR + std::string(event_data_key));
+       IF_FAIL_RETURN(error == ERR_NONE, error);
 
        // Set reference information
        ret = ctx::rule_validator::set_ref_info(TYPE_ATTR, &(entry->jref), name, key, event_data_key);
@@ -819,37 +813,29 @@ EXTAPI int context_trigger_rule_entry_add_comparison_int(context_trigger_rule_en
        _D("BEGIN");
        ASSERT_NOT_NULL(entry && key && op);
 
-       int error;
-       std::string converted_op;
-
        std::string name;
        (entry->jentry).get(NULL, CT_RULE_EVENT_ITEM, &name);
 
        bool ret = ctx::rule_validator::check_comparison_int(name, key, op, value);
        IF_FAIL_RETURN(ret, CONTEXT_TRIGGER_ERROR_INVALID_RULE);
 
-       error = convert_operator(TYPE_INT_STR, op, &converted_op);      // Err: Invalid operator
-       if (error != ERR_NONE){
-               return error;
-       }
-
-       error = context_trigger_rule_entry_add_comparison_int_internal(entry, key, converted_op, value);
+       int error = context_trigger_rule_entry_add_comparison_int_internal(entry, key, op, value);
        return error;
 }
 /*
 EXTAPI int context_trigger_rule_entry_add_comparison_double(context_trigger_rule_entry_h entry, const char* key, const char* op, double value)
 {
+       _D("BEGIN");
        ASSERT_NOT_NULL(entry && key && op);
 
-       int error;
-       std::string converted_op;
+       std::string name;
+       (entry->jentry).get(NULL, CT_RULE_EVENT_ITEM, &name);
 
-       error = convert_operator("double", op, &converted_op);  // Err: Invalid operator
-       if (error != ERR_NONE){
-               return error;
-       }
+       // TODO: check_comparison_double()
+       bool ret = ctx::rule_validator::check_comparison_double(name, key, op, value);
+       IF_FAIL_RETURN(ret, CONTEXT_TRIGGER_ERROR_INVALID_RULE);
 
-       error = context_trigger_rule_entry_add_comparison_internal(entry, key, converted_op, double_to_string(value));
+       int error = context_trigger_rule_entry_add_comparison_internal(entry, key, op, double_to_string(value));
        return error;
 }
 */
@@ -858,21 +844,13 @@ EXTAPI int context_trigger_rule_entry_add_comparison_string(context_trigger_rule
        _D("BEGIN");
        ASSERT_NOT_NULL(entry && key && op && value);
 
-       int error;
-       std::string converted_op;
-
        std::string name;
        (entry->jentry).get(NULL, CT_RULE_EVENT_ITEM, &name);
 
        bool ret = ctx::rule_validator::check_comparison_string(name, key, op, value);
        IF_FAIL_RETURN(ret, CONTEXT_TRIGGER_ERROR_INVALID_RULE);
 
-       error = convert_operator(TYPE_STRING_STR, op, &converted_op);   // Err: Invalid operator
-       if (error != ERR_NONE){
-               return error;
-       }
-
-       error = context_trigger_rule_entry_add_comparison_string_internal(entry, key, converted_op, value);
+       int error = context_trigger_rule_entry_add_comparison_string_internal(entry, key, op, value);
        return error;
 }
 
@@ -999,25 +977,6 @@ std::string convert_logical_type_to_string(context_trigger_logical_type_e logica
        return str;
 }
 
-int convert_operator(std::string type, std::string op, std::string* converted_op)
-{
-       if (op.compare("==") == 0) {
-               *converted_op = "eq";
-               return CONTEXT_TRIGGER_ERROR_NONE;
-       } else if(op.compare("!=") == 0) {
-               *converted_op = "neq";
-               return CONTEXT_TRIGGER_ERROR_NONE;
-       }
-
-       if (type.compare(TYPE_INT_STR) == 0 || type.compare(TYPE_DOUBLE_STR) == 0) {
-               if (op.compare("<") == 0 || op.compare("<=") == 0 || op.compare(">") == 0 || op.compare(">=") == 0) {
-                       *converted_op = op;
-                       return CONTEXT_TRIGGER_ERROR_NONE;
-               }
-       }
-
-       return CONTEXT_TRIGGER_ERROR_INVALID_RULE;
-}
 /*
 std::string int_to_string(int value)
 {
index a94fa74..86b20e4 100644 (file)
@@ -216,6 +216,8 @@ bool ctx::rule_validator::check_comparison_int(std::string name, std::string key
        IF_FAIL_RETURN(ret, false);
 
        // Err: Invalid operator for the value
+       ret = is_valid_operator(TYPE_INT_STR, op);
+       IF_FAIL_RETURN(ret, false);
 
        // Err: Inappropriate value
        //    a. normal case
@@ -249,6 +251,8 @@ bool ctx::rule_validator::check_comparison_string(std::string name, std::string
        IF_FAIL_RETURN(ret, false);
 
        // Err: Invalid operator for the value
+       ret = is_valid_operator(TYPE_STRING_STR, op);
+       IF_FAIL_RETURN(ret, false);
 
        // Err: Inappropriate value
        //    a. normal case
@@ -407,3 +411,19 @@ bool ctx::rule_validator::check_referential_data(std::string name, ctx::json& re
 
        return true;
 }
+
+bool ctx::rule_validator::is_valid_operator(std::string type, std::string op)
+{
+       if (op == CONTEXT_TRIGGER_EQUAL_TO || op == CONTEXT_TRIGGER_NOT_EQUAL_TO) {
+               return true;
+       }
+
+       if (type == TYPE_INT_STR || type == TYPE_DOUBLE_STR) {
+               if (op == CONTEXT_TRIGGER_GREATER_THAN || op == CONTEXT_TRIGGER_GREATER_THAN_OR_EQUAL_TO ||
+                       op == CONTEXT_TRIGGER_LESS_THAN || op == CONTEXT_TRIGGER_LESS_THAN_OR_EQUAL_TO) {
+                       return true;
+               }
+       }
+
+       return false;
+}
index eb7a8cc..1ca63d5 100644 (file)
@@ -33,6 +33,7 @@ namespace ctx {
                bool set_ref_info(int type, ctx::json *jref, std::string name, std::string key, std::string ref_key);
                std::string get_data_type(int type, std::string name, std::string key);
                bool check_referential_data(std::string name, ctx::json &ref_info);
+               bool is_valid_operator(std::string type, std::string op);
 
        }
 }      /* namespace ctx */