| 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 |
Integer cycling by point, interval, and sequence classes. |
| 18 |
""" |
| 19 |
|
| 20 |
import contextlib |
| 21 |
import re |
| 22 |
|
| 23 |
from cylc.flow.cycling import ( |
| 24 |
PointBase, IntervalBase, SequenceBase, ExclusionBase, parse_exclusion, cmp |
| 25 |
) |
| 26 |
from cylc.flow.exceptions import ( |
| 27 |
CylcMissingContextPointError, |
| 28 |
IntervalParsingError, |
| 29 |
PointParsingError, |
| 30 |
SequenceParsingError, |
| 31 |
) |
| 32 |
|
| 33 |
CYCLER_TYPE_INTEGER = "integer" |
| 34 |
CYCLER_TYPE_SORT_KEY_INTEGER = 0 |
| 35 |
|
| 36 |
# TODO - abbreviated integer recurrences? |
| 37 |
|
| 38 |
# INTEGER RECURRENCE REGEXES |
| 39 |
# |
| 40 |
# Intended to be integer analogues of the ISO8601 date time notation. |
| 41 |
# |
| 42 |
# Unlike ISO8601 time points there is no supported equivalent of a |
| 43 |
# truncated point, so e.g. 'T00' has no direct analogue. Instead, we |
| 44 |
# can use absolute integer points such as "5" or "10". We also can't |
| 45 |
# extrapolate intervals from the date-time truncation information - |
| 46 |
# e.g. assuming 'T00/P1D' from 'T00'. |
| 47 |
# |
| 48 |
# We can also use relative point notation in a similar way to the |
| 49 |
# date-time offset notation. For example, we can write "5 after the |
| 50 |
# initial cycle point" as '+P5'. |
| 51 |
# |
| 52 |
# In the following regular expression comments: |
| 53 |
# START and END: either absolute integers such as '1' or '5', or |
| 54 |
# initial-relative (start) or final-relative (end) offsets |
| 55 |
# such as '+P2' or '-P5'. |
| 56 |
# INITIAL and FINAL: the initial cycle point and final cycle point. |
| 57 |
# INTV: an integer interval such as 'P2'. |
| 58 |
# n: an integer denoting the number of repetitions. |
| 59 |
# format_num meanings: |
| 60 |
# 1: run n times between START and END |
| 61 |
# 3: start at START, keep adding INTV (if n, only for n points) |
| 62 |
# 4: start at END, keep subtracting INTV (if n, only for n points) |
| 63 |
|
| 64 |
RE_COMPONENTS = { |
| 65 |
"end": r"(?P<end>[^PR/][^/]*)", |
| 66 |
"intv": r"(?P<intv>P[^/]*)", |
| 67 |
"reps_1": r"R(?P<reps>1)", |
| 68 |
"reps": r"R(?P<reps>\d+)", |
| 69 |
"start": r"(?P<start>[^PR/][^/]*)" |
| 70 |
} |
| 71 |
|
| 72 |
RECURRENCE_FORMAT_RECS = [ |
| 73 |
(re.compile(regex % RE_COMPONENTS), format_num) |
| 74 |
for (regex, format_num) in [ |
| 75 |
# START (not supported) |
| 76 |
# (r"^%(start)s$", 3), |
| 77 |
# Rn/START/END |
| 78 |
# e.g. R3/0/10 |
| 79 |
(r"^%(reps)s/%(start)s/%(end)s$", 1), |
| 80 |
# START/INTV, implies R/START/INTV |
| 81 |
# e.g. +P5/P3, 2/P2 |
| 82 |
(r"^%(start)s/%(intv)s/?$", 3), |
| 83 |
# INTV, implies R/INITIAL/INTV |
| 84 |
# e.g. P3, P10 |
| 85 |
(r"^%(intv)s$", 3), |
| 86 |
# INTV/END, implies R/INTV/END, count backwards from END |
| 87 |
# e.g. P3/-P1 |
| 88 |
(r"^%(intv)s/%(end)s$", 4), |
| 89 |
# Rn/START (not supported) |
| 90 |
# (r"^%(reps)s?/%(start)s/?$", 3), |
| 91 |
# but: R1/START (supported) |
| 92 |
# e.g. R1/5, R1/+P3 |
| 93 |
(r"^%(reps_1)s?/%(start)s/?$", 3), |
| 94 |
(r"^%(reps)s?/%(start)s/%(intv)s$", 3), |
| 95 |
# Rn/START/INTV |
| 96 |
# e.g. R2/3/P3 |
| 97 |
(r"^%(reps)s?/(?P<start>)/%(intv)s$", 3), |
| 98 |
# Rn/INTV/END |
| 99 |
# e.g. R5/P2/10, R7/P1/+P20 |
| 100 |
(r"^%(reps)s?/%(intv)s/%(end)s$", 4), |
| 101 |
# Rn/INTV, implies R/INTV/FINAL |
| 102 |
# e.g. R5/P2, R7/P1 |
| 103 |
(r"^%(reps)s?/%(intv)s/?$", 4), |
| 104 |
# Rn//END (not supported) |
| 105 |
# (r"^%(reps_1)s//%(end)s$", 4), |
| 106 |
# R1, run once at INITIAL |
| 107 |
# e.g. R1, R1/ |
| 108 |
(r"^%(reps_1)s/?(?P<start>$)", 3), |
| 109 |
# R1//END, run once at END. |
| 110 |
# e.g. R1//-P2 |
| 111 |
(r"^%(reps_1)s//%(end)s$", 4) |
| 112 |
] |
| 113 |
] |
| 114 |
|
| 115 |
|
| 116 |
REC_RELATIVE_POINT = re.compile(r"^[-+]P\d+$") |
| 117 |
REC_INTERVAL = re.compile(r"^[-+]?P\d+$") |
| 118 |
|
| 119 |
|
| 120 |
class IntegerPoint(PointBase): |
| 121 |
|
| 122 |
"""A single point in an integer sequence.""" |
| 123 |
|
| 124 |
TYPE = CYCLER_TYPE_INTEGER |
| 125 |
TYPE_SORT_KEY = CYCLER_TYPE_SORT_KEY_INTEGER |
| 126 |
|
| 127 |
__slots__ = ('value') |
| 128 |
|
| 129 |
def __init__(self, value): |
| 130 |
if isinstance(value, int): |
| 131 |
value = str(value) |
| 132 |
super(IntegerPoint, self).__init__(str(value)) |
| 133 |
|
| 134 |
def add(self, other): |
| 135 |
"""Add other.value to self.value as integers.""" |
| 136 |
return IntegerPoint(int(self) + int(other)) |
| 137 |
|
| 138 |
def _cmp(self, other: 'IntegerPoint') -> int: |
| 139 |
"""Compare self.value to self.other as integers with 'cmp'.""" |
| 140 |
return cmp(int(self), int(other)) |
| 141 |
|
| 142 |
def sub(self, other): |
| 143 |
"""Subtract other.value from self.value as integers.""" |
| 144 |
if isinstance(other, IntegerPoint): |
| 145 |
return IntegerInterval.from_integer(int(self) - int(other)) |
| 146 |
return IntegerPoint(int(self) - int(other)) |
| 147 |
|
| 148 |
def standardise(self, allow_truncated=True): |
| 149 |
"""Format self.value into a standard representation and check it.""" |
| 150 |
try: |
| 151 |
self.value = str(int(self)) |
| 152 |
except (TypeError, ValueError) as exc: |
| 153 |
raise PointParsingError(type(self), self.value, exc) from None |
| 154 |
return self |
| 155 |
|
| 156 |
def __int__(self): |
| 157 |
# Provide a nice way to use the string self.value in calculations. |
| 158 |
return int(self.value) |
| 159 |
|
| 160 |
|
| 161 |
class IntegerInterval(IntervalBase): |
| 162 |
|
| 163 |
"""The interval between points in an integer sequence.""" |
| 164 |
|
| 165 |
TYPE = CYCLER_TYPE_INTEGER |
| 166 |
TYPE_SORT_KEY = CYCLER_TYPE_SORT_KEY_INTEGER |
| 167 |
|
| 168 |
__slots__ = ('value') |
| 169 |
|
| 170 |
@classmethod |
| 171 |
def from_integer(cls, integer): |
| 172 |
"""Return an instance of this class using integer.""" |
| 173 |
if integer < 0: |
| 174 |
value = "-P" + str(abs(integer)) |
| 175 |
else: |
| 176 |
value = "P" + str(integer) |
| 177 |
return IntegerInterval(value) |
| 178 |
|
| 179 |
@classmethod |
| 180 |
def get_null(cls): |
| 181 |
"""Return a null interval.""" |
| 182 |
return IntegerInterval("P0") |
| 183 |
|
| 184 |
@classmethod |
| 185 |
def get_null_offset(cls): |
| 186 |
"""Return a null offset.""" |
| 187 |
return IntegerInterval("+P0") |
| 188 |
|
| 189 |
def __init__(self, value): |
| 190 |
if (not isinstance(value, str) or |
| 191 |
not REC_INTERVAL.search(value)): |
| 192 |
raise IntervalParsingError("IntegerInterval", repr(value)) |
| 193 |
super(IntegerInterval, self).__init__(value) |
| 194 |
|
| 195 |
def add(self, other): |
| 196 |
"""Add other to self as integers (point or interval).""" |
| 197 |
if isinstance(other, IntegerInterval): |
| 198 |
return IntegerInterval.from_integer(int(self) + int(other)) |
| 199 |
return IntegerPoint(int(self) + int(other)) |
| 200 |
|
| 201 |
def _cmp(self, other: 'IntegerInterval') -> int: |
| 202 |
"""Compare other to self as integers.""" |
| 203 |
return cmp(int(self), int(other)) |
| 204 |
|
| 205 |
def sub(self, other): |
| 206 |
"""Subtract other from self as integers.""" |
| 207 |
if isinstance(other, IntegerInterval): |
| 208 |
return IntegerInterval.from_integer(int(self) - int(other)) |
| 209 |
return IntegerPoint(int(self) - int(other)) |
| 210 |
|
| 211 |
def __abs__(self): |
| 212 |
# Return an interval with absolute values for all properties. |
| 213 |
return IntegerInterval.from_integer(abs(int(self))) |
| 214 |
|
| 215 |
def __int__(self): |
| 216 |
# Provide a nice way to use the string self.value in calculations. |
| 217 |
return int(self.value.replace("P", "")) |
| 218 |
|
| 219 |
def __mul__(self, factor): |
| 220 |
# Return an interval with all properties multiplied by factor. |
| 221 |
return IntegerInterval.from_integer(int(self) * factor) |
| 222 |
|
| 223 |
def __bool__(self): |
| 224 |
# Return True if the interval has any non-zero properties. |
| 225 |
return bool(int(self)) |
| 226 |
|
| 227 |
|
| 228 |
class IntegerExclusions(ExclusionBase): |
| 229 |
"""A collection of integer exclusion points, or sequences of |
| 230 |
integers that are treated in an exclusionary manner.""" |
| 231 |
|
| 232 |
__slots__ = ExclusionBase.__slots__ |
| 233 |
|
| 234 |
def __init__(self, excl_points, start_point, end_point=None): |
| 235 |
"""creates an exclusions object that can contain integer points |
| 236 |
or integer sequences to be used as excluded points.""" |
| 237 |
super(IntegerExclusions, self).__init__(start_point, end_point) |
| 238 |
|
| 239 |
self.build_exclusions(excl_points) |
| 240 |
|
| 241 |
def build_exclusions(self, excl_points): |
| 242 |
for point in excl_points: |
| 243 |
try: |
| 244 |
# Try making an integer point |
| 245 |
integer_point = get_point_from_expression( |
| 246 |
point, |
| 247 |
None, |
| 248 |
is_required=False, |
| 249 |
).standardise() |
| 250 |
if integer_point not in self.exclusion_points: |
| 251 |
self.exclusion_points.append(integer_point) |
| 252 |
except PointParsingError: |
| 253 |
# Try making an integer sequence |
| 254 |
integer_exclusion_sequence = (IntegerSequence( |
| 255 |
point, self.exclusion_start_point, |
| 256 |
self.exclusion_end_point)) |
| 257 |
self.exclusion_sequences.append(integer_exclusion_sequence) |
| 258 |
|
| 259 |
|
| 260 |
class IntegerSequence(SequenceBase): |
| 261 |
"""Integer points at a regular interval.""" |
| 262 |
|
| 263 |
TYPE = CYCLER_TYPE_INTEGER |
| 264 |
TYPE_SORT_KEY = CYCLER_TYPE_SORT_KEY_INTEGER |
| 265 |
|
| 266 |
__slots__ = ('p_context_start', 'p_context_stop', 'p_start', 'p_stop', |
| 267 |
'i_step', 'i_offset', 'exclusions') |
| 268 |
|
| 269 |
@classmethod |
| 270 |
def get_async_expr(cls, start_point=None): |
| 271 |
"""Express a one-off sequence at the initial cycle point.""" |
| 272 |
if start_point is None: |
| 273 |
return "R1" |
| 274 |
return 'R1/' + str(start_point) |
| 275 |
|
| 276 |
def __init__(self, dep_section, p_context_start, p_context_stop=None): |
| 277 |
"""Parse state (start, stop, interval) from a graph section heading. |
| 278 |
The start and stop points are always on-sequence, context points |
| 279 |
might not be. If computed start and stop points are out of bounds, |
| 280 |
they will be set to None. Context is used only initially to define |
| 281 |
the sequence bounds.""" |
| 282 |
SequenceBase.__init__( |
| 283 |
self, dep_section, p_context_start, p_context_stop) |
| 284 |
|
| 285 |
# start context always exists |
| 286 |
self.p_context_start = IntegerPoint(p_context_start) |
| 287 |
# stop context may exist |
| 288 |
if p_context_stop: |
| 289 |
self.p_context_stop = IntegerPoint(p_context_stop) |
| 290 |
else: |
| 291 |
self.p_context_stop = None |
| 292 |
|
| 293 |
# state variables: start, stop, and step |
| 294 |
self.p_start = None |
| 295 |
self.p_stop = None |
| 296 |
self.i_step = None |
| 297 |
|
| 298 |
# offset must be stored to compute the runahead limit |
| 299 |
self.i_offset = IntegerInterval('P0') |
| 300 |
|
| 301 |
self.exclusions = None |
| 302 |
|
| 303 |
expression, excl_points = parse_exclusion(dep_section) |
| 304 |
|
| 305 |
for rec, format_num in RECURRENCE_FORMAT_RECS: |
| 306 |
results = rec.match(expression) |
| 307 |
if not results: |
| 308 |
continue |
| 309 |
reps = results.groupdict().get("reps") |
| 310 |
if reps is not None: |
| 311 |
reps = int(reps) |
| 312 |
start = results.groupdict().get("start") |
| 313 |
stop = results.groupdict().get("end") |
| 314 |
intv = results.groupdict().get("intv") |
| 315 |
if not start: |
| 316 |
start = None |
| 317 |
if not stop: |
| 318 |
stop = None |
| 319 |
if not intv: |
| 320 |
intv = None |
| 321 |
start_required = (format_num in [1, 3]) |
| 322 |
end_required = (format_num in [1, 4]) |
| 323 |
break |
| 324 |
else: |
| 325 |
raise SequenceParsingError( |
| 326 |
f'Invalid integer recurrence: {expression}' |
| 327 |
) |
| 328 |
|
| 329 |
self.p_start = get_point_from_expression( |
| 330 |
start, self.p_context_start, is_required=start_required) |
| 331 |
self.p_stop = get_point_from_expression( |
| 332 |
stop, self.p_context_stop, is_required=end_required) |
| 333 |
if intv: |
| 334 |
self.i_step = IntegerInterval(intv) |
| 335 |
if format_num == 3: |
| 336 |
# REPEAT/START/PERIOD |
| 337 |
if not intv or reps is not None and reps <= 1: |
| 338 |
# one-off |
| 339 |
self.i_step = None |
| 340 |
self.p_stop = self.p_start |
| 341 |
else: |
| 342 |
self.i_step = IntegerInterval(intv) |
| 343 |
if reps: |
| 344 |
self.p_stop = self.p_start + self.i_step * (reps - 1) |
| 345 |
elif self.p_context_stop: |
| 346 |
# stop at the point <= self.p_context_stop |
| 347 |
# use p_start as an on-sequence reference |
| 348 |
remainder = (int(self.p_context_stop - self.p_start) % |
| 349 |
int(self.i_step)) |
| 350 |
self.p_stop = ( |
| 351 |
self.p_context_stop - IntegerInterval.from_integer( |
| 352 |
remainder) |
| 353 |
) |
| 354 |
elif format_num == 1: |
| 355 |
# REPEAT/START/STOP |
| 356 |
if reps == 1: |
| 357 |
# one-off: ignore stop point |
| 358 |
self.i_step = None |
| 359 |
self.p_stop = self.p_start |
| 360 |
else: |
| 361 |
self.i_step = IntegerInterval.from_integer( |
| 362 |
int(self.p_stop - self.p_start) / (reps - 1) |
| 363 |
) |
| 364 |
else: |
| 365 |
# This means that format_num == 4. |
| 366 |
# REPEAT/PERIOD/STOP |
| 367 |
if reps is not None: |
| 368 |
if reps <= 1: |
| 369 |
# one-off |
| 370 |
self.p_start = self.p_stop |
| 371 |
self.i_step = None |
| 372 |
else: |
| 373 |
self.i_step = IntegerInterval(intv) |
| 374 |
self.p_start = ( |
| 375 |
self.p_stop - self.i_step * (reps - 1)) |
| 376 |
else: |
| 377 |
remainder = (int(self.p_context_stop - self.p_start) % |
| 378 |
int(self.i_step)) |
| 379 |
self.p_start = ( |
| 380 |
self.p_context_start - IntegerInterval.from_integer( |
| 381 |
remainder) |
| 382 |
) |
| 383 |
|
| 384 |
if self.i_step and self.i_step < IntegerInterval.get_null(): |
| 385 |
# (TODO - this should be easy to handle but needs testing) |
| 386 |
raise ValueError( # TODO - raise appropriate exception |
| 387 |
"ERROR, negative intervals not supported yet: %s" % |
| 388 |
self.i_step |
| 389 |
) |
| 390 |
|
| 391 |
if self.i_step and self.p_start < self.p_context_start: |
| 392 |
# start from first point >= context start |
| 393 |
remainder = ( |
| 394 |
int(self.p_context_start - self.p_start) % int(self.i_step)) |
| 395 |
self.p_start = ( |
| 396 |
self.p_context_start + IntegerInterval.from_integer( |
| 397 |
remainder) |
| 398 |
) |
| 399 |
# if i_step is None here, points will just be None (out of bounds) |
| 400 |
|
| 401 |
if (self.i_step and self.p_stop and self.p_context_stop and |
| 402 |
self.p_stop > self.p_context_stop): |
| 403 |
# stop at first point <= context stop |
| 404 |
remainder = ( |
| 405 |
int(self.p_context_stop - self.p_start) % int(self.i_step)) |
| 406 |
self.p_stop = ( |
| 407 |
self.p_context_stop - self.i_step + |
| 408 |
IntegerInterval.from_integer(remainder) |
| 409 |
) |
| 410 |
# if i_step is None here, points will just be None (out of bounds) |
| 411 |
|
| 412 |
# Create a list of multiple exclusion points, if there are any. |
| 413 |
if excl_points: |
| 414 |
self.exclusions = IntegerExclusions(excl_points, |
| 415 |
self.p_start, self.p_stop) |
| 416 |
else: |
| 417 |
self.exclusions = None |
| 418 |
|
| 419 |
def get_interval(self): |
| 420 |
"""Return the cycling interval of this sequence.""" |
| 421 |
# interval may be None (a one-off sequence) |
| 422 |
return self.i_step |
| 423 |
|
| 424 |
def get_offset(self): |
| 425 |
"""Deprecated: return the offset used for this sequence.""" |
| 426 |
return self.i_offset |
| 427 |
|
| 428 |
def set_offset(self, i_offset): |
| 429 |
"""Deprecated: alter state to offset the entire sequence.""" |
| 430 |
if not i_offset.value: |
| 431 |
# no offset |
| 432 |
return |
| 433 |
if not self.i_step: |
| 434 |
# this is a one-off sequence |
| 435 |
self.p_start += i_offset |
| 436 |
self.p_stop += i_offset |
| 437 |
if self.p_start < self.p_context_start: |
| 438 |
self.p_start = self.p_stop = None |
| 439 |
return |
| 440 |
if not int(i_offset) % int(self.i_step): |
| 441 |
# offset is a multiple of step |
| 442 |
return |
| 443 |
# shift to 0 < offset < interval |
| 444 |
i_offset = IntegerInterval.from_integer( |
| 445 |
int(i_offset) % int(self.i_step)) |
| 446 |
self.i_offset = i_offset |
| 447 |
self.p_start += i_offset # can be negative |
| 448 |
if self.p_start < self.p_context_start: |
| 449 |
self.p_start += self.i_step |
| 450 |
self.p_stop += i_offset |
| 451 |
if self.p_stop > self.p_context_stop: |
| 452 |
self.p_stop -= self.i_step |
| 453 |
|
| 454 |
def is_on_sequence(self, point): |
| 455 |
"""Is point on-sequence, disregarding bounds?""" |
| 456 |
if self.exclusions and point in self.exclusions: |
| 457 |
return False |
| 458 |
if self.i_step: |
| 459 |
return int(point - self.p_start) % int(self.i_step) == 0 |
| 460 |
else: |
| 461 |
return point == self.p_start |
| 462 |
|
| 463 |
def _get_point_in_bounds(self, point): |
| 464 |
"""Return point, or None if out of bounds.""" |
| 465 |
if point >= self.p_start and ( |
| 466 |
self.p_stop is None or point <= self.p_stop): |
| 467 |
return point |
| 468 |
else: |
| 469 |
return None |
| 470 |
|
| 471 |
def is_valid(self, point): |
| 472 |
"""Is point on-sequence and in-bounds?""" |
| 473 |
return (self.is_on_sequence(point) and |
| 474 |
point >= self.p_start and |
| 475 |
(self.p_stop is None or |
| 476 |
point <= self.p_stop)) |
| 477 |
|
| 478 |
def get_prev_point(self, point): |
| 479 |
"""Return the previous point < point, or None if out of bounds.""" |
| 480 |
# Only used in computing special sequential task prerequisites. |
| 481 |
if not self.i_step: |
| 482 |
# implies a one-off task was declared sequential |
| 483 |
# TODO - check this results in sensible behaviour |
| 484 |
return None |
| 485 |
i = int(point - self.p_start) % int(self.i_step) |
| 486 |
if i: |
| 487 |
prev_point = point - IntegerInterval.from_integer(i) |
| 488 |
else: |
| 489 |
prev_point = point - self.i_step |
| 490 |
ret = self._get_point_in_bounds(prev_point) |
| 491 |
if self.exclusions and ret in self.exclusions: |
| 492 |
return self.get_prev_point(ret) |
| 493 |
return ret |
| 494 |
|
| 495 |
def get_nearest_prev_point(self, point): |
| 496 |
"""Return the largest point < some arbitrary point.""" |
| 497 |
if self.is_on_sequence(point): |
| 498 |
return self.get_prev_point(point) |
| 499 |
sequence_point = self._get_point_in_bounds(self.p_start) |
| 500 |
prev_point = None |
| 501 |
while sequence_point is not None: |
| 502 |
if sequence_point > point: |
| 503 |
# Technically, >=, but we already test for this above. |
| 504 |
break |
| 505 |
prev_point = sequence_point |
| 506 |
sequence_point = self.get_next_point(sequence_point) |
| 507 |
if self.exclusions and prev_point in self.exclusions: |
| 508 |
return self.get_nearest_prev_point(prev_point) |
| 509 |
return prev_point |
| 510 |
|
| 511 |
def get_next_point(self, point): |
| 512 |
"""Return the next point > point, or None if out of bounds.""" |
| 513 |
if not self.i_step: |
| 514 |
# this is a one-off sequence |
| 515 |
# TODO - is this needed? if so, check it gives sensible behaviour |
| 516 |
if point < self.p_start: |
| 517 |
return self.p_start |
| 518 |
else: |
| 519 |
return None |
| 520 |
i = int(point - self.p_start) % int(self.i_step) |
| 521 |
next_point = point + self.i_step - IntegerInterval.from_integer(i) |
| 522 |
ret = self._get_point_in_bounds(next_point) |
| 523 |
if self.exclusions and ret and ret in self.exclusions: |
| 524 |
return self.get_next_point(ret) |
| 525 |
return ret |
| 526 |
|
| 527 |
def get_next_point_on_sequence(self, point): |
| 528 |
"""Return the next point > point assuming that point is on-sequence, |
| 529 |
or None if out of bounds.""" |
| 530 |
# This can be used when working with a single sequence. |
| 531 |
if not self.i_step: |
| 532 |
return None |
| 533 |
next_point = point + self.i_step |
| 534 |
ret = self._get_point_in_bounds(next_point) |
| 535 |
if self.exclusions and ret and ret in self.exclusions: |
| 536 |
return self.get_next_point_on_sequence(ret) |
| 537 |
return ret |
| 538 |
|
| 539 |
def get_first_point(self, point): |
| 540 |
"""Return the first point >= to point, or None if out of bounds.""" |
| 541 |
# Used to find the first point >= workflow initial cycle point. |
| 542 |
if point <= self.p_start: |
| 543 |
point = self._get_point_in_bounds(self.p_start) |
| 544 |
elif self.is_on_sequence(point): |
| 545 |
point = self._get_point_in_bounds(point) |
| 546 |
else: |
| 547 |
point = self.get_next_point(point) |
| 548 |
if self.exclusions and point and point in self.exclusions: |
| 549 |
return self.get_next_point_on_sequence(point) |
| 550 |
return point |
| 551 |
|
| 552 |
def get_start_point(self): |
| 553 |
"""Return the first point in this sequence, or None.""" |
| 554 |
if self.exclusions and self.p_start in self.exclusions: |
| 555 |
return self.get_next_point_on_sequence(self.p_start) |
| 556 |
return self.p_start |
| 557 |
|
| 558 |
def get_stop_point(self): |
| 559 |
"""Return the last point in this sequence, or None if unbounded.""" |
| 560 |
if self.exclusions and self.p_stop in self.exclusions: |
| 561 |
return self.get_prev_point(self.p_stop) |
| 562 |
return self.p_stop |
| 563 |
|
| 564 |
def __eq__(self, other): |
| 565 |
# Return True if other (sequence) is equal to self. |
| 566 |
if ( |
| 567 |
self.i_step |
| 568 |
and not other.i_step |
| 569 |
or not self.i_step |
| 570 |
and other.i_step |
| 571 |
): |
| 572 |
return False |
| 573 |
else: |
| 574 |
return ( |
| 575 |
self.i_step == other.i_step |
| 576 |
and self.p_start == other.p_start |
| 577 |
and self.p_stop == other.p_stop |
| 578 |
and self.exclusions == other.exclusions |
| 579 |
) |
| 580 |
|
| 581 |
def __hash__(self): |
| 582 |
return hash(tuple(getattr(self, attr) for attr in self.__slots__)) |
| 583 |
|
| 584 |
def __lt__(self, other: 'IntegerSequence') -> bool: |
| 585 |
for attr in self.__slots__: |
| 586 |
with contextlib.suppress(TypeError): |
| 587 |
if getattr(self, attr) < getattr(other, attr): |
| 588 |
return True |
| 589 |
return False |
| 590 |
|
| 591 |
def __repr__(self) -> str: |
| 592 |
""" |
| 593 |
>>> IntegerSequence('R6/3/P1', '2') |
| 594 |
<IntegerSequence start=3, stop=8, step=P1, self.p_context_start=2, |
| 595 |
i_offset=P0> |
| 596 |
|
| 597 |
>>> IntegerSequence('R6/P1/3', '2', '4') |
| 598 |
<IntegerSequence start=2, stop=3, step=P1, self.p_context_stop=4, |
| 599 |
i_offset=P0> |
| 600 |
""" |
| 601 |
ret = f"start={self.p_start}, stop={self.p_stop}, step={self.i_step}" |
| 602 |
if self.p_context_start not in {self.p_start, None}: |
| 603 |
ret += f", {self.p_context_start=}" |
| 604 |
if self.p_context_stop not in {self.p_stop, None}: |
| 605 |
ret += f", {self.p_context_stop=}" |
| 606 |
for attr in ('i_offset', 'exclusions'): |
| 607 |
if (value := getattr(self, attr)) is not None: |
| 608 |
ret += f", {attr}={value}" |
| 609 |
return f"<{type(self).__name__} {ret}>" |
| 610 |
|
| 611 |
|
| 612 |
def init_from_cfg(_): |
| 613 |
"""Placeholder function required by all cycling modules.""" |
| 614 |
pass |
| 615 |
|
| 616 |
|
| 617 |
def get_dump_format() -> None: |
| 618 |
"""Return cycle point string dump format.""" |
| 619 |
# Not used for integer cycling. |
| 620 |
return None |
| 621 |
|
| 622 |
|
| 623 |
def get_point_relative(point_expr, context_point): |
| 624 |
"""Create a point from relative_string applied to base_point.""" |
| 625 |
if REC_RELATIVE_POINT.search(point_expr): |
| 626 |
# This is a relative point expression e.g. '+P2' or '-P12'. |
| 627 |
return context_point + IntegerInterval(point_expr) |
| 628 |
# This is an absolute point expression e.g. '4'. |
| 629 |
return IntegerPoint(point_expr) |
| 630 |
|
| 631 |
|
| 632 |
def get_point_from_expression(point_expr, context_point, is_required=False): |
| 633 |
"""Return a point from an absolute or relative point_expr.""" |
| 634 |
if point_expr is None and context_point is None: |
| 635 |
if is_required: |
| 636 |
raise CylcMissingContextPointError( |
| 637 |
"Missing context cycle point." |
| 638 |
) |
| 639 |
return None |
| 640 |
if point_expr is None: |
| 641 |
return context_point |
| 642 |
return get_point_relative(point_expr, context_point) |
| 643 |
|
| 644 |
|
| 645 |
def is_offset_absolute(offset_string): |
| 646 |
"""Return True if offset_string is a point rather than an interval.""" |
| 647 |
return not REC_RELATIVE_POINT.search(offset_string) |