From c176ec7041a37cd53979dc00632e1dded2b7510a Mon Sep 17 00:00:00 2001 From: Mu-Woong Lee Date: Mon, 24 Jul 2017 20:56:48 +0900 Subject: [PATCH] Add sufficiency checking operators to the JobContext and Attribute classes 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 --- src/shared/Attribute.cpp | 38 ++++++++++++++++++++++++++++++++++++++ src/shared/Attribute.h | 11 ++++++++++- src/shared/JobContext.cpp | 35 +++++++++++++++++++++++++++++++++++ src/shared/JobContext.h | 2 ++ 4 files changed, 85 insertions(+), 1 deletion(-) diff --git a/src/shared/Attribute.cpp b/src/shared/Attribute.cpp index d5e1543..aafc6b6 100644 --- a/src/shared/Attribute.cpp +++ b/src/shared/Attribute.cpp @@ -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; +} diff --git a/src/shared/Attribute.h b/src/shared/Attribute.h index 4647dae..e774b3a 100644 --- a/src/shared/Attribute.h +++ b/src/shared/Attribute.h @@ -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 __targets; std::set __nonTargets; diff --git a/src/shared/JobContext.cpp b/src/shared/JobContext.cpp index 5e21ee2..39a26c8 100644 --- a/src/shared/JobContext.cpp +++ b/src/shared/JobContext.cpp @@ -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; diff --git a/src/shared/JobContext.h b/src/shared/JobContext.h index ed88645..94986b9 100644 --- a/src/shared/JobContext.h +++ b/src/shared/JobContext.h @@ -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); -- 2.7.4