onroute-policy-engine 2.0.0 → 2.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,4 @@
1
+ import { Policy } from 'onroute-policy-engine';
2
+ import { AxleConfiguration } from 'onroute-policy-engine/types';
3
+ export declare function getVehicleDisplayCodeHelper(policy: Policy, configuration: Array<string>, axleConfiguration: Array<AxleConfiguration>): string;
4
+ export declare function getUniversalDisplayCode(policy: Policy, axleConfiguration: Array<AxleConfiguration>): string;
@@ -0,0 +1,183 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getVehicleDisplayCodeHelper = getVehicleDisplayCodeHelper;
4
+ exports.getUniversalDisplayCode = getUniversalDisplayCode;
5
+ function getVehicleDisplayCodeHelper(policy, configuration, axleConfiguration) {
6
+ if (!policy.policyDefinition.vehicleDisplayCodeDefaults) {
7
+ throw new Error('Unable to construct vehicle display code; missing vehicleDisplayCodeDefaults in policy configuration');
8
+ }
9
+ // Empty configuration, empty display code
10
+ if (configuration.length === 0) {
11
+ return '';
12
+ }
13
+ if (canCreateStandardCode(policy, configuration, axleConfiguration)) {
14
+ const defs = policy.policyDefinition.vehicleDisplayCodeDefaults;
15
+ const displayCodeTokens = [];
16
+ displayCodeTokens.push(defs.prefixStandard || '');
17
+ const powerUnit = policy.getPowerUnitDefinition(configuration[0]);
18
+ // Add the power unit display code, e.g. TT
19
+ displayCodeTokens.push((powerUnit === null || powerUnit === void 0 ? void 0 : powerUnit.displayCodePrefix) || '');
20
+ // Add the number of axles in the steer axle unit
21
+ displayCodeTokens.push(axleConfiguration[0].numberOfAxles.toString());
22
+ // Add the power unit steer axle display code, e.g. S
23
+ displayCodeTokens.push((powerUnit === null || powerUnit === void 0 ? void 0 : powerUnit.displayCodeSteerAxle) || '');
24
+ // Add the steer axle unit index label (first axle unit)
25
+ displayCodeTokens.push('1');
26
+ // Add the padding if necessary. Padding is added once for
27
+ // every axle above 1 in the steer axle unit.
28
+ displayCodeTokens.push(defs.paddingStandard.repeat(axleConfiguration[0].numberOfAxles - 1));
29
+ // Add the number of axles in the drive axle unit
30
+ displayCodeTokens.push(axleConfiguration[1].numberOfAxles.toString());
31
+ // Add the power unit drive axle display code, e.g. D
32
+ displayCodeTokens.push((powerUnit === null || powerUnit === void 0 ? void 0 : powerUnit.displayCodeDriveAxle) || '');
33
+ // Add the drive axle unit index label (second axle unit)
34
+ displayCodeTokens.push('2');
35
+ // Add the padding if necessary. Padding is added once for
36
+ // every axle above 1 in the drive axle unit.
37
+ if (axleConfiguration.length > 2) {
38
+ displayCodeTokens.push(defs.paddingStandard.repeat(axleConfiguration[1].numberOfAxles - 1));
39
+ }
40
+ let currentAxleIndex = 2;
41
+ configuration.slice(1).forEach((v) => {
42
+ const trailer = policy.getTrailerDefinition(v);
43
+ if (!(trailer === null || trailer === void 0 ? void 0 : trailer.ignoreForAxleCalculation)) {
44
+ // Add the number of axles of the trailer
45
+ displayCodeTokens.push(axleConfiguration[currentAxleIndex].numberOfAxles.toString());
46
+ // Add the trailer display code, e.g. T
47
+ displayCodeTokens.push((trailer === null || trailer === void 0 ? void 0 : trailer.displayCode) || '');
48
+ // Add the axle unit index label, with prefix if axle unit
49
+ // number is more than a single digit, e.g. '.10'
50
+ if (currentAxleIndex >= 9) {
51
+ displayCodeTokens.push(defs.multiDigitPrefix);
52
+ }
53
+ displayCodeTokens.push((currentAxleIndex + 1).toString());
54
+ // Add the padding if necessary. Padding is added once for
55
+ // every axle above 1 in the drive axle unit.
56
+ if (axleConfiguration.length > currentAxleIndex + 1) {
57
+ displayCodeTokens.push(defs.paddingStandard.repeat(axleConfiguration[currentAxleIndex].numberOfAxles - 1));
58
+ }
59
+ currentAxleIndex++;
60
+ }
61
+ });
62
+ return displayCodeTokens.join('');
63
+ }
64
+ else {
65
+ return getUniversalDisplayCode(policy, axleConfiguration);
66
+ }
67
+ }
68
+ function getUniversalDisplayCode(policy, axleConfiguration) {
69
+ const defs = policy.policyDefinition.vehicleDisplayCodeDefaults;
70
+ if (!defs) {
71
+ throw new Error('Unable to construct vehicle display code; missing vehicleDisplayCodeDefaults in policy configuration');
72
+ }
73
+ // Empty axle configuration, empty display code
74
+ if (axleConfiguration.length === 0) {
75
+ return '';
76
+ }
77
+ const displayCodeTokens = [];
78
+ displayCodeTokens.push(defs.prefixUniversal || '');
79
+ let currentAxleIndex = 0;
80
+ axleConfiguration.forEach((a) => {
81
+ if (currentAxleIndex > 0) {
82
+ // Add the universal spacing glyph, e.g. 'MU' , if this is
83
+ // not the first axle unit
84
+ if (a.interaxleSpacing &&
85
+ a.interaxleSpacing <= defs.spacingUniversalSmallMax) {
86
+ displayCodeTokens.push(defs.spacingUniversalSmall);
87
+ }
88
+ else if (a.interaxleSpacing &&
89
+ a.interaxleSpacing >= defs.spacingUniversalLargeMin) {
90
+ displayCodeTokens.push(defs.spacingUniversalLarge);
91
+ }
92
+ else {
93
+ displayCodeTokens.push(defs.spacingUniversalDefault);
94
+ }
95
+ }
96
+ if (a.numberOfAxles >= defs.thresholdAxlesUniversal) {
97
+ // The number of axles in this axle unit is above the threshold
98
+ // for the number of axles that can be represented by the simple
99
+ // universal formula
100
+ displayCodeTokens.push(defs.thresholdAxlesUniversal.toString());
101
+ // Add the over axles code, e.g. '+'
102
+ displayCodeTokens.push(defs.overAxlesCodeUniversal);
103
+ // Add the universal axle code, e.g. 'U'
104
+ displayCodeTokens.push(defs.universalAxleCode);
105
+ // Add the axle unit index label, with prefix if axle unit
106
+ // number is more than a single digit, e.g. '.10'
107
+ if (currentAxleIndex >= 9) {
108
+ displayCodeTokens.push(defs.multiDigitPrefix);
109
+ }
110
+ displayCodeTokens.push((currentAxleIndex + 1).toString());
111
+ // Add the padding. Padding is added once for every axle
112
+ // above 1, up to the universal threshold.
113
+ displayCodeTokens.push(defs.paddingUniversal.repeat(defs.thresholdAxlesUniversal - 1));
114
+ // Add the extra axle unit glyps, e.g. 'XU', for
115
+ // each axle unit above the universal threshold
116
+ for (let i = defs.thresholdAxlesUniversal + 1; i < a.numberOfAxles; i++) {
117
+ displayCodeTokens.push(defs.extraAxleUniversal);
118
+ }
119
+ // Add the end axle unit glyph, e.g. 'EU'
120
+ displayCodeTokens.push(defs.endAxleUniversal);
121
+ }
122
+ else {
123
+ // Add the number of axles in the axle unit, e.g. '2'
124
+ displayCodeTokens.push(a.numberOfAxles.toString());
125
+ // Add the universal axle code, e.g. 'U'
126
+ displayCodeTokens.push(defs.universalAxleCode);
127
+ // Add the axle unit index label, with prefix if axle unit
128
+ // number is more than a single digit, e.g. '.10'
129
+ if (currentAxleIndex >= 9) {
130
+ displayCodeTokens.push(defs.multiDigitPrefix);
131
+ }
132
+ displayCodeTokens.push((currentAxleIndex + 1).toString());
133
+ // Add the padding if necessary. Padding is added once for
134
+ // every axle above 1 in the axle unit.
135
+ if (axleConfiguration.length > currentAxleIndex + 1) {
136
+ displayCodeTokens.push(defs.paddingUniversal.repeat(axleConfiguration[currentAxleIndex].numberOfAxles - 1));
137
+ }
138
+ }
139
+ currentAxleIndex++;
140
+ });
141
+ return displayCodeTokens.join('');
142
+ }
143
+ function canCreateStandardCode(policy, configuration, axleConfiguration) {
144
+ var _a;
145
+ // Check that all of the vehicle types are known,
146
+ // and that all have display codes configured
147
+ const powerUnit = policy.getPowerUnitDefinition(configuration[0]);
148
+ if (!powerUnit ||
149
+ !powerUnit.displayCodePrefix ||
150
+ !powerUnit.displayCodeSteerAxle ||
151
+ !powerUnit.displayCodeDriveAxle) {
152
+ return false;
153
+ }
154
+ // Check all the trailers (if there are any)
155
+ for (let i = 1; i < configuration.length; i++) {
156
+ const trailer = policy.getTrailerDefinition(configuration[i]);
157
+ if (!trailer ||
158
+ (!trailer.displayCode && !trailer.ignoreForAxleCalculation)) {
159
+ return false;
160
+ }
161
+ }
162
+ // Check that none of the axles exceed the max standard
163
+ const maxAxles = ((_a = policy.policyDefinition.vehicleDisplayCodeDefaults) === null || _a === void 0 ? void 0 : _a.maxAxlesStandard) || 0;
164
+ for (let j = 0; j < axleConfiguration.length; j++) {
165
+ if (axleConfiguration[j].numberOfAxles > maxAxles) {
166
+ return false;
167
+ }
168
+ }
169
+ // Count all non-ignorable vehicles
170
+ const vehicleCount = configuration.filter((v) => {
171
+ const vehicle = policy.getVehicleDefinition(v);
172
+ if (vehicle && !vehicle.ignoreForAxleCalculation) {
173
+ return true;
174
+ }
175
+ return false;
176
+ }).length;
177
+ // The vehicle list does not correspond with the number of non-
178
+ // ignorable axles, so the universal diagram is the only option
179
+ if (axleConfiguration.length !== vehicleCount + 1) {
180
+ return false;
181
+ }
182
+ return true;
183
+ }
@@ -94,7 +94,6 @@ function addRuntimeFacts(engine, policy) {
94
94
  const dateFrom = (0, dayjs_1.default)(dateFromStr, enum_1.PermitAppInfo.PermitDateFormat);
95
95
  const dateTo = (0, dayjs_1.default)(dateToStr, enum_1.PermitAppInfo.PermitDateFormat);
96
96
  const daysBetween = dateTo.diff(dateFrom, 'day');
97
- console.log(`Calculated daysBetween '${dateFromStr}' and '${dateToStr}' as '${daysBetween}'`);
98
97
  return daysBetween;
99
98
  });
100
99
  /**
@@ -245,4 +245,5 @@ export declare class Policy {
245
245
  * none are configured
246
246
  */
247
247
  getStandardTireSizes(): Array<StandardTireSize>;
248
+ getVehicleDisplayCode(configuration: Array<string>, axleConfiguration: Array<AxleConfiguration>): string;
248
249
  }
@@ -19,6 +19,7 @@ const vehicles_helper_1 = require("./helper/vehicles.helper");
19
19
  const conditions_helper_1 = require("./helper/conditions.helper");
20
20
  const dimensions_helper_1 = require("./helper/dimensions.helper");
21
21
  const dimensions_helper_2 = require("./helper/dimensions.helper");
22
+ const display_code_helper_1 = require("./helper/display-code-helper");
22
23
  /** Class representing commercial vehicle policy. */
23
24
  class Policy {
24
25
  /**
@@ -26,18 +27,19 @@ class Policy {
26
27
  * @param definition Policy definition to validate permits against
27
28
  */
28
29
  constructor(definition, authorizations) {
30
+ const cleanVersion = (0, valid_1.default)(version_1.version);
29
31
  // Check compatibility of the policy engine with the policy config
30
- if (!(0, valid_1.default)(version_1.version)) {
32
+ if (!cleanVersion) {
31
33
  throw new Error(`Cannot determine the version of the policy engine for compatibility: invalid semver: ${version_1.version}`);
32
34
  }
33
35
  else if (!(0, valid_1.default)(definition.minPEVersion)) {
34
36
  throw new Error(`Cannot determine the minimum PE version for the configuration: invalid semver: ${definition.minPEVersion}`);
35
37
  }
36
- else if ((0, lt_1.default)(version_1.version, definition.minPEVersion)) {
37
- throw new Error(`Current policy engine version is less than minimum version required for policy config: ${version_1.version} > ${definition.minPEVersion}`);
38
+ else if ((0, lt_1.default)(cleanVersion, definition.minPEVersion)) {
39
+ throw new Error(`Current policy engine version is less than minimum version required for policy config: ${cleanVersion} > ${definition.minPEVersion}`);
38
40
  }
39
- else if ((0, major_1.default)(version_1.version) > (0, major_1.default)(definition.minPEVersion)) {
40
- throw new Error(`Current policy config minimum version is at least one major version behind policy engine: major(${definition.minPEVersion}) < major(${version_1.version}))`);
41
+ else if ((0, major_1.default)(cleanVersion) > (0, major_1.default)(definition.minPEVersion)) {
42
+ throw new Error(`Current policy config minimum version is at least one major version behind policy engine: major(${definition.minPEVersion}) < major(${cleanVersion}))`);
41
43
  }
42
44
  this.policyDefinition = definition;
43
45
  this.setSpecialAuthorizations(authorizations);
@@ -747,5 +749,8 @@ class Policy {
747
749
  return new Array();
748
750
  }
749
751
  }
752
+ getVehicleDisplayCode(configuration, axleConfiguration) {
753
+ return (0, display_code_helper_1.getVehicleDisplayCodeHelper)(this, configuration, axleConfiguration);
754
+ }
750
755
  }
751
756
  exports.Policy = Policy;
@@ -0,0 +1,18 @@
1
+ export type VehicleDisplayCodeDefaults = {
2
+ prefixStandard: string;
3
+ prefixUniversal: string;
4
+ paddingStandard: string;
5
+ paddingUniversal: string;
6
+ spacingUniversalDefault: string;
7
+ spacingUniversalSmall: string;
8
+ spacingUniversalLarge: string;
9
+ spacingUniversalSmallMax: number;
10
+ spacingUniversalLargeMin: number;
11
+ extraAxleUniversal: string;
12
+ endAxleUniversal: string;
13
+ maxAxlesStandard: number;
14
+ thresholdAxlesUniversal: number;
15
+ overAxlesCodeUniversal: string;
16
+ universalAxleCode: string;
17
+ multiDigitPrefix: string;
18
+ };
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -4,6 +4,7 @@ export { BridgeCalculationResult } from './bridge-calculation-result';
4
4
  export { Commodity } from './commodity';
5
5
  export { CostRule } from './cost-rule';
6
6
  export { DimensionModifier, VehicleRelatives } from './dimension-modifier';
7
+ export { VehicleDisplayCodeDefaults } from './display-code-defaults';
7
8
  export { PermitFacts } from './facts';
8
9
  export { GeographicRegion } from './geographic-region';
9
10
  export { IdentifiedObject } from './identified-object';
@@ -1,4 +1,4 @@
1
- import { GeographicRegion, PermitType, DefaultWeightDimensions, VehicleTypes, Commodity, SizeDimension, VehicleCategories, RangeMatrix, BridgeCalculationConstants, PermitConditionDefinition, StandardTireSize } from 'onroute-policy-engine/types';
1
+ import { GeographicRegion, PermitType, DefaultWeightDimensions, VehicleTypes, Commodity, SizeDimension, VehicleCategories, RangeMatrix, BridgeCalculationConstants, PermitConditionDefinition, StandardTireSize, VehicleDisplayCodeDefaults } from 'onroute-policy-engine/types';
2
2
  import { RuleProperties } from 'json-rules-engine';
3
3
  export type PolicyDefinition = {
4
4
  minPEVersion: string;
@@ -14,4 +14,5 @@ export type PolicyDefinition = {
14
14
  bridgeCalculationConstants: BridgeCalculationConstants;
15
15
  conditions?: Array<PermitConditionDefinition>;
16
16
  standardTireSizes?: Array<StandardTireSize>;
17
+ vehicleDisplayCodeDefaults?: VehicleDisplayCodeDefaults;
17
18
  };
@@ -3,15 +3,20 @@ export type VehicleType = IdentifiedObject & {
3
3
  category: string;
4
4
  defaultSizeDimensions?: SizeDimension;
5
5
  ignoreForSizeDimensions?: boolean;
6
+ ignoreForAxleCalculation?: boolean;
6
7
  isLcv?: boolean;
7
8
  conditions?: Array<ConditionRequirement>;
8
9
  additionalAxleSubType?: string;
9
10
  };
10
11
  export type PowerUnitType = VehicleType & {
11
12
  defaultWeightDimensions?: Array<PowerUnitWeightDimension>;
13
+ displayCodePrefix?: string;
14
+ displayCodeSteerAxle?: string;
15
+ displayCodeDriveAxle?: string;
12
16
  };
13
17
  export type TrailerType = VehicleType & {
14
18
  defaultWeightDimensions?: Array<TrailerWeightDimension>;
19
+ displayCode?: string;
15
20
  };
16
21
  export type VehicleTypes = {
17
22
  powerUnitTypes: Array<PowerUnitType>;
package/dist/version.d.ts CHANGED
@@ -1 +1 @@
1
- export declare const version = "v2.0.0";
1
+ export declare const version = "v2.1.0";
package/dist/version.js CHANGED
@@ -2,4 +2,4 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.version = void 0;
4
4
  // Generated by genversion.
5
- exports.version = 'v2.0.0';
5
+ exports.version = 'v2.1.0';
package/package.json CHANGED
@@ -90,5 +90,5 @@
90
90
  ],
91
91
  "testEnvironment": "node"
92
92
  },
93
- "version": "v2.0.0"
93
+ "version": "v2.1.0"
94
94
  }