@itwin/ecschema-metadata 3.5.0-dev.26 → 3.5.0-dev.27
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/lib/cjs/Constants.d.ts +23 -23
- package/lib/cjs/Constants.js +33 -33
- package/lib/cjs/Constants.js.map +1 -1
- package/lib/cjs/Context.d.ts +139 -139
- package/lib/cjs/Context.js +204 -204
- package/lib/cjs/DelayedPromise.d.ts +88 -88
- package/lib/cjs/DelayedPromise.js +91 -91
- package/lib/cjs/DelayedPromise.js.map +1 -1
- package/lib/cjs/Deserialization/AbstractParser.d.ts +45 -45
- package/lib/cjs/Deserialization/AbstractParser.js +11 -11
- package/lib/cjs/Deserialization/AbstractParser.js.map +1 -1
- package/lib/cjs/Deserialization/Helper.d.ts +251 -251
- package/lib/cjs/Deserialization/Helper.js +843 -843
- package/lib/cjs/Deserialization/Helper.js.map +1 -1
- package/lib/cjs/Deserialization/JsonParser.d.ts +165 -165
- package/lib/cjs/Deserialization/JsonParser.js +680 -680
- package/lib/cjs/Deserialization/JsonParser.js.map +1 -1
- package/lib/cjs/Deserialization/JsonProps.d.ts +282 -282
- package/lib/cjs/Deserialization/JsonProps.js +9 -9
- package/lib/cjs/Deserialization/JsonProps.js.map +1 -1
- package/lib/cjs/Deserialization/SchemaGraphUtil.d.ts +34 -34
- package/lib/cjs/Deserialization/SchemaGraphUtil.js +77 -77
- package/lib/cjs/Deserialization/SchemaGraphUtil.js.map +1 -1
- package/lib/cjs/Deserialization/XmlParser.d.ts +85 -85
- package/lib/cjs/Deserialization/XmlParser.js +973 -973
- package/lib/cjs/Deserialization/XmlParser.js.map +1 -1
- package/lib/cjs/Deserialization/XmlSerializationUtils.d.ts +54 -54
- package/lib/cjs/Deserialization/XmlSerializationUtils.js +174 -174
- package/lib/cjs/Deserialization/XmlSerializationUtils.js.map +1 -1
- package/lib/cjs/ECName.d.ts +30 -30
- package/lib/cjs/ECName.js +85 -85
- package/lib/cjs/ECName.js.map +1 -1
- package/lib/cjs/ECObjects.d.ts +162 -162
- package/lib/cjs/ECObjects.js +453 -453
- package/lib/cjs/ECObjects.js.map +1 -1
- package/lib/cjs/Exception.d.ts +43 -43
- package/lib/cjs/Exception.js +86 -86
- package/lib/cjs/Exception.js.map +1 -1
- package/lib/cjs/Interfaces.d.ts +66 -66
- package/lib/cjs/Interfaces.js +9 -9
- package/lib/cjs/Interfaces.js.map +1 -1
- package/lib/cjs/Metadata/Class.d.ts +243 -243
- package/lib/cjs/Metadata/Class.js +558 -558
- package/lib/cjs/Metadata/Class.js.map +1 -1
- package/lib/cjs/Metadata/Constant.d.ts +64 -64
- package/lib/cjs/Metadata/Constant.js +123 -123
- package/lib/cjs/Metadata/Constant.js.map +1 -1
- package/lib/cjs/Metadata/CustomAttribute.d.ts +23 -23
- package/lib/cjs/Metadata/CustomAttribute.js +27 -27
- package/lib/cjs/Metadata/CustomAttribute.js.map +1 -1
- package/lib/cjs/Metadata/CustomAttributeClass.d.ts +39 -39
- package/lib/cjs/Metadata/CustomAttributeClass.js +68 -68
- package/lib/cjs/Metadata/CustomAttributeClass.js.map +1 -1
- package/lib/cjs/Metadata/EntityClass.d.ts +77 -77
- package/lib/cjs/Metadata/EntityClass.js +230 -230
- package/lib/cjs/Metadata/EntityClass.js.map +1 -1
- package/lib/cjs/Metadata/Enumeration.d.ts +86 -86
- package/lib/cjs/Metadata/Enumeration.js +159 -159
- package/lib/cjs/Metadata/Enumeration.js.map +1 -1
- package/lib/cjs/Metadata/Format.d.ts +98 -98
- package/lib/cjs/Metadata/Format.js +242 -242
- package/lib/cjs/Metadata/Format.js.map +1 -1
- package/lib/cjs/Metadata/InvertedUnit.d.ts +49 -49
- package/lib/cjs/Metadata/InvertedUnit.js +97 -97
- package/lib/cjs/Metadata/InvertedUnit.js.map +1 -1
- package/lib/cjs/Metadata/KindOfQuantity.d.ts +65 -65
- package/lib/cjs/Metadata/KindOfQuantity.js +225 -225
- package/lib/cjs/Metadata/KindOfQuantity.js.map +1 -1
- package/lib/cjs/Metadata/Mixin.d.ts +54 -54
- package/lib/cjs/Metadata/Mixin.js +113 -113
- package/lib/cjs/Metadata/Mixin.js.map +1 -1
- package/lib/cjs/Metadata/OverrideFormat.d.ts +55 -55
- package/lib/cjs/Metadata/OverrideFormat.js +88 -88
- package/lib/cjs/Metadata/OverrideFormat.js.map +1 -1
- package/lib/cjs/Metadata/Phenomenon.d.ts +33 -33
- package/lib/cjs/Metadata/Phenomenon.js +59 -59
- package/lib/cjs/Metadata/Phenomenon.js.map +1 -1
- package/lib/cjs/Metadata/Property.d.ts +197 -197
- package/lib/cjs/Metadata/Property.js +491 -491
- package/lib/cjs/Metadata/Property.js.map +1 -1
- package/lib/cjs/Metadata/PropertyCategory.d.ts +39 -39
- package/lib/cjs/Metadata/PropertyCategory.js +61 -61
- package/lib/cjs/Metadata/PropertyCategory.js.map +1 -1
- package/lib/cjs/Metadata/RelationshipClass.d.ts +152 -152
- package/lib/cjs/Metadata/RelationshipClass.js +349 -349
- package/lib/cjs/Metadata/RelationshipClass.js.map +1 -1
- package/lib/cjs/Metadata/Schema.d.ts +291 -291
- package/lib/cjs/Metadata/Schema.js +520 -520
- package/lib/cjs/Metadata/Schema.js.map +1 -1
- package/lib/cjs/Metadata/SchemaItem.d.ts +61 -61
- package/lib/cjs/Metadata/SchemaItem.js +136 -136
- package/lib/cjs/Metadata/SchemaItem.js.map +1 -1
- package/lib/cjs/Metadata/Unit.d.ts +76 -76
- package/lib/cjs/Metadata/Unit.js +162 -162
- package/lib/cjs/Metadata/UnitSystem.d.ts +20 -20
- package/lib/cjs/Metadata/UnitSystem.js +29 -29
- package/lib/cjs/Metadata/UnitSystem.js.map +1 -1
- package/lib/cjs/PropertyTypes.d.ts +49 -49
- package/lib/cjs/PropertyTypes.js +91 -91
- package/lib/cjs/PropertyTypes.js.map +1 -1
- package/lib/cjs/SchemaJsonLocater.d.ts +34 -34
- package/lib/cjs/SchemaJsonLocater.js +42 -42
- package/lib/cjs/SchemaJsonLocater.js.map +1 -1
- package/lib/cjs/SchemaKey.d.ts +110 -110
- package/lib/cjs/SchemaKey.js +219 -219
- package/lib/cjs/SchemaKey.js.map +1 -1
- package/lib/cjs/SchemaLoader.d.ts +23 -23
- package/lib/cjs/SchemaLoader.js +47 -47
- package/lib/cjs/SchemaLoader.js.map +1 -1
- package/lib/cjs/SchemaPartVisitorDelegate.d.ts +274 -274
- package/lib/cjs/SchemaPartVisitorDelegate.js +212 -212
- package/lib/cjs/SchemaPartVisitorDelegate.js.map +1 -1
- package/lib/cjs/UnitConversion/Graph.d.ts +34 -34
- package/lib/cjs/UnitConversion/Graph.js +83 -83
- package/lib/cjs/UnitConversion/Graph.js.map +1 -1
- package/lib/cjs/UnitConversion/Parser.d.ts +8 -8
- package/lib/cjs/UnitConversion/Parser.js +42 -42
- package/lib/cjs/UnitConversion/Parser.js.map +1 -1
- package/lib/cjs/UnitConversion/UnitConversion.d.ts +45 -45
- package/lib/cjs/UnitConversion/UnitConversion.js +77 -77
- package/lib/cjs/UnitConversion/UnitConversion.js.map +1 -1
- package/lib/cjs/UnitConversion/UnitConverter.d.ts +39 -39
- package/lib/cjs/UnitConversion/UnitConverter.js +116 -116
- package/lib/cjs/UnitConversion/UnitConverter.js.map +1 -1
- package/lib/cjs/UnitConversion/UnitTree.d.ts +43 -43
- package/lib/cjs/UnitConversion/UnitTree.js +169 -169
- package/lib/cjs/UnitConversion/UnitTree.js.map +1 -1
- package/lib/cjs/UnitProvider/SchemaUnitProvider.d.ts +77 -77
- package/lib/cjs/UnitProvider/SchemaUnitProvider.js +236 -236
- package/lib/cjs/UnitProvider/SchemaUnitProvider.js.map +1 -1
- package/lib/cjs/Validation/SchemaWalker.d.ts +23 -23
- package/lib/cjs/Validation/SchemaWalker.js +53 -53
- package/lib/cjs/Validation/SchemaWalker.js.map +1 -1
- package/lib/cjs/ecschema-metadata.d.ts +51 -51
- package/lib/cjs/ecschema-metadata.js +96 -96
- package/lib/cjs/ecschema-metadata.js.map +1 -1
- package/lib/cjs/utils/SchemaGraph.d.ts +25 -25
- package/lib/cjs/utils/SchemaGraph.js +65 -65
- package/lib/cjs/utils/SchemaGraph.js.map +1 -1
- package/package.json +7 -7
|
@@ -1,40 +1,40 @@
|
|
|
1
|
-
import { SchemaContext } from "../Context";
|
|
2
|
-
import { UnitConversion } from "./UnitConversion";
|
|
3
|
-
/**
|
|
4
|
-
* Class constructed with SchemaContext and used to calculate [[UnitConversion]] between Units
|
|
5
|
-
* @internal
|
|
6
|
-
*/
|
|
7
|
-
export declare class UnitConverter {
|
|
8
|
-
private readonly _context;
|
|
9
|
-
private _uGraph;
|
|
10
|
-
/**
|
|
11
|
-
* Create Converter context
|
|
12
|
-
* @param _context SchemaContext with contexts added to it.
|
|
13
|
-
*/
|
|
14
|
-
constructor(_context: SchemaContext);
|
|
15
|
-
/**
|
|
16
|
-
* Find conversion between from and to units, formatted {schemaName}.{schemaItemName} or {schemaName}:{schemaItemName}
|
|
17
|
-
* @param fromUnit SchemaItem full name of source unit
|
|
18
|
-
* @param toUnit SchemaItem full name of target unit
|
|
19
|
-
* @returns [[UnitConversion]] converting fromUnit -> toUnit with a factor and an offset
|
|
20
|
-
* @throws Error if from and to Units' SchemaItem is not found in Schema or Schema prefix is not found in SchemaContext
|
|
21
|
-
* @throws Error if from and to Units do not belong to the same phenomenon
|
|
22
|
-
* @throws Error if definitions' SchemaItems cannot be found in its own or referenced Schemas
|
|
23
|
-
* @throws Error if base units of source and target unit do not match
|
|
24
|
-
*/
|
|
25
|
-
calculateConversion(fromUnit: string, toUnit: string): Promise<UnitConversion>;
|
|
26
|
-
/**
|
|
27
|
-
* @param from Source unit converted from
|
|
28
|
-
* @param to Target unit converted to
|
|
29
|
-
* @internal
|
|
30
|
-
*/
|
|
31
|
-
private processUnits;
|
|
32
|
-
/**
|
|
33
|
-
* Check if fromBaseUnits's base units and exponents matches toBaseUnits's
|
|
34
|
-
* @param fromBaseUnits Map of base units for source unit
|
|
35
|
-
* @param toBaseUnits Map of base units for target unit
|
|
36
|
-
* @internal
|
|
37
|
-
*/
|
|
38
|
-
private checkBaseUnitsMatch;
|
|
39
|
-
}
|
|
1
|
+
import { SchemaContext } from "../Context";
|
|
2
|
+
import { UnitConversion } from "./UnitConversion";
|
|
3
|
+
/**
|
|
4
|
+
* Class constructed with SchemaContext and used to calculate [[UnitConversion]] between Units
|
|
5
|
+
* @internal
|
|
6
|
+
*/
|
|
7
|
+
export declare class UnitConverter {
|
|
8
|
+
private readonly _context;
|
|
9
|
+
private _uGraph;
|
|
10
|
+
/**
|
|
11
|
+
* Create Converter context
|
|
12
|
+
* @param _context SchemaContext with contexts added to it.
|
|
13
|
+
*/
|
|
14
|
+
constructor(_context: SchemaContext);
|
|
15
|
+
/**
|
|
16
|
+
* Find conversion between from and to units, formatted {schemaName}.{schemaItemName} or {schemaName}:{schemaItemName}
|
|
17
|
+
* @param fromUnit SchemaItem full name of source unit
|
|
18
|
+
* @param toUnit SchemaItem full name of target unit
|
|
19
|
+
* @returns [[UnitConversion]] converting fromUnit -> toUnit with a factor and an offset
|
|
20
|
+
* @throws Error if from and to Units' SchemaItem is not found in Schema or Schema prefix is not found in SchemaContext
|
|
21
|
+
* @throws Error if from and to Units do not belong to the same phenomenon
|
|
22
|
+
* @throws Error if definitions' SchemaItems cannot be found in its own or referenced Schemas
|
|
23
|
+
* @throws Error if base units of source and target unit do not match
|
|
24
|
+
*/
|
|
25
|
+
calculateConversion(fromUnit: string, toUnit: string): Promise<UnitConversion>;
|
|
26
|
+
/**
|
|
27
|
+
* @param from Source unit converted from
|
|
28
|
+
* @param to Target unit converted to
|
|
29
|
+
* @internal
|
|
30
|
+
*/
|
|
31
|
+
private processUnits;
|
|
32
|
+
/**
|
|
33
|
+
* Check if fromBaseUnits's base units and exponents matches toBaseUnits's
|
|
34
|
+
* @param fromBaseUnits Map of base units for source unit
|
|
35
|
+
* @param toBaseUnits Map of base units for target unit
|
|
36
|
+
* @internal
|
|
37
|
+
*/
|
|
38
|
+
private checkBaseUnitsMatch;
|
|
39
|
+
}
|
|
40
40
|
//# sourceMappingURL=UnitConverter.d.ts.map
|
|
@@ -1,117 +1,117 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.UnitConverter = void 0;
|
|
4
|
-
/*---------------------------------------------------------------------------------------------
|
|
5
|
-
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
|
|
6
|
-
* See LICENSE.md in the project root for license terms and full copyright notice.
|
|
7
|
-
*--------------------------------------------------------------------------------------------*/
|
|
8
|
-
const core_bentley_1 = require("@itwin/core-bentley");
|
|
9
|
-
const SchemaItem_1 = require("../Metadata/SchemaItem");
|
|
10
|
-
const Unit_1 = require("../Metadata/Unit");
|
|
11
|
-
const SchemaKey_1 = require("../SchemaKey");
|
|
12
|
-
const UnitConversion_1 = require("./UnitConversion");
|
|
13
|
-
const UnitTree_1 = require("./UnitTree");
|
|
14
|
-
/**
|
|
15
|
-
* Class constructed with SchemaContext and used to calculate [[UnitConversion]] between Units
|
|
16
|
-
* @internal
|
|
17
|
-
*/
|
|
18
|
-
class UnitConverter {
|
|
19
|
-
/**
|
|
20
|
-
* Create Converter context
|
|
21
|
-
* @param _context SchemaContext with contexts added to it.
|
|
22
|
-
*/
|
|
23
|
-
constructor(_context) {
|
|
24
|
-
this._context = _context;
|
|
25
|
-
this._uGraph = new UnitTree_1.UnitGraph(this._context);
|
|
26
|
-
}
|
|
27
|
-
/**
|
|
28
|
-
* Find conversion between from and to units, formatted {schemaName}.{schemaItemName} or {schemaName}:{schemaItemName}
|
|
29
|
-
* @param fromUnit SchemaItem full name of source unit
|
|
30
|
-
* @param toUnit SchemaItem full name of target unit
|
|
31
|
-
* @returns [[UnitConversion]] converting fromUnit -> toUnit with a factor and an offset
|
|
32
|
-
* @throws Error if from and to Units' SchemaItem is not found in Schema or Schema prefix is not found in SchemaContext
|
|
33
|
-
* @throws Error if from and to Units do not belong to the same phenomenon
|
|
34
|
-
* @throws Error if definitions' SchemaItems cannot be found in its own or referenced Schemas
|
|
35
|
-
* @throws Error if base units of source and target unit do not match
|
|
36
|
-
*/
|
|
37
|
-
async calculateConversion(fromUnit, toUnit) {
|
|
38
|
-
const [fromSchemaName, fromSchemaItemName] = SchemaItem_1.SchemaItem.parseFullName(fromUnit);
|
|
39
|
-
const [toSchemaName, toSchemaItemName] = SchemaItem_1.SchemaItem.parseFullName(toUnit);
|
|
40
|
-
const fromSchemaKey = new SchemaKey_1.SchemaKey(fromSchemaName);
|
|
41
|
-
const toSchemaKey = new SchemaKey_1.SchemaKey(toSchemaName);
|
|
42
|
-
const fromSchema = await this._context.getSchema(fromSchemaKey);
|
|
43
|
-
const toSchema = await this._context.getSchema(toSchemaKey);
|
|
44
|
-
if (!fromSchema || !toSchema) {
|
|
45
|
-
throw new core_bentley_1.BentleyError(core_bentley_1.BentleyStatus.ERROR, "Cannot find from's and/or to's schema", () => {
|
|
46
|
-
return { from: fromUnit, fromSchema: fromSchemaName, to: toUnit, toSchema: toSchemaName };
|
|
47
|
-
});
|
|
48
|
-
}
|
|
49
|
-
const from = await this._uGraph.resolveUnit(fromSchemaItemName, fromSchema);
|
|
50
|
-
const to = await this._uGraph.resolveUnit(toSchemaItemName, toSchema);
|
|
51
|
-
return this.processUnits(from, to);
|
|
52
|
-
}
|
|
53
|
-
/**
|
|
54
|
-
* @param from Source unit converted from
|
|
55
|
-
* @param to Target unit converted to
|
|
56
|
-
* @internal
|
|
57
|
-
*/
|
|
58
|
-
async processUnits(from, to) {
|
|
59
|
-
if (from.key.matches(to.key))
|
|
60
|
-
return UnitConversion_1.UnitConversion.identity;
|
|
61
|
-
const areCompatible = await Unit_1.Unit.areCompatible(from, to);
|
|
62
|
-
if (!areCompatible)
|
|
63
|
-
throw new core_bentley_1.BentleyError(core_bentley_1.BentleyStatus.ERROR, `Source and target units do not belong to same phenomenon`, () => {
|
|
64
|
-
return { from, to };
|
|
65
|
-
});
|
|
66
|
-
// Add nodes and subsequent children to graph
|
|
67
|
-
await this._uGraph.addUnit(from);
|
|
68
|
-
await this._uGraph.addUnit(to);
|
|
69
|
-
const fromBaseUnits = new Map();
|
|
70
|
-
const toBaseUnits = new Map();
|
|
71
|
-
// Calculate map of UnitConversions to get between from -> base
|
|
72
|
-
const fromMapStore = this._uGraph.reduce(from, fromBaseUnits);
|
|
73
|
-
// Calculate map of UnitConversions to get between base -> to
|
|
74
|
-
const toMapStore = this._uGraph.reduce(to, toBaseUnits);
|
|
75
|
-
if (!this.checkBaseUnitsMatch(fromBaseUnits, toBaseUnits))
|
|
76
|
-
throw new core_bentley_1.BentleyError(core_bentley_1.BentleyStatus.ERROR, `Source and target units do not have matching base units`, () => {
|
|
77
|
-
return { from, to };
|
|
78
|
-
});
|
|
79
|
-
// Final calculations to get singular UnitConversion between from -> to
|
|
80
|
-
const fromMap = fromMapStore.get(from.key.fullName) || UnitConversion_1.UnitConversion.identity;
|
|
81
|
-
const toMap = toMapStore.get(to.key.fullName) || UnitConversion_1.UnitConversion.identity;
|
|
82
|
-
const fromInverse = fromMap.inverse();
|
|
83
|
-
return fromInverse.compose(toMap);
|
|
84
|
-
}
|
|
85
|
-
/**
|
|
86
|
-
* Check if fromBaseUnits's base units and exponents matches toBaseUnits's
|
|
87
|
-
* @param fromBaseUnits Map of base units for source unit
|
|
88
|
-
* @param toBaseUnits Map of base units for target unit
|
|
89
|
-
* @internal
|
|
90
|
-
*/
|
|
91
|
-
checkBaseUnitsMatch(fromBaseUnits, toBaseUnits) {
|
|
92
|
-
// Trim maps of "One" and value that equal zero as they do not affect the base units and calculations
|
|
93
|
-
for (const [key, value] of fromBaseUnits.entries()) {
|
|
94
|
-
const [, schemaItemName] = SchemaItem_1.SchemaItem.parseFullName(key);
|
|
95
|
-
if (schemaItemName === "ONE" || value === 0) {
|
|
96
|
-
fromBaseUnits.delete(key);
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
for (const [key, value] of toBaseUnits.entries()) {
|
|
100
|
-
const [, schemaItemName] = SchemaItem_1.SchemaItem.parseFullName(key);
|
|
101
|
-
if (schemaItemName === "ONE" || value === 0) {
|
|
102
|
-
toBaseUnits.delete(key);
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
if (fromBaseUnits.size !== toBaseUnits.size)
|
|
106
|
-
return false;
|
|
107
|
-
for (const key of fromBaseUnits.keys()) {
|
|
108
|
-
if (!toBaseUnits.has(key) || fromBaseUnits.get(key) !== toBaseUnits.get(key)) {
|
|
109
|
-
// Mismatching key or value
|
|
110
|
-
return false;
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
return true;
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
exports.UnitConverter = UnitConverter;
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.UnitConverter = void 0;
|
|
4
|
+
/*---------------------------------------------------------------------------------------------
|
|
5
|
+
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
|
|
6
|
+
* See LICENSE.md in the project root for license terms and full copyright notice.
|
|
7
|
+
*--------------------------------------------------------------------------------------------*/
|
|
8
|
+
const core_bentley_1 = require("@itwin/core-bentley");
|
|
9
|
+
const SchemaItem_1 = require("../Metadata/SchemaItem");
|
|
10
|
+
const Unit_1 = require("../Metadata/Unit");
|
|
11
|
+
const SchemaKey_1 = require("../SchemaKey");
|
|
12
|
+
const UnitConversion_1 = require("./UnitConversion");
|
|
13
|
+
const UnitTree_1 = require("./UnitTree");
|
|
14
|
+
/**
|
|
15
|
+
* Class constructed with SchemaContext and used to calculate [[UnitConversion]] between Units
|
|
16
|
+
* @internal
|
|
17
|
+
*/
|
|
18
|
+
class UnitConverter {
|
|
19
|
+
/**
|
|
20
|
+
* Create Converter context
|
|
21
|
+
* @param _context SchemaContext with contexts added to it.
|
|
22
|
+
*/
|
|
23
|
+
constructor(_context) {
|
|
24
|
+
this._context = _context;
|
|
25
|
+
this._uGraph = new UnitTree_1.UnitGraph(this._context);
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Find conversion between from and to units, formatted {schemaName}.{schemaItemName} or {schemaName}:{schemaItemName}
|
|
29
|
+
* @param fromUnit SchemaItem full name of source unit
|
|
30
|
+
* @param toUnit SchemaItem full name of target unit
|
|
31
|
+
* @returns [[UnitConversion]] converting fromUnit -> toUnit with a factor and an offset
|
|
32
|
+
* @throws Error if from and to Units' SchemaItem is not found in Schema or Schema prefix is not found in SchemaContext
|
|
33
|
+
* @throws Error if from and to Units do not belong to the same phenomenon
|
|
34
|
+
* @throws Error if definitions' SchemaItems cannot be found in its own or referenced Schemas
|
|
35
|
+
* @throws Error if base units of source and target unit do not match
|
|
36
|
+
*/
|
|
37
|
+
async calculateConversion(fromUnit, toUnit) {
|
|
38
|
+
const [fromSchemaName, fromSchemaItemName] = SchemaItem_1.SchemaItem.parseFullName(fromUnit);
|
|
39
|
+
const [toSchemaName, toSchemaItemName] = SchemaItem_1.SchemaItem.parseFullName(toUnit);
|
|
40
|
+
const fromSchemaKey = new SchemaKey_1.SchemaKey(fromSchemaName);
|
|
41
|
+
const toSchemaKey = new SchemaKey_1.SchemaKey(toSchemaName);
|
|
42
|
+
const fromSchema = await this._context.getSchema(fromSchemaKey);
|
|
43
|
+
const toSchema = await this._context.getSchema(toSchemaKey);
|
|
44
|
+
if (!fromSchema || !toSchema) {
|
|
45
|
+
throw new core_bentley_1.BentleyError(core_bentley_1.BentleyStatus.ERROR, "Cannot find from's and/or to's schema", () => {
|
|
46
|
+
return { from: fromUnit, fromSchema: fromSchemaName, to: toUnit, toSchema: toSchemaName };
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
const from = await this._uGraph.resolveUnit(fromSchemaItemName, fromSchema);
|
|
50
|
+
const to = await this._uGraph.resolveUnit(toSchemaItemName, toSchema);
|
|
51
|
+
return this.processUnits(from, to);
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* @param from Source unit converted from
|
|
55
|
+
* @param to Target unit converted to
|
|
56
|
+
* @internal
|
|
57
|
+
*/
|
|
58
|
+
async processUnits(from, to) {
|
|
59
|
+
if (from.key.matches(to.key))
|
|
60
|
+
return UnitConversion_1.UnitConversion.identity;
|
|
61
|
+
const areCompatible = await Unit_1.Unit.areCompatible(from, to);
|
|
62
|
+
if (!areCompatible)
|
|
63
|
+
throw new core_bentley_1.BentleyError(core_bentley_1.BentleyStatus.ERROR, `Source and target units do not belong to same phenomenon`, () => {
|
|
64
|
+
return { from, to };
|
|
65
|
+
});
|
|
66
|
+
// Add nodes and subsequent children to graph
|
|
67
|
+
await this._uGraph.addUnit(from);
|
|
68
|
+
await this._uGraph.addUnit(to);
|
|
69
|
+
const fromBaseUnits = new Map();
|
|
70
|
+
const toBaseUnits = new Map();
|
|
71
|
+
// Calculate map of UnitConversions to get between from -> base
|
|
72
|
+
const fromMapStore = this._uGraph.reduce(from, fromBaseUnits);
|
|
73
|
+
// Calculate map of UnitConversions to get between base -> to
|
|
74
|
+
const toMapStore = this._uGraph.reduce(to, toBaseUnits);
|
|
75
|
+
if (!this.checkBaseUnitsMatch(fromBaseUnits, toBaseUnits))
|
|
76
|
+
throw new core_bentley_1.BentleyError(core_bentley_1.BentleyStatus.ERROR, `Source and target units do not have matching base units`, () => {
|
|
77
|
+
return { from, to };
|
|
78
|
+
});
|
|
79
|
+
// Final calculations to get singular UnitConversion between from -> to
|
|
80
|
+
const fromMap = fromMapStore.get(from.key.fullName) || UnitConversion_1.UnitConversion.identity;
|
|
81
|
+
const toMap = toMapStore.get(to.key.fullName) || UnitConversion_1.UnitConversion.identity;
|
|
82
|
+
const fromInverse = fromMap.inverse();
|
|
83
|
+
return fromInverse.compose(toMap);
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Check if fromBaseUnits's base units and exponents matches toBaseUnits's
|
|
87
|
+
* @param fromBaseUnits Map of base units for source unit
|
|
88
|
+
* @param toBaseUnits Map of base units for target unit
|
|
89
|
+
* @internal
|
|
90
|
+
*/
|
|
91
|
+
checkBaseUnitsMatch(fromBaseUnits, toBaseUnits) {
|
|
92
|
+
// Trim maps of "One" and value that equal zero as they do not affect the base units and calculations
|
|
93
|
+
for (const [key, value] of fromBaseUnits.entries()) {
|
|
94
|
+
const [, schemaItemName] = SchemaItem_1.SchemaItem.parseFullName(key);
|
|
95
|
+
if (schemaItemName === "ONE" || value === 0) {
|
|
96
|
+
fromBaseUnits.delete(key);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
for (const [key, value] of toBaseUnits.entries()) {
|
|
100
|
+
const [, schemaItemName] = SchemaItem_1.SchemaItem.parseFullName(key);
|
|
101
|
+
if (schemaItemName === "ONE" || value === 0) {
|
|
102
|
+
toBaseUnits.delete(key);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
if (fromBaseUnits.size !== toBaseUnits.size)
|
|
106
|
+
return false;
|
|
107
|
+
for (const key of fromBaseUnits.keys()) {
|
|
108
|
+
if (!toBaseUnits.has(key) || fromBaseUnits.get(key) !== toBaseUnits.get(key)) {
|
|
109
|
+
// Mismatching key or value
|
|
110
|
+
return false;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
return true;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
exports.UnitConverter = UnitConverter;
|
|
117
117
|
//# sourceMappingURL=UnitConverter.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"UnitConverter.js","sourceRoot":"","sources":["../../../src/UnitConversion/UnitConverter.ts"],"names":[],"mappings":";;;AAAA;;;+FAG+F;AAC/F,sDAAkE;AAGlE,uDAAoD;AACpD,2CAAwC;AACxC,4CAAyC;AACzC,qDAAkD;AAClD,yCAAuC;AAEvC;;;GAGG;AACH,MAAa,aAAa;IAGxB;;;OAGG;IACH,YAA6B,QAAuB;QAAvB,aAAQ,GAAR,QAAQ,CAAe;QAClD,IAAI,CAAC,OAAO,GAAG,IAAI,oBAAS,CAAC,IA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AAlHD,sCAkHC","sourcesContent":["/*---------------------------------------------------------------------------------------------\
|
|
1
|
+
{"version":3,"file":"UnitConverter.js","sourceRoot":"","sources":["../../../src/UnitConversion/UnitConverter.ts"],"names":[],"mappings":";;;AAAA;;;+FAG+F;AAC/F,sDAAkE;AAGlE,uDAAoD;AACpD,2CAAwC;AACxC,4CAAyC;AACzC,qDAAkD;AAClD,yCAAuC;AAEvC;;;GAGG;AACH,MAAa,aAAa;IAGxB;;;OAGG;IACH,YAA6B,QAAuB;QAAvB,aAAQ,GAAR,QAAQ,CAAe;QAClD,IAAI,CAAC,OAAO,GAAG,IAAI,oBAAS,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;IAC9C,CAAC;IAED;;;;;;;;;OASG;IACI,KAAK,CAAC,mBAAmB,CAAC,QAAgB,EAAE,MAAc;QAC/D,MAAM,CAAC,cAAc,EAAE,kBAAkB,CAAC,GAAG,uBAAU,CAAC,aAAa,CAAC,QAAQ,CAAC,CAAC;QAChF,MAAM,CAAC,YAAY,EAAE,gBAAgB,CAAC,GAAG,uBAAU,CAAC,aAAa,CAAC,MAAM,CAAC,CAAC;QAC1E,MAAM,aAAa,GAAG,IAAI,qBAAS,CAAC,cAAc,CAAC,CAAC;QACpD,MAAM,WAAW,GAAG,IAAI,qBAAS,CAAC,YAAY,CAAC,CAAC;QAEhD,MAAM,UAAU,GAAG,MAAM,IAAI,CAAC,QAAQ,CAAC,SAAS,CAAC,aAAa,CAAC,CAAC;QAChE,MAAM,QAAQ,GAAG,MAAM,IAAI,CAAC,QAAQ,CAAC,SAAS,CAAC,WAAW,CAAC,CAAC;QAE5D,IAAI,CAAC,UAAU,IAAI,CAAC,QAAQ,EAAE;YAC5B,MAAM,IAAI,2BAAY,CAAC,4BAAa,CAAC,KAAK,EAAE,uCAAuC,EAAE,GAAG,EAAE;gBACxF,OAAO,EAAE,IAAI,EAAE,QAAQ,EAAE,UAAU,EAAE,cAAc,EAAE,EAAE,EAAE,MAAM,EAAE,QAA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GAAG,CAAC,CAAC;aACzB;SACF;QAED,IAAI,aAAa,CAAC,IAAI,KAAK,WAAW,CAAC,IAAI;YACzC,OAAO,KAAK,CAAC;QAEf,KAAK,MAAM,GAAG,IAAI,aAAa,CAAC,IAAI,EAAE,EAAE;YACtC,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC,GAAG,CAAC,IAAI,aAAa,CAAC,GAAG,CAAC,GAAG,CAAC,KAAK,WAAW,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE;gBAC5E,2BAA2B;gBAC3B,OAAO,KAAK,CAAC;aACd;SACF;QAED,OAAO,IAAI,CAAC;IACd,CAAC;CACF;AAlHD,sCAkHC","sourcesContent":["/*---------------------------------------------------------------------------------------------\n* Copyright (c) Bentley Systems, Incorporated. All rights reserved.\n* See LICENSE.md in the project root for license terms and full copyright notice.\n*--------------------------------------------------------------------------------------------*/\nimport { BentleyError, BentleyStatus } from \"@itwin/core-bentley\";\nimport { SchemaContext } from \"../Context\";\nimport { Constant } from \"../Metadata/Constant\";\nimport { SchemaItem } from \"../Metadata/SchemaItem\";\nimport { Unit } from \"../Metadata/Unit\";\nimport { SchemaKey } from \"../SchemaKey\";\nimport { UnitConversion } from \"./UnitConversion\";\nimport { UnitGraph } from \"./UnitTree\";\n\n/**\n * Class constructed with SchemaContext and used to calculate [[UnitConversion]] between Units\n * @internal\n */\nexport class UnitConverter {\n private _uGraph: UnitGraph;\n\n /**\n * Create Converter context\n * @param _context SchemaContext with contexts added to it.\n */\n constructor(private readonly _context: SchemaContext) {\n this._uGraph = new UnitGraph(this._context);\n }\n\n /**\n * Find conversion between from and to units, formatted {schemaName}.{schemaItemName} or {schemaName}:{schemaItemName}\n * @param fromUnit SchemaItem full name of source unit\n * @param toUnit SchemaItem full name of target unit\n * @returns [[UnitConversion]] converting fromUnit -> toUnit with a factor and an offset\n * @throws Error if from and to Units' SchemaItem is not found in Schema or Schema prefix is not found in SchemaContext\n * @throws Error if from and to Units do not belong to the same phenomenon\n * @throws Error if definitions' SchemaItems cannot be found in its own or referenced Schemas\n * @throws Error if base units of source and target unit do not match\n */\n public async calculateConversion(fromUnit: string, toUnit: string): Promise<UnitConversion> {\n const [fromSchemaName, fromSchemaItemName] = SchemaItem.parseFullName(fromUnit);\n const [toSchemaName, toSchemaItemName] = SchemaItem.parseFullName(toUnit);\n const fromSchemaKey = new SchemaKey(fromSchemaName);\n const toSchemaKey = new SchemaKey(toSchemaName);\n\n const fromSchema = await this._context.getSchema(fromSchemaKey);\n const toSchema = await this._context.getSchema(toSchemaKey);\n\n if (!fromSchema || !toSchema) {\n throw new BentleyError(BentleyStatus.ERROR, \"Cannot find from's and/or to's schema\", () => {\n return { from: fromUnit, fromSchema: fromSchemaName, to: toUnit, toSchema: toSchemaName };\n });\n }\n\n const from = await this._uGraph.resolveUnit(fromSchemaItemName, fromSchema);\n const to = await this._uGraph.resolveUnit(toSchemaItemName, toSchema);\n\n return this.processUnits(from, to);\n }\n\n /**\n * @param from Source unit converted from\n * @param to Target unit converted to\n * @internal\n */\n private async processUnits(from: Unit | Constant, to: Unit | Constant): Promise<UnitConversion> {\n if (from.key.matches(to.key))\n return UnitConversion.identity;\n\n const areCompatible = await Unit.areCompatible(from as Unit, to as Unit);\n if (!areCompatible)\n throw new BentleyError(BentleyStatus.ERROR, `Source and target units do not belong to same phenomenon`, () => {\n return { from, to };\n });\n\n // Add nodes and subsequent children to graph\n await this._uGraph.addUnit(from);\n await this._uGraph.addUnit(to);\n\n const fromBaseUnits = new Map<string, number>();\n const toBaseUnits = new Map<string, number>();\n // Calculate map of UnitConversions to get between from -> base\n const fromMapStore = this._uGraph.reduce(from, fromBaseUnits);\n // Calculate map of UnitConversions to get between base -> to\n const toMapStore = this._uGraph.reduce(to, toBaseUnits);\n\n if (!this.checkBaseUnitsMatch(fromBaseUnits, toBaseUnits))\n throw new BentleyError(BentleyStatus.ERROR, `Source and target units do not have matching base units`, () => {\n return { from, to };\n });\n\n // Final calculations to get singular UnitConversion between from -> to\n const fromMap = fromMapStore.get(from.key.fullName) || UnitConversion.identity;\n const toMap = toMapStore.get(to.key.fullName) || UnitConversion.identity;\n const fromInverse = fromMap.inverse();\n return fromInverse.compose(toMap);\n }\n\n /**\n * Check if fromBaseUnits's base units and exponents matches toBaseUnits's\n * @param fromBaseUnits Map of base units for source unit\n * @param toBaseUnits Map of base units for target unit\n * @internal\n */\n private checkBaseUnitsMatch(fromBaseUnits: Map<string, number>, toBaseUnits: Map<string, number>): boolean {\n // Trim maps of \"One\" and value that equal zero as they do not affect the base units and calculations\n for (const [key, value] of fromBaseUnits.entries()) {\n const [, schemaItemName] = SchemaItem.parseFullName(key);\n if (schemaItemName === \"ONE\" || value === 0) {\n fromBaseUnits.delete(key);\n }\n }\n\n for (const [key, value] of toBaseUnits.entries()) {\n const [, schemaItemName] = SchemaItem.parseFullName(key);\n if (schemaItemName === \"ONE\" || value === 0) {\n toBaseUnits.delete(key);\n }\n }\n\n if (fromBaseUnits.size !== toBaseUnits.size)\n return false;\n\n for (const key of fromBaseUnits.keys()) {\n if (!toBaseUnits.has(key) || fromBaseUnits.get(key) !== toBaseUnits.get(key)) {\n // Mismatching key or value\n return false;\n }\n }\n\n return true;\n }\n}\n"]}
|
|
@@ -1,44 +1,44 @@
|
|
|
1
|
-
import { SchemaContext } from "../Context";
|
|
2
|
-
import { Constant } from "../Metadata/Constant";
|
|
3
|
-
import { Schema } from "../Metadata/Schema";
|
|
4
|
-
import { Unit } from "../Metadata/Unit";
|
|
5
|
-
import { UnitConversion } from "./UnitConversion";
|
|
6
|
-
import { Graph } from "./Graph";
|
|
7
|
-
/** @internal */
|
|
8
|
-
export declare class GraphUtils {
|
|
9
|
-
/**
|
|
10
|
-
* DFS traversal - Post order
|
|
11
|
-
* @param _graph Graph to traverse
|
|
12
|
-
* @param start Starting node
|
|
13
|
-
* @param keyFrom Get key from label
|
|
14
|
-
* @param op Reducing function
|
|
15
|
-
* @param initial Initial label
|
|
16
|
-
*/
|
|
17
|
-
static dfsReduce<T>(_graph: Graph<Unit | Constant>, key: string, op: (previous: T, current: string) => T, initial: T, baseUnitsMap: Map<string, number>, accumulatedExponent: number): T;
|
|
18
|
-
}
|
|
19
|
-
/** @internal */
|
|
20
|
-
export declare class UnitGraph {
|
|
21
|
-
private _context;
|
|
22
|
-
private _graph;
|
|
23
|
-
constructor(_context: SchemaContext);
|
|
24
|
-
/**
|
|
25
|
-
* Tries to find the unit/constant given by name in currentSchema
|
|
26
|
-
* @param name SchemaItem name or parsed definition to find unit of; Could be {schemaName}:{schemaItemName} or {alias}:{schemaItemName} or {schemaItemName}
|
|
27
|
-
* @param currentSchema schema to find name in; name could also be in a referenced schema of current schema
|
|
28
|
-
*/
|
|
29
|
-
resolveUnit(name: string, currentSchema: Schema): Promise<Unit | Constant>;
|
|
30
|
-
/**
|
|
31
|
-
* Adds unit and corresponding children to graph as well as edges between units
|
|
32
|
-
* @param unit Current unit to be added to graph
|
|
33
|
-
*/
|
|
34
|
-
addUnit(unit: Unit | Constant): Promise<void>;
|
|
35
|
-
private isIdentity;
|
|
36
|
-
/**
|
|
37
|
-
* Reduce the tree to produce a single map
|
|
38
|
-
* @param unit Unit to be processed
|
|
39
|
-
* @param stopNodes The tree exploration should stop here
|
|
40
|
-
*/
|
|
41
|
-
reduce(unit: Unit | Constant, baseUnitsMap: Map<string, number>): Map<string, UnitConversion>;
|
|
42
|
-
private reducingFunction;
|
|
43
|
-
}
|
|
1
|
+
import { SchemaContext } from "../Context";
|
|
2
|
+
import { Constant } from "../Metadata/Constant";
|
|
3
|
+
import { Schema } from "../Metadata/Schema";
|
|
4
|
+
import { Unit } from "../Metadata/Unit";
|
|
5
|
+
import { UnitConversion } from "./UnitConversion";
|
|
6
|
+
import { Graph } from "./Graph";
|
|
7
|
+
/** @internal */
|
|
8
|
+
export declare class GraphUtils {
|
|
9
|
+
/**
|
|
10
|
+
* DFS traversal - Post order
|
|
11
|
+
* @param _graph Graph to traverse
|
|
12
|
+
* @param start Starting node
|
|
13
|
+
* @param keyFrom Get key from label
|
|
14
|
+
* @param op Reducing function
|
|
15
|
+
* @param initial Initial label
|
|
16
|
+
*/
|
|
17
|
+
static dfsReduce<T>(_graph: Graph<Unit | Constant>, key: string, op: (previous: T, current: string) => T, initial: T, baseUnitsMap: Map<string, number>, accumulatedExponent: number): T;
|
|
18
|
+
}
|
|
19
|
+
/** @internal */
|
|
20
|
+
export declare class UnitGraph {
|
|
21
|
+
private _context;
|
|
22
|
+
private _graph;
|
|
23
|
+
constructor(_context: SchemaContext);
|
|
24
|
+
/**
|
|
25
|
+
* Tries to find the unit/constant given by name in currentSchema
|
|
26
|
+
* @param name SchemaItem name or parsed definition to find unit of; Could be {schemaName}:{schemaItemName} or {alias}:{schemaItemName} or {schemaItemName}
|
|
27
|
+
* @param currentSchema schema to find name in; name could also be in a referenced schema of current schema
|
|
28
|
+
*/
|
|
29
|
+
resolveUnit(name: string, currentSchema: Schema): Promise<Unit | Constant>;
|
|
30
|
+
/**
|
|
31
|
+
* Adds unit and corresponding children to graph as well as edges between units
|
|
32
|
+
* @param unit Current unit to be added to graph
|
|
33
|
+
*/
|
|
34
|
+
addUnit(unit: Unit | Constant): Promise<void>;
|
|
35
|
+
private isIdentity;
|
|
36
|
+
/**
|
|
37
|
+
* Reduce the tree to produce a single map
|
|
38
|
+
* @param unit Unit to be processed
|
|
39
|
+
* @param stopNodes The tree exploration should stop here
|
|
40
|
+
*/
|
|
41
|
+
reduce(unit: Unit | Constant, baseUnitsMap: Map<string, number>): Map<string, UnitConversion>;
|
|
42
|
+
private reducingFunction;
|
|
43
|
+
}
|
|
44
44
|
//# sourceMappingURL=UnitTree.d.ts.map
|