@jsb188/schedule 1.1.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +61 -0
- package/src/constants/schedule.ts +549 -0
- package/src/css.d.ts +7 -0
- package/src/libs/gantt-geometry.ts +771 -0
- package/src/libs/gantt-histogram.ts +124 -0
- package/src/libs/gantt-keyboard.ts +113 -0
- package/src/libs/gantt-state.ts +57 -0
- package/src/libs/gantt-url.ts +63 -0
- package/src/modules/Gantt.helpers.ts +138 -0
- package/src/modules/Gantt.tsx +719 -0
- package/src/types/schedule.d.ts +1149 -0
- package/src/ui/ActivityFormUI.tsx +133 -0
- package/src/ui/GanttEditorsUI.tsx +102 -0
- package/src/ui/GanttHistogramUI.tsx +236 -0
- package/src/ui/GanttUI.tsx +792 -0
- package/src/ui/ProjectFormUI.tsx +273 -0
- package/src/ui/ProjectMobileListUI.tsx +80 -0
- package/src/ui/ProjectToolbarUI.tsx +54 -0
- package/src/ui/RelationshipListUI.tsx +176 -0
- package/src/ui/ScheduleRunUI.tsx +173 -0
- package/src/ui/ScheduleSettingsUI.tsx +420 -0
- package/src/utils/coverage.helpers.ts +679 -0
- package/src/utils/coverage.ts +630 -0
- package/src/utils/cpm.helpers.ts +674 -0
- package/src/utils/cpm.ts +907 -0
- package/src/utils/cpmLeveling.helpers.ts +564 -0
- package/src/utils/cpmLeveling.ts +367 -0
- package/src/utils/defaults.ts +8 -0
- package/src/utils/eligibility.ts +457 -0
- package/src/utils/planCsv.ts +344 -0
- package/src/utils/predecessorText.ts +96 -0
- package/src/utils/recurrence.ts +616 -0
- package/src/utils/request.ts +153 -0
- package/src/utils/resource.ts +182 -0
- package/src/utils/rule.ts +216 -0
- package/src/utils/run.ts +43 -0
- package/src/utils/series.ts +161 -0
- package/src/utils/slot.ts +208 -0
- package/src/utils/workTime.ts +1382 -0
- package/tests/coverage.fixtures.ts +111 -0
- package/tests/coverage.test.ts +503 -0
- package/tests/cpm.bench.test.ts +154 -0
- package/tests/cpm.test.ts +354 -0
- package/tests/cpmCorrectness.test.ts +327 -0
- package/tests/cpmLeveling.test.ts +471 -0
- package/tests/eligibility.test.ts +320 -0
- package/tests/gantt-geometry.test.ts +367 -0
- package/tests/gantt-histogram.test.ts +78 -0
- package/tests/gantt-keyboard.test.ts +69 -0
- package/tests/helpers/cpmFixtures.ts +101 -0
- package/tests/projectInputs.test.ts +165 -0
- package/tests/recurrence.test.ts +68 -0
- package/tests/request.test.ts +99 -0
- package/tests/resource.test.ts +95 -0
- package/tests/rule.test.ts +98 -0
- package/tests/series.test.ts +120 -0
- package/tests/workTime.test.ts +1007 -0
- package/tests/workTimeContracts.test.ts +111 -0
- package/tsconfig.json +13 -0
- package/vitest.config.ts +8 -0
|
@@ -0,0 +1,674 @@
|
|
|
1
|
+
import {
|
|
2
|
+
SCHEDULE_ACTIVITY_TYPE_ENUMS,
|
|
3
|
+
SCHEDULE_CONSTRAINT_TYPE_ENUMS,
|
|
4
|
+
SCHEDULE_DIAGNOSTIC_CAP_PER_CODE,
|
|
5
|
+
SCHEDULE_DIAGNOSTIC_MAX_ACTIVITY_IDS,
|
|
6
|
+
SCHEDULE_LEVELING_PRIORITY_RULE_ENUMS,
|
|
7
|
+
SCHEDULE_RELATIONSHIP_TYPE_ENUMS,
|
|
8
|
+
} from '../constants/schedule.ts';
|
|
9
|
+
import type {
|
|
10
|
+
CpmActivityInput,
|
|
11
|
+
CpmRelationshipInput,
|
|
12
|
+
CpmScheduleInput,
|
|
13
|
+
ScheduleActivityStatusEnum,
|
|
14
|
+
ScheduleDiagnosticCodeEnum,
|
|
15
|
+
ScheduleDiagnosticObj,
|
|
16
|
+
ScheduleWorkCalendarDef,
|
|
17
|
+
} from '../types/schedule.d.ts';
|
|
18
|
+
import {
|
|
19
|
+
addWorkingMinutes,
|
|
20
|
+
getWorkingMinutesPerDay,
|
|
21
|
+
isValidWorkTimeZone,
|
|
22
|
+
isWorkingCalendar,
|
|
23
|
+
snapToWorkingFinish,
|
|
24
|
+
snapToWorkingStart,
|
|
25
|
+
workingMinutesBetween,
|
|
26
|
+
type WorkCalendarTable,
|
|
27
|
+
} from './workTime.ts';
|
|
28
|
+
|
|
29
|
+
export {
|
|
30
|
+
getDayEntry,
|
|
31
|
+
getDateKeyForDayIndex,
|
|
32
|
+
getDayIndexForDateKey,
|
|
33
|
+
getCumulativeMinutesAt,
|
|
34
|
+
getWorkingMinutesOnDayBetween,
|
|
35
|
+
} from './workTime.ts';
|
|
36
|
+
|
|
37
|
+
export interface CpmCalendar {
|
|
38
|
+
def: ScheduleWorkCalendarDef;
|
|
39
|
+
minutesPerDay: number;
|
|
40
|
+
table?: WorkCalendarTable;
|
|
41
|
+
}
|
|
42
|
+
export interface CpmConstraintSide {
|
|
43
|
+
onOrAfterMs: number | null;
|
|
44
|
+
onOrBeforeMs: number | null;
|
|
45
|
+
onMs: number | null;
|
|
46
|
+
mandatoryMs: number | null;
|
|
47
|
+
}
|
|
48
|
+
export interface CpmConstraints {
|
|
49
|
+
start: CpmConstraintSide;
|
|
50
|
+
finish: CpmConstraintSide;
|
|
51
|
+
alap: boolean;
|
|
52
|
+
hasMandatory: boolean;
|
|
53
|
+
hasStartConstraint: boolean;
|
|
54
|
+
}
|
|
55
|
+
export interface CpmActivityState {
|
|
56
|
+
input: CpmActivityInput;
|
|
57
|
+
calendar: CpmCalendar;
|
|
58
|
+
status: ScheduleActivityStatusEnum;
|
|
59
|
+
remaining: number;
|
|
60
|
+
isDriving: boolean;
|
|
61
|
+
constraints: CpmConstraints;
|
|
62
|
+
predecessors: CpmRelationshipInput[];
|
|
63
|
+
successors: CpmRelationshipInput[];
|
|
64
|
+
es: number;
|
|
65
|
+
ef: number;
|
|
66
|
+
ls: number;
|
|
67
|
+
lf: number;
|
|
68
|
+
start: number;
|
|
69
|
+
finish: number;
|
|
70
|
+
resumeStart: number;
|
|
71
|
+
totalFloat: number | null;
|
|
72
|
+
freeFloat: number | null;
|
|
73
|
+
drivingPredecessorIds: string[];
|
|
74
|
+
drivenBy: 'LOGIC_START' | 'LOGIC_FINISH' | 'FLOOR' | 'CONSTRAINT' | 'ACTUALS';
|
|
75
|
+
onLongestPath: boolean;
|
|
76
|
+
isCritical: boolean;
|
|
77
|
+
levelingDelay: number;
|
|
78
|
+
constrainedBy: string | null;
|
|
79
|
+
}
|
|
80
|
+
export interface CpmCalendarSet {
|
|
81
|
+
byId: Map<string, CpmCalendar>;
|
|
82
|
+
defaultCalendar: CpmCalendar;
|
|
83
|
+
rangeFromMs: number;
|
|
84
|
+
rangeToMs: number;
|
|
85
|
+
}
|
|
86
|
+
export interface CpmContext {
|
|
87
|
+
input: CpmScheduleInput;
|
|
88
|
+
states: Map<string, CpmActivityState>;
|
|
89
|
+
calendarSet: CpmCalendarSet;
|
|
90
|
+
collector: DiagnosticCollector;
|
|
91
|
+
floorMs: number;
|
|
92
|
+
dataDateMs: number;
|
|
93
|
+
projectFinishMs: number;
|
|
94
|
+
lateFinishBasisMs: number;
|
|
95
|
+
progressLogic: 'RETAINED' | 'OVERRIDE';
|
|
96
|
+
lagCalendar: 'CONTINUOUS' | 'PROJECT' | 'PREDECESSOR' | 'SUCCESSOR';
|
|
97
|
+
criticalityMode: 'FLOAT' | 'LONGEST_PATH';
|
|
98
|
+
criticalThreshold: number;
|
|
99
|
+
cursorActivityId: string | null;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const FILE_CONSTANTS = {
|
|
103
|
+
drivingTypes: new Set(['TASK', 'START_MILESTONE', 'FINISH_MILESTONE']),
|
|
104
|
+
secondaryTypes: new Set([
|
|
105
|
+
'START_ON_OR_AFTER',
|
|
106
|
+
'START_ON_OR_BEFORE',
|
|
107
|
+
'FINISH_ON_OR_AFTER',
|
|
108
|
+
'FINISH_ON_OR_BEFORE',
|
|
109
|
+
]),
|
|
110
|
+
dayMs: 86_400_000,
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
/* Count every finding while bounding the amount persisted in one run. */
|
|
114
|
+
export class DiagnosticCollector {
|
|
115
|
+
readonly items: ScheduleDiagnosticObj[] = [];
|
|
116
|
+
readonly counts: Partial<Record<ScheduleDiagnosticCodeEnum, number>> = {};
|
|
117
|
+
truncated = false;
|
|
118
|
+
/* Preserve exact totals even after the visible diagnostic cap is reached. */
|
|
119
|
+
add(diagnostic: ScheduleDiagnosticObj): void {
|
|
120
|
+
const count = (this.counts[diagnostic.code] || 0) + 1;
|
|
121
|
+
this.counts[diagnostic.code] = count;
|
|
122
|
+
if (count <= SCHEDULE_DIAGNOSTIC_CAP_PER_CODE) {
|
|
123
|
+
this.items.push(
|
|
124
|
+
diagnostic.activityIds
|
|
125
|
+
? {
|
|
126
|
+
...diagnostic,
|
|
127
|
+
activityIds: diagnostic.activityIds.slice(
|
|
128
|
+
0,
|
|
129
|
+
SCHEDULE_DIAGNOSTIC_MAX_ACTIVITY_IDS,
|
|
130
|
+
),
|
|
131
|
+
}
|
|
132
|
+
: diagnostic,
|
|
133
|
+
);
|
|
134
|
+
} else this.truncated = true;
|
|
135
|
+
}
|
|
136
|
+
/* Test whether a diagnostic was reported, including capped entries. */
|
|
137
|
+
has(code: ScheduleDiagnosticCodeEnum): boolean {
|
|
138
|
+
return !!this.counts[code];
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/* Read a finite instant; absent optional dates remain absent. */
|
|
143
|
+
export function instant(
|
|
144
|
+
value: Date | string | null | undefined,
|
|
145
|
+
): number | null {
|
|
146
|
+
return value == null || value === '' ? null : new Date(value).getTime();
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/* Normalize a nonnegative duration without letting NaN contaminate dates. */
|
|
150
|
+
export function nonnegative(value: number | null | undefined): number {
|
|
151
|
+
return Number.isFinite(value) && Number(value) >= 0
|
|
152
|
+
? Math.round(Number(value))
|
|
153
|
+
: 0;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/* Identify activities that drive the project network. */
|
|
157
|
+
export function isDrivingActivity(activity: CpmActivityInput): boolean {
|
|
158
|
+
return FILE_CONSTANTS.drivingTypes.has(activity.type);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/* Derive progress from actual dates rather than trusting an independent status. */
|
|
162
|
+
export function getActivityStatus(
|
|
163
|
+
activity: CpmActivityInput,
|
|
164
|
+
): ScheduleActivityStatusEnum {
|
|
165
|
+
return activity.actualFinishAt
|
|
166
|
+
? 'COMPLETE'
|
|
167
|
+
: activity.actualStartAt
|
|
168
|
+
? 'IN_PROGRESS'
|
|
169
|
+
: 'NOT_STARTED';
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/* Milestones and summaries consume no work; started tasks use their remaining duration. */
|
|
173
|
+
export function getRemainingMinutes(
|
|
174
|
+
activity: CpmActivityInput,
|
|
175
|
+
status: ScheduleActivityStatusEnum,
|
|
176
|
+
): number {
|
|
177
|
+
return status === 'COMPLETE' || activity.type !== 'TASK'
|
|
178
|
+
? 0
|
|
179
|
+
: nonnegative(
|
|
180
|
+
status === 'IN_PROGRESS' && activity.remainingDurationMinutes != null
|
|
181
|
+
? activity.remainingDurationMinutes
|
|
182
|
+
: activity.plannedDurationMinutes,
|
|
183
|
+
);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/* Compare numeric identifiers numerically, preserving deterministic ordering for other ids. */
|
|
187
|
+
export function compareActivityIds(a: string, b: string): number {
|
|
188
|
+
if (/^\d+$/.test(a) && /^\d+$/.test(b)) {
|
|
189
|
+
const left = BigInt(a);
|
|
190
|
+
const right = BigInt(b);
|
|
191
|
+
if (left !== right) return left < right ? -1 : 1;
|
|
192
|
+
}
|
|
193
|
+
return a < b ? -1 : a > b ? 1 : 0;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/* Normalize constraints once so every scheduling pass applies the same policy. */
|
|
197
|
+
export function getConstraints(activity: CpmActivityInput): CpmConstraints {
|
|
198
|
+
const result: CpmConstraints = {
|
|
199
|
+
start: {
|
|
200
|
+
onOrAfterMs: null,
|
|
201
|
+
onOrBeforeMs: null,
|
|
202
|
+
onMs: null,
|
|
203
|
+
mandatoryMs: null,
|
|
204
|
+
},
|
|
205
|
+
finish: {
|
|
206
|
+
onOrAfterMs: null,
|
|
207
|
+
onOrBeforeMs: null,
|
|
208
|
+
onMs: null,
|
|
209
|
+
mandatoryMs: null,
|
|
210
|
+
},
|
|
211
|
+
alap: false,
|
|
212
|
+
hasMandatory: false,
|
|
213
|
+
hasStartConstraint: false,
|
|
214
|
+
};
|
|
215
|
+
for (const [index, constraint] of [
|
|
216
|
+
activity.primaryConstraint,
|
|
217
|
+
activity.secondaryConstraint,
|
|
218
|
+
].entries()) {
|
|
219
|
+
if (
|
|
220
|
+
!constraint ||
|
|
221
|
+
(index === 1 && !FILE_CONSTANTS.secondaryTypes.has(constraint.type))
|
|
222
|
+
)
|
|
223
|
+
continue;
|
|
224
|
+
if (constraint.type === 'AS_LATE_AS_POSSIBLE') {
|
|
225
|
+
result.alap = true;
|
|
226
|
+
continue;
|
|
227
|
+
}
|
|
228
|
+
const at = instant(constraint.at);
|
|
229
|
+
if (at == null || !Number.isFinite(at)) continue;
|
|
230
|
+
const side = constraint.type.includes('START')
|
|
231
|
+
? result.start
|
|
232
|
+
: result.finish;
|
|
233
|
+
if (constraint.type.startsWith('MANDATORY')) side.mandatoryMs = at;
|
|
234
|
+
else if (constraint.type.endsWith('_OR_AFTER'))
|
|
235
|
+
side.onOrAfterMs = Math.max(side.onOrAfterMs ?? -Infinity, at);
|
|
236
|
+
else if (constraint.type.endsWith('_OR_BEFORE'))
|
|
237
|
+
side.onOrBeforeMs = Math.min(side.onOrBeforeMs ?? Infinity, at);
|
|
238
|
+
else side.onMs = at;
|
|
239
|
+
}
|
|
240
|
+
result.hasMandatory =
|
|
241
|
+
result.start.mandatoryMs != null || result.finish.mandatoryMs != null;
|
|
242
|
+
result.hasStartConstraint = Object.values(result.start).some(
|
|
243
|
+
(value) => value != null,
|
|
244
|
+
);
|
|
245
|
+
return result;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/* Validate public input before table arithmetic, retaining recoverable problems as warnings. */
|
|
249
|
+
export function validateCpmScheduleInput(
|
|
250
|
+
input: CpmScheduleInput,
|
|
251
|
+
collector: DiagnosticCollector,
|
|
252
|
+
): { fatal: boolean; referencedCalendarIds: Set<string> } {
|
|
253
|
+
let fatal = false;
|
|
254
|
+
/* Record an invalid field and remember whether the run must stop. */
|
|
255
|
+
const invalid = (
|
|
256
|
+
message: string,
|
|
257
|
+
activityId?: string,
|
|
258
|
+
error = false,
|
|
259
|
+
calendarId?: string,
|
|
260
|
+
) => {
|
|
261
|
+
collector.add({
|
|
262
|
+
code: 'INVALID_INPUT',
|
|
263
|
+
severity: error ? 'ERROR' : 'WARN',
|
|
264
|
+
message,
|
|
265
|
+
activityId,
|
|
266
|
+
calendarId,
|
|
267
|
+
});
|
|
268
|
+
fatal ||= error;
|
|
269
|
+
};
|
|
270
|
+
const referencedCalendarIds = new Set<string>();
|
|
271
|
+
if (!input.calendars[input.defaultCalendarId]) {
|
|
272
|
+
collector.add({
|
|
273
|
+
code: 'EMPTY_CALENDAR',
|
|
274
|
+
severity: 'ERROR',
|
|
275
|
+
message: 'The project has no default work calendar.',
|
|
276
|
+
});
|
|
277
|
+
fatal = true;
|
|
278
|
+
} else referencedCalendarIds.add(input.defaultCalendarId);
|
|
279
|
+
const unknown = new Map<string, number>();
|
|
280
|
+
for (const row of [
|
|
281
|
+
...input.activities,
|
|
282
|
+
...(input.options?.leveling?.enabled ? input.resources || [] : []),
|
|
283
|
+
]) {
|
|
284
|
+
if (!row.calendarId) continue;
|
|
285
|
+
if (input.calendars[row.calendarId])
|
|
286
|
+
referencedCalendarIds.add(row.calendarId);
|
|
287
|
+
else unknown.set(row.calendarId, (unknown.get(row.calendarId) || 0) + 1);
|
|
288
|
+
}
|
|
289
|
+
for (const id of referencedCalendarIds) {
|
|
290
|
+
const calendar = input.calendars[id];
|
|
291
|
+
if (!isValidWorkTimeZone(calendar.timeZone))
|
|
292
|
+
invalid(
|
|
293
|
+
`Work calendar ${id} has an invalid time zone.`,
|
|
294
|
+
undefined,
|
|
295
|
+
true,
|
|
296
|
+
id,
|
|
297
|
+
);
|
|
298
|
+
if (!isWorkingCalendar(calendar)) {
|
|
299
|
+
const count = input.activities.filter(
|
|
300
|
+
(a) => (a.calendarId || input.defaultCalendarId) === id,
|
|
301
|
+
).length;
|
|
302
|
+
collector.add({
|
|
303
|
+
code: 'EMPTY_CALENDAR',
|
|
304
|
+
severity: 'ERROR',
|
|
305
|
+
calendarId: id,
|
|
306
|
+
message: `Work calendar ${id} has no working time; ${count} activities use it.`,
|
|
307
|
+
});
|
|
308
|
+
fatal = true;
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
for (const key of [
|
|
312
|
+
'projectStartAt',
|
|
313
|
+
'dataDateAt',
|
|
314
|
+
'mustFinishByAt',
|
|
315
|
+
] as const) {
|
|
316
|
+
const ms = instant(input[key]);
|
|
317
|
+
if (
|
|
318
|
+
(key !== 'mustFinishByAt' && ms == null) ||
|
|
319
|
+
(ms != null && !Number.isFinite(ms))
|
|
320
|
+
)
|
|
321
|
+
invalid(`Invalid ${key}.`, undefined, true);
|
|
322
|
+
}
|
|
323
|
+
const ids = new Set<string>();
|
|
324
|
+
for (const a of input.activities) {
|
|
325
|
+
if (ids.has(a.id)) invalid('Duplicate activity id.', a.id, true);
|
|
326
|
+
ids.add(a.id);
|
|
327
|
+
if (!(SCHEDULE_ACTIVITY_TYPE_ENUMS as readonly string[]).includes(a.type))
|
|
328
|
+
invalid('Unknown activity type.', a.id, true);
|
|
329
|
+
for (const value of [a.plannedDurationMinutes, a.remainingDurationMinutes])
|
|
330
|
+
if (value != null && (!Number.isFinite(value) || value < 0))
|
|
331
|
+
invalid('Invalid duration; zero is used.', a.id);
|
|
332
|
+
for (const value of [
|
|
333
|
+
a.actualStartAt,
|
|
334
|
+
a.actualFinishAt,
|
|
335
|
+
a.primaryConstraint?.at,
|
|
336
|
+
a.secondaryConstraint?.at,
|
|
337
|
+
]) {
|
|
338
|
+
if (value != null && !Number.isFinite(instant(value)))
|
|
339
|
+
invalid('Invalid activity date.', a.id, true);
|
|
340
|
+
}
|
|
341
|
+
if (
|
|
342
|
+
a.actualStartAt &&
|
|
343
|
+
a.actualFinishAt &&
|
|
344
|
+
instant(a.actualFinishAt)! < instant(a.actualStartAt)!
|
|
345
|
+
)
|
|
346
|
+
invalid('Actual finish precedes actual start.', a.id, true);
|
|
347
|
+
if (
|
|
348
|
+
a.primaryConstraint &&
|
|
349
|
+
!(SCHEDULE_CONSTRAINT_TYPE_ENUMS as readonly string[]).includes(
|
|
350
|
+
a.primaryConstraint.type,
|
|
351
|
+
)
|
|
352
|
+
)
|
|
353
|
+
invalid('Unknown primary constraint type.', a.id, true);
|
|
354
|
+
if (
|
|
355
|
+
a.secondaryConstraint &&
|
|
356
|
+
!FILE_CONSTANTS.secondaryTypes.has(a.secondaryConstraint.type)
|
|
357
|
+
)
|
|
358
|
+
invalid(
|
|
359
|
+
'Secondary constraints must be soft start or finish bounds; this constraint is ignored.',
|
|
360
|
+
a.id,
|
|
361
|
+
);
|
|
362
|
+
if (
|
|
363
|
+
a.levelingPriority != null &&
|
|
364
|
+
(!Number.isInteger(a.levelingPriority) ||
|
|
365
|
+
a.levelingPriority < 1 ||
|
|
366
|
+
a.levelingPriority > 10)
|
|
367
|
+
)
|
|
368
|
+
invalid(
|
|
369
|
+
'Leveling priority must be an integer from 1 to 10; 5 is used.',
|
|
370
|
+
a.id,
|
|
371
|
+
);
|
|
372
|
+
}
|
|
373
|
+
for (const [calendarId, count] of unknown)
|
|
374
|
+
collector.add({
|
|
375
|
+
code: 'UNKNOWN_CALENDAR',
|
|
376
|
+
severity: 'WARN',
|
|
377
|
+
calendarId,
|
|
378
|
+
message: `Unknown work calendar ${calendarId}; ${count} rows use the project default.`,
|
|
379
|
+
});
|
|
380
|
+
const pairs = new Set<string>();
|
|
381
|
+
for (const rel of input.relationships) {
|
|
382
|
+
if (
|
|
383
|
+
!ids.has(rel.predecessorId) ||
|
|
384
|
+
!ids.has(rel.successorId) ||
|
|
385
|
+
rel.predecessorId === rel.successorId
|
|
386
|
+
) {
|
|
387
|
+
collector.add({
|
|
388
|
+
code: 'UNKNOWN_ACTIVITY',
|
|
389
|
+
severity: 'WARN',
|
|
390
|
+
activityId: rel.successorId,
|
|
391
|
+
relatedActivityId: rel.predecessorId,
|
|
392
|
+
message:
|
|
393
|
+
'Relationship has an unknown activity or a self-loop and is ignored.',
|
|
394
|
+
});
|
|
395
|
+
continue;
|
|
396
|
+
}
|
|
397
|
+
const pair = JSON.stringify([rel.predecessorId, rel.successorId]);
|
|
398
|
+
if (pairs.has(pair))
|
|
399
|
+
invalid(
|
|
400
|
+
'Duplicate relationship pair; the first is kept.',
|
|
401
|
+
rel.successorId,
|
|
402
|
+
);
|
|
403
|
+
pairs.add(pair);
|
|
404
|
+
if (
|
|
405
|
+
!(SCHEDULE_RELATIONSHIP_TYPE_ENUMS as readonly string[]).includes(
|
|
406
|
+
rel.type,
|
|
407
|
+
)
|
|
408
|
+
)
|
|
409
|
+
invalid(
|
|
410
|
+
'Unknown relationship type; the relationship is ignored.',
|
|
411
|
+
rel.successorId,
|
|
412
|
+
);
|
|
413
|
+
if (rel.lagMinutes != null && !Number.isFinite(rel.lagMinutes))
|
|
414
|
+
invalid('Invalid lag; zero is used.', rel.successorId);
|
|
415
|
+
}
|
|
416
|
+
const options = input.options;
|
|
417
|
+
for (const [key, allowed] of [
|
|
418
|
+
['criticalityMode', ['FLOAT', 'LONGEST_PATH']],
|
|
419
|
+
['progressLogic', ['RETAINED', 'OVERRIDE']],
|
|
420
|
+
['lagCalendar', ['PREDECESSOR', 'SUCCESSOR', 'PROJECT', 'CONTINUOUS']],
|
|
421
|
+
] as const) {
|
|
422
|
+
if (
|
|
423
|
+
options?.[key] &&
|
|
424
|
+
!(allowed as readonly string[]).includes(options[key]!)
|
|
425
|
+
)
|
|
426
|
+
invalid(`Unknown ${key}; the default is used.`);
|
|
427
|
+
}
|
|
428
|
+
if (
|
|
429
|
+
options?.leveling?.priorityRule?.some(
|
|
430
|
+
(key) =>
|
|
431
|
+
!(SCHEDULE_LEVELING_PRIORITY_RULE_ENUMS as readonly string[]).includes(
|
|
432
|
+
key,
|
|
433
|
+
),
|
|
434
|
+
)
|
|
435
|
+
)
|
|
436
|
+
invalid('Unknown leveling priority rule; the default is used.');
|
|
437
|
+
if (options?.leveling?.enabled) {
|
|
438
|
+
const resourceIds = new Set<string>();
|
|
439
|
+
for (const r of input.resources || []) {
|
|
440
|
+
if (resourceIds.has(r.id))
|
|
441
|
+
invalid(`Duplicate resource id ${r.id}.`, undefined, true);
|
|
442
|
+
resourceIds.add(r.id);
|
|
443
|
+
if (
|
|
444
|
+
r.maxUnitsPerDay != null &&
|
|
445
|
+
(!Number.isFinite(r.maxUnitsPerDay) || r.maxUnitsPerDay < 0)
|
|
446
|
+
)
|
|
447
|
+
invalid(`Invalid daily capacity for resource ${r.id}; zero is used.`);
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
return { fatal, referencedCalendarIds };
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
/* Prepare referenced calendars, keeping immutable tables separate from lazy arithmetic caches. */
|
|
454
|
+
export function buildCpmCalendarSet(
|
|
455
|
+
input: CpmScheduleInput,
|
|
456
|
+
referencedIds: Set<string>,
|
|
457
|
+
floorMs: number,
|
|
458
|
+
horizonDays: number,
|
|
459
|
+
): CpmCalendarSet {
|
|
460
|
+
const byId = new Map<string, CpmCalendar>();
|
|
461
|
+
for (const id of referencedIds) {
|
|
462
|
+
const def = input.calendars[id];
|
|
463
|
+
byId.set(id, { def, minutesPerDay: getWorkingMinutesPerDay(def) });
|
|
464
|
+
}
|
|
465
|
+
return {
|
|
466
|
+
byId,
|
|
467
|
+
defaultCalendar: byId.get(input.defaultCalendarId)!,
|
|
468
|
+
rangeFromMs: floorMs - 366 * FILE_CONSTANTS.dayMs,
|
|
469
|
+
rangeToMs:
|
|
470
|
+
floorMs + (Math.max(horizonDays, 730) + 366) * FILE_CONSTANTS.dayMs,
|
|
471
|
+
};
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
/* Resolve missing activity/resource calendar ids to the validated project default. */
|
|
475
|
+
export function getActivityCalendar(
|
|
476
|
+
set: CpmCalendarSet,
|
|
477
|
+
calendarId?: string | null,
|
|
478
|
+
): CpmCalendar {
|
|
479
|
+
return (calendarId && set.byId.get(calendarId)) || set.defaultCalendar;
|
|
480
|
+
}
|
|
481
|
+
/* Snap an instant forward to working time. */
|
|
482
|
+
export function snapStart(cal: CpmCalendar, ms: number): number {
|
|
483
|
+
return snapToWorkingStart(cal.def, new Date(ms)).getTime();
|
|
484
|
+
}
|
|
485
|
+
/* Snap an instant backward to working time. */
|
|
486
|
+
export function snapFinish(cal: CpmCalendar, ms: number): number {
|
|
487
|
+
return snapToWorkingFinish(cal.def, new Date(ms)).getTime();
|
|
488
|
+
}
|
|
489
|
+
/* Add a signed working duration on one calendar. */
|
|
490
|
+
export function addMinutes(
|
|
491
|
+
cal: CpmCalendar,
|
|
492
|
+
ms: number,
|
|
493
|
+
minutes: number,
|
|
494
|
+
): number {
|
|
495
|
+
return addWorkingMinutes(cal.def, new Date(ms), minutes).getTime();
|
|
496
|
+
}
|
|
497
|
+
/* Measure a signed whole-minute distance on one working calendar. */
|
|
498
|
+
export function minutesBetween(cal: CpmCalendar, a: number, b: number): number {
|
|
499
|
+
return Math.round(workingMinutesBetween(cal.def, new Date(a), new Date(b)));
|
|
500
|
+
}
|
|
501
|
+
/* Compare instants through working time so weekends do not break driving ties. */
|
|
502
|
+
export function isSameWorkingInstant(
|
|
503
|
+
cal: CpmCalendar,
|
|
504
|
+
a: number,
|
|
505
|
+
b: number,
|
|
506
|
+
): boolean {
|
|
507
|
+
return a === b || minutesBetween(cal, a, b) === 0;
|
|
508
|
+
}
|
|
509
|
+
/* Apply a relationship lag using the selected lag calendar. */
|
|
510
|
+
export function addLag(
|
|
511
|
+
context: CpmContext,
|
|
512
|
+
ms: number,
|
|
513
|
+
lag: number,
|
|
514
|
+
predecessor: CpmActivityState,
|
|
515
|
+
successor: CpmActivityState,
|
|
516
|
+
): number {
|
|
517
|
+
if (!lag) return ms;
|
|
518
|
+
if (context.lagCalendar === 'CONTINUOUS') return ms + lag * 60_000;
|
|
519
|
+
return addMinutes(
|
|
520
|
+
context.lagCalendar === 'PROJECT'
|
|
521
|
+
? context.calendarSet.defaultCalendar
|
|
522
|
+
: context.lagCalendar === 'SUCCESSOR'
|
|
523
|
+
? successor.calendar
|
|
524
|
+
: predecessor.calendar,
|
|
525
|
+
ms,
|
|
526
|
+
lag,
|
|
527
|
+
);
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
/* Produce Kahn order with an index queue and return only the unresolved driving graph. */
|
|
531
|
+
export function getTopologicalOrder(
|
|
532
|
+
states: Map<string, CpmActivityState>,
|
|
533
|
+
driving = true,
|
|
534
|
+
): { order: CpmActivityState[]; residual: CpmActivityState[] } {
|
|
535
|
+
const indegree = new Map<string, number>();
|
|
536
|
+
const order: CpmActivityState[] = [];
|
|
537
|
+
for (const state of states.values()) {
|
|
538
|
+
if (state.isDriving !== driving) continue;
|
|
539
|
+
const count = state.predecessors.filter(
|
|
540
|
+
(rel) => states.get(rel.predecessorId)?.isDriving === driving,
|
|
541
|
+
).length;
|
|
542
|
+
indegree.set(state.input.id, count);
|
|
543
|
+
if (!count) order.push(state);
|
|
544
|
+
}
|
|
545
|
+
for (let head = 0; head < order.length; head++) {
|
|
546
|
+
for (const rel of order[head].successors) {
|
|
547
|
+
const count = indegree.get(rel.successorId);
|
|
548
|
+
if (count == null) continue;
|
|
549
|
+
indegree.set(rel.successorId, count - 1);
|
|
550
|
+
if (count === 1) order.push(states.get(rel.successorId)!);
|
|
551
|
+
}
|
|
552
|
+
}
|
|
553
|
+
return {
|
|
554
|
+
order,
|
|
555
|
+
residual: [...indegree]
|
|
556
|
+
.filter(([, count]) => count > 0)
|
|
557
|
+
.map(([id]) => states.get(id)!),
|
|
558
|
+
};
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
/* Find actual cyclic components iteratively; descendants of loops never become cycle members. */
|
|
562
|
+
export function findStronglyConnectedComponents(
|
|
563
|
+
states: CpmActivityState[],
|
|
564
|
+
): CpmActivityState[][] {
|
|
565
|
+
const byId = new Map(states.map((state) => [state.input.id, state]));
|
|
566
|
+
const indices = new Map<string, number>();
|
|
567
|
+
const low = new Map<string, number>();
|
|
568
|
+
const active = new Set<string>();
|
|
569
|
+
const stack: CpmActivityState[] = [];
|
|
570
|
+
const components: CpmActivityState[][] = [];
|
|
571
|
+
let index = 0;
|
|
572
|
+
for (const root of states) {
|
|
573
|
+
if (indices.has(root.input.id)) continue;
|
|
574
|
+
const frames: Array<{
|
|
575
|
+
state: CpmActivityState;
|
|
576
|
+
edge: number;
|
|
577
|
+
parent?: string;
|
|
578
|
+
}> = [{ state: root, edge: 0 }];
|
|
579
|
+
while (frames.length) {
|
|
580
|
+
const frame = frames[frames.length - 1];
|
|
581
|
+
const id = frame.state.input.id;
|
|
582
|
+
if (!indices.has(id)) {
|
|
583
|
+
indices.set(id, index);
|
|
584
|
+
low.set(id, index++);
|
|
585
|
+
stack.push(frame.state);
|
|
586
|
+
active.add(id);
|
|
587
|
+
}
|
|
588
|
+
const rel = frame.state.successors[frame.edge++];
|
|
589
|
+
if (rel) {
|
|
590
|
+
const next = byId.get(rel.successorId);
|
|
591
|
+
if (!next) continue;
|
|
592
|
+
if (!indices.has(next.input.id))
|
|
593
|
+
frames.push({ state: next, edge: 0, parent: id });
|
|
594
|
+
else if (active.has(next.input.id))
|
|
595
|
+
low.set(id, Math.min(low.get(id)!, indices.get(next.input.id)!));
|
|
596
|
+
continue;
|
|
597
|
+
}
|
|
598
|
+
frames.pop();
|
|
599
|
+
if (frame.parent)
|
|
600
|
+
low.set(frame.parent, Math.min(low.get(frame.parent)!, low.get(id)!));
|
|
601
|
+
if (low.get(id) === indices.get(id)) {
|
|
602
|
+
const component: CpmActivityState[] = [];
|
|
603
|
+
let member: CpmActivityState;
|
|
604
|
+
do {
|
|
605
|
+
member = stack.pop()!;
|
|
606
|
+
active.delete(member.input.id);
|
|
607
|
+
component.push(member);
|
|
608
|
+
} while (member.input.id !== id);
|
|
609
|
+
if (component.length > 1)
|
|
610
|
+
components.push(
|
|
611
|
+
component.sort((a, b) =>
|
|
612
|
+
compareActivityIds(a.input.id, b.input.id),
|
|
613
|
+
),
|
|
614
|
+
);
|
|
615
|
+
}
|
|
616
|
+
}
|
|
617
|
+
}
|
|
618
|
+
return components.sort((a, b) =>
|
|
619
|
+
compareActivityIds(a[0].input.id, b[0].input.id),
|
|
620
|
+
);
|
|
621
|
+
}
|
|
622
|
+
|
|
623
|
+
/* Describe a real path back to the smallest member, capping only its displayed hops. */
|
|
624
|
+
export function describeCycle(members: CpmActivityState[]): string {
|
|
625
|
+
const anchor = members[0].input.id;
|
|
626
|
+
const byId = new Map(members.map((s) => [s.input.id, s]));
|
|
627
|
+
const seen = new Set([anchor]);
|
|
628
|
+
const frames = [{ id: anchor, edge: 0 }];
|
|
629
|
+
while (frames.length) {
|
|
630
|
+
const frame = frames[frames.length - 1];
|
|
631
|
+
const rel = byId.get(frame.id)!.successors[frame.edge++];
|
|
632
|
+
if (!rel) {
|
|
633
|
+
frames.pop();
|
|
634
|
+
continue;
|
|
635
|
+
}
|
|
636
|
+
if (!byId.has(rel.successorId)) continue;
|
|
637
|
+
if (rel.successorId === anchor) {
|
|
638
|
+
const path = frames.map((f) => f.id);
|
|
639
|
+
return `Relationship loop: ${path.slice(0, 20).join(' → ')}${path.length > 20 ? ` → … (${path.length - 20} more)` : ''} → ${anchor}`;
|
|
640
|
+
}
|
|
641
|
+
if (!seen.has(rel.successorId)) {
|
|
642
|
+
seen.add(rel.successorId);
|
|
643
|
+
frames.push({ id: rel.successorId, edge: 0 });
|
|
644
|
+
}
|
|
645
|
+
}
|
|
646
|
+
return `Relationship loop containing ${anchor}.`;
|
|
647
|
+
}
|
|
648
|
+
|
|
649
|
+
/* Sort WBS paths once for subtree binary searches. */
|
|
650
|
+
export function buildWbsPathIndex(
|
|
651
|
+
paths: Record<string, string>,
|
|
652
|
+
): Array<[string, string]> {
|
|
653
|
+
return Object.entries(paths).sort((a, b) =>
|
|
654
|
+
a[1] < b[1] ? -1 : a[1] > b[1] ? 1 : compareActivityIds(a[0], b[0]),
|
|
655
|
+
);
|
|
656
|
+
}
|
|
657
|
+
/* Return the contiguous range belonging to one materialized WBS ancestry prefix. */
|
|
658
|
+
export function getWbsSubtreeRange(
|
|
659
|
+
index: Array<[string, string]>,
|
|
660
|
+
prefix: string,
|
|
661
|
+
): [number, number] {
|
|
662
|
+
/* Find the first path at least equal to one lexicographic bound. */
|
|
663
|
+
const bound = (key: string) => {
|
|
664
|
+
let low = 0;
|
|
665
|
+
let high = index.length;
|
|
666
|
+
while (low < high) {
|
|
667
|
+
const mid = (low + high) >>> 1;
|
|
668
|
+
if (index[mid][1] < key) low = mid + 1;
|
|
669
|
+
else high = mid;
|
|
670
|
+
}
|
|
671
|
+
return low;
|
|
672
|
+
};
|
|
673
|
+
return [bound(prefix), bound(`${prefix}\uffff`)];
|
|
674
|
+
}
|