onroute-policy-engine 0.1.0 → 0.2.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.
- package/README.md +5 -4
- package/dist/enum/accessory-vehicle-type.d.ts +4 -0
- package/dist/enum/accessory-vehicle-type.js +8 -0
- package/dist/enum/facts.d.ts +5 -1
- package/dist/enum/facts.js +4 -0
- package/dist/enum/index.d.ts +2 -0
- package/dist/enum/index.js +5 -1
- package/dist/enum/permit-app-info.d.ts +8 -2
- package/dist/enum/permit-app-info.js +8 -2
- package/dist/enum/validation-result-code.d.ts +2 -1
- package/dist/enum/validation-result-code.js +1 -0
- package/dist/enum/validation-result-type.d.ts +2 -1
- package/dist/enum/validation-result-type.js +1 -0
- package/dist/enum/vehicle-types.d.ts +4 -0
- package/dist/enum/vehicle-types.js +8 -0
- package/dist/helper/facts.helper.d.ts +2 -9
- package/dist/helper/facts.helper.js +63 -26
- package/dist/helper/lists.helper.d.ts +13 -0
- package/dist/helper/lists.helper.js +32 -1
- package/dist/helper/rules-engine.helper.js +32 -2
- package/dist/policy-engine.d.ts +82 -2
- package/dist/policy-engine.js +516 -6
- package/dist/rule-operator/custom-operators.js +5 -1
- package/dist/types/commodity.d.ts +10 -3
- package/dist/types/cost-rule.d.ts +4 -0
- package/dist/types/cost-rule.js +2 -0
- package/dist/types/index.d.ts +4 -2
- package/dist/types/permit-type.d.ts +2 -1
- package/dist/types/policy-definition.d.ts +2 -1
- package/dist/types/range-matrix.d.ts +9 -0
- package/dist/types/range-matrix.js +2 -0
- package/dist/types/region-size-override.d.ts +3 -3
- package/dist/types/self-issuable.d.ts +1 -1
- package/dist/types/size-dimension.d.ts +7 -7
- package/dist/types/vehicle-type.d.ts +1 -0
- package/dist/types/vehicle.d.ts +11 -5
- package/dist/validation-result.d.ts +1 -0
- package/dist/validation-results.d.ts +5 -0
- package/dist/validation-results.js +10 -1
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -10,7 +10,7 @@ A JSON-based rules engine to validate onRouteBC permit applications against comm
|
|
|
10
10
|
import Policy from 'orbc-policy-engine';
|
|
11
11
|
|
|
12
12
|
// Instantiate a new Policy object
|
|
13
|
-
//
|
|
13
|
+
// policyDefinition is a JSON object of type PolicyDefinition
|
|
14
14
|
const policy: Policy = new Policy(policyDefinition);
|
|
15
15
|
|
|
16
16
|
// Get list of all available permit types (ID and name)
|
|
@@ -31,19 +31,20 @@ const commodities: Map<string, string> = policy.getCommodities(permitTypeId);
|
|
|
31
31
|
// Get list of all vehicle types valid to be added to a configuration,
|
|
32
32
|
// by permit type and commodity. Requires supplying the vehicles already
|
|
33
33
|
// added to the configuration, or empty array if starting from scratch
|
|
34
|
-
const allowableVehicles: Map<string, string> = policy.
|
|
34
|
+
const allowableVehicles: Map<string, string> = policy.getNextPermittableVehicles(
|
|
35
35
|
permitTypeId,
|
|
36
36
|
commodityId,
|
|
37
37
|
currentConfiguration);
|
|
38
38
|
|
|
39
39
|
// Validate a permit application against policy
|
|
40
40
|
// permitApplication is a JSON object of type PermitApplication
|
|
41
|
-
// A PermitApplication is
|
|
41
|
+
// A PermitApplication is the standard permitData object wrapped
|
|
42
42
|
// in an object with a permitType key. For example:
|
|
43
43
|
// {
|
|
44
44
|
// permitType: 'TROS',
|
|
45
45
|
// permitData: { ... }
|
|
46
46
|
// }
|
|
47
|
-
|
|
47
|
+
// Note this is an async call due to the reliance on json-rules-engine
|
|
48
|
+
const results: ValidationResults = await policy.validate(permitApplication);
|
|
48
49
|
```
|
|
49
50
|
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AccessoryVehicleType = void 0;
|
|
4
|
+
var AccessoryVehicleType;
|
|
5
|
+
(function (AccessoryVehicleType) {
|
|
6
|
+
AccessoryVehicleType["Booster"] = "BOOSTER";
|
|
7
|
+
AccessoryVehicleType["Jeep"] = "JEEPSRG";
|
|
8
|
+
})(AccessoryVehicleType || (exports.AccessoryVehicleType = AccessoryVehicleType = {}));
|
package/dist/enum/facts.d.ts
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
export declare enum PolicyFacts {
|
|
2
2
|
AllowedVehicles = "allowedVehicles",
|
|
3
3
|
DaysInPermitYear = "daysInPermitYear",
|
|
4
|
-
ValidationDate = "validationDate"
|
|
4
|
+
ValidationDate = "validationDate",
|
|
5
|
+
FixedCost = "fixedCost",
|
|
6
|
+
CostPerMonth = "costPerMonth",
|
|
7
|
+
RangeMatrixCostLookup = "rangeMatrixCostLookup",
|
|
8
|
+
ConfigurationIsValid = "configurationIsValid"
|
|
5
9
|
}
|
package/dist/enum/facts.js
CHANGED
|
@@ -6,4 +6,8 @@ var PolicyFacts;
|
|
|
6
6
|
PolicyFacts["AllowedVehicles"] = "allowedVehicles";
|
|
7
7
|
PolicyFacts["DaysInPermitYear"] = "daysInPermitYear";
|
|
8
8
|
PolicyFacts["ValidationDate"] = "validationDate";
|
|
9
|
+
PolicyFacts["FixedCost"] = "fixedCost";
|
|
10
|
+
PolicyFacts["CostPerMonth"] = "costPerMonth";
|
|
11
|
+
PolicyFacts["RangeMatrixCostLookup"] = "rangeMatrixCostLookup";
|
|
12
|
+
PolicyFacts["ConfigurationIsValid"] = "configurationIsValid";
|
|
9
13
|
})(PolicyFacts || (exports.PolicyFacts = PolicyFacts = {}));
|
package/dist/enum/index.d.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
|
+
export { AccessoryVehicleType } from './accessory-vehicle-type';
|
|
1
2
|
export { PolicyFacts } from './facts';
|
|
2
3
|
export { PermitAppInfo } from './permit-app-info';
|
|
3
4
|
export { RelativePosition } from './relative-position';
|
|
4
5
|
export { ValidationResultCode } from './validation-result-code';
|
|
5
6
|
export { ValidationResultType } from './validation-result-type';
|
|
7
|
+
export { VehicleTypes } from './vehicle-types';
|
package/dist/enum/index.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.ValidationResultType = exports.ValidationResultCode = exports.RelativePosition = exports.PermitAppInfo = exports.PolicyFacts = void 0;
|
|
3
|
+
exports.VehicleTypes = exports.ValidationResultType = exports.ValidationResultCode = exports.RelativePosition = exports.PermitAppInfo = exports.PolicyFacts = exports.AccessoryVehicleType = void 0;
|
|
4
|
+
var accessory_vehicle_type_1 = require("./accessory-vehicle-type");
|
|
5
|
+
Object.defineProperty(exports, "AccessoryVehicleType", { enumerable: true, get: function () { return accessory_vehicle_type_1.AccessoryVehicleType; } });
|
|
4
6
|
var facts_1 = require("./facts");
|
|
5
7
|
Object.defineProperty(exports, "PolicyFacts", { enumerable: true, get: function () { return facts_1.PolicyFacts; } });
|
|
6
8
|
var permit_app_info_1 = require("./permit-app-info");
|
|
@@ -11,3 +13,5 @@ var validation_result_code_1 = require("./validation-result-code");
|
|
|
11
13
|
Object.defineProperty(exports, "ValidationResultCode", { enumerable: true, get: function () { return validation_result_code_1.ValidationResultCode; } });
|
|
12
14
|
var validation_result_type_1 = require("./validation-result-type");
|
|
13
15
|
Object.defineProperty(exports, "ValidationResultType", { enumerable: true, get: function () { return validation_result_type_1.ValidationResultType; } });
|
|
16
|
+
var vehicle_types_1 = require("./vehicle-types");
|
|
17
|
+
Object.defineProperty(exports, "VehicleTypes", { enumerable: true, get: function () { return vehicle_types_1.VehicleTypes; } });
|
|
@@ -4,7 +4,13 @@
|
|
|
4
4
|
* referenced from within source code.
|
|
5
5
|
*/
|
|
6
6
|
export declare enum PermitAppInfo {
|
|
7
|
-
|
|
7
|
+
PermitType = "permitType",
|
|
8
|
+
PermitData = "permitData",
|
|
9
|
+
PermitStartDate = "$.startDate",
|
|
8
10
|
PermitDateFormat = "YYYY-MM-DD",
|
|
9
|
-
CompanyName = "
|
|
11
|
+
CompanyName = "$.companyName",
|
|
12
|
+
PermitDuration = "$.permitDuration",
|
|
13
|
+
PowerUnitType = "$.vehicleDetails.vehicleSubType",
|
|
14
|
+
TrailerList = "$.vehicleConfiguration.trailers",
|
|
15
|
+
Commodity = "$.permittedCommodity"
|
|
10
16
|
}
|
|
@@ -8,7 +8,13 @@ exports.PermitAppInfo = void 0;
|
|
|
8
8
|
*/
|
|
9
9
|
var PermitAppInfo;
|
|
10
10
|
(function (PermitAppInfo) {
|
|
11
|
-
PermitAppInfo["
|
|
11
|
+
PermitAppInfo["PermitType"] = "permitType";
|
|
12
|
+
PermitAppInfo["PermitData"] = "permitData";
|
|
13
|
+
PermitAppInfo["PermitStartDate"] = "$.startDate";
|
|
12
14
|
PermitAppInfo["PermitDateFormat"] = "YYYY-MM-DD";
|
|
13
|
-
PermitAppInfo["CompanyName"] = "
|
|
15
|
+
PermitAppInfo["CompanyName"] = "$.companyName";
|
|
16
|
+
PermitAppInfo["PermitDuration"] = "$.permitDuration";
|
|
17
|
+
PermitAppInfo["PowerUnitType"] = "$.vehicleDetails.vehicleSubType";
|
|
18
|
+
PermitAppInfo["TrailerList"] = "$.vehicleConfiguration.trailers";
|
|
19
|
+
PermitAppInfo["Commodity"] = "$.permittedCommodity";
|
|
14
20
|
})(PermitAppInfo || (exports.PermitAppInfo = PermitAppInfo = {}));
|
|
@@ -2,5 +2,6 @@ export declare enum ValidationResultCode {
|
|
|
2
2
|
PermitTypeUnknown = "permit-type-unknown",
|
|
3
3
|
FieldValidationError = "field-validation-error",
|
|
4
4
|
ConfigurationInvalid = "configuration-invalid",
|
|
5
|
-
GeneralResult = "general-result"
|
|
5
|
+
GeneralResult = "general-result",
|
|
6
|
+
CostValue = "cost-value"
|
|
6
7
|
}
|
|
@@ -7,4 +7,5 @@ var ValidationResultCode;
|
|
|
7
7
|
ValidationResultCode["FieldValidationError"] = "field-validation-error";
|
|
8
8
|
ValidationResultCode["ConfigurationInvalid"] = "configuration-invalid";
|
|
9
9
|
ValidationResultCode["GeneralResult"] = "general-result";
|
|
10
|
+
ValidationResultCode["CostValue"] = "cost-value";
|
|
10
11
|
})(ValidationResultCode || (exports.ValidationResultCode = ValidationResultCode = {}));
|
|
@@ -7,4 +7,5 @@ var ValidationResultType;
|
|
|
7
7
|
ValidationResultType["Requirement"] = "requirement";
|
|
8
8
|
ValidationResultType["Warning"] = "warning";
|
|
9
9
|
ValidationResultType["Information"] = "information";
|
|
10
|
+
ValidationResultType["Cost"] = "cost";
|
|
10
11
|
})(ValidationResultType || (exports.ValidationResultType = ValidationResultType = {}));
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.VehicleTypes = void 0;
|
|
4
|
+
var VehicleTypes;
|
|
5
|
+
(function (VehicleTypes) {
|
|
6
|
+
VehicleTypes["PowerUnits"] = "powerUnits";
|
|
7
|
+
VehicleTypes["Trailers"] = "trailers";
|
|
8
|
+
})(VehicleTypes || (exports.VehicleTypes = VehicleTypes = {}));
|
|
@@ -1,15 +1,8 @@
|
|
|
1
1
|
import { Engine } from 'json-rules-engine';
|
|
2
|
-
import {
|
|
2
|
+
import { Policy } from '../policy-engine';
|
|
3
3
|
/**
|
|
4
4
|
* Adds runtime facts for the validation. For example, adds the
|
|
5
5
|
* validation date for comparison against startDate of the permit.
|
|
6
6
|
* @param engine json-rules-engine Engine instance to add facts to.
|
|
7
7
|
*/
|
|
8
|
-
export declare function addRuntimeFacts(engine: Engine): void;
|
|
9
|
-
/**
|
|
10
|
-
* Flattens the permit application, using keys taken from the PermitFacts enum so
|
|
11
|
-
* the facts can be used more easily within validation rules.
|
|
12
|
-
* @param permitApplication Permit application object to flatten.
|
|
13
|
-
* @returns Flattened permit application object for validation.
|
|
14
|
-
*/
|
|
15
|
-
export declare function transformPermitFacts(permitApplication: any): PermitFacts;
|
|
8
|
+
export declare function addRuntimeFacts(engine: Engine, policy: Policy): void;
|
|
@@ -3,39 +3,76 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.
|
|
6
|
+
exports.addRuntimeFacts = void 0;
|
|
7
7
|
const dayjs_1 = __importDefault(require("dayjs"));
|
|
8
|
-
const flattie_1 = require("flattie");
|
|
9
8
|
const enum_1 = require("onroute-policy-engine/enum");
|
|
10
9
|
/**
|
|
11
10
|
* Adds runtime facts for the validation. For example, adds the
|
|
12
11
|
* validation date for comparison against startDate of the permit.
|
|
13
12
|
* @param engine json-rules-engine Engine instance to add facts to.
|
|
14
13
|
*/
|
|
15
|
-
function addRuntimeFacts(engine) {
|
|
16
|
-
const today = (0, dayjs_1.default)().format(enum_1.PermitAppInfo.PermitDateFormat
|
|
17
|
-
engine.addFact(enum_1.PolicyFacts.ValidationDate
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
14
|
+
function addRuntimeFacts(engine, policy) {
|
|
15
|
+
const today = (0, dayjs_1.default)().format(enum_1.PermitAppInfo.PermitDateFormat);
|
|
16
|
+
engine.addFact(enum_1.PolicyFacts.ValidationDate, today);
|
|
17
|
+
/**
|
|
18
|
+
* Add runtime fact for number of days in the permit year.
|
|
19
|
+
* Will be either 365 or 366, for use when comparing against
|
|
20
|
+
* duration for 1 year permits.
|
|
21
|
+
*/
|
|
22
|
+
engine.addFact(enum_1.PolicyFacts.DaysInPermitYear, async function (params, almanac) {
|
|
23
|
+
const startDate = await almanac.factValue(enum_1.PermitAppInfo.PermitData, {}, enum_1.PermitAppInfo.PermitStartDate);
|
|
24
|
+
const dateFrom = (0, dayjs_1.default)(startDate, enum_1.PermitAppInfo.PermitDateFormat);
|
|
25
|
+
const daysInPermitYear = dateFrom.add(1, 'year').diff(dateFrom, 'day');
|
|
26
|
+
return daysInPermitYear;
|
|
27
|
+
});
|
|
28
|
+
/**
|
|
29
|
+
* Adds a runtime fact specifying whether the vehicle configuration
|
|
30
|
+
* in the permit application is valid for the permit type and
|
|
31
|
+
* commodity. Will return true or false.
|
|
32
|
+
*/
|
|
33
|
+
engine.addFact(enum_1.PolicyFacts.ConfigurationIsValid.toString(), async function (params, almanac) {
|
|
34
|
+
const powerUnit = await almanac.factValue(enum_1.PermitAppInfo.PermitData, {}, enum_1.PermitAppInfo.PowerUnitType);
|
|
35
|
+
const trailerList = await almanac.factValue(enum_1.PermitAppInfo.PermitData, {}, enum_1.PermitAppInfo.TrailerList);
|
|
36
|
+
let fullVehicleConfiguration = [];
|
|
37
|
+
fullVehicleConfiguration.push(powerUnit);
|
|
38
|
+
fullVehicleConfiguration = fullVehicleConfiguration.concat(trailerList.map((t) => t.vehicleSubType));
|
|
39
|
+
const permitType = await almanac.factValue(enum_1.PermitAppInfo.PermitType);
|
|
40
|
+
const commodity = await almanac.factValue(enum_1.PermitAppInfo.PermitData, {}, enum_1.PermitAppInfo.Commodity);
|
|
41
|
+
let isValid;
|
|
42
|
+
try {
|
|
43
|
+
isValid = policy.isConfigurationValid(permitType, commodity, fullVehicleConfiguration);
|
|
44
|
+
}
|
|
45
|
+
catch (e) {
|
|
46
|
+
console.log(`Error validating vehicle configuration: '{e.message}'`);
|
|
47
|
+
isValid = false;
|
|
48
|
+
}
|
|
49
|
+
return isValid;
|
|
50
|
+
});
|
|
51
|
+
/**
|
|
52
|
+
* Add runtime fact for a fixed permit cost, the cost supplied
|
|
53
|
+
* as a parameter.
|
|
54
|
+
*/
|
|
55
|
+
engine.addFact(enum_1.PolicyFacts.FixedCost.toString(), async function (params) {
|
|
56
|
+
return params.cost;
|
|
57
|
+
});
|
|
58
|
+
/**
|
|
59
|
+
* Add runtime fact for cost per month, where month is defined by
|
|
60
|
+
* policy as a 30 day period or portion thereof, except in the
|
|
61
|
+
* case of a full year in which case it is 12 months.
|
|
62
|
+
*/
|
|
63
|
+
engine.addFact(enum_1.PolicyFacts.CostPerMonth.toString(), async function (params, almanac) {
|
|
64
|
+
const duration = await almanac.factValue(enum_1.PermitAppInfo.PermitData, {}, enum_1.PermitAppInfo.PermitDuration);
|
|
65
|
+
const daysInPermitYear = await almanac.factValue(enum_1.PolicyFacts.DaysInPermitYear.toString());
|
|
66
|
+
let months = Math.floor(duration / 30);
|
|
67
|
+
const extraDays = duration % 30;
|
|
68
|
+
if (extraDays > 0 && duration !== daysInPermitYear) {
|
|
69
|
+
// Add an extra month for a partial month unless it is
|
|
70
|
+
// a full year. Note this does not handle the case where 2
|
|
71
|
+
// or more years duration is specified, since that is not
|
|
72
|
+
// a valid permit and does not warrant considetation.
|
|
73
|
+
months++;
|
|
74
|
+
}
|
|
75
|
+
return months * params.cost;
|
|
24
76
|
});
|
|
25
77
|
}
|
|
26
78
|
exports.addRuntimeFacts = addRuntimeFacts;
|
|
27
|
-
/**
|
|
28
|
-
* Flattens the permit application, using keys taken from the PermitFacts enum so
|
|
29
|
-
* the facts can be used more easily within validation rules.
|
|
30
|
-
* @param permitApplication Permit application object to flatten.
|
|
31
|
-
* @returns Flattened permit application object for validation.
|
|
32
|
-
*/
|
|
33
|
-
function transformPermitFacts(permitApplication) {
|
|
34
|
-
// Flatten the permit application so all properties can be accessed
|
|
35
|
-
// by key
|
|
36
|
-
const permitFacts = (0, flattie_1.flattie)(permitApplication);
|
|
37
|
-
// Add the app itself as a fact to be used by more complex rules
|
|
38
|
-
permitFacts.app = permitApplication;
|
|
39
|
-
return permitFacts;
|
|
40
|
-
}
|
|
41
|
-
exports.transformPermitFacts = transformPermitFacts;
|
|
@@ -9,3 +9,16 @@ import { IdentifiedObject } from 'onroute-policy-engine/types';
|
|
|
9
9
|
* @returns Map of object ID to object name.
|
|
10
10
|
*/
|
|
11
11
|
export declare function extractIdentifiedObjects(objects: Array<IdentifiedObject>, filter?: Array<string>): Map<string, string>;
|
|
12
|
+
/**
|
|
13
|
+
* Intersects two maps containing string id and string description to
|
|
14
|
+
* return a new map containing those entries common to both.
|
|
15
|
+
* This only checks that the keys are common, not the values. If the
|
|
16
|
+
* values are different, this returns the value from the first map.
|
|
17
|
+
* If any values in the first map are null or undefined, these will
|
|
18
|
+
* be replaced with empty string.
|
|
19
|
+
* @param first The first map
|
|
20
|
+
* @param second The second map
|
|
21
|
+
* @returns New map containing entries common to both input maps
|
|
22
|
+
*/
|
|
23
|
+
export declare function intersectIdMaps(first: Map<string, string>, second: Map<string, string>): Map<string, string>;
|
|
24
|
+
export declare function getIdFromName(list: Array<IdentifiedObject>, name: string): string | null;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.extractIdentifiedObjects = void 0;
|
|
3
|
+
exports.getIdFromName = exports.intersectIdMaps = exports.extractIdentifiedObjects = void 0;
|
|
4
4
|
/**
|
|
5
5
|
* Extracts a map containing just the ID and name of each of the supplied
|
|
6
6
|
* identified objects.
|
|
@@ -20,3 +20,34 @@ function extractIdentifiedObjects(objects, filter) {
|
|
|
20
20
|
return objectMap;
|
|
21
21
|
}
|
|
22
22
|
exports.extractIdentifiedObjects = extractIdentifiedObjects;
|
|
23
|
+
/**
|
|
24
|
+
* Intersects two maps containing string id and string description to
|
|
25
|
+
* return a new map containing those entries common to both.
|
|
26
|
+
* This only checks that the keys are common, not the values. If the
|
|
27
|
+
* values are different, this returns the value from the first map.
|
|
28
|
+
* If any values in the first map are null or undefined, these will
|
|
29
|
+
* be replaced with empty string.
|
|
30
|
+
* @param first The first map
|
|
31
|
+
* @param second The second map
|
|
32
|
+
* @returns New map containing entries common to both input maps
|
|
33
|
+
*/
|
|
34
|
+
function intersectIdMaps(first, second) {
|
|
35
|
+
var _a;
|
|
36
|
+
const combinedMap = new Map();
|
|
37
|
+
for (const key in first.keys()) {
|
|
38
|
+
if (second.has(key)) {
|
|
39
|
+
combinedMap.set(key, (_a = first.get(key)) !== null && _a !== void 0 ? _a : '');
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return combinedMap;
|
|
43
|
+
}
|
|
44
|
+
exports.intersectIdMaps = intersectIdMaps;
|
|
45
|
+
function getIdFromName(list, name) {
|
|
46
|
+
let id = null;
|
|
47
|
+
const identifiedObject = list.find((i) => i.name == name);
|
|
48
|
+
if (identifiedObject) {
|
|
49
|
+
id = identifiedObject.id;
|
|
50
|
+
}
|
|
51
|
+
return id;
|
|
52
|
+
}
|
|
53
|
+
exports.getIdFromName = getIdFromName;
|
|
@@ -11,7 +11,10 @@ const enum_1 = require("onroute-policy-engine/enum");
|
|
|
11
11
|
* @returns json-rules-engine Engine instance.
|
|
12
12
|
*/
|
|
13
13
|
function getEngine(policyDefinition) {
|
|
14
|
-
const engine = new json_rules_engine_1.Engine(
|
|
14
|
+
const engine = new json_rules_engine_1.Engine([], {
|
|
15
|
+
replaceFactsInEventParams: true,
|
|
16
|
+
allowUndefinedFacts: true,
|
|
17
|
+
});
|
|
15
18
|
custom_operators_1.CustomOperators.forEach((o) => engine.addOperator(o));
|
|
16
19
|
policyDefinition.commonRules.forEach((r) => engine.addRule(r));
|
|
17
20
|
return engine;
|
|
@@ -27,9 +30,36 @@ function getRulesEngines(policy) {
|
|
|
27
30
|
var _a, _b;
|
|
28
31
|
const engineMap = new Map();
|
|
29
32
|
(_b = (_a = policy.policyDefinition) === null || _a === void 0 ? void 0 : _a.permitTypes) === null || _b === void 0 ? void 0 : _b.forEach((permitType) => {
|
|
30
|
-
var _a;
|
|
33
|
+
var _a, _b;
|
|
31
34
|
const engine = getEngine(policy.policyDefinition);
|
|
32
35
|
(_a = permitType.rules) === null || _a === void 0 ? void 0 : _a.forEach((r) => engine.addRule(r));
|
|
36
|
+
// Convert the cost definitions into proper rules so the
|
|
37
|
+
// results can be added to the validation run. This takes advantage
|
|
38
|
+
// of the replaceFactsInEventParams option for the json-rules-engine
|
|
39
|
+
// Engine object.
|
|
40
|
+
(_b = permitType.costRules) === null || _b === void 0 ? void 0 : _b.forEach((c) => {
|
|
41
|
+
const costRule = {
|
|
42
|
+
conditions: {
|
|
43
|
+
all: [
|
|
44
|
+
{
|
|
45
|
+
fact: c.fact,
|
|
46
|
+
params: c.params,
|
|
47
|
+
operator: 'greaterThanInclusive',
|
|
48
|
+
value: 0,
|
|
49
|
+
},
|
|
50
|
+
],
|
|
51
|
+
},
|
|
52
|
+
event: {
|
|
53
|
+
type: 'cost',
|
|
54
|
+
params: {
|
|
55
|
+
message: 'Calculated permit cost',
|
|
56
|
+
code: 'cost-value',
|
|
57
|
+
cost: c,
|
|
58
|
+
},
|
|
59
|
+
},
|
|
60
|
+
};
|
|
61
|
+
engine.addRule(costRule);
|
|
62
|
+
});
|
|
33
63
|
let allowedVehicles;
|
|
34
64
|
if (permitType.allowedVehicles && permitType.allowedVehicles.length > 0) {
|
|
35
65
|
allowedVehicles = permitType.allowedVehicles;
|
package/dist/policy-engine.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { PolicyDefinition, PermitType } from 'onroute-policy-engine/types';
|
|
1
|
+
import { PolicyDefinition, PermitType, Commodity, SizeDimension } from 'onroute-policy-engine/types';
|
|
2
2
|
import { Engine } from 'json-rules-engine';
|
|
3
3
|
import { ValidationResults } from './validation-results';
|
|
4
4
|
/** Class representing commercial vehicle policy. */
|
|
@@ -36,10 +36,84 @@ export declare class Policy {
|
|
|
36
36
|
* Gets a list of all configured commodities in the policy definition.
|
|
37
37
|
* @param permitTypeId Return only commodities valid for this permit type.
|
|
38
38
|
* If not supplied, return all commodities configured in policy. If permit
|
|
39
|
-
* type does not require commodity (e.g. 'TROS'), return
|
|
39
|
+
* type does not require commodity (e.g. 'TROS'), return empty map.
|
|
40
40
|
* @returns Map of commodity IDs to commodity names.
|
|
41
41
|
*/
|
|
42
42
|
getCommodities(permitTypeId?: string): Map<string, string>;
|
|
43
|
+
/**
|
|
44
|
+
* Gets a list of all allowable vehicle types for the given permit type
|
|
45
|
+
* and commodity. Includes all power units and trailers.
|
|
46
|
+
* @param permitTypeId ID of the permit type to get vehicles for
|
|
47
|
+
* @param commodityId ID of the commodity to get vehicles for
|
|
48
|
+
* @returns Map of two separate maps, one keyed on 'powerUnits' and the
|
|
49
|
+
* other keyed on 'trailers'. Each map consists of a string id for the
|
|
50
|
+
* vehicle, and a string common name for the vehicle.
|
|
51
|
+
*/
|
|
52
|
+
getPermittableVehicleTypes(permitTypeId: string, commodityId: string): Map<string, Map<string, string>>;
|
|
53
|
+
/**
|
|
54
|
+
* Gets a list of all allowable power unit types for the given permit type
|
|
55
|
+
* and commodity.
|
|
56
|
+
* @param permitTypeId ID of the permit type to get power units for
|
|
57
|
+
* @param commodityId ID of the commodity to get power units for
|
|
58
|
+
* @returns Map of power unit IDs to power unit names.
|
|
59
|
+
*/
|
|
60
|
+
getPermittablePowerUnitTypes(permitTypeId: string, commodityId: string): Map<string, string>;
|
|
61
|
+
/**
|
|
62
|
+
* Gets the next permittable vehicles based on the supplied permit type,
|
|
63
|
+
* commodity, and current configuration.
|
|
64
|
+
* If the permit type does not require commodity or if the commodity
|
|
65
|
+
* is not valid for the permit type, this will return an empty map
|
|
66
|
+
* (meaning no vehicles are permittable).
|
|
67
|
+
* @param permitTypeId ID of the permit type to get vehicles for
|
|
68
|
+
* @param commodityId ID of the commodity to get vehicles for
|
|
69
|
+
* @param currentConfiguration Current vehicle configuration to which will be
|
|
70
|
+
* added the next permittable vehicle
|
|
71
|
+
* @returns Map of vehicle ID to vehicle name. Includes both power units and
|
|
72
|
+
* trailers in a single return value (if applicable).
|
|
73
|
+
*/
|
|
74
|
+
getNextPermittableVehicles(permitTypeId: string, commodityId: string, currentConfiguration: Array<string>): Map<string, string>;
|
|
75
|
+
/**
|
|
76
|
+
* Returns whether the supplied configuration is valid for the given permit type
|
|
77
|
+
* and commodity. If the permit type does not require a commodity, this method
|
|
78
|
+
* will return false. This will not validate incomplete configurations - if the
|
|
79
|
+
* current configuration has only a power unit this will return false even if the
|
|
80
|
+
* power unit is acceptable because there is no trailer which is mandatory.
|
|
81
|
+
* If this is called for a permit type that does not require commodity, or with
|
|
82
|
+
* a commodity not permitted for the permit type, it will return false.
|
|
83
|
+
* @param permitTypeId ID of the permit type to validate the configuration against
|
|
84
|
+
* @param commodityId ID of the commodity used for the configuration
|
|
85
|
+
* @param currentConfiguration Current vehicle configuration to validate
|
|
86
|
+
* @param validatePartial Whether to validate a partial configuration (e.g. one
|
|
87
|
+
* that does not include a trailer). This will just return whether or not there
|
|
88
|
+
* are any invalid vehicles in the configuration. If true, an empty current
|
|
89
|
+
* configuration will return true from this method.
|
|
90
|
+
*/
|
|
91
|
+
isConfigurationValid(permitTypeId: string, commodityId: string, currentConfiguration: Array<string>, validatePartial?: boolean): boolean;
|
|
92
|
+
/**
|
|
93
|
+
* Gets the maximum size dimensions for a given permit type, commodity,
|
|
94
|
+
* and vehicle configuration. A vehicle configuration's size dimension is
|
|
95
|
+
* dictated by the configuration on the trailer. For configurations that
|
|
96
|
+
* are just a power unit, a pseudo trailer type 'NONE' is used and the
|
|
97
|
+
* size dimension is configured on that. Accessory category trailers
|
|
98
|
+
* (e.g. jeeps and boosters) are not used for configuration since they
|
|
99
|
+
* do not impact the size dimension in policy.
|
|
100
|
+
* @param permitTypeId ID of the permit type to get size dimension for
|
|
101
|
+
* @param commodityId ID of the commodity to get size dimension for
|
|
102
|
+
* @param currentConfiguration Current vehicle configuration to get size dimension for
|
|
103
|
+
* @param regions List of regions the vehicle will be traveling in. If not
|
|
104
|
+
* supplied this defaults to the most restrictive size dimension (if multiple
|
|
105
|
+
* are configured).
|
|
106
|
+
* @returns SizeDimension for the given permit type, commodity, and configuration
|
|
107
|
+
*/
|
|
108
|
+
getSizeDimension(permitTypeId: string, commodityId: string, configuration: Array<string>, regions?: Array<string>): SizeDimension | null;
|
|
109
|
+
/**
|
|
110
|
+
* Selects the correct size dimension from a list of potential candidates, based
|
|
111
|
+
* on the modifier of each dimension. If none of the modifiers match, returns
|
|
112
|
+
* null.
|
|
113
|
+
* @param sizeDimensions Size dimension options to choose from
|
|
114
|
+
* @param currentConfiguration The full vehicle configuration
|
|
115
|
+
*/
|
|
116
|
+
selectCorrectSizeDimension(sizeDimensions: Array<SizeDimension>, configuration: Array<string>, sizeTrailer: string): SizeDimension | null;
|
|
43
117
|
/**
|
|
44
118
|
* Gets a list of all configured power unit types in the policy definition.
|
|
45
119
|
* @returns Map of power unit type IDs to power unit type names.
|
|
@@ -56,4 +130,10 @@ export declare class Policy {
|
|
|
56
130
|
* @returns PermitType definition for the supplied ID.
|
|
57
131
|
*/
|
|
58
132
|
getPermitTypeDefinition(type?: string): PermitType | null;
|
|
133
|
+
/**
|
|
134
|
+
* Gets a full Commodity definition by ID
|
|
135
|
+
* @param type Type ID of the commodity to return.
|
|
136
|
+
* @returns Commodity definition for the supplied ID.
|
|
137
|
+
*/
|
|
138
|
+
getCommodityDefinition(type?: string): Commodity | null;
|
|
59
139
|
}
|