@kruschdev/pascal-mep-kernel 0.0.0-stage → 0.1.0

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.
@@ -0,0 +1,615 @@
1
+ import {
2
+ INCHES_TO_METERS2,
3
+ METERS_TO_FEET2,
4
+ round32
5
+ } from "./chunk-jfzqztgh.js";
6
+
7
+ // src/rules/evaluator.ts
8
+ var OUTDOOR_ROOM_TOKENS = new Set([
9
+ "exterior",
10
+ "outdoor",
11
+ "outdoors",
12
+ "patio",
13
+ "deck",
14
+ "porch",
15
+ "balcony",
16
+ "yard"
17
+ ]);
18
+ function normalizeToken(value) {
19
+ return typeof value === "string" ? value.trim().toLowerCase() : "";
20
+ }
21
+ function matchesAnyToken(value, targets) {
22
+ if (targets.length === 0)
23
+ return false;
24
+ const norm = normalizeToken(value);
25
+ if (!norm)
26
+ return false;
27
+ for (const t of targets) {
28
+ const nt = normalizeToken(t);
29
+ if (nt === "*" || nt === norm)
30
+ return true;
31
+ }
32
+ return false;
33
+ }
34
+ function clampNonNegativeMargin(margin) {
35
+ if (typeof margin !== "number" || !Number.isFinite(margin) || margin <= 0)
36
+ return 0;
37
+ return margin;
38
+ }
39
+ function effectivePlacementThreshold2(auditLimit, placementMarginM, direction) {
40
+ const margin = clampNonNegativeMargin(placementMarginM);
41
+ return direction === "min" ? auditLimit + margin : auditLimit - margin;
42
+ }
43
+ function resolveThreshold(auditLimit, placementMarginM, direction, mode) {
44
+ return mode === "placement" ? effectivePlacementThreshold2(auditLimit, placementMarginM, direction) : auditLimit;
45
+ }
46
+ function makeViolation(rule, candidate, message) {
47
+ return {
48
+ ruleId: rule.id,
49
+ codeRef: rule.codeRef,
50
+ severity: rule.severity,
51
+ category: rule.category,
52
+ title: rule.title,
53
+ message,
54
+ suggestedFix: rule.suggestedFix,
55
+ nodeIds: candidate.id ? [candidate.id] : []
56
+ };
57
+ }
58
+ function candidateY(candidate) {
59
+ const y = candidate.pos[1];
60
+ return Number.isFinite(y) ? y : 0;
61
+ }
62
+ function evaluateProhibitedZone(rule, candidate, mode) {
63
+ const { params } = rule;
64
+ if (!matchesAnyToken(candidate.kind, params.targetKinds) && !matchesAnyToken(candidate.role, params.targetKinds)) {
65
+ return [];
66
+ }
67
+ const violations = [];
68
+ const roomNorm = normalizeToken(candidate.roomCategory);
69
+ const roomProhibited = roomNorm.length > 0 && matchesAnyToken(roomNorm, params.prohibitedRoomCategories);
70
+ if (roomProhibited) {
71
+ violations.push(makeViolation(rule, candidate, `${candidate.kind} is prohibited in "${candidate.roomCategory}" (${rule.codeRef}).`));
72
+ }
73
+ const isOutdoors = candidate.isOutdoors === true || OUTDOOR_ROOM_TOKENS.has(roomNorm);
74
+ if (isOutdoors && params.forbiddenNemaOutdoors && params.forbiddenNemaOutdoors.length > 0 && matchesAnyToken(candidate.nemaRating, params.forbiddenNemaOutdoors)) {
75
+ violations.push(makeViolation(rule, candidate, `${candidate.kind} with NEMA rating "${candidate.nemaRating}" is forbidden outdoors (${rule.codeRef}).`));
76
+ }
77
+ if (params.requireBelowCeilingForAttic) {
78
+ if (candidate.ceilingHeightM !== undefined && Number.isFinite(candidate.ceilingHeightM)) {
79
+ const y = candidateY(candidate);
80
+ const maxAllowedY = resolveThreshold(candidate.ceilingHeightM, rule.placementMarginM, "max", mode);
81
+ if (y > maxAllowedY) {
82
+ violations.push(makeViolation(rule, candidate, `${candidate.kind} elevation ${round32(y)}m exceeds ceiling limit ${round32(maxAllowedY)}m (${rule.codeRef}).`));
83
+ }
84
+ } else if (roomNorm === "attic" && !roomProhibited) {
85
+ violations.push(makeViolation(rule, candidate, `${candidate.kind} is prohibited in attic space above the ceiling (${rule.codeRef}).`));
86
+ }
87
+ }
88
+ return violations;
89
+ }
90
+ function evaluateFixtureKeepout(rule, candidate, mode) {
91
+ const { params } = rule;
92
+ if (!matchesAnyToken(candidate.role, params.targetRoles) && !matchesAnyToken(candidate.kind, params.targetRoles)) {
93
+ return [];
94
+ }
95
+ const nearFixtures = candidate.nearFixtures;
96
+ if (!nearFixtures || nearFixtures.length === 0)
97
+ return [];
98
+ const y = candidateY(candidate);
99
+ const effMinH = resolveThreshold(params.minHorizontalM, rule.placementMarginM, "min", mode);
100
+ if (params.verticalBandM) {
101
+ const [bandLow, bandHigh] = params.verticalBandM;
102
+ const effLow = resolveThreshold(bandLow, rule.placementMarginM, "max", mode);
103
+ const effHigh = resolveThreshold(bandHigh, rule.placementMarginM, "min", mode);
104
+ if (y < effLow || y > effHigh)
105
+ return [];
106
+ }
107
+ if (params.minVerticalM !== undefined && Number.isFinite(params.minVerticalM)) {
108
+ const effMinV = resolveThreshold(params.minVerticalM, rule.placementMarginM, "max", mode);
109
+ if (y < effMinV)
110
+ return [];
111
+ }
112
+ if (params.maxVerticalM !== undefined && Number.isFinite(params.maxVerticalM)) {
113
+ const effMaxV = resolveThreshold(params.maxVerticalM, rule.placementMarginM, "min", mode);
114
+ if (y > effMaxV)
115
+ return [];
116
+ }
117
+ const violations = [];
118
+ for (const fx of nearFixtures) {
119
+ if (!matchesAnyToken(fx.kind, params.fixtureKinds))
120
+ continue;
121
+ if (!Number.isFinite(fx.horizontalDistM))
122
+ continue;
123
+ if (params.respectWallSeparation && (fx.separatedByWall === true || candidate.wallSeparated === true)) {
124
+ continue;
125
+ }
126
+ const violatesHorizontal = effMinH <= 0 ? fx.horizontalDistM <= 0 : fx.horizontalDistM < effMinH;
127
+ if (violatesHorizontal) {
128
+ violations.push(makeViolation(rule, candidate, `${candidate.role ?? candidate.kind} is ${round32(fx.horizontalDistM)}m from "${fx.kind}" (minimum required ${round32(effMinH)}m, ${rule.codeRef}).`));
129
+ }
130
+ }
131
+ return violations;
132
+ }
133
+ function evaluateOpeningKeepout(rule, candidate, mode) {
134
+ const { params } = rule;
135
+ if (!matchesAnyToken(candidate.role, params.targetRoles) && !matchesAnyToken(candidate.kind, params.targetRoles)) {
136
+ return [];
137
+ }
138
+ if (!candidate.openingKind)
139
+ return [];
140
+ if (candidate.openingKind === "door" && !params.blockDoors)
141
+ return [];
142
+ if (candidate.openingKind === "window" && !params.blockWindows)
143
+ return [];
144
+ const effTrim = resolveThreshold(params.trimClearanceM, rule.placementMarginM, "min", mode);
145
+ if (candidate.openingHorizontalDistM !== undefined && Number.isFinite(candidate.openingHorizontalDistM)) {
146
+ const clearHorizontally = effTrim <= 0 ? candidate.openingHorizontalDistM > 0 : candidate.openingHorizontalDistM >= effTrim;
147
+ if (clearHorizontally)
148
+ return [];
149
+ }
150
+ const y = candidateY(candidate);
151
+ const hasSill = candidate.openingSillYM !== undefined && Number.isFinite(candidate.openingSillYM);
152
+ const hasHead = candidate.openingHeadYM !== undefined && Number.isFinite(candidate.openingHeadYM);
153
+ if (params.floorToCeilingSillThresholdM !== undefined && Number.isFinite(params.floorToCeilingSillThresholdM) && hasSill) {
154
+ const effSillThreshold = resolveThreshold(params.floorToCeilingSillThresholdM, rule.placementMarginM, "min", mode);
155
+ const maxHeadY = hasHead ? candidate.openingHeadYM + effTrim : Number.POSITIVE_INFINITY;
156
+ if (candidate.openingSillYM <= effSillThreshold && y <= maxHeadY) {
157
+ return [
158
+ makeViolation(rule, candidate, `${candidate.role ?? candidate.kind} falls within floor-to-ceiling ${candidate.openingKind} glazing (sill ${round32(candidate.openingSillYM)}m <= ${round32(effSillThreshold)}m, ${rule.codeRef}).`)
159
+ ];
160
+ }
161
+ }
162
+ if (hasSill || hasHead) {
163
+ const lowY = hasSill ? candidate.openingSillYM - effTrim : Number.NEGATIVE_INFINITY;
164
+ const highY = hasHead ? candidate.openingHeadYM + effTrim : Number.POSITIVE_INFINITY;
165
+ if (y >= lowY && y <= highY) {
166
+ return [
167
+ makeViolation(rule, candidate, `${candidate.role ?? candidate.kind} at y=${round32(y)}m intersects ${candidate.openingKind} opening/trim keepout [${hasSill ? round32(lowY) : "-inf"}m, ${hasHead ? round32(highY) : "+inf"}m] (${rule.codeRef}).`)
168
+ ];
169
+ }
170
+ return [];
171
+ }
172
+ return [
173
+ makeViolation(rule, candidate, `${candidate.role ?? candidate.kind} violates ${candidate.openingKind} keepout clearance (${round32(effTrim)}m required, ${rule.codeRef}).`)
174
+ ];
175
+ }
176
+ function evaluateElevationBand(rule, candidate, mode) {
177
+ const { params } = rule;
178
+ if (!matchesAnyToken(candidate.kind, params.targetKinds) && !matchesAnyToken(candidate.role, params.targetKinds)) {
179
+ return [];
180
+ }
181
+ const violations = [];
182
+ const y = candidateY(candidate);
183
+ if (params.minYM !== undefined && Number.isFinite(params.minYM)) {
184
+ const effMinY = resolveThreshold(params.minYM, rule.placementMarginM, "min", mode);
185
+ if (y < effMinY) {
186
+ violations.push(makeViolation(rule, candidate, `${candidate.kind} elevation ${round32(y)}m is below minimum ${round32(effMinY)}m (${rule.codeRef}).`));
187
+ }
188
+ }
189
+ if (params.maxYM !== undefined && Number.isFinite(params.maxYM)) {
190
+ const effMaxY = resolveThreshold(params.maxYM, rule.placementMarginM, "max", mode);
191
+ if (y > effMaxY) {
192
+ violations.push(makeViolation(rule, candidate, `${candidate.kind} elevation ${round32(y)}m exceeds maximum ${round32(effMaxY)}m (${rule.codeRef}).`));
193
+ }
194
+ }
195
+ if (params.minAboveFloodRimM !== undefined && Number.isFinite(params.minAboveFloodRimM) && candidate.floodRimM !== undefined && Number.isFinite(candidate.floodRimM)) {
196
+ const effClearance = resolveThreshold(params.minAboveFloodRimM, rule.placementMarginM, "min", mode);
197
+ const requiredY = candidate.floodRimM + effClearance;
198
+ if (y < requiredY) {
199
+ violations.push(makeViolation(rule, candidate, `${candidate.kind} elevation ${round32(y)}m is below flood-rim minimum ${round32(requiredY)}m (${round32(candidate.floodRimM)}m + ${round32(effClearance)}m, ${rule.codeRef}).`));
200
+ }
201
+ }
202
+ if (params.maxHandleHeightM !== undefined && Number.isFinite(params.maxHandleHeightM)) {
203
+ const handleY = candidate.handleHeightM !== undefined && Number.isFinite(candidate.handleHeightM) ? candidate.handleHeightM : y;
204
+ const effMaxHandle = resolveThreshold(params.maxHandleHeightM, rule.placementMarginM, "max", mode);
205
+ if (handleY > effMaxHandle) {
206
+ violations.push(makeViolation(rule, candidate, `${candidate.kind} handle height ${round32(handleY)}m exceeds maximum ${round32(effMaxHandle)}m (${rule.codeRef}).`));
207
+ }
208
+ }
209
+ if (params.roofMarginM !== undefined && Number.isFinite(params.roofMarginM) && candidate.roofSurfaceYM !== undefined && Number.isFinite(candidate.roofSurfaceYM)) {
210
+ const effRoofMargin = resolveThreshold(params.roofMarginM, rule.placementMarginM, "min", mode);
211
+ const maxAllowedY = candidate.roofSurfaceYM - effRoofMargin;
212
+ if (y > maxAllowedY) {
213
+ violations.push(makeViolation(rule, candidate, `${candidate.kind} elevation ${round32(y)}m encroaches on roof surface ${round32(candidate.roofSurfaceYM)}m (required margin ${round32(effRoofMargin)}m, ${rule.codeRef}).`));
214
+ }
215
+ }
216
+ if (params.atticThresholdOffsetM !== undefined && Number.isFinite(params.atticThresholdOffsetM) && candidate.ceilingHeightM !== undefined && Number.isFinite(candidate.ceilingHeightM)) {
217
+ const atticBoundary = candidate.ceilingHeightM + params.atticThresholdOffsetM;
218
+ const effBoundary = resolveThreshold(atticBoundary, rule.placementMarginM, "max", mode);
219
+ if (y > effBoundary) {
220
+ violations.push(makeViolation(rule, candidate, `${candidate.kind} elevation ${round32(y)}m exceeds attic threshold ${round32(effBoundary)}m (${rule.codeRef}).`));
221
+ }
222
+ }
223
+ return violations;
224
+ }
225
+ function evaluateWorkingSpace(rule, candidate, mode) {
226
+ const { params } = rule;
227
+ if (!matchesAnyToken(candidate.kind, params.targetKinds) && !matchesAnyToken(candidate.role, params.targetKinds)) {
228
+ return [];
229
+ }
230
+ const violations = [];
231
+ if (candidate.workingSpaceWidthM !== undefined && Number.isFinite(candidate.workingSpaceWidthM)) {
232
+ const effW = resolveThreshold(params.widthM, rule.placementMarginM, "min", mode);
233
+ if (candidate.workingSpaceWidthM < effW) {
234
+ violations.push(makeViolation(rule, candidate, `${candidate.kind} working space width ${round32(candidate.workingSpaceWidthM)}m is below required ${round32(effW)}m (${rule.codeRef}).`));
235
+ }
236
+ }
237
+ if (candidate.workingSpaceDepthM !== undefined && Number.isFinite(candidate.workingSpaceDepthM)) {
238
+ const effD = resolveThreshold(params.depthM, rule.placementMarginM, "min", mode);
239
+ if (candidate.workingSpaceDepthM < effD) {
240
+ violations.push(makeViolation(rule, candidate, `${candidate.kind} working space depth ${round32(candidate.workingSpaceDepthM)}m is below required ${round32(effD)}m (${rule.codeRef}).`));
241
+ }
242
+ }
243
+ if (candidate.workingSpaceHeightM !== undefined && Number.isFinite(candidate.workingSpaceHeightM)) {
244
+ const effH = resolveThreshold(params.heightM, rule.placementMarginM, "min", mode);
245
+ if (candidate.workingSpaceHeightM < effH) {
246
+ violations.push(makeViolation(rule, candidate, `${candidate.kind} working space headroom ${round32(candidate.workingSpaceHeightM)}m is below required ${round32(effH)}m (${rule.codeRef}).`));
247
+ }
248
+ }
249
+ if (params.dedicatedOverheadHeightM !== undefined && Number.isFinite(params.dedicatedOverheadHeightM) && candidate.dedicatedOverheadClearHeightM !== undefined && Number.isFinite(candidate.dedicatedOverheadClearHeightM)) {
250
+ const effOverhead = resolveThreshold(params.dedicatedOverheadHeightM, rule.placementMarginM, "min", mode);
251
+ if (candidate.dedicatedOverheadClearHeightM < effOverhead) {
252
+ violations.push(makeViolation(rule, candidate, `${candidate.kind} dedicated overhead clearance ${round32(candidate.dedicatedOverheadClearHeightM)}m is below required ${round32(effOverhead)}m (${rule.codeRef}).`));
253
+ }
254
+ }
255
+ return violations;
256
+ }
257
+ function evaluateMaterialPolicy(rule, candidate) {
258
+ const { params } = rule;
259
+ const systemToken = candidate.system ?? candidate.kind;
260
+ if (!matchesAnyToken(systemToken, params.targetSystems)) {
261
+ return [];
262
+ }
263
+ const violations = [];
264
+ const hasContextFlag = Boolean(params.forbiddenInCommercial || params.forbiddenInPlenum || params.forbiddenInWetCrawlspace);
265
+ const hasForbiddenList = Boolean(params.forbiddenMaterials && params.forbiddenMaterials.length > 0);
266
+ const hasMaterial = candidate.material !== undefined && normalizeToken(candidate.material).length > 0;
267
+ let flaggedByForbidden = false;
268
+ if (!hasContextFlag) {
269
+ if (hasForbiddenList && hasMaterial && matchesAnyToken(candidate.material, params.forbiddenMaterials)) {
270
+ flaggedByForbidden = true;
271
+ violations.push(makeViolation(rule, candidate, `Material "${candidate.material}" is forbidden for system "${systemToken}" (${rule.codeRef}).`));
272
+ }
273
+ } else {
274
+ const materialMatches = hasForbiddenList ? hasMaterial && matchesAnyToken(candidate.material, params.forbiddenMaterials) : true;
275
+ if (materialMatches) {
276
+ const matLabel = hasMaterial ? `Material "${candidate.material}"` : `System "${systemToken}"`;
277
+ if (params.forbiddenInCommercial && candidate.occupancy === "commercial") {
278
+ flaggedByForbidden = true;
279
+ violations.push(makeViolation(rule, candidate, `${matLabel} is forbidden in commercial occupancy (${rule.codeRef}).`));
280
+ }
281
+ if (params.forbiddenInPlenum && candidate.inPlenum === true) {
282
+ flaggedByForbidden = true;
283
+ violations.push(makeViolation(rule, candidate, `${matLabel} is forbidden in environmental air plenums (${rule.codeRef}).`));
284
+ }
285
+ if (params.forbiddenInWetCrawlspace && candidate.inWetCrawlspace === true) {
286
+ flaggedByForbidden = true;
287
+ violations.push(makeViolation(rule, candidate, `${matLabel} is forbidden in wet crawlspaces (${rule.codeRef}).`));
288
+ }
289
+ }
290
+ }
291
+ if (!flaggedByForbidden && params.requiredMaterials && params.requiredMaterials.length > 0 && hasMaterial && !matchesAnyToken(candidate.material, params.requiredMaterials)) {
292
+ violations.push(makeViolation(rule, candidate, `Material "${candidate.material}" is not permitted for "${systemToken}"; required one of [${params.requiredMaterials.join(", ")}] (${rule.codeRef}).`));
293
+ }
294
+ return violations;
295
+ }
296
+ function evaluateRoutingConstraint(rule, candidate, mode) {
297
+ const { params } = rule;
298
+ const systemToken = candidate.system ?? candidate.kind;
299
+ if (!matchesAnyToken(systemToken, params.targetSystems)) {
300
+ return [];
301
+ }
302
+ const violations = [];
303
+ const marginFt = clampNonNegativeMargin(rule.placementMarginM) * METERS_TO_FEET2;
304
+ if (params.maxBendDegrees !== undefined && Number.isFinite(params.maxBendDegrees) && candidate.bendDegrees !== undefined && Number.isFinite(candidate.bendDegrees)) {
305
+ if (candidate.bendDegrees > params.maxBendDegrees) {
306
+ violations.push(makeViolation(rule, candidate, `${systemToken} total bends ${round32(candidate.bendDegrees)}° exceed maximum ${round32(params.maxBendDegrees)}° (${rule.codeRef}).`));
307
+ }
308
+ }
309
+ if (params.minSlopeInPerFt !== undefined && Number.isFinite(params.minSlopeInPerFt) && candidate.slopeInPerFt !== undefined && Number.isFinite(candidate.slopeInPerFt)) {
310
+ const slopeMarginInPerFt = clampNonNegativeMargin(params.placementSlopeMarginInPerFt);
311
+ const effMinSlope = resolveThreshold(params.minSlopeInPerFt, slopeMarginInPerFt, "min", mode);
312
+ if (candidate.slopeInPerFt < effMinSlope) {
313
+ violations.push(makeViolation(rule, candidate, `${systemToken} slope ${round32(candidate.slopeInPerFt)} in/ft is below minimum ${round32(effMinSlope)} in/ft (${rule.codeRef}).`));
314
+ }
315
+ }
316
+ if (params.maxEffectiveLengthFt !== undefined && Number.isFinite(params.maxEffectiveLengthFt) && candidate.effectiveLengthFt !== undefined && Number.isFinite(candidate.effectiveLengthFt)) {
317
+ const effMaxFt = resolveThreshold(params.maxEffectiveLengthFt, marginFt, "max", mode);
318
+ if (candidate.effectiveLengthFt > effMaxFt) {
319
+ violations.push(makeViolation(rule, candidate, `${systemToken} effective length ${round32(candidate.effectiveLengthFt)} ft exceeds maximum ${round32(effMaxFt)} ft (${rule.codeRef}).`));
320
+ }
321
+ }
322
+ if (params.maxLengthFtWithoutCleanout !== undefined && Number.isFinite(params.maxLengthFtWithoutCleanout) && candidate.hasCleanout !== true) {
323
+ const runLenFt = candidate.runLengthFt !== undefined && Number.isFinite(candidate.runLengthFt) ? candidate.runLengthFt : candidate.effectiveLengthFt;
324
+ if (runLenFt !== undefined && Number.isFinite(runLenFt)) {
325
+ const effMaxCleanoutFt = resolveThreshold(params.maxLengthFtWithoutCleanout, marginFt, "max", mode);
326
+ if (runLenFt > effMaxCleanoutFt) {
327
+ violations.push(makeViolation(rule, candidate, `${systemToken} run length ${round32(runLenFt)} ft exceeds ${round32(effMaxCleanoutFt)} ft without a cleanout (${rule.codeRef}).`));
328
+ }
329
+ }
330
+ }
331
+ if (params.minNominalSizeIn !== undefined && Number.isFinite(params.minNominalSizeIn) && candidate.nominalSizeIn !== undefined && Number.isFinite(candidate.nominalSizeIn)) {
332
+ if (candidate.nominalSizeIn < params.minNominalSizeIn) {
333
+ violations.push(makeViolation(rule, candidate, `${systemToken} nominal size ${round32(candidate.nominalSizeIn)}" is below minimum ${round32(params.minNominalSizeIn)}" (${rule.codeRef}).`));
334
+ }
335
+ }
336
+ return violations;
337
+ }
338
+ function evaluateWallCavityFit(rule, candidate, mode) {
339
+ const { params } = rule;
340
+ if (candidate.nominalSizeIn === undefined || !Number.isFinite(candidate.nominalSizeIn) || candidate.wallThicknessM === undefined || !Number.isFinite(candidate.wallThicknessM)) {
341
+ return [];
342
+ }
343
+ if (candidate.nominalSizeIn < params.minPipeNominalIn) {
344
+ return [];
345
+ }
346
+ const effMinWall = resolveThreshold(params.minWallThicknessM, rule.placementMarginM, "min", mode);
347
+ const is2x4Wall = candidate.wallThicknessM <= params.max2x4WallThicknessM;
348
+ if (is2x4Wall || candidate.wallThicknessM < effMinWall) {
349
+ const wallNote = is2x4Wall ? `2x4 wall (${round32(candidate.wallThicknessM)}m <= ${round32(params.max2x4WallThicknessM)}m)` : `wall thickness ${round32(candidate.wallThicknessM)}m`;
350
+ return [
351
+ makeViolation(rule, candidate, `${round32(candidate.nominalSizeIn)}" pipe cannot fit in ${wallNote}; requires wall thickness >= ${round32(effMinWall)}m (${rule.codeRef}).`)
352
+ ];
353
+ }
354
+ return [];
355
+ }
356
+ function evaluateWindSeismicThreshold(rule, candidate, mode) {
357
+ const { params } = rule;
358
+ const violations = [];
359
+ const wind = candidate.designWindMph !== undefined && Number.isFinite(candidate.designWindMph) ? candidate.designWindMph : undefined;
360
+ const sdc = candidate.seismicDesignCategory;
361
+ const isHighSeismic = sdc !== undefined && matchesAnyToken(sdc, params.highSeismicCategories);
362
+ const isSillBoltSeismic = sdc !== undefined && matchesAnyToken(sdc, params.sillBoltSeismicCategories);
363
+ const marginIn = clampNonNegativeMargin(rule.placementMarginM) / INCHES_TO_METERS2;
364
+ if (candidate.hasRoofTie === false && (wind !== undefined && wind >= params.roofTieWindMph || isHighSeismic)) {
365
+ violations.push(makeViolation(rule, candidate, `Roof-to-wall tie required at ${wind !== undefined ? `${round32(wind)} mph` : `SDC ${sdc}`} (${rule.codeRef}).`));
366
+ }
367
+ if (candidate.hasStrap === false && (wind !== undefined && wind >= params.strapWindMph || isHighSeismic)) {
368
+ violations.push(makeViolation(rule, candidate, `Uplift strap required at ${wind !== undefined ? `${round32(wind)} mph` : `SDC ${sdc}`} (${rule.codeRef}).`));
369
+ }
370
+ if (candidate.hasHeavyStrap === false && wind !== undefined && wind >= params.heavyStrapWindMph) {
371
+ violations.push(makeViolation(rule, candidate, `Heavy uplift strap required at ${round32(wind)} mph (>= ${round32(params.heavyStrapWindMph)} mph, ${rule.codeRef}).`));
372
+ }
373
+ if (candidate.hasHoldDown === false && (wind !== undefined && wind >= params.holdDownWindMph || isHighSeismic)) {
374
+ violations.push(makeViolation(rule, candidate, `Hold-down connector required at ${wind !== undefined ? `${round32(wind)} mph` : `SDC ${sdc}`} (${rule.codeRef}).`));
375
+ }
376
+ if (candidate.hasSillBolt === false && isSillBoltSeismic) {
377
+ violations.push(makeViolation(rule, candidate, `Seismic sill anchor bolt required in SDC "${sdc}" (${rule.codeRef}).`));
378
+ }
379
+ if (candidate.footingWidthIn !== undefined && Number.isFinite(candidate.footingWidthIn)) {
380
+ const effMinFooting = resolveThreshold(params.minFootingWidthIn, marginIn, "min", mode);
381
+ if (candidate.footingWidthIn < effMinFooting) {
382
+ violations.push(makeViolation(rule, candidate, `Spread footing width ${round32(candidate.footingWidthIn)}" is below minimum ${round32(effMinFooting)}" (${rule.codeRef}).`));
383
+ }
384
+ }
385
+ if (candidate.bearingIn !== undefined && Number.isFinite(candidate.bearingIn)) {
386
+ let effMinBearing;
387
+ if (params.minBearingLengthIn !== undefined && Number.isFinite(params.minBearingLengthIn)) {
388
+ const baseMinBearingIn = Math.max(0, params.minBearingLengthIn);
389
+ const toleranceIn = clampNonNegativeMargin(params.minBearingToleranceIn);
390
+ effMinBearing = mode === "audit" ? Math.max(0, baseMinBearingIn - toleranceIn) : resolveThreshold(baseMinBearingIn, marginIn, "min", mode);
391
+ } else {
392
+ effMinBearing = resolveThreshold(params.minBearingToleranceIn, marginIn, "min", mode);
393
+ }
394
+ if (candidate.bearingIn < effMinBearing) {
395
+ violations.push(makeViolation(rule, candidate, `Bearing length ${round32(candidate.bearingIn)}" is below minimum ${round32(effMinBearing)}" (${rule.codeRef}).`));
396
+ }
397
+ }
398
+ return violations;
399
+ }
400
+ function evaluateSingleRule(rule, candidate, mode) {
401
+ switch (rule.kind) {
402
+ case "prohibited-zone":
403
+ return evaluateProhibitedZone(rule, candidate, mode);
404
+ case "fixture-keepout":
405
+ return evaluateFixtureKeepout(rule, candidate, mode);
406
+ case "opening-keepout":
407
+ return evaluateOpeningKeepout(rule, candidate, mode);
408
+ case "elevation-band":
409
+ return evaluateElevationBand(rule, candidate, mode);
410
+ case "working-space":
411
+ return evaluateWorkingSpace(rule, candidate, mode);
412
+ case "material-policy":
413
+ return evaluateMaterialPolicy(rule, candidate);
414
+ case "routing-constraint":
415
+ return evaluateRoutingConstraint(rule, candidate, mode);
416
+ case "wall-cavity-fit":
417
+ return evaluateWallCavityFit(rule, candidate, mode);
418
+ case "wind-seismic-threshold":
419
+ return evaluateWindSeismicThreshold(rule, candidate, mode);
420
+ default: {
421
+ const _exhaustive = rule;
422
+ return _exhaustive;
423
+ }
424
+ }
425
+ }
426
+ function evaluatePlacementCandidate2(rules, candidate, mode = "placement") {
427
+ const violations = [];
428
+ for (const rule of rules) {
429
+ const ruleViolations = evaluateSingleRule(rule, candidate, mode);
430
+ for (const v of ruleViolations) {
431
+ violations.push(v);
432
+ }
433
+ }
434
+ return {
435
+ legal: violations.length === 0,
436
+ violations
437
+ };
438
+ }
439
+ function filterLegalCandidates2(rules, candidates) {
440
+ const legal = [];
441
+ const rejected = [];
442
+ for (const candidate of candidates) {
443
+ const result = evaluatePlacementCandidate2(rules, candidate, "placement");
444
+ if (result.legal) {
445
+ legal.push(candidate);
446
+ } else {
447
+ rejected.push({ candidate, violations: result.violations });
448
+ }
449
+ }
450
+ return { legal, rejected };
451
+ }
452
+ function evaluateDeclarativeRules2(rules, candidates, opts) {
453
+ const activeRules = rules.filter((r) => r.trade === opts.trade);
454
+ const violations = [];
455
+ for (const candidate of candidates) {
456
+ const result = evaluatePlacementCandidate2(activeRules, candidate, "audit");
457
+ for (const v of result.violations) {
458
+ violations.push(v);
459
+ }
460
+ }
461
+ const errorCount = violations.filter((v) => v.severity === "error").length;
462
+ const warningCount = violations.filter((v) => v.severity === "warning").length;
463
+ const ok = errorCount === 0;
464
+ const tool = opts.tool ?? "evaluate_declarative_rules";
465
+ const summary = violations.length === 0 ? `${opts.trade} audit passed: 0 violations across ${candidates.length} candidate(s) (${activeRules.length} rule(s) evaluated).` : `${opts.trade} audit found ${violations.length} violation(s) (${errorCount} error(s), ${warningCount} warning(s)) across ${candidates.length} candidate(s) (${activeRules.length} rule(s) evaluated).`;
466
+ return {
467
+ ok,
468
+ tool,
469
+ summary,
470
+ data: {
471
+ trade: opts.trade,
472
+ rulesEvaluated: activeRules.length,
473
+ violationCount: violations.length,
474
+ violations
475
+ }
476
+ };
477
+ }
478
+ function validateRuleTable2(rules) {
479
+ const errors = [];
480
+ const seenIds = new Set;
481
+ for (let i = 0;i < rules.length; i++) {
482
+ const rule = rules[i];
483
+ const label = rule.id?.trim() ? `rule "${rule.id}" (index ${i})` : `rule at index ${i}`;
484
+ if (typeof rule.id !== "string" || rule.id.trim().length === 0) {
485
+ errors.push(`${label}: id must be a non-empty string.`);
486
+ } else if (seenIds.has(rule.id)) {
487
+ errors.push(`${label}: duplicate rule id "${rule.id}".`);
488
+ } else {
489
+ seenIds.add(rule.id);
490
+ }
491
+ if (typeof rule.codeRef !== "string" || rule.codeRef.trim().length === 0) {
492
+ errors.push(`${label}: codeRef must be a non-empty string.`);
493
+ }
494
+ if (rule.placementMarginM !== undefined) {
495
+ if (typeof rule.placementMarginM !== "number" || !Number.isFinite(rule.placementMarginM) || rule.placementMarginM < 0) {
496
+ errors.push(`${label}: placementMarginM must be a finite number >= 0 (got ${String(rule.placementMarginM)}).`);
497
+ }
498
+ }
499
+ switch (rule.kind) {
500
+ case "fixture-keepout": {
501
+ if (!Number.isFinite(rule.params.minHorizontalM) || rule.params.minHorizontalM < 0) {
502
+ errors.push(`${label}: minHorizontalM must be a finite number >= 0.`);
503
+ }
504
+ if (rule.params.minVerticalM !== undefined && (!Number.isFinite(rule.params.minVerticalM) || rule.params.minVerticalM < 0)) {
505
+ errors.push(`${label}: minVerticalM must be a finite number >= 0.`);
506
+ }
507
+ if (rule.params.maxVerticalM !== undefined && (!Number.isFinite(rule.params.maxVerticalM) || rule.params.maxVerticalM < 0)) {
508
+ errors.push(`${label}: maxVerticalM must be a finite number >= 0.`);
509
+ }
510
+ if (rule.params.minVerticalM !== undefined && rule.params.maxVerticalM !== undefined && Number.isFinite(rule.params.minVerticalM) && Number.isFinite(rule.params.maxVerticalM) && rule.params.minVerticalM > rule.params.maxVerticalM) {
511
+ errors.push(`${label}: minVerticalM (${rule.params.minVerticalM}) cannot exceed maxVerticalM (${rule.params.maxVerticalM}).`);
512
+ }
513
+ if (rule.params.verticalBandM) {
514
+ const [low, high] = rule.params.verticalBandM;
515
+ if (!Number.isFinite(low) || !Number.isFinite(high) || low > high) {
516
+ errors.push(`${label}: verticalBandM must be finite [low, high] with low <= high.`);
517
+ }
518
+ }
519
+ break;
520
+ }
521
+ case "opening-keepout": {
522
+ if (!Number.isFinite(rule.params.trimClearanceM) || rule.params.trimClearanceM < 0) {
523
+ errors.push(`${label}: trimClearanceM must be a finite number >= 0.`);
524
+ }
525
+ if (rule.params.floorToCeilingSillThresholdM !== undefined && (!Number.isFinite(rule.params.floorToCeilingSillThresholdM) || rule.params.floorToCeilingSillThresholdM < 0)) {
526
+ errors.push(`${label}: floorToCeilingSillThresholdM must be a finite number >= 0.`);
527
+ }
528
+ break;
529
+ }
530
+ case "elevation-band": {
531
+ if (rule.params.minYM !== undefined && !Number.isFinite(rule.params.minYM)) {
532
+ errors.push(`${label}: minYM must be a finite number.`);
533
+ }
534
+ if (rule.params.maxYM !== undefined && !Number.isFinite(rule.params.maxYM)) {
535
+ errors.push(`${label}: maxYM must be a finite number.`);
536
+ }
537
+ if (rule.params.minYM !== undefined && rule.params.maxYM !== undefined && Number.isFinite(rule.params.minYM) && Number.isFinite(rule.params.maxYM) && rule.params.minYM > rule.params.maxYM) {
538
+ errors.push(`${label}: minYM (${rule.params.minYM}) cannot exceed maxYM (${rule.params.maxYM}).`);
539
+ }
540
+ if (rule.params.minAboveFloodRimM !== undefined && (!Number.isFinite(rule.params.minAboveFloodRimM) || rule.params.minAboveFloodRimM < 0)) {
541
+ errors.push(`${label}: minAboveFloodRimM must be a finite number >= 0.`);
542
+ }
543
+ if (rule.params.maxHandleHeightM !== undefined && (!Number.isFinite(rule.params.maxHandleHeightM) || rule.params.maxHandleHeightM <= 0)) {
544
+ errors.push(`${label}: maxHandleHeightM must be a finite number > 0.`);
545
+ }
546
+ if (rule.params.roofMarginM !== undefined && !Number.isFinite(rule.params.roofMarginM)) {
547
+ errors.push(`${label}: roofMarginM must be a finite number.`);
548
+ }
549
+ if (rule.params.atticThresholdOffsetM !== undefined && !Number.isFinite(rule.params.atticThresholdOffsetM)) {
550
+ errors.push(`${label}: atticThresholdOffsetM must be a finite number.`);
551
+ }
552
+ break;
553
+ }
554
+ case "working-space": {
555
+ if (!Number.isFinite(rule.params.widthM) || rule.params.widthM < 0 || !Number.isFinite(rule.params.depthM) || rule.params.depthM < 0 || !Number.isFinite(rule.params.heightM) || rule.params.heightM < 0) {
556
+ errors.push(`${label}: widthM, depthM, and heightM must be finite numbers >= 0.`);
557
+ }
558
+ if (rule.params.dedicatedOverheadHeightM !== undefined && (!Number.isFinite(rule.params.dedicatedOverheadHeightM) || rule.params.dedicatedOverheadHeightM < 0)) {
559
+ errors.push(`${label}: dedicatedOverheadHeightM must be a finite number >= 0.`);
560
+ }
561
+ break;
562
+ }
563
+ case "routing-constraint": {
564
+ const optionalNonNegFields = [
565
+ ["maxBendDegrees", rule.params.maxBendDegrees],
566
+ ["minSlopeInPerFt", rule.params.minSlopeInPerFt],
567
+ ["placementSlopeMarginInPerFt", rule.params.placementSlopeMarginInPerFt],
568
+ ["maxEffectiveLengthFt", rule.params.maxEffectiveLengthFt],
569
+ ["maxLengthFtWithoutCleanout", rule.params.maxLengthFtWithoutCleanout],
570
+ ["minNominalSizeIn", rule.params.minNominalSizeIn]
571
+ ];
572
+ for (const [field, val] of optionalNonNegFields) {
573
+ if (val !== undefined && (!Number.isFinite(val) || val < 0)) {
574
+ errors.push(`${label}: ${field} must be a finite number >= 0.`);
575
+ }
576
+ }
577
+ break;
578
+ }
579
+ case "wall-cavity-fit": {
580
+ if (!Number.isFinite(rule.params.minPipeNominalIn) || rule.params.minPipeNominalIn < 0 || !Number.isFinite(rule.params.minWallThicknessM) || rule.params.minWallThicknessM < 0 || !Number.isFinite(rule.params.max2x4WallThicknessM) || rule.params.max2x4WallThicknessM < 0) {
581
+ errors.push(`${label}: wall-cavity-fit dimensions must be finite numbers >= 0.`);
582
+ }
583
+ break;
584
+ }
585
+ case "wind-seismic-threshold": {
586
+ const nums = [
587
+ rule.params.roofTieWindMph,
588
+ rule.params.strapWindMph,
589
+ rule.params.heavyStrapWindMph,
590
+ rule.params.holdDownWindMph,
591
+ rule.params.minFootingWidthIn,
592
+ rule.params.minBearingToleranceIn
593
+ ];
594
+ if (nums.some((n) => !Number.isFinite(n) || n < 0)) {
595
+ errors.push(`${label}: wind-seismic-threshold numeric limits must be finite numbers >= 0.`);
596
+ }
597
+ if (rule.params.minBearingLengthIn !== undefined && (!Number.isFinite(rule.params.minBearingLengthIn) || rule.params.minBearingLengthIn < 0)) {
598
+ errors.push(`${label}: minBearingLengthIn must be a finite number >= 0.`);
599
+ }
600
+ break;
601
+ }
602
+ case "prohibited-zone":
603
+ case "material-policy":
604
+ break;
605
+ default: {
606
+ const _exhaustive = rule;
607
+ }
608
+ }
609
+ }
610
+ return {
611
+ valid: errors.length === 0,
612
+ errors
613
+ };
614
+ }
615
+ export { effectivePlacementThreshold2, evaluatePlacementCandidate2, filterLegalCandidates2, evaluateDeclarativeRules2, validateRuleTable2 };
@@ -0,0 +1,9 @@
1
+ /**
2
+ * `@kruschdev/pascal-mep-kernel` — Shared zero-dependency 3-layer spatial anatomy,
3
+ * legal-cavity MEP routing graph, and single-source declarative code constraint engine
4
+ * for Pascal Editor plugins.
5
+ */
6
+ export * from './types';
7
+ export * from './anatomy/index';
8
+ export * from './router/index';
9
+ export * from './rules/index';