Add sufficiency checking operators to the JobContext and Attribute classes 20/140320/1
authorMu-Woong Lee <muwoong.lee@samsung.com>
Mon, 24 Jul 2017 11:56:48 +0000 (20:56 +0900)
committerMu-Woong Lee <muwoong.lee@samsung.com>
Mon, 24 Jul 2017 11:56:48 +0000 (20:56 +0900)
The operator <= is used to define sufficiency checkers
as "S => N" ("N <= S" in reverse) usually denotes "if S, then N".

Change-Id: I26cefe67a69d569373b3ceb5bd9328c14c336ac2
Signed-off-by: Mu-Woong Lee <muwoong.lee@samsung.com>
src/shared/Attribute.cpp
src/shared/Attribute.h
src/shared/JobContext.cpp
src/shared/JobContext.h

index d5e1543a1b9809adb19d95d45aad6a46bdb9be06..aafc6b6e8cc6daa8b7d1049e8066f4d477e34b4f 100644 (file)
@@ -147,6 +147,28 @@ void IntegerAttribute::toJson(Json::Value& jsonNode) const
        }
 }
 
+bool IntegerAttribute::operator<=(const int& antecedent)
+{
+       if (antecedent < __closedLowerBound || antecedent > __closedUpperBound)
+               return false;
+
+       if (antecedent <= __openLowerBound || antecedent >= __openUpperBound)
+               return false;
+
+       if (!__targets.empty() && __targets.find(antecedent) == __targets.end())
+               return false;
+
+       if (!__nonTargets.empty() && __nonTargets.find(antecedent) != __targets.end())
+               return false;
+
+       return true;
+}
+
+bool IntegerAttribute::operator<=(const std::string& antecedent)
+{
+       return false;
+}
+
 
 StringAttribute::StringAttribute(const std::string& name) :
        Attribute(name)
@@ -198,3 +220,19 @@ void StringAttribute::toJson(Json::Value& jsonNode) const
                jsonNode[KEY_NON_TARGET].append(nonTarget);
        }
 }
+
+bool StringAttribute::operator<=(const int& antecedent)
+{
+       return false;
+}
+
+bool StringAttribute::operator<=(const std::string& antecedent)
+{
+       if (!__targets.empty() && __targets.find(antecedent) == __targets.end())
+               return false;
+
+       if (!__nonTargets.empty() && __nonTargets.find(antecedent) != __targets.end())
+               return false;
+
+       return true;
+}
index 4647dae71677471d458e265b061508fdb1608ae4..e774b3a09c4f4fca944d5b105fe1cbf3678bec56 100644 (file)
@@ -36,10 +36,13 @@ namespace ctx {
 
                virtual ~Attribute();
 
+               const std::string& getName() const;
+
                virtual Attribute::Type getType() const = 0;
                virtual void toJson(Json::Value& jsonNode) const = 0;
 
-               const std::string& getName() const;
+               virtual bool operator<=(const int& antecedent) = 0;
+               virtual bool operator<=(const std::string& antecedent) = 0;
 
                static Attribute* build(const std::string& name, Json::Value& jsonNode);
 
@@ -68,6 +71,9 @@ namespace ctx {
 
                void toJson(Json::Value& jsonNode) const;
 
+               bool operator<=(const int& antecedent);
+               bool operator<=(const std::string& antecedent);
+
        private:
                int __closedLowerBound;
                int __closedUpperBound;
@@ -91,6 +97,9 @@ namespace ctx {
 
                void toJson(Json::Value& jsonNode) const;
 
+               bool operator<=(const int& antecedent);
+               bool operator<=(const std::string& antecedent);
+
        private:
                std::set<std::string> __targets;
                std::set<std::string> __nonTargets;
index 5e21ee2c9e5d83a2ff227d6f354f186c50a394e9..39a26c8823fba1ed7a715c4632f9ef97c0c7e937 100644 (file)
@@ -99,6 +99,41 @@ void JobContext::toJson(Json::Value& jsonNode) const
        jsonNode[KEY_DISJUNCTION] = __disjunction;
 }
 
+bool JobContext::operator<=(const Json::Value& antecedent)
+{
+       auto isSatisfied = [&](Attribute*& attr)->bool {
+               const char* name = attr->getName().c_str();
+               const Json::Value* value = antecedent.find(name, name + strlen(name));
+
+               if (!value)
+                       return false;
+
+               if (value->isInt())
+                       return (*attr) <= value->asInt();
+
+               if (value->isString())
+                       return (*attr) <= value->asString();
+
+               return false;
+       };
+
+       if (isDisjunction()) {  /* Legacy support */
+               for (auto& attr : __attributes) {
+                       if (isSatisfied(attr))
+                               return true;
+               }
+
+               return __attributes.empty();
+       }
+
+       for (auto& attr : __attributes) {
+               if (!isSatisfied(attr))
+                       return false;
+       }
+
+       return true;
+}
+
 bool JobContext::isDisjunction() const
 {
        return __disjunction;
index ed8864513e4a593df095bd9918aa224f2d5386f6..94986b91474faca8a2f6176c848ecaaa902c1cca 100644 (file)
@@ -48,6 +48,8 @@ namespace ctx {
 
                void toJson(Json::Value& jsonNode) const;
 
+               bool operator<=(const Json::Value& antecedent);
+
                /* Legacy support */
                bool isDisjunction() const;
                JobContext& setDisjunction(bool disjunction);