| 1 |
# THIS FILE IS PART OF THE CYLC WORKFLOW ENGINE. |
| 2 |
# Copyright (C) NIWA & British Crown (Met Office) & Contributors. |
| 3 |
# |
| 4 |
# This program is free software: you can redistribute it and/or modify |
| 5 |
# it under the terms of the GNU General Public License as published by |
| 6 |
# the Free Software Foundation, either version 3 of the License, or |
| 7 |
# (at your option) any later version. |
| 8 |
# |
| 9 |
# This program is distributed in the hope that it will be useful, |
| 10 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
# GNU General Public License for more details. |
| 13 |
# |
| 14 |
# You should have received a copy of the GNU General Public License |
| 15 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 |
|
| 17 |
from typing import ( |
| 18 |
TYPE_CHECKING, |
| 19 |
Optional, |
| 20 |
Tuple, |
| 21 |
) |
| 22 |
|
| 23 |
from cylc.flow.cycling.loader import ( |
| 24 |
get_interval, |
| 25 |
get_point, |
| 26 |
get_point_relative, |
| 27 |
) |
| 28 |
from cylc.flow.prerequisite import Prerequisite |
| 29 |
from cylc.flow.task_qualifiers import ALT_QUALIFIERS |
| 30 |
|
| 31 |
|
| 32 |
if TYPE_CHECKING: |
| 33 |
from cylc.flow.cycling import PointBase |
| 34 |
from cylc.flow.taskdef import TaskDef |
| 35 |
|
| 36 |
|
| 37 |
# Task trigger names (e.g. foo:fail => bar). |
| 38 |
# Can use "foo:fail => bar" or "foo:failed => bar", etc. |
| 39 |
|
| 40 |
|
| 41 |
class TaskTrigger: |
| 42 |
"""Class representing an upstream dependency. |
| 43 |
|
| 44 |
Args: |
| 45 |
task_name: The name of the upstream task. |
| 46 |
cycle_point_offset: String representing the offset of the |
| 47 |
upstream task (e.g. -P1D) if this dependency is not an absolute |
| 48 |
one. Else None. |
| 49 |
output: The task state / output for this trigger e.g. succeeded. |
| 50 |
|
| 51 |
""" |
| 52 |
|
| 53 |
__slots__ = ['task_name', 'cycle_point_offset', 'output', |
| 54 |
'offset_is_irregular', 'offset_is_absolute', |
| 55 |
'offset_is_from_icp', 'initial_point'] |
| 56 |
|
| 57 |
def __init__( |
| 58 |
self, |
| 59 |
task_name: str, |
| 60 |
cycle_point_offset: Optional[str], |
| 61 |
output: str, |
| 62 |
offset_is_irregular: bool = False, |
| 63 |
offset_is_absolute: bool = False, |
| 64 |
offset_is_from_icp: bool = False, |
| 65 |
initial_point: 'Optional[PointBase]' = None, |
| 66 |
): |
| 67 |
self.task_name = task_name |
| 68 |
self.cycle_point_offset = cycle_point_offset |
| 69 |
self.output = output |
| 70 |
self.offset_is_irregular = offset_is_irregular |
| 71 |
self.offset_is_from_icp = offset_is_from_icp |
| 72 |
self.offset_is_absolute = offset_is_absolute |
| 73 |
self.initial_point = initial_point |
| 74 |
# NEED TO DISTINGUISH BETWEEN ABSOLUTE OFFSETS |
| 75 |
# 2000, 20000101T0600Z, 2000-01-01T06:00+00:00, ... |
| 76 |
# AND NON-ABSOLUTE IRREGULAR: |
| 77 |
# -PT6H+P1D, T00, ... |
| 78 |
if self.offset_is_irregular and any( |
| 79 |
self.cycle_point_offset.startswith(c) # type: ignore[union-attr] |
| 80 |
for c in ['P', '+', '-', 'T'] |
| 81 |
): |
| 82 |
self.offset_is_absolute = False |
| 83 |
|
| 84 |
def get_parent_point(self, from_point): |
| 85 |
"""Return the specific parent point of this trigger. |
| 86 |
|
| 87 |
Args: |
| 88 |
from_point (cylc.flow.cycling.PointBase): parent task point. |
| 89 |
|
| 90 |
Returns: |
| 91 |
cylc.flow.cycling.PointBase: cycle point of the child. |
| 92 |
|
| 93 |
""" |
| 94 |
if self.cycle_point_offset is None: |
| 95 |
point = from_point |
| 96 |
elif self.offset_is_absolute: |
| 97 |
point = get_point(self.cycle_point_offset).standardise() |
| 98 |
else: |
| 99 |
if self.offset_is_from_icp: |
| 100 |
from_point = self.initial_point |
| 101 |
# works with offset_is_irregular or not: |
| 102 |
point = get_point_relative(self.cycle_point_offset, from_point) |
| 103 |
return point |
| 104 |
|
| 105 |
def get_child_point(self, from_point, seq): |
| 106 |
"""Return the specific child task point of this trigger. |
| 107 |
|
| 108 |
Args: |
| 109 |
from_point (cylc.flow.cycling.PointBase): base point. |
| 110 |
seq: the cycling sequence to find the child point. |
| 111 |
|
| 112 |
Returns: |
| 113 |
cylc.flow.cycling.PointBase: cycle point of the child. |
| 114 |
|
| 115 |
""" |
| 116 |
if self.cycle_point_offset is None: |
| 117 |
point = from_point |
| 118 |
elif self.offset_is_absolute or self.offset_is_from_icp: |
| 119 |
# First child is at start of sequence. |
| 120 |
# E.g. for "R/1/P1 = foo[2] => bar" |
| 121 |
# foo.2 should spawn bar.1; then we auto-spawn bar.2,3,... |
| 122 |
point = seq.get_start_point() |
| 123 |
elif self.offset_is_irregular: |
| 124 |
# Change offset sign to find children |
| 125 |
# e.g. -P1D+PT18H to +P1D-PT18H |
| 126 |
point = get_point_relative( |
| 127 |
self.cycle_point_offset.translate( |
| 128 |
self.cycle_point_offset.maketrans('-+', '+-')), from_point) |
| 129 |
else: |
| 130 |
point = from_point - get_interval(self.cycle_point_offset) |
| 131 |
return point |
| 132 |
|
| 133 |
def get_point(self, point): |
| 134 |
"""Return the point of the output to which this TaskTrigger pertains. |
| 135 |
|
| 136 |
Args: |
| 137 |
point (cylc.flow.cycling.PointBase): cycle point of dependent task. |
| 138 |
|
| 139 |
Returns: |
| 140 |
cylc.flow.cycling.PointBase: cycle point of the dependency. |
| 141 |
|
| 142 |
""" |
| 143 |
if self.offset_is_absolute: |
| 144 |
point = get_point(self.cycle_point_offset).standardise() |
| 145 |
elif self.offset_is_from_icp: |
| 146 |
point = get_point_relative( |
| 147 |
self.cycle_point_offset, self.initial_point) |
| 148 |
elif self.cycle_point_offset: |
| 149 |
point = get_point_relative(self.cycle_point_offset, point) |
| 150 |
return point |
| 151 |
|
| 152 |
def __str__(self): |
| 153 |
if not self.offset_is_irregular and self.offset_is_absolute: |
| 154 |
point = get_point(self.cycle_point_offset).standardise() |
| 155 |
return '%s[%s]:%s' % (self.task_name, point, self.output) |
| 156 |
elif self.cycle_point_offset: |
| 157 |
return '%s[%s]:%s' % (self.task_name, self.cycle_point_offset, |
| 158 |
self.output) |
| 159 |
else: |
| 160 |
return '%s:%s' % (self.task_name, self.output) |
| 161 |
|
| 162 |
def __repr__(self) -> str: |
| 163 |
""" |
| 164 |
>>> TaskTrigger('', '', '') |
| 165 |
<TaskTrigger ...> |
| 166 |
""" |
| 167 |
return f"<{type(self).__name__} {self}>" |
| 168 |
|
| 169 |
def __hash__(self) -> int: |
| 170 |
return hash(( |
| 171 |
self.task_name, |
| 172 |
self.cycle_point_offset, |
| 173 |
self.output, |
| 174 |
self.offset_is_irregular, |
| 175 |
self.offset_is_from_icp, |
| 176 |
self.offset_is_absolute, |
| 177 |
self.initial_point, |
| 178 |
)) |
| 179 |
|
| 180 |
def __eq__(self, other: object) -> bool: |
| 181 |
if not isinstance(other, TaskTrigger): |
| 182 |
return NotImplemented |
| 183 |
return hash(self) == hash(other) |
| 184 |
|
| 185 |
@staticmethod |
| 186 |
def standardise_name(name: str) -> str: |
| 187 |
"""Replace trigger name aliases with standard names. |
| 188 |
|
| 189 |
Arg name should be a valid standard name or alias, otherwise assumed |
| 190 |
to be a custom trigger and return as-is. |
| 191 |
|
| 192 |
Examples: |
| 193 |
>>> TaskTrigger.standardise_name('foo') |
| 194 |
'foo' |
| 195 |
>>> TaskTrigger.standardise_name('succeed') |
| 196 |
'succeeded' |
| 197 |
>>> TaskTrigger.standardise_name('succeeded') |
| 198 |
'succeeded' |
| 199 |
|
| 200 |
""" |
| 201 |
return ALT_QUALIFIERS.get(name, name) |
| 202 |
|
| 203 |
|
| 204 |
class Dependency: |
| 205 |
"""A graph dependency in its abstract form. |
| 206 |
|
| 207 |
Used to generate cylc.flow.prerequisite.Prerequisite objects. |
| 208 |
|
| 209 |
Args: |
| 210 |
exp (list): A (nested) list of TaskTrigger objects and conditional |
| 211 |
characters representing the dependency. E.G: "foo & bar" would be |
| 212 |
[<TaskTrigger("foo")>, "&", <TaskTrigger("Bar")>]. |
| 213 |
task_triggers (set): A set of TaskTrigger objects contained in the |
| 214 |
expression (exp). |
| 215 |
suicide (bool): True if this is a suicide trigger else False. |
| 216 |
|
| 217 |
""" |
| 218 |
|
| 219 |
__slots__ = ['_exp', 'task_triggers', 'suicide'] |
| 220 |
|
| 221 |
def __init__(self, exp, task_triggers, suicide): |
| 222 |
self._exp = exp |
| 223 |
self.task_triggers: Tuple[ |
| 224 |
TaskTrigger |
| 225 |
] = tuple(task_triggers) # More memory efficient. |
| 226 |
self.suicide = suicide |
| 227 |
|
| 228 |
def get_prerequisite( |
| 229 |
self, point: 'PointBase', tdef: 'TaskDef' |
| 230 |
) -> Prerequisite: |
| 231 |
"""Generate a Prerequisite object from this dependency. |
| 232 |
|
| 233 |
Args: |
| 234 |
point: The cycle point at which to generate the Prerequisite for. |
| 235 |
tdef: The TaskDef of the dependent task. |
| 236 |
|
| 237 |
""" |
| 238 |
cpre = Prerequisite(point) |
| 239 |
|
| 240 |
# Loop over TaskTrigger instances. |
| 241 |
for task_trigger in self.task_triggers: |
| 242 |
key = ( |
| 243 |
task_trigger.get_point(point), |
| 244 |
task_trigger.task_name, |
| 245 |
task_trigger.output, |
| 246 |
) |
| 247 |
if task_trigger.cycle_point_offset is not None: |
| 248 |
# Compute trigger cycle point from offset. |
| 249 |
if task_trigger.offset_is_from_icp: |
| 250 |
prereq_offset_point = get_point_relative( |
| 251 |
task_trigger.cycle_point_offset, tdef.initial_point) |
| 252 |
else: |
| 253 |
prereq_offset_point = get_point_relative( |
| 254 |
task_trigger.cycle_point_offset, point) |
| 255 |
if prereq_offset_point < tdef.initial_point: |
| 256 |
# Pre-initial dependency - treat as satisfied |
| 257 |
cpre[key] = True |
| 258 |
continue |
| 259 |
if prereq_offset_point > point: |
| 260 |
# Update tdef.max_future_prereq_offset. |
| 261 |
prereq_offset = prereq_offset_point - point |
| 262 |
if ( |
| 263 |
tdef.max_future_prereq_offset is None |
| 264 |
or prereq_offset > tdef.max_future_prereq_offset |
| 265 |
): |
| 266 |
tdef.max_future_prereq_offset = prereq_offset |
| 267 |
cpre[key] = ( |
| 268 |
prereq_offset_point < tdef.start_point |
| 269 |
and point >= tdef.start_point |
| 270 |
) |
| 271 |
else: |
| 272 |
# Trigger is within the same cycle point. |
| 273 |
# Register task message with Prerequisite object. |
| 274 |
cpre[key] = False |
| 275 |
cpre.set_conditional_expr(self.get_expression(point)) |
| 276 |
return cpre |
| 277 |
|
| 278 |
def get_expression(self, point): |
| 279 |
"""Return the expression as a string. |
| 280 |
|
| 281 |
Args: |
| 282 |
point (cylc.flow.cycling.PointBase): The cycle point at which to |
| 283 |
generate the expression string for. |
| 284 |
|
| 285 |
Return: |
| 286 |
string: The expression as a parsable string in the cylc graph |
| 287 |
format. |
| 288 |
|
| 289 |
""" |
| 290 |
return ''.join(self._stringify_list(self._exp, point)) |
| 291 |
|
| 292 |
def __str__(self): |
| 293 |
ret = [] |
| 294 |
if self.suicide: |
| 295 |
ret.append('!') |
| 296 |
for item in self._exp: |
| 297 |
if isinstance(item, list): |
| 298 |
ret.append(str(item)) |
| 299 |
else: |
| 300 |
ret.append('( %s )' % str(item)) |
| 301 |
return ' '.join(ret) |
| 302 |
|
| 303 |
def __repr__(self) -> str: |
| 304 |
""" |
| 305 |
>>> from unittest.mock import Mock |
| 306 |
>>> Dependency(exp=[Mock()], task_triggers=[Mock()], suicide=False) |
| 307 |
<Dependency ...> |
| 308 |
""" |
| 309 |
return f"<{type(self).__name__} {self}>" |
| 310 |
|
| 311 |
@classmethod |
| 312 |
def _stringify_list(cls, nested_expr, point): |
| 313 |
"""Stringify a nested list of TaskTrigger objects.""" |
| 314 |
ret = [] |
| 315 |
for item in nested_expr: |
| 316 |
if isinstance(item, TaskTrigger): |
| 317 |
ret.append( |
| 318 |
Prerequisite.MESSAGE_TEMPLATE % ( |
| 319 |
item.get_point(point), |
| 320 |
item.task_name, |
| 321 |
item.output, |
| 322 |
) |
| 323 |
) |
| 324 |
elif isinstance(item, list): |
| 325 |
ret.extend(['('] + cls._stringify_list(item, point) + [')']) |
| 326 |
else: |
| 327 |
ret.append(item) |
| 328 |
return ret |