@itwin/ecschema-metadata 4.0.0-dev.23 → 4.0.0-dev.24

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.
Files changed (140) hide show
  1. package/lib/cjs/Constants.d.ts +23 -23
  2. package/lib/cjs/Constants.js +33 -33
  3. package/lib/cjs/Constants.js.map +1 -1
  4. package/lib/cjs/Context.d.ts +139 -139
  5. package/lib/cjs/Context.js +204 -204
  6. package/lib/cjs/DelayedPromise.d.ts +88 -88
  7. package/lib/cjs/DelayedPromise.js +91 -91
  8. package/lib/cjs/DelayedPromise.js.map +1 -1
  9. package/lib/cjs/Deserialization/AbstractParser.d.ts +45 -45
  10. package/lib/cjs/Deserialization/AbstractParser.js +11 -11
  11. package/lib/cjs/Deserialization/AbstractParser.js.map +1 -1
  12. package/lib/cjs/Deserialization/Helper.d.ts +251 -251
  13. package/lib/cjs/Deserialization/Helper.js +843 -843
  14. package/lib/cjs/Deserialization/Helper.js.map +1 -1
  15. package/lib/cjs/Deserialization/JsonParser.d.ts +165 -165
  16. package/lib/cjs/Deserialization/JsonParser.js +680 -680
  17. package/lib/cjs/Deserialization/JsonParser.js.map +1 -1
  18. package/lib/cjs/Deserialization/JsonProps.d.ts +282 -282
  19. package/lib/cjs/Deserialization/JsonProps.js +9 -9
  20. package/lib/cjs/Deserialization/JsonProps.js.map +1 -1
  21. package/lib/cjs/Deserialization/SchemaGraphUtil.d.ts +34 -34
  22. package/lib/cjs/Deserialization/SchemaGraphUtil.js +77 -77
  23. package/lib/cjs/Deserialization/SchemaGraphUtil.js.map +1 -1
  24. package/lib/cjs/Deserialization/XmlParser.d.ts +85 -85
  25. package/lib/cjs/Deserialization/XmlParser.js +973 -973
  26. package/lib/cjs/Deserialization/XmlParser.js.map +1 -1
  27. package/lib/cjs/Deserialization/XmlSerializationUtils.d.ts +54 -54
  28. package/lib/cjs/Deserialization/XmlSerializationUtils.js +174 -174
  29. package/lib/cjs/Deserialization/XmlSerializationUtils.js.map +1 -1
  30. package/lib/cjs/ECName.d.ts +30 -30
  31. package/lib/cjs/ECName.js +85 -85
  32. package/lib/cjs/ECName.js.map +1 -1
  33. package/lib/cjs/ECObjects.d.ts +162 -162
  34. package/lib/cjs/ECObjects.js +453 -453
  35. package/lib/cjs/ECObjects.js.map +1 -1
  36. package/lib/cjs/Exception.d.ts +43 -43
  37. package/lib/cjs/Exception.js +84 -84
  38. package/lib/cjs/Exception.js.map +1 -1
  39. package/lib/cjs/Interfaces.d.ts +66 -66
  40. package/lib/cjs/Interfaces.js +9 -9
  41. package/lib/cjs/Interfaces.js.map +1 -1
  42. package/lib/cjs/Metadata/Class.d.ts +243 -243
  43. package/lib/cjs/Metadata/Class.js +558 -558
  44. package/lib/cjs/Metadata/Class.js.map +1 -1
  45. package/lib/cjs/Metadata/Constant.d.ts +64 -64
  46. package/lib/cjs/Metadata/Constant.js +123 -123
  47. package/lib/cjs/Metadata/Constant.js.map +1 -1
  48. package/lib/cjs/Metadata/CustomAttribute.d.ts +23 -23
  49. package/lib/cjs/Metadata/CustomAttribute.js +27 -27
  50. package/lib/cjs/Metadata/CustomAttribute.js.map +1 -1
  51. package/lib/cjs/Metadata/CustomAttributeClass.d.ts +39 -39
  52. package/lib/cjs/Metadata/CustomAttributeClass.js +68 -68
  53. package/lib/cjs/Metadata/CustomAttributeClass.js.map +1 -1
  54. package/lib/cjs/Metadata/EntityClass.d.ts +77 -77
  55. package/lib/cjs/Metadata/EntityClass.js +230 -230
  56. package/lib/cjs/Metadata/EntityClass.js.map +1 -1
  57. package/lib/cjs/Metadata/Enumeration.d.ts +86 -86
  58. package/lib/cjs/Metadata/Enumeration.js +159 -159
  59. package/lib/cjs/Metadata/Enumeration.js.map +1 -1
  60. package/lib/cjs/Metadata/Format.d.ts +98 -98
  61. package/lib/cjs/Metadata/Format.js +242 -242
  62. package/lib/cjs/Metadata/Format.js.map +1 -1
  63. package/lib/cjs/Metadata/InvertedUnit.d.ts +49 -49
  64. package/lib/cjs/Metadata/InvertedUnit.js +97 -97
  65. package/lib/cjs/Metadata/InvertedUnit.js.map +1 -1
  66. package/lib/cjs/Metadata/KindOfQuantity.d.ts +65 -65
  67. package/lib/cjs/Metadata/KindOfQuantity.js +225 -225
  68. package/lib/cjs/Metadata/KindOfQuantity.js.map +1 -1
  69. package/lib/cjs/Metadata/Mixin.d.ts +54 -54
  70. package/lib/cjs/Metadata/Mixin.js +113 -113
  71. package/lib/cjs/Metadata/Mixin.js.map +1 -1
  72. package/lib/cjs/Metadata/OverrideFormat.d.ts +55 -55
  73. package/lib/cjs/Metadata/OverrideFormat.js +88 -88
  74. package/lib/cjs/Metadata/OverrideFormat.js.map +1 -1
  75. package/lib/cjs/Metadata/Phenomenon.d.ts +33 -33
  76. package/lib/cjs/Metadata/Phenomenon.js +59 -59
  77. package/lib/cjs/Metadata/Phenomenon.js.map +1 -1
  78. package/lib/cjs/Metadata/Property.d.ts +197 -197
  79. package/lib/cjs/Metadata/Property.js +491 -491
  80. package/lib/cjs/Metadata/Property.js.map +1 -1
  81. package/lib/cjs/Metadata/PropertyCategory.d.ts +39 -39
  82. package/lib/cjs/Metadata/PropertyCategory.js +61 -61
  83. package/lib/cjs/Metadata/PropertyCategory.js.map +1 -1
  84. package/lib/cjs/Metadata/RelationshipClass.d.ts +152 -152
  85. package/lib/cjs/Metadata/RelationshipClass.js +349 -349
  86. package/lib/cjs/Metadata/RelationshipClass.js.map +1 -1
  87. package/lib/cjs/Metadata/Schema.d.ts +291 -291
  88. package/lib/cjs/Metadata/Schema.js +520 -520
  89. package/lib/cjs/Metadata/Schema.js.map +1 -1
  90. package/lib/cjs/Metadata/SchemaItem.d.ts +61 -61
  91. package/lib/cjs/Metadata/SchemaItem.js +136 -136
  92. package/lib/cjs/Metadata/SchemaItem.js.map +1 -1
  93. package/lib/cjs/Metadata/Unit.d.ts +76 -76
  94. package/lib/cjs/Metadata/Unit.js +162 -162
  95. package/lib/cjs/Metadata/UnitSystem.d.ts +20 -20
  96. package/lib/cjs/Metadata/UnitSystem.js +29 -29
  97. package/lib/cjs/Metadata/UnitSystem.js.map +1 -1
  98. package/lib/cjs/PropertyTypes.d.ts +49 -49
  99. package/lib/cjs/PropertyTypes.js +91 -91
  100. package/lib/cjs/PropertyTypes.js.map +1 -1
  101. package/lib/cjs/SchemaJsonLocater.d.ts +34 -34
  102. package/lib/cjs/SchemaJsonLocater.js +42 -42
  103. package/lib/cjs/SchemaJsonLocater.js.map +1 -1
  104. package/lib/cjs/SchemaKey.d.ts +110 -110
  105. package/lib/cjs/SchemaKey.js +219 -219
  106. package/lib/cjs/SchemaKey.js.map +1 -1
  107. package/lib/cjs/SchemaLoader.d.ts +23 -23
  108. package/lib/cjs/SchemaLoader.js +47 -47
  109. package/lib/cjs/SchemaLoader.js.map +1 -1
  110. package/lib/cjs/SchemaPartVisitorDelegate.d.ts +274 -274
  111. package/lib/cjs/SchemaPartVisitorDelegate.js +212 -212
  112. package/lib/cjs/SchemaPartVisitorDelegate.js.map +1 -1
  113. package/lib/cjs/UnitConversion/Graph.d.ts +34 -34
  114. package/lib/cjs/UnitConversion/Graph.js +83 -83
  115. package/lib/cjs/UnitConversion/Graph.js.map +1 -1
  116. package/lib/cjs/UnitConversion/Parser.d.ts +8 -8
  117. package/lib/cjs/UnitConversion/Parser.js +42 -42
  118. package/lib/cjs/UnitConversion/Parser.js.map +1 -1
  119. package/lib/cjs/UnitConversion/UnitConversion.d.ts +45 -45
  120. package/lib/cjs/UnitConversion/UnitConversion.js +77 -77
  121. package/lib/cjs/UnitConversion/UnitConversion.js.map +1 -1
  122. package/lib/cjs/UnitConversion/UnitConverter.d.ts +39 -39
  123. package/lib/cjs/UnitConversion/UnitConverter.js +116 -116
  124. package/lib/cjs/UnitConversion/UnitConverter.js.map +1 -1
  125. package/lib/cjs/UnitConversion/UnitTree.d.ts +43 -43
  126. package/lib/cjs/UnitConversion/UnitTree.js +169 -169
  127. package/lib/cjs/UnitConversion/UnitTree.js.map +1 -1
  128. package/lib/cjs/UnitProvider/SchemaUnitProvider.d.ts +77 -77
  129. package/lib/cjs/UnitProvider/SchemaUnitProvider.js +236 -236
  130. package/lib/cjs/UnitProvider/SchemaUnitProvider.js.map +1 -1
  131. package/lib/cjs/Validation/SchemaWalker.d.ts +23 -23
  132. package/lib/cjs/Validation/SchemaWalker.js +53 -53
  133. package/lib/cjs/Validation/SchemaWalker.js.map +1 -1
  134. package/lib/cjs/ecschema-metadata.d.ts +51 -51
  135. package/lib/cjs/ecschema-metadata.js +96 -96
  136. package/lib/cjs/ecschema-metadata.js.map +1 -1
  137. package/lib/cjs/utils/SchemaGraph.d.ts +25 -25
  138. package/lib/cjs/utils/SchemaGraph.js +65 -65
  139. package/lib/cjs/utils/SchemaGraph.js.map +1 -1
  140. package/package.json +7 -7
@@ -1,237 +1,237 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.SchemaUnitProvider = 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 Context_1 = require("../Context");
10
- const SchemaItem_1 = require("../Metadata/SchemaItem");
11
- const SchemaKey_1 = require("../SchemaKey");
12
- const Unit_1 = require("../Metadata/Unit");
13
- const ECObjects_1 = require("../ECObjects");
14
- const UnitConverter_1 = require("../UnitConversion/UnitConverter");
15
- /**
16
- * Class used to find Units in SchemaContext by attributes such as Phenomenon and DisplayLabel.
17
- * @alpha
18
- */
19
- class SchemaUnitProvider {
20
- /**
21
- *
22
- * @param contextOrLocater The SchemaContext or a different ISchemaLocater implementation used to retrieve the schema. The SchemaContext
23
- * class implements the ISchemaLocater interface. If the provided locater is not a SchemaContext instance a new SchemaContext will be
24
- * created and the locater will be added.
25
- * @param _unitExtraData Additional data like alternate display label not found in Units Schema to match with Units; Defaults to empty array.
26
- */
27
- constructor(contextOrLocater, _unitExtraData = []) {
28
- this._unitExtraData = _unitExtraData;
29
- if (contextOrLocater instanceof Context_1.SchemaContext) {
30
- this._context = contextOrLocater;
31
- }
32
- else {
33
- this._context = new Context_1.SchemaContext();
34
- this._context.addLocater(contextOrLocater);
35
- }
36
- this._unitConverter = new UnitConverter_1.UnitConverter(this._context);
37
- }
38
- /**
39
- * Find unit in a schema that has unitName.
40
- * @param unitName Full name of unit.
41
- * @returns UnitProps interface from @itwin/core-quantity whose name matches unitName.
42
- */
43
- async findUnitByName(unitName) {
44
- const unit = await this.findECUnitByName(unitName);
45
- return this.getUnitsProps(unit);
46
- }
47
- /**
48
- * Find all units in context that belongs to given phenomenon.
49
- * @param phenomenon Full name of phenomenon.
50
- * @returns Array of UnitProps (from @itwin/core-quantity) interface objects whose name matches phenomenon param.
51
- */
52
- async getUnitsByFamily(phenomenon) {
53
- // Check if schema exists and phenomenon exists in schema
54
- const [schemaName, schemaItemName] = SchemaItem_1.SchemaItem.parseFullName(phenomenon);
55
- const schemaKey = new SchemaKey_1.SchemaKey(schemaName);
56
- const schema = await this._context.getSchema(schemaKey);
57
- if (!schema) {
58
- throw new core_bentley_1.BentleyError(core_bentley_1.BentleyStatus.ERROR, "Cannot find schema for phenomenon", () => {
59
- return { phenomenon, schema: schemaName };
60
- });
61
- }
62
- const itemKey = new SchemaKey_1.SchemaItemKey(schemaItemName, schema.schemaKey);
63
- const phenom = await this._context.getSchemaItem(itemKey);
64
- if (!phenom)
65
- throw new core_bentley_1.BentleyError(core_bentley_1.BentleyStatus.ERROR, "Cannot find schema item/phenomenon", () => {
66
- return { item: schemaItemName, schema: schemaName };
67
- });
68
- if (phenom.schemaItemType !== ECObjects_1.SchemaItemType.Phenomenon)
69
- throw new core_bentley_1.BentleyError(core_bentley_1.BentleyStatus.ERROR, "Item is not a phenomenon", () => {
70
- return { itemType: phenom.key.fullName };
71
- });
72
- // Find units' full name that match given phenomenon param.
73
- const filteredUnits = [];
74
- const schemaItems = this._context.getSchemaItems();
75
- let { value, done } = schemaItems.next();
76
- while (!done) {
77
- if (Unit_1.Unit.isUnit(value)) {
78
- const foundPhenomenon = await value.phenomenon;
79
- if (foundPhenomenon && foundPhenomenon.key.matchesFullName(phenomenon)) {
80
- const unitProps = this.getUnitsProps(value);
81
- filteredUnits.push(unitProps);
82
- }
83
- }
84
- ({ value, done } = schemaItems.next());
85
- }
86
- return filteredUnits;
87
- }
88
- /**
89
- * Find alternate display labels associated with unitName, if any.
90
- * @param unitName Full name of Unit.
91
- */
92
- getAlternateDisplayLabels(unitName) {
93
- let alternateLabels = [];
94
- for (const entry of this._unitExtraData) {
95
- if (entry.name.toLowerCase() === unitName.toLowerCase()) {
96
- alternateLabels = entry.altDisplayLabels;
97
- }
98
- }
99
- return alternateLabels;
100
- }
101
- /**
102
- * Finds Unit by unitLabel, which could be a display label in the schema or alternate an display label defined in
103
- * this._unitExtraData. If there are duplicates of the same display label in the context or teh same alternate display
104
- * labels, specify schemaName, phenomenon, or unitSystem to get a specific unit.
105
- *
106
- * @param unitLabel Display label or alternate display label to query unit by.
107
- * @param schemaName Ensure Unit with unitLabel belongs to Schema with schemaName.
108
- * @param phenomenon Full name of phenomenon that Unit belongs to.
109
- * @param unitSystem Full name of unitSystem that Unit belongs to.
110
- * @returns The UnitProps interface from the @itwin/core-quantity package.
111
- */
112
- async findUnit(unitLabel, schemaName, phenomenon, unitSystem) {
113
- const findLabel = unitLabel.toLowerCase();
114
- const findSchema = schemaName ? schemaName.toLowerCase() : undefined;
115
- const findPhenomenon = phenomenon ? phenomenon.toLowerCase() : undefined;
116
- const findUnitSystem = unitSystem ? unitSystem.toLowerCase() : undefined;
117
- let foundUnit;
118
- try {
119
- try {
120
- foundUnit = await this.findUnitByDisplayLabel(findLabel, findSchema, findPhenomenon, findUnitSystem);
121
- }
122
- catch (err) {
123
- // If there is no Unit with display label that matches label, then check for alternate display labels that may match
124
- foundUnit = await this.findUnitByAltDisplayLabel(findLabel, findSchema, findPhenomenon, findUnitSystem);
125
- }
126
- }
127
- catch (err) {
128
- throw new core_bentley_1.BentleyError(core_bentley_1.BentleyStatus.ERROR, "Cannot find unit with label", () => {
129
- return { unitLabel };
130
- });
131
- }
132
- return this.getUnitsProps(foundUnit);
133
- }
134
- /**
135
- * Gets the @itwin/core-quantity UnitConversion for the given fromUnit and toUnit.
136
- * @param fromUnit The UnitProps of the 'from' unit.
137
- * @param toUnit The UnitProps of the 'to' unit.
138
- * @returns The UnitConversion interface from the @itwin/core-quantity package.
139
- */
140
- async getConversion(fromUnit, toUnit) {
141
- const conversion = await this._unitConverter.calculateConversion(fromUnit.name, toUnit.name);
142
- return {
143
- factor: conversion.factor,
144
- offset: conversion.offset,
145
- };
146
- }
147
- /**
148
- * Find unit in a schema that has unitName.
149
- * @param unitName Full name of unit.
150
- * @returns Unit whose full name matches unitName.
151
- */
152
- async findECUnitByName(unitName) {
153
- // Check if schema exists and unit exists in schema
154
- const [schemaName, schemaItemName] = SchemaItem_1.SchemaItem.parseFullName(unitName);
155
- const schemaKey = new SchemaKey_1.SchemaKey(schemaName);
156
- const schema = await this._context.getSchema(schemaKey);
157
- if (!schema) {
158
- throw new core_bentley_1.BentleyError(core_bentley_1.BentleyStatus.ERROR, "Cannot find schema for unit", () => {
159
- return { schema: schemaName, unit: unitName };
160
- });
161
- }
162
- const itemKey = new SchemaKey_1.SchemaItemKey(schemaItemName, schema.schemaKey);
163
- const item = await this._context.getSchemaItem(itemKey);
164
- if (!item)
165
- throw new core_bentley_1.BentleyError(core_bentley_1.BentleyStatus.ERROR, "Cannot find schema item/unit", () => {
166
- return { item: schemaItemName, schema: schemaName };
167
- });
168
- if (item.schemaItemType === ECObjects_1.SchemaItemType.Unit)
169
- return item;
170
- throw new core_bentley_1.BentleyError(core_bentley_1.BentleyStatus.ERROR, "Item is not a unit", () => {
171
- return { itemType: item.key.fullName };
172
- });
173
- }
174
- /**
175
- * Gets the @itwin/core UnitProps for the given Unit.
176
- * @param unit The Unit to convert.
177
- * @returns UnitProps interface from @itwin/core.
178
- */
179
- getUnitsProps(unit) {
180
- var _a;
181
- return {
182
- name: unit.fullName,
183
- label: (_a = unit.label) !== null && _a !== void 0 ? _a : "",
184
- phenomenon: unit.phenomenon ? unit.phenomenon.fullName : "",
185
- isValid: true,
186
- system: unit.unitSystem === undefined ? "" : unit.unitSystem.fullName,
187
- };
188
- }
189
- /**
190
- * Finds Unit by displayLabel and that it belongs to schemaName, phenomenon, and unitSystem if defined.
191
- * @internal
192
- */
193
- async findUnitByDisplayLabel(displayLabel, schemaName, phenomenon, unitSystem) {
194
- var _a;
195
- const schemaItems = this._context.getSchemaItems();
196
- let { value, done } = schemaItems.next();
197
- while (!done) {
198
- if (Unit_1.Unit.isUnit(value) && ((_a = value.label) === null || _a === void 0 ? void 0 : _a.toLowerCase()) === displayLabel) {
199
- const currPhenomenon = await value.phenomenon;
200
- const currUnitSystem = await value.unitSystem;
201
- if (!schemaName || value.schema.name.toLowerCase() === schemaName)
202
- if (!phenomenon || (currPhenomenon && currPhenomenon.key.matchesFullName(phenomenon)))
203
- if (!unitSystem || (currUnitSystem && currUnitSystem.key.matchesFullName(unitSystem)))
204
- return value;
205
- }
206
- ({ value, done } = schemaItems.next());
207
- }
208
- throw new core_bentley_1.BentleyError(core_bentley_1.BentleyStatus.ERROR, "Cannot find unit with display label", () => {
209
- return { displayLabel };
210
- });
211
- }
212
- /**
213
- * Finds Unit by altDisplayLabel and that it belongs to schemaName, phenomenon, and unitSystem if defined.
214
- * @internal
215
- */
216
- async findUnitByAltDisplayLabel(altDisplayLabel, schemaName, phenomenon, unitSystem) {
217
- for (const entry of this._unitExtraData) {
218
- if (entry.altDisplayLabels && entry.altDisplayLabels.length > 0) {
219
- if (entry.altDisplayLabels.findIndex((ref) => ref.toLowerCase() === altDisplayLabel) !== -1) {
220
- // Found altDisplayLabel that matches label to find
221
- const unit = await this.findECUnitByName(entry.name);
222
- const foundPhenomenon = await unit.phenomenon;
223
- const foundUnitSystem = await unit.unitSystem;
224
- if (!schemaName || unit.schema.name.toLowerCase() === schemaName)
225
- if (!phenomenon || (foundPhenomenon && foundPhenomenon.key.matchesFullName(phenomenon)))
226
- if (!unitSystem || (foundUnitSystem && foundUnitSystem.key.matchesFullName(unitSystem)))
227
- return unit;
228
- }
229
- }
230
- }
231
- throw new core_bentley_1.BentleyError(core_bentley_1.BentleyStatus.ERROR, "Cannot find unit with alternate display label", () => {
232
- return { altDisplayLabel };
233
- });
234
- }
235
- }
236
- exports.SchemaUnitProvider = SchemaUnitProvider;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SchemaUnitProvider = 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 Context_1 = require("../Context");
10
+ const SchemaItem_1 = require("../Metadata/SchemaItem");
11
+ const SchemaKey_1 = require("../SchemaKey");
12
+ const Unit_1 = require("../Metadata/Unit");
13
+ const ECObjects_1 = require("../ECObjects");
14
+ const UnitConverter_1 = require("../UnitConversion/UnitConverter");
15
+ /**
16
+ * Class used to find Units in SchemaContext by attributes such as Phenomenon and DisplayLabel.
17
+ * @alpha
18
+ */
19
+ class SchemaUnitProvider {
20
+ /**
21
+ *
22
+ * @param contextOrLocater The SchemaContext or a different ISchemaLocater implementation used to retrieve the schema. The SchemaContext
23
+ * class implements the ISchemaLocater interface. If the provided locater is not a SchemaContext instance a new SchemaContext will be
24
+ * created and the locater will be added.
25
+ * @param _unitExtraData Additional data like alternate display label not found in Units Schema to match with Units; Defaults to empty array.
26
+ */
27
+ constructor(contextOrLocater, _unitExtraData = []) {
28
+ this._unitExtraData = _unitExtraData;
29
+ if (contextOrLocater instanceof Context_1.SchemaContext) {
30
+ this._context = contextOrLocater;
31
+ }
32
+ else {
33
+ this._context = new Context_1.SchemaContext();
34
+ this._context.addLocater(contextOrLocater);
35
+ }
36
+ this._unitConverter = new UnitConverter_1.UnitConverter(this._context);
37
+ }
38
+ /**
39
+ * Find unit in a schema that has unitName.
40
+ * @param unitName Full name of unit.
41
+ * @returns UnitProps interface from @itwin/core-quantity whose name matches unitName.
42
+ */
43
+ async findUnitByName(unitName) {
44
+ const unit = await this.findECUnitByName(unitName);
45
+ return this.getUnitsProps(unit);
46
+ }
47
+ /**
48
+ * Find all units in context that belongs to given phenomenon.
49
+ * @param phenomenon Full name of phenomenon.
50
+ * @returns Array of UnitProps (from @itwin/core-quantity) interface objects whose name matches phenomenon param.
51
+ */
52
+ async getUnitsByFamily(phenomenon) {
53
+ // Check if schema exists and phenomenon exists in schema
54
+ const [schemaName, schemaItemName] = SchemaItem_1.SchemaItem.parseFullName(phenomenon);
55
+ const schemaKey = new SchemaKey_1.SchemaKey(schemaName);
56
+ const schema = await this._context.getSchema(schemaKey);
57
+ if (!schema) {
58
+ throw new core_bentley_1.BentleyError(core_bentley_1.BentleyStatus.ERROR, "Cannot find schema for phenomenon", () => {
59
+ return { phenomenon, schema: schemaName };
60
+ });
61
+ }
62
+ const itemKey = new SchemaKey_1.SchemaItemKey(schemaItemName, schema.schemaKey);
63
+ const phenom = await this._context.getSchemaItem(itemKey);
64
+ if (!phenom)
65
+ throw new core_bentley_1.BentleyError(core_bentley_1.BentleyStatus.ERROR, "Cannot find schema item/phenomenon", () => {
66
+ return { item: schemaItemName, schema: schemaName };
67
+ });
68
+ if (phenom.schemaItemType !== ECObjects_1.SchemaItemType.Phenomenon)
69
+ throw new core_bentley_1.BentleyError(core_bentley_1.BentleyStatus.ERROR, "Item is not a phenomenon", () => {
70
+ return { itemType: phenom.key.fullName };
71
+ });
72
+ // Find units' full name that match given phenomenon param.
73
+ const filteredUnits = [];
74
+ const schemaItems = this._context.getSchemaItems();
75
+ let { value, done } = schemaItems.next();
76
+ while (!done) {
77
+ if (Unit_1.Unit.isUnit(value)) {
78
+ const foundPhenomenon = await value.phenomenon;
79
+ if (foundPhenomenon && foundPhenomenon.key.matchesFullName(phenomenon)) {
80
+ const unitProps = this.getUnitsProps(value);
81
+ filteredUnits.push(unitProps);
82
+ }
83
+ }
84
+ ({ value, done } = schemaItems.next());
85
+ }
86
+ return filteredUnits;
87
+ }
88
+ /**
89
+ * Find alternate display labels associated with unitName, if any.
90
+ * @param unitName Full name of Unit.
91
+ */
92
+ getAlternateDisplayLabels(unitName) {
93
+ let alternateLabels = [];
94
+ for (const entry of this._unitExtraData) {
95
+ if (entry.name.toLowerCase() === unitName.toLowerCase()) {
96
+ alternateLabels = entry.altDisplayLabels;
97
+ }
98
+ }
99
+ return alternateLabels;
100
+ }
101
+ /**
102
+ * Finds Unit by unitLabel, which could be a display label in the schema or alternate an display label defined in
103
+ * this._unitExtraData. If there are duplicates of the same display label in the context or teh same alternate display
104
+ * labels, specify schemaName, phenomenon, or unitSystem to get a specific unit.
105
+ *
106
+ * @param unitLabel Display label or alternate display label to query unit by.
107
+ * @param schemaName Ensure Unit with unitLabel belongs to Schema with schemaName.
108
+ * @param phenomenon Full name of phenomenon that Unit belongs to.
109
+ * @param unitSystem Full name of unitSystem that Unit belongs to.
110
+ * @returns The UnitProps interface from the @itwin/core-quantity package.
111
+ */
112
+ async findUnit(unitLabel, schemaName, phenomenon, unitSystem) {
113
+ const findLabel = unitLabel.toLowerCase();
114
+ const findSchema = schemaName ? schemaName.toLowerCase() : undefined;
115
+ const findPhenomenon = phenomenon ? phenomenon.toLowerCase() : undefined;
116
+ const findUnitSystem = unitSystem ? unitSystem.toLowerCase() : undefined;
117
+ let foundUnit;
118
+ try {
119
+ try {
120
+ foundUnit = await this.findUnitByDisplayLabel(findLabel, findSchema, findPhenomenon, findUnitSystem);
121
+ }
122
+ catch (err) {
123
+ // If there is no Unit with display label that matches label, then check for alternate display labels that may match
124
+ foundUnit = await this.findUnitByAltDisplayLabel(findLabel, findSchema, findPhenomenon, findUnitSystem);
125
+ }
126
+ }
127
+ catch (err) {
128
+ throw new core_bentley_1.BentleyError(core_bentley_1.BentleyStatus.ERROR, "Cannot find unit with label", () => {
129
+ return { unitLabel };
130
+ });
131
+ }
132
+ return this.getUnitsProps(foundUnit);
133
+ }
134
+ /**
135
+ * Gets the @itwin/core-quantity UnitConversion for the given fromUnit and toUnit.
136
+ * @param fromUnit The UnitProps of the 'from' unit.
137
+ * @param toUnit The UnitProps of the 'to' unit.
138
+ * @returns The UnitConversion interface from the @itwin/core-quantity package.
139
+ */
140
+ async getConversion(fromUnit, toUnit) {
141
+ const conversion = await this._unitConverter.calculateConversion(fromUnit.name, toUnit.name);
142
+ return {
143
+ factor: conversion.factor,
144
+ offset: conversion.offset,
145
+ };
146
+ }
147
+ /**
148
+ * Find unit in a schema that has unitName.
149
+ * @param unitName Full name of unit.
150
+ * @returns Unit whose full name matches unitName.
151
+ */
152
+ async findECUnitByName(unitName) {
153
+ // Check if schema exists and unit exists in schema
154
+ const [schemaName, schemaItemName] = SchemaItem_1.SchemaItem.parseFullName(unitName);
155
+ const schemaKey = new SchemaKey_1.SchemaKey(schemaName);
156
+ const schema = await this._context.getSchema(schemaKey);
157
+ if (!schema) {
158
+ throw new core_bentley_1.BentleyError(core_bentley_1.BentleyStatus.ERROR, "Cannot find schema for unit", () => {
159
+ return { schema: schemaName, unit: unitName };
160
+ });
161
+ }
162
+ const itemKey = new SchemaKey_1.SchemaItemKey(schemaItemName, schema.schemaKey);
163
+ const item = await this._context.getSchemaItem(itemKey);
164
+ if (!item)
165
+ throw new core_bentley_1.BentleyError(core_bentley_1.BentleyStatus.ERROR, "Cannot find schema item/unit", () => {
166
+ return { item: schemaItemName, schema: schemaName };
167
+ });
168
+ if (item.schemaItemType === ECObjects_1.SchemaItemType.Unit)
169
+ return item;
170
+ throw new core_bentley_1.BentleyError(core_bentley_1.BentleyStatus.ERROR, "Item is not a unit", () => {
171
+ return { itemType: item.key.fullName };
172
+ });
173
+ }
174
+ /**
175
+ * Gets the @itwin/core UnitProps for the given Unit.
176
+ * @param unit The Unit to convert.
177
+ * @returns UnitProps interface from @itwin/core.
178
+ */
179
+ getUnitsProps(unit) {
180
+ var _a;
181
+ return {
182
+ name: unit.fullName,
183
+ label: (_a = unit.label) !== null && _a !== void 0 ? _a : "",
184
+ phenomenon: unit.phenomenon ? unit.phenomenon.fullName : "",
185
+ isValid: true,
186
+ system: unit.unitSystem === undefined ? "" : unit.unitSystem.fullName,
187
+ };
188
+ }
189
+ /**
190
+ * Finds Unit by displayLabel and that it belongs to schemaName, phenomenon, and unitSystem if defined.
191
+ * @internal
192
+ */
193
+ async findUnitByDisplayLabel(displayLabel, schemaName, phenomenon, unitSystem) {
194
+ var _a;
195
+ const schemaItems = this._context.getSchemaItems();
196
+ let { value, done } = schemaItems.next();
197
+ while (!done) {
198
+ if (Unit_1.Unit.isUnit(value) && ((_a = value.label) === null || _a === void 0 ? void 0 : _a.toLowerCase()) === displayLabel) {
199
+ const currPhenomenon = await value.phenomenon;
200
+ const currUnitSystem = await value.unitSystem;
201
+ if (!schemaName || value.schema.name.toLowerCase() === schemaName)
202
+ if (!phenomenon || (currPhenomenon && currPhenomenon.key.matchesFullName(phenomenon)))
203
+ if (!unitSystem || (currUnitSystem && currUnitSystem.key.matchesFullName(unitSystem)))
204
+ return value;
205
+ }
206
+ ({ value, done } = schemaItems.next());
207
+ }
208
+ throw new core_bentley_1.BentleyError(core_bentley_1.BentleyStatus.ERROR, "Cannot find unit with display label", () => {
209
+ return { displayLabel };
210
+ });
211
+ }
212
+ /**
213
+ * Finds Unit by altDisplayLabel and that it belongs to schemaName, phenomenon, and unitSystem if defined.
214
+ * @internal
215
+ */
216
+ async findUnitByAltDisplayLabel(altDisplayLabel, schemaName, phenomenon, unitSystem) {
217
+ for (const entry of this._unitExtraData) {
218
+ if (entry.altDisplayLabels && entry.altDisplayLabels.length > 0) {
219
+ if (entry.altDisplayLabels.findIndex((ref) => ref.toLowerCase() === altDisplayLabel) !== -1) {
220
+ // Found altDisplayLabel that matches label to find
221
+ const unit = await this.findECUnitByName(entry.name);
222
+ const foundPhenomenon = await unit.phenomenon;
223
+ const foundUnitSystem = await unit.unitSystem;
224
+ if (!schemaName || unit.schema.name.toLowerCase() === schemaName)
225
+ if (!phenomenon || (foundPhenomenon && foundPhenomenon.key.matchesFullName(phenomenon)))
226
+ if (!unitSystem || (foundUnitSystem && foundUnitSystem.key.matchesFullName(unitSystem)))
227
+ return unit;
228
+ }
229
+ }
230
+ }
231
+ throw new core_bentley_1.BentleyError(core_bentley_1.BentleyStatus.ERROR, "Cannot find unit with alternate display label", () => {
232
+ return { altDisplayLabel };
233
+ });
234
+ }
235
+ }
236
+ exports.SchemaUnitProvider = SchemaUnitProvider;
237
237
  //# sourceMappingURL=SchemaUnitProvider.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"SchemaUnitProvider.js","sourceRoot":"","sources":["../../../src/UnitProvider/SchemaUnitProvider.ts"],"names":[],"mappings":";;;AAAA;;;+FAG+F;AAC/F,sDAAkE;AAElE,wCAA2D;AAC3D,uDAAoD;AACpD,4CAAwD;AACxD,2CAAwC;AACxC,4CAA8C;AAC9C,mEAAgE;AAEhE;;;GAGG;AACH,MAAa,kBAAkB;IAI7B;;;;;;OAMG;IACH,YAAY,gBAAgC,EAAU,iBAAkC,EAAE;QAApC,mBAAc,GAAd,cAAc,CAAsB;QACxF,IAAI,gBAAgB,YAAY,uBAAa,EAAE;YAC7C,IAAI,CAAC,QAAQ,GAAG,gBAAgB,CAAC;SAClC;aAAM;YACL,IAAI,CAAC,QAAQ,GAAG,IAAI,uBAAa,EAAE,CAAC;YACpC,IAAI,CAAC,QAAQ,CAAC,UAAU,CAAC,gBAAgB,CAAC,CAAC;SAC5C;QACD,IAAI,CAAC,cAAc,GAAG,IAAI,6BAAa,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;IACzD,CAAC;IAED;;;;OAIG;IACI,KAAK,CAAC,cAAc,CAAC,QAAgB;QAC1C,MAAM,IAAI,GAAG,MAAM,IAAI,CAAC,gB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,CAAC;QAC1B,CAAC,CAAC,CAAC;IACL,CAAC;IAED;;;OAGG;IACK,KAAK,CAAC,yBAAyB,CAAC,eAAuB,EAAE,UAAmB,EAAE,UAAmB,EAAE,UAAmB;QAC5H,KAAK,MAAM,KAAK,IAAI,IAAI,CAAC,cAAc,EAAE;YACvC,IAAI,KAAK,CAAC,gBAAgB,IAAI,KAAK,CAAC,gBAAgB,CAAC,MAAM,GAAG,CAAC,EAAE;gBAC/D,IAAI,KAAK,CAAC,gBAAgB,CAAC,SAAS,CAAC,CAAC,GAAW,EAAE,EAAE,CAAC,GAAG,CAAC,WAAW,EAAE,KAAK,eAAe,CAAC,KAAK,CAAC,CAAC,EAAE;oBACnG,mDAAmD;oBACnD,MAAM,IAAI,GAAG,MAAM,IAAI,CAAC,gBAAgB,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;oBACrD,MAAM,eAAe,GAAG,MAAM,IAAI,CAAC,UAAU,CAAC;oBAC9C,MAAM,eAAe,GAAG,MAAM,IAAI,CAAC,UAAU,CAAC;oBAC9C,IAAI,CAAC,UAAU,IAAI,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,WAAW,EAAE,KAAK,UAAU;wBAC9D,IAAI,CAAC,UAAU,IAAI,CAAC,eAAe,IAAI,eAAe,CAAC,GAAG,CAAC,eAAe,CAAC,UAAU,CAAC,CAAC;4BACrF,IAAI,CAAC,UAAU,IAAI,CAAC,eAAe,IAAI,eAAe,CAAC,GAAG,CAAC,eAAe,CAAC,UAAU,CAAC,CAAC;gCACrF,OAAO,IAAI,CAAC;iBACnB;aACF;SACF;QAED,MAAM,IAAI,2BAAY,CAAC,4BAAa,CAAC,KAAK,EAAE,+CAA+C,EAAE,GAAG,EAAE;YAChG,OAAO,EAAE,eAAe,EAAE,CAAC;QAC7B,CAAC,CAAC,CAAC;IACL,CAAC;CACF;AA5OD,gDA4OC","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 { UnitConversion, UnitExtraData, UnitProps, UnitsProvider } from \"@itwin/core-quantity\";\nimport { ISchemaLocater, SchemaContext } from \"../Context\";\nimport { SchemaItem } from \"../Metadata/SchemaItem\";\nimport { SchemaItemKey, SchemaKey } from \"../SchemaKey\";\nimport { Unit } from \"../Metadata/Unit\";\nimport { SchemaItemType } from \"../ECObjects\";\nimport { UnitConverter } from \"../UnitConversion/UnitConverter\";\n\n/**\n * Class used to find Units in SchemaContext by attributes such as Phenomenon and DisplayLabel.\n * @alpha\n */\nexport class SchemaUnitProvider implements UnitsProvider {\n private _unitConverter: UnitConverter;\n private _context: SchemaContext;\n\n /**\n *\n * @param contextOrLocater The SchemaContext or a different ISchemaLocater implementation used to retrieve the schema. The SchemaContext\n * class implements the ISchemaLocater interface. If the provided locater is not a SchemaContext instance a new SchemaContext will be\n * created and the locater will be added.\n * @param _unitExtraData Additional data like alternate display label not found in Units Schema to match with Units; Defaults to empty array.\n */\n constructor(contextOrLocater: ISchemaLocater, private _unitExtraData: UnitExtraData[] = []){\n if (contextOrLocater instanceof SchemaContext) {\n this._context = contextOrLocater;\n } else {\n this._context = new SchemaContext();\n this._context.addLocater(contextOrLocater);\n }\n this._unitConverter = new UnitConverter(this._context);\n }\n\n /**\n * Find unit in a schema that has unitName.\n * @param unitName Full name of unit.\n * @returns UnitProps interface from @itwin/core-quantity whose name matches unitName.\n */\n public async findUnitByName(unitName: string): Promise<UnitProps> {\n const unit = await this.findECUnitByName(unitName);\n return this.getUnitsProps(unit);\n }\n\n /**\n * Find all units in context that belongs to given phenomenon.\n * @param phenomenon Full name of phenomenon.\n * @returns Array of UnitProps (from @itwin/core-quantity) interface objects whose name matches phenomenon param.\n */\n public async getUnitsByFamily(phenomenon: string): Promise<Array<UnitProps>> {\n // Check if schema exists and phenomenon exists in schema\n const [schemaName, schemaItemName] = SchemaItem.parseFullName(phenomenon);\n const schemaKey = new SchemaKey(schemaName);\n const schema = await this._context.getSchema(schemaKey);\n\n if (!schema) {\n throw new BentleyError(BentleyStatus.ERROR, \"Cannot find schema for phenomenon\", () => {\n return { phenomenon, schema: schemaName };\n });\n }\n\n const itemKey = new SchemaItemKey(schemaItemName, schema.schemaKey);\n const phenom = await this._context.getSchemaItem(itemKey);\n if (!phenom)\n throw new BentleyError(BentleyStatus.ERROR, \"Cannot find schema item/phenomenon\", () => {\n return { item: schemaItemName, schema: schemaName };\n });\n\n if (phenom.schemaItemType !== SchemaItemType.Phenomenon)\n throw new BentleyError(BentleyStatus.ERROR, \"Item is not a phenomenon\", () => {\n return { itemType: phenom.key.fullName };\n });\n\n // Find units' full name that match given phenomenon param.\n const filteredUnits: Array<UnitProps> = [];\n const schemaItems = this._context.getSchemaItems();\n let { value, done } = schemaItems.next();\n while (!done) {\n if (Unit.isUnit(value)) {\n const foundPhenomenon = await value.phenomenon;\n if (foundPhenomenon && foundPhenomenon.key.matchesFullName(phenomenon)) {\n const unitProps = this.getUnitsProps(value);\n filteredUnits.push(unitProps);\n }\n }\n ({ value, done } = schemaItems.next());\n }\n\n return filteredUnits;\n }\n\n /**\n * Find alternate display labels associated with unitName, if any.\n * @param unitName Full name of Unit.\n */\n public getAlternateDisplayLabels(unitName: string): Array<string> {\n let alternateLabels: Array<string> = [];\n for (const entry of this._unitExtraData) {\n if (entry.name.toLowerCase() === unitName.toLowerCase()) {\n alternateLabels = entry.altDisplayLabels;\n }\n }\n\n return alternateLabels;\n }\n\n /**\n * Finds Unit by unitLabel, which could be a display label in the schema or alternate an display label defined in\n * this._unitExtraData. If there are duplicates of the same display label in the context or teh same alternate display\n * labels, specify schemaName, phenomenon, or unitSystem to get a specific unit.\n *\n * @param unitLabel Display label or alternate display label to query unit by.\n * @param schemaName Ensure Unit with unitLabel belongs to Schema with schemaName.\n * @param phenomenon Full name of phenomenon that Unit belongs to.\n * @param unitSystem Full name of unitSystem that Unit belongs to.\n * @returns The UnitProps interface from the @itwin/core-quantity package.\n */\n public async findUnit(unitLabel: string, schemaName?: string, phenomenon?: string, unitSystem?: string): Promise<UnitProps> {\n const findLabel = unitLabel.toLowerCase();\n const findSchema = schemaName ? schemaName.toLowerCase() : undefined;\n const findPhenomenon = phenomenon ? phenomenon.toLowerCase() : undefined;\n const findUnitSystem = unitSystem ? unitSystem.toLowerCase() : undefined;\n let foundUnit: Unit | undefined;\n\n try {\n try {\n foundUnit = await this.findUnitByDisplayLabel(findLabel, findSchema, findPhenomenon, findUnitSystem);\n } catch (err) {\n // If there is no Unit with display label that matches label, then check for alternate display labels that may match\n foundUnit = await this.findUnitByAltDisplayLabel(findLabel, findSchema, findPhenomenon, findUnitSystem);\n }\n } catch (err) {\n throw new BentleyError(BentleyStatus.ERROR, \"Cannot find unit with label\", () => {\n return { unitLabel };\n });\n }\n\n return this.getUnitsProps(foundUnit);\n }\n\n /**\n * Gets the @itwin/core-quantity UnitConversion for the given fromUnit and toUnit.\n * @param fromUnit The UnitProps of the 'from' unit.\n * @param toUnit The UnitProps of the 'to' unit.\n * @returns The UnitConversion interface from the @itwin/core-quantity package.\n */\n public async getConversion(fromUnit: UnitProps, toUnit: UnitProps): Promise<UnitConversion> {\n const conversion = await this._unitConverter.calculateConversion(fromUnit.name, toUnit.name);\n return {\n factor: conversion.factor,\n offset: conversion.offset,\n };\n }\n\n /**\n * Find unit in a schema that has unitName.\n * @param unitName Full name of unit.\n * @returns Unit whose full name matches unitName.\n */\n private async findECUnitByName(unitName: string): Promise<Unit> {\n // Check if schema exists and unit exists in schema\n const [schemaName, schemaItemName] = SchemaItem.parseFullName(unitName);\n const schemaKey = new SchemaKey(schemaName);\n const schema = await this._context.getSchema(schemaKey);\n\n if (!schema) {\n throw new BentleyError(BentleyStatus.ERROR, \"Cannot find schema for unit\", () => {\n return { schema: schemaName, unit: unitName };\n });\n }\n\n const itemKey = new SchemaItemKey(schemaItemName, schema.schemaKey);\n const item = await this._context.getSchemaItem<Unit>(itemKey);\n if (!item)\n throw new BentleyError(BentleyStatus.ERROR, \"Cannot find schema item/unit\", () => {\n return { item: schemaItemName, schema: schemaName };\n });\n\n if (item.schemaItemType === SchemaItemType.Unit)\n return item;\n\n throw new BentleyError(BentleyStatus.ERROR, \"Item is not a unit\", () => {\n return { itemType: item.key.fullName };\n });\n }\n\n /**\n * Gets the @itwin/core UnitProps for the given Unit.\n * @param unit The Unit to convert.\n * @returns UnitProps interface from @itwin/core.\n */\n private getUnitsProps(unit: Unit): UnitProps {\n return {\n name: unit.fullName,\n label: unit.label ?? \"\",\n phenomenon: unit.phenomenon ? unit.phenomenon.fullName : \"\",\n isValid: true,\n system: unit.unitSystem === undefined ? \"\" : unit.unitSystem.fullName,\n };\n }\n\n /**\n * Finds Unit by displayLabel and that it belongs to schemaName, phenomenon, and unitSystem if defined.\n * @internal\n */\n private async findUnitByDisplayLabel(displayLabel: string, schemaName?: string, phenomenon?: string, unitSystem?: string): Promise<Unit> {\n const schemaItems = this._context.getSchemaItems();\n let { value, done } = schemaItems.next();\n while (!done) {\n if (Unit.isUnit(value) && value.label?.toLowerCase() === displayLabel) {\n const currPhenomenon = await value.phenomenon;\n const currUnitSystem = await value.unitSystem;\n if (!schemaName || value.schema.name.toLowerCase() === schemaName)\n if (!phenomenon || (currPhenomenon && currPhenomenon.key.matchesFullName(phenomenon)))\n if (!unitSystem || (currUnitSystem && currUnitSystem.key.matchesFullName(unitSystem)))\n return value;\n }\n ({ value, done } = schemaItems.next());\n }\n\n throw new BentleyError(BentleyStatus.ERROR, \"Cannot find unit with display label\", () => {\n return { displayLabel };\n });\n }\n\n /**\n * Finds Unit by altDisplayLabel and that it belongs to schemaName, phenomenon, and unitSystem if defined.\n * @internal\n */\n private async findUnitByAltDisplayLabel(altDisplayLabel: string, schemaName?: string, phenomenon?: string, unitSystem?: string): Promise<Unit> {\n for (const entry of this._unitExtraData) {\n if (entry.altDisplayLabels && entry.altDisplayLabels.length > 0) {\n if (entry.altDisplayLabels.findIndex((ref: string) => ref.toLowerCase() === altDisplayLabel) !== -1) {\n // Found altDisplayLabel that matches label to find\n const unit = await this.findECUnitByName(entry.name);\n const foundPhenomenon = await unit.phenomenon;\n const foundUnitSystem = await unit.unitSystem;\n if (!schemaName || unit.schema.name.toLowerCase() === schemaName)\n if (!phenomenon || (foundPhenomenon && foundPhenomenon.key.matchesFullName(phenomenon)))\n if (!unitSystem || (foundUnitSystem && foundUnitSystem.key.matchesFullName(unitSystem)))\n return unit;\n }\n }\n }\n\n throw new BentleyError(BentleyStatus.ERROR, \"Cannot find unit with alternate display label\", () => {\n return { altDisplayLabel };\n });\n }\n}\n"]}
1
+ {"version":3,"file":"SchemaUnitProvider.js","sourceRoot":"","sources":["../../../src/UnitProvider/SchemaUnitProvider.ts"],"names":[],"mappings":";;;AAAA;;;+FAG+F;AAC/F,sDAAkE;AAElE,wCAA2D;AAC3D,uDAAoD;AACpD,4CAAwD;AACxD,2CAAwC;AACxC,4CAA8C;AAC9C,mEAAgE;AAEhE;;;GAGG;AACH,MAAa,kBAAkB;IAI7B;;;;;;OAMG;IACH,YAAY,gBAAgC,EAAU,iBAAkC,EAAE;QAApC,mBAAc,GAAd,cAAc,CAAsB;QACxF,IAAI,gBAAgB,YAAY,uBAAa,EAAE;YAC7C,IAAI,CAAC,QAAQ,GAAG,gBAAgB,CAAC;SAClC;aAAM;YACL,IAAI,CAAC,QAAQ,GAAG,IAAI,uBAAa,EAAE,CAAC;YACpC,IAAI,CAAC,QAAQ,CAAC,UAAU,CAAC,gBAAgB,CAAC,CAAC;SAC5C;QACD,IAAI,CAAC,cAAc,GAAG,IAAI,6BAAa,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;IACzD,CAAC;IAED;;;;OAIG;IACI,KAAK,CAAC,cAAc,CAAC,QAAgB;QAC1C,MAAM,IAAI,GAAG,MAAM,IAAI,CAAC,gBAAgB,CAAC,QAAQ,CAAC,CAAC;QACnD,OAAO,IAAI,CAAC,aAA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K,KAAK,CAAC,yBAAyB,CAAC,eAAuB,EAAE,UAAmB,EAAE,UAAmB,EAAE,UAAmB;QAC5H,KAAK,MAAM,KAAK,IAAI,IAAI,CAAC,cAAc,EAAE;YACvC,IAAI,KAAK,CAAC,gBAAgB,IAAI,KAAK,CAAC,gBAAgB,CAAC,MAAM,GAAG,CAAC,EAAE;gBAC/D,IAAI,KAAK,CAAC,gBAAgB,CAAC,SAAS,CAAC,CAAC,GAAW,EAAE,EAAE,CAAC,GAAG,CAAC,WAAW,EAAE,KAAK,eAAe,CAAC,KAAK,CAAC,CAAC,EAAE;oBACnG,mDAAmD;oBACnD,MAAM,IAAI,GAAG,MAAM,IAAI,CAAC,gBAAgB,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;oBACrD,MAAM,eAAe,GAAG,MAAM,IAAI,CAAC,UAAU,CAAC;oBAC9C,MAAM,eAAe,GAAG,MAAM,IAAI,CAAC,UAAU,CAAC;oBAC9C,IAAI,CAAC,UAAU,IAAI,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,WAAW,EAAE,KAAK,UAAU;wBAC9D,IAAI,CAAC,UAAU,IAAI,CAAC,eAAe,IAAI,eAAe,CAAC,GAAG,CAAC,eAAe,CAAC,UAAU,CAAC,CAAC;4BACrF,IAAI,CAAC,UAAU,IAAI,CAAC,eAAe,IAAI,eAAe,CAAC,GAAG,CAAC,eAAe,CAAC,UAAU,CAAC,CAAC;gCACrF,OAAO,IAAI,CAAC;iBACnB;aACF;SACF;QAED,MAAM,IAAI,2BAAY,CAAC,4BAAa,CAAC,KAAK,EAAE,+CAA+C,EAAE,GAAG,EAAE;YAChG,OAAO,EAAE,eAAe,EAAE,CAAC;QAC7B,CAAC,CAAC,CAAC;IACL,CAAC;CACF;AA5OD,gDA4OC","sourcesContent":["/*---------------------------------------------------------------------------------------------\r\n* Copyright (c) Bentley Systems, Incorporated. All rights reserved.\r\n* See LICENSE.md in the project root for license terms and full copyright notice.\r\n*--------------------------------------------------------------------------------------------*/\r\nimport { BentleyError, BentleyStatus } from \"@itwin/core-bentley\";\r\nimport { UnitConversion, UnitExtraData, UnitProps, UnitsProvider } from \"@itwin/core-quantity\";\r\nimport { ISchemaLocater, SchemaContext } from \"../Context\";\r\nimport { SchemaItem } from \"../Metadata/SchemaItem\";\r\nimport { SchemaItemKey, SchemaKey } from \"../SchemaKey\";\r\nimport { Unit } from \"../Metadata/Unit\";\r\nimport { SchemaItemType } from \"../ECObjects\";\r\nimport { UnitConverter } from \"../UnitConversion/UnitConverter\";\r\n\r\n/**\r\n * Class used to find Units in SchemaContext by attributes such as Phenomenon and DisplayLabel.\r\n * @alpha\r\n */\r\nexport class SchemaUnitProvider implements UnitsProvider {\r\n private _unitConverter: UnitConverter;\r\n private _context: SchemaContext;\r\n\r\n /**\r\n *\r\n * @param contextOrLocater The SchemaContext or a different ISchemaLocater implementation used to retrieve the schema. The SchemaContext\r\n * class implements the ISchemaLocater interface. If the provided locater is not a SchemaContext instance a new SchemaContext will be\r\n * created and the locater will be added.\r\n * @param _unitExtraData Additional data like alternate display label not found in Units Schema to match with Units; Defaults to empty array.\r\n */\r\n constructor(contextOrLocater: ISchemaLocater, private _unitExtraData: UnitExtraData[] = []){\r\n if (contextOrLocater instanceof SchemaContext) {\r\n this._context = contextOrLocater;\r\n } else {\r\n this._context = new SchemaContext();\r\n this._context.addLocater(contextOrLocater);\r\n }\r\n this._unitConverter = new UnitConverter(this._context);\r\n }\r\n\r\n /**\r\n * Find unit in a schema that has unitName.\r\n * @param unitName Full name of unit.\r\n * @returns UnitProps interface from @itwin/core-quantity whose name matches unitName.\r\n */\r\n public async findUnitByName(unitName: string): Promise<UnitProps> {\r\n const unit = await this.findECUnitByName(unitName);\r\n return this.getUnitsProps(unit);\r\n }\r\n\r\n /**\r\n * Find all units in context that belongs to given phenomenon.\r\n * @param phenomenon Full name of phenomenon.\r\n * @returns Array of UnitProps (from @itwin/core-quantity) interface objects whose name matches phenomenon param.\r\n */\r\n public async getUnitsByFamily(phenomenon: string): Promise<Array<UnitProps>> {\r\n // Check if schema exists and phenomenon exists in schema\r\n const [schemaName, schemaItemName] = SchemaItem.parseFullName(phenomenon);\r\n const schemaKey = new SchemaKey(schemaName);\r\n const schema = await this._context.getSchema(schemaKey);\r\n\r\n if (!schema) {\r\n throw new BentleyError(BentleyStatus.ERROR, \"Cannot find schema for phenomenon\", () => {\r\n return { phenomenon, schema: schemaName };\r\n });\r\n }\r\n\r\n const itemKey = new SchemaItemKey(schemaItemName, schema.schemaKey);\r\n const phenom = await this._context.getSchemaItem(itemKey);\r\n if (!phenom)\r\n throw new BentleyError(BentleyStatus.ERROR, \"Cannot find schema item/phenomenon\", () => {\r\n return { item: schemaItemName, schema: schemaName };\r\n });\r\n\r\n if (phenom.schemaItemType !== SchemaItemType.Phenomenon)\r\n throw new BentleyError(BentleyStatus.ERROR, \"Item is not a phenomenon\", () => {\r\n return { itemType: phenom.key.fullName };\r\n });\r\n\r\n // Find units' full name that match given phenomenon param.\r\n const filteredUnits: Array<UnitProps> = [];\r\n const schemaItems = this._context.getSchemaItems();\r\n let { value, done } = schemaItems.next();\r\n while (!done) {\r\n if (Unit.isUnit(value)) {\r\n const foundPhenomenon = await value.phenomenon;\r\n if (foundPhenomenon && foundPhenomenon.key.matchesFullName(phenomenon)) {\r\n const unitProps = this.getUnitsProps(value);\r\n filteredUnits.push(unitProps);\r\n }\r\n }\r\n ({ value, done } = schemaItems.next());\r\n }\r\n\r\n return filteredUnits;\r\n }\r\n\r\n /**\r\n * Find alternate display labels associated with unitName, if any.\r\n * @param unitName Full name of Unit.\r\n */\r\n public getAlternateDisplayLabels(unitName: string): Array<string> {\r\n let alternateLabels: Array<string> = [];\r\n for (const entry of this._unitExtraData) {\r\n if (entry.name.toLowerCase() === unitName.toLowerCase()) {\r\n alternateLabels = entry.altDisplayLabels;\r\n }\r\n }\r\n\r\n return alternateLabels;\r\n }\r\n\r\n /**\r\n * Finds Unit by unitLabel, which could be a display label in the schema or alternate an display label defined in\r\n * this._unitExtraData. If there are duplicates of the same display label in the context or teh same alternate display\r\n * labels, specify schemaName, phenomenon, or unitSystem to get a specific unit.\r\n *\r\n * @param unitLabel Display label or alternate display label to query unit by.\r\n * @param schemaName Ensure Unit with unitLabel belongs to Schema with schemaName.\r\n * @param phenomenon Full name of phenomenon that Unit belongs to.\r\n * @param unitSystem Full name of unitSystem that Unit belongs to.\r\n * @returns The UnitProps interface from the @itwin/core-quantity package.\r\n */\r\n public async findUnit(unitLabel: string, schemaName?: string, phenomenon?: string, unitSystem?: string): Promise<UnitProps> {\r\n const findLabel = unitLabel.toLowerCase();\r\n const findSchema = schemaName ? schemaName.toLowerCase() : undefined;\r\n const findPhenomenon = phenomenon ? phenomenon.toLowerCase() : undefined;\r\n const findUnitSystem = unitSystem ? unitSystem.toLowerCase() : undefined;\r\n let foundUnit: Unit | undefined;\r\n\r\n try {\r\n try {\r\n foundUnit = await this.findUnitByDisplayLabel(findLabel, findSchema, findPhenomenon, findUnitSystem);\r\n } catch (err) {\r\n // If there is no Unit with display label that matches label, then check for alternate display labels that may match\r\n foundUnit = await this.findUnitByAltDisplayLabel(findLabel, findSchema, findPhenomenon, findUnitSystem);\r\n }\r\n } catch (err) {\r\n throw new BentleyError(BentleyStatus.ERROR, \"Cannot find unit with label\", () => {\r\n return { unitLabel };\r\n });\r\n }\r\n\r\n return this.getUnitsProps(foundUnit);\r\n }\r\n\r\n /**\r\n * Gets the @itwin/core-quantity UnitConversion for the given fromUnit and toUnit.\r\n * @param fromUnit The UnitProps of the 'from' unit.\r\n * @param toUnit The UnitProps of the 'to' unit.\r\n * @returns The UnitConversion interface from the @itwin/core-quantity package.\r\n */\r\n public async getConversion(fromUnit: UnitProps, toUnit: UnitProps): Promise<UnitConversion> {\r\n const conversion = await this._unitConverter.calculateConversion(fromUnit.name, toUnit.name);\r\n return {\r\n factor: conversion.factor,\r\n offset: conversion.offset,\r\n };\r\n }\r\n\r\n /**\r\n * Find unit in a schema that has unitName.\r\n * @param unitName Full name of unit.\r\n * @returns Unit whose full name matches unitName.\r\n */\r\n private async findECUnitByName(unitName: string): Promise<Unit> {\r\n // Check if schema exists and unit exists in schema\r\n const [schemaName, schemaItemName] = SchemaItem.parseFullName(unitName);\r\n const schemaKey = new SchemaKey(schemaName);\r\n const schema = await this._context.getSchema(schemaKey);\r\n\r\n if (!schema) {\r\n throw new BentleyError(BentleyStatus.ERROR, \"Cannot find schema for unit\", () => {\r\n return { schema: schemaName, unit: unitName };\r\n });\r\n }\r\n\r\n const itemKey = new SchemaItemKey(schemaItemName, schema.schemaKey);\r\n const item = await this._context.getSchemaItem<Unit>(itemKey);\r\n if (!item)\r\n throw new BentleyError(BentleyStatus.ERROR, \"Cannot find schema item/unit\", () => {\r\n return { item: schemaItemName, schema: schemaName };\r\n });\r\n\r\n if (item.schemaItemType === SchemaItemType.Unit)\r\n return item;\r\n\r\n throw new BentleyError(BentleyStatus.ERROR, \"Item is not a unit\", () => {\r\n return { itemType: item.key.fullName };\r\n });\r\n }\r\n\r\n /**\r\n * Gets the @itwin/core UnitProps for the given Unit.\r\n * @param unit The Unit to convert.\r\n * @returns UnitProps interface from @itwin/core.\r\n */\r\n private getUnitsProps(unit: Unit): UnitProps {\r\n return {\r\n name: unit.fullName,\r\n label: unit.label ?? \"\",\r\n phenomenon: unit.phenomenon ? unit.phenomenon.fullName : \"\",\r\n isValid: true,\r\n system: unit.unitSystem === undefined ? \"\" : unit.unitSystem.fullName,\r\n };\r\n }\r\n\r\n /**\r\n * Finds Unit by displayLabel and that it belongs to schemaName, phenomenon, and unitSystem if defined.\r\n * @internal\r\n */\r\n private async findUnitByDisplayLabel(displayLabel: string, schemaName?: string, phenomenon?: string, unitSystem?: string): Promise<Unit> {\r\n const schemaItems = this._context.getSchemaItems();\r\n let { value, done } = schemaItems.next();\r\n while (!done) {\r\n if (Unit.isUnit(value) && value.label?.toLowerCase() === displayLabel) {\r\n const currPhenomenon = await value.phenomenon;\r\n const currUnitSystem = await value.unitSystem;\r\n if (!schemaName || value.schema.name.toLowerCase() === schemaName)\r\n if (!phenomenon || (currPhenomenon && currPhenomenon.key.matchesFullName(phenomenon)))\r\n if (!unitSystem || (currUnitSystem && currUnitSystem.key.matchesFullName(unitSystem)))\r\n return value;\r\n }\r\n ({ value, done } = schemaItems.next());\r\n }\r\n\r\n throw new BentleyError(BentleyStatus.ERROR, \"Cannot find unit with display label\", () => {\r\n return { displayLabel };\r\n });\r\n }\r\n\r\n /**\r\n * Finds Unit by altDisplayLabel and that it belongs to schemaName, phenomenon, and unitSystem if defined.\r\n * @internal\r\n */\r\n private async findUnitByAltDisplayLabel(altDisplayLabel: string, schemaName?: string, phenomenon?: string, unitSystem?: string): Promise<Unit> {\r\n for (const entry of this._unitExtraData) {\r\n if (entry.altDisplayLabels && entry.altDisplayLabels.length > 0) {\r\n if (entry.altDisplayLabels.findIndex((ref: string) => ref.toLowerCase() === altDisplayLabel) !== -1) {\r\n // Found altDisplayLabel that matches label to find\r\n const unit = await this.findECUnitByName(entry.name);\r\n const foundPhenomenon = await unit.phenomenon;\r\n const foundUnitSystem = await unit.unitSystem;\r\n if (!schemaName || unit.schema.name.toLowerCase() === schemaName)\r\n if (!phenomenon || (foundPhenomenon && foundPhenomenon.key.matchesFullName(phenomenon)))\r\n if (!unitSystem || (foundUnitSystem && foundUnitSystem.key.matchesFullName(unitSystem)))\r\n return unit;\r\n }\r\n }\r\n }\r\n\r\n throw new BentleyError(BentleyStatus.ERROR, \"Cannot find unit with alternate display label\", () => {\r\n return { altDisplayLabel };\r\n });\r\n }\r\n}\r\n"]}
@@ -1,24 +1,24 @@
1
- import { Schema } from "../Metadata/Schema";
2
- import { ISchemaPartVisitor } from "../SchemaPartVisitorDelegate";
3
- /**
4
- * The purpose of this class is to traverse a given schema, allowing clients to hook into
5
- * the traversal process via Visitors to allow for custom processing of the schema elements.
6
- * @internal
7
- */
8
- export declare class SchemaWalker {
9
- private _visitorHelper;
10
- private _schema?;
11
- /**
12
- * Initializes a new SchemaWalker instance.
13
- * @param visitor An ISchemaWalkerVisitor implementation whose methods will be called during schema traversal.
14
- */
15
- constructor(visitor: ISchemaPartVisitor);
16
- /**
17
- * Traverses the given Schema, calling ISchemaWalkerVisitor methods along the way.
18
- * @param schema The Schema to traverse.
19
- */
20
- traverseSchema<T extends Schema>(schema: T): Promise<T>;
21
- private traverseSchemaItem;
22
- private traverseClass;
23
- }
1
+ import { Schema } from "../Metadata/Schema";
2
+ import { ISchemaPartVisitor } from "../SchemaPartVisitorDelegate";
3
+ /**
4
+ * The purpose of this class is to traverse a given schema, allowing clients to hook into
5
+ * the traversal process via Visitors to allow for custom processing of the schema elements.
6
+ * @internal
7
+ */
8
+ export declare class SchemaWalker {
9
+ private _visitorHelper;
10
+ private _schema?;
11
+ /**
12
+ * Initializes a new SchemaWalker instance.
13
+ * @param visitor An ISchemaWalkerVisitor implementation whose methods will be called during schema traversal.
14
+ */
15
+ constructor(visitor: ISchemaPartVisitor);
16
+ /**
17
+ * Traverses the given Schema, calling ISchemaWalkerVisitor methods along the way.
18
+ * @param schema The Schema to traverse.
19
+ */
20
+ traverseSchema<T extends Schema>(schema: T): Promise<T>;
21
+ private traverseSchemaItem;
22
+ private traverseClass;
23
+ }
24
24
  //# sourceMappingURL=SchemaWalker.d.ts.map