File size: 11,111 Bytes
4021124 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 | # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
# http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.
"""Conditions for condition steps.
Ideally, some of these comparison conditions would be implemented as "partial classes",
but use of functools.partial doesn't set correct metadata/type information.
"""
from __future__ import absolute_import
import abc
from enum import Enum
from typing import Dict, List, Union
import attr
from sagemaker.workflow import is_pipeline_variable
from sagemaker.workflow.entities import (
DefaultEnumMeta,
Entity,
Expression,
PrimitiveType,
RequestType,
)
from sagemaker.workflow.execution_variables import ExecutionVariable
from sagemaker.workflow.parameters import Parameter
from sagemaker.workflow.properties import Properties
from sagemaker.workflow.entities import PipelineVariable
# TODO: consider base class for those with an expr method, rather than defining a type here
ConditionValueType = Union[ExecutionVariable, Parameter, Properties]
class ConditionTypeEnum(Enum, metaclass=DefaultEnumMeta):
"""Condition type enum."""
EQ = "Equals"
GT = "GreaterThan"
GTE = "GreaterThanOrEqualTo"
IN = "In"
LT = "LessThan"
LTE = "LessThanOrEqualTo"
NOT = "Not"
OR = "Or"
@attr.s
class Condition(Entity):
"""Abstract Condition entity.
Attributes:
condition_type (ConditionTypeEnum): The type of condition.
"""
condition_type: ConditionTypeEnum = attr.ib(factory=ConditionTypeEnum.factory)
@property
@abc.abstractmethod
def _referenced_steps(self) -> List[str]:
"""List of step names that this function depends on."""
@attr.s
class ConditionComparison(Condition):
"""Generic comparison condition that can be used to derive specific condition comparisons.
Attributes:
left (Union[ConditionValueType, PrimitiveType]): The execution variable, parameter,
property, or Python primitive value to use in the comparison.
right (Union[ConditionValueType, PrimitiveType]): The execution variable,
parameter, property, or Python primitive value to compare to.
"""
left: Union[ConditionValueType, PrimitiveType] = attr.ib(default=None)
right: Union[ConditionValueType, PrimitiveType] = attr.ib(default=None)
def to_request(self) -> RequestType:
"""Get the request structure for workflow service calls."""
return {
"Type": self.condition_type.value,
"LeftValue": self.left,
"RightValue": self.right,
}
@property
def _referenced_steps(self) -> List[str]:
"""List of step names that this function depends on."""
steps = []
if isinstance(self.left, PipelineVariable):
steps.extend(self.left._referenced_steps)
if isinstance(self.right, PipelineVariable):
steps.extend(self.right._referenced_steps)
return steps
class ConditionEquals(ConditionComparison):
"""A condition for equality comparisons."""
def __init__(
self,
left: Union[ConditionValueType, PrimitiveType],
right: Union[ConditionValueType, PrimitiveType],
):
"""Construct A condition for equality comparisons.
Args:
left (Union[ConditionValueType, PrimitiveType]): The execution variable,
parameter, property, or Python primitive value to use in the comparison.
right (Union[ConditionValueType, PrimitiveType]): The execution
variable, parameter, property, or Python primitive value to compare to.
"""
super(ConditionEquals, self).__init__(ConditionTypeEnum.EQ, left, right)
class ConditionGreaterThan(ConditionComparison):
"""A condition for greater than comparisons."""
def __init__(
self,
left: Union[ConditionValueType, PrimitiveType],
right: Union[ConditionValueType, PrimitiveType],
):
"""Construct an instance of ConditionGreaterThan for greater than comparisons.
Args:
left (Union[ConditionValueType, PrimitiveType]): The execution variable,
parameter, property, or Python primitive value to use in the comparison.
right (Union[ConditionValueType, PrimitiveType]): The execution
variable, parameter, property, or Python primitive value to compare to.
"""
super(ConditionGreaterThan, self).__init__(ConditionTypeEnum.GT, left, right)
class ConditionGreaterThanOrEqualTo(ConditionComparison):
"""A condition for greater than or equal to comparisons."""
def __init__(
self,
left: Union[ConditionValueType, PrimitiveType],
right: Union[ConditionValueType, PrimitiveType],
):
"""Construct of ConditionGreaterThanOrEqualTo for greater than or equal to comparisons.
Args:
left (Union[ConditionValueType, PrimitiveType]): The execution variable,
parameter, property, or Python primitive value to use in the comparison.
right (Union[ConditionValueType, PrimitiveType]): The execution
variable, parameter, property, or Python primitive value to compare to.
"""
super(ConditionGreaterThanOrEqualTo, self).__init__(ConditionTypeEnum.GTE, left, right)
class ConditionLessThan(ConditionComparison):
"""A condition for less than comparisons."""
def __init__(
self,
left: Union[ConditionValueType, PrimitiveType],
right: Union[ConditionValueType, PrimitiveType],
):
"""Construct an instance of ConditionLessThan for less than comparisons.
Args:
left (Union[ConditionValueType, PrimitiveType]): The execution variable,
parameter, property, or Python primitive value to use in the comparison.
right (Union[ConditionValueType, PrimitiveType]): The execution
variable, parameter, property, or Python primitive value to compare to.
"""
super(ConditionLessThan, self).__init__(ConditionTypeEnum.LT, left, right)
class ConditionLessThanOrEqualTo(ConditionComparison):
"""A condition for less than or equal to comparisons."""
def __init__(
self,
left: Union[ConditionValueType, PrimitiveType],
right: Union[ConditionValueType, PrimitiveType],
):
"""Construct ConditionLessThanOrEqualTo for less than or equal to comparisons.
Args:
left (Union[ConditionValueType, PrimitiveType]): The execution variable,
parameter, property, or Python primitive value to use in the comparison.
right (Union[ConditionValueType, PrimitiveType]): The execution
variable, parameter, property, or Python primitive value to compare to.
"""
super(ConditionLessThanOrEqualTo, self).__init__(ConditionTypeEnum.LTE, left, right)
class ConditionIn(Condition):
"""A condition to check membership."""
def __init__(
self,
value: Union[ConditionValueType, PrimitiveType],
in_values: List[Union[ConditionValueType, PrimitiveType]],
):
"""Construct a `ConditionIn` condition to check membership.
Args:
value (Union[ConditionValueType, PrimitiveType]): The execution variable,
parameter, property or primitive value to check for membership.
in_values (List[Union[ConditionValueType, PrimitiveType]]): The list
of values to check for membership in.
"""
super(ConditionIn, self).__init__(ConditionTypeEnum.IN)
self.value = value
self.in_values = in_values
def to_request(self) -> RequestType:
"""Get the request structure for workflow service calls."""
return {
"Type": self.condition_type.value,
"QueryValue": self.value,
"Values": self.in_values,
}
@property
def _referenced_steps(self) -> List[str]:
"""List of step names that this function depends on."""
steps = []
if isinstance(self.value, PipelineVariable):
steps.extend(self.value._referenced_steps)
for in_value in self.in_values:
if isinstance(in_value, PipelineVariable):
steps.extend(in_value._referenced_steps)
return steps
class ConditionNot(Condition):
"""A condition for negating another `Condition`."""
def __init__(self, expression: Condition):
"""Construct a `ConditionNot` condition for negating another `Condition`.
Attributes:
expression (Condition): A `Condition` to take the negation of.
"""
super(ConditionNot, self).__init__(ConditionTypeEnum.NOT)
self.expression = expression
def to_request(self) -> RequestType:
"""Get the request structure for workflow service calls."""
return {"Type": self.condition_type.value, "Expression": self.expression.to_request()}
@property
def _referenced_steps(self) -> List[str]:
"""List of step names that this function depends on."""
return self.expression._referenced_steps
class ConditionOr(Condition):
"""A condition for taking the logical OR of a list of `Condition` instances."""
def __init__(self, conditions: List[Condition] = None):
"""Construct a `ConditionOr` condition.
Attributes:
conditions (List[Condition]): A list of `Condition` instances to logically OR.
"""
super(ConditionOr, self).__init__(ConditionTypeEnum.OR)
self.conditions = conditions or []
def to_request(self) -> RequestType:
"""Get the request structure for workflow service calls."""
return {
"Type": self.condition_type.value,
"Conditions": [condition.to_request() for condition in self.conditions],
}
@property
def _referenced_steps(self) -> List[str]:
"""List of step names that this function depends on."""
steps = []
for condition in self.conditions:
steps.extend(condition._referenced_steps)
return steps
def primitive_or_expr(
value: Union[ExecutionVariable, Expression, PrimitiveType, Parameter, Properties]
) -> Union[Dict[str, str], PrimitiveType]:
"""Provide the expression of the value or return value if it is a primitive.
Args:
value (Union[ConditionValueType, PrimitiveType]): The value to evaluate.
Returns:
Either the expression of the value or the primitive value.
"""
if is_pipeline_variable(value):
return value.expr
return value
|