@itwin/ecschema-metadata 4.0.0-dev.52 → 4.0.0-dev.55

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 (117) 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.d.ts.map +1 -1
  8. package/lib/cjs/DelayedPromise.js +91 -91
  9. package/lib/cjs/DelayedPromise.js.map +1 -1
  10. package/lib/cjs/Deserialization/AbstractParser.d.ts +45 -45
  11. package/lib/cjs/Deserialization/AbstractParser.d.ts.map +1 -1
  12. package/lib/cjs/Deserialization/AbstractParser.js +11 -11
  13. package/lib/cjs/Deserialization/Helper.d.ts +251 -251
  14. package/lib/cjs/Deserialization/Helper.js +843 -843
  15. package/lib/cjs/Deserialization/JsonParser.d.ts +165 -165
  16. package/lib/cjs/Deserialization/JsonParser.js +680 -680
  17. package/lib/cjs/Deserialization/JsonProps.d.ts +282 -282
  18. package/lib/cjs/Deserialization/JsonProps.d.ts.map +1 -1
  19. package/lib/cjs/Deserialization/JsonProps.js +9 -9
  20. package/lib/cjs/Deserialization/SchemaGraphUtil.d.ts +34 -34
  21. package/lib/cjs/Deserialization/SchemaGraphUtil.js +77 -77
  22. package/lib/cjs/Deserialization/XmlParser.d.ts +85 -85
  23. package/lib/cjs/Deserialization/XmlParser.js +973 -973
  24. package/lib/cjs/Deserialization/XmlSerializationUtils.d.ts +54 -54
  25. package/lib/cjs/Deserialization/XmlSerializationUtils.js +174 -174
  26. package/lib/cjs/ECName.d.ts +30 -30
  27. package/lib/cjs/ECName.js +85 -85
  28. package/lib/cjs/ECObjects.d.ts +162 -162
  29. package/lib/cjs/ECObjects.js +453 -453
  30. package/lib/cjs/Exception.d.ts +43 -43
  31. package/lib/cjs/Exception.js +84 -84
  32. package/lib/cjs/Interfaces.d.ts +66 -66
  33. package/lib/cjs/Interfaces.d.ts.map +1 -1
  34. package/lib/cjs/Interfaces.js +9 -9
  35. package/lib/cjs/Metadata/Class.d.ts +243 -243
  36. package/lib/cjs/Metadata/Class.js +558 -558
  37. package/lib/cjs/Metadata/Class.js.map +1 -1
  38. package/lib/cjs/Metadata/Constant.d.ts +64 -64
  39. package/lib/cjs/Metadata/Constant.js +123 -123
  40. package/lib/cjs/Metadata/CustomAttribute.d.ts +23 -23
  41. package/lib/cjs/Metadata/CustomAttribute.js +27 -27
  42. package/lib/cjs/Metadata/CustomAttributeClass.d.ts +39 -39
  43. package/lib/cjs/Metadata/CustomAttributeClass.js +68 -68
  44. package/lib/cjs/Metadata/CustomAttributeClass.js.map +1 -1
  45. package/lib/cjs/Metadata/EntityClass.d.ts +77 -77
  46. package/lib/cjs/Metadata/EntityClass.js +230 -230
  47. package/lib/cjs/Metadata/Enumeration.d.ts +86 -86
  48. package/lib/cjs/Metadata/Enumeration.d.ts.map +1 -1
  49. package/lib/cjs/Metadata/Enumeration.js +159 -159
  50. package/lib/cjs/Metadata/Enumeration.js.map +1 -1
  51. package/lib/cjs/Metadata/Format.d.ts +98 -98
  52. package/lib/cjs/Metadata/Format.js +242 -242
  53. package/lib/cjs/Metadata/InvertedUnit.d.ts +49 -49
  54. package/lib/cjs/Metadata/InvertedUnit.js +97 -97
  55. package/lib/cjs/Metadata/KindOfQuantity.d.ts +65 -65
  56. package/lib/cjs/Metadata/KindOfQuantity.js +225 -225
  57. package/lib/cjs/Metadata/KindOfQuantity.js.map +1 -1
  58. package/lib/cjs/Metadata/Mixin.d.ts +54 -54
  59. package/lib/cjs/Metadata/Mixin.js +113 -113
  60. package/lib/cjs/Metadata/Mixin.js.map +1 -1
  61. package/lib/cjs/Metadata/OverrideFormat.d.ts +55 -55
  62. package/lib/cjs/Metadata/OverrideFormat.js +88 -88
  63. package/lib/cjs/Metadata/Phenomenon.d.ts +33 -33
  64. package/lib/cjs/Metadata/Phenomenon.js +59 -59
  65. package/lib/cjs/Metadata/Property.d.ts +197 -197
  66. package/lib/cjs/Metadata/Property.d.ts.map +1 -1
  67. package/lib/cjs/Metadata/Property.js +491 -491
  68. package/lib/cjs/Metadata/Property.js.map +1 -1
  69. package/lib/cjs/Metadata/PropertyCategory.d.ts +39 -39
  70. package/lib/cjs/Metadata/PropertyCategory.js +61 -61
  71. package/lib/cjs/Metadata/PropertyCategory.js.map +1 -1
  72. package/lib/cjs/Metadata/RelationshipClass.d.ts +152 -152
  73. package/lib/cjs/Metadata/RelationshipClass.js +349 -349
  74. package/lib/cjs/Metadata/RelationshipClass.js.map +1 -1
  75. package/lib/cjs/Metadata/Schema.d.ts +291 -291
  76. package/lib/cjs/Metadata/Schema.d.ts.map +1 -1
  77. package/lib/cjs/Metadata/Schema.js +520 -520
  78. package/lib/cjs/Metadata/Schema.js.map +1 -1
  79. package/lib/cjs/Metadata/SchemaItem.d.ts +61 -61
  80. package/lib/cjs/Metadata/SchemaItem.js +136 -136
  81. package/lib/cjs/Metadata/Unit.d.ts +76 -76
  82. package/lib/cjs/Metadata/Unit.js +162 -162
  83. package/lib/cjs/Metadata/UnitSystem.d.ts +20 -20
  84. package/lib/cjs/Metadata/UnitSystem.js +29 -29
  85. package/lib/cjs/PropertyTypes.d.ts +49 -49
  86. package/lib/cjs/PropertyTypes.js +91 -91
  87. package/lib/cjs/PropertyTypes.js.map +1 -1
  88. package/lib/cjs/SchemaJsonLocater.d.ts +34 -34
  89. package/lib/cjs/SchemaJsonLocater.d.ts.map +1 -1
  90. package/lib/cjs/SchemaJsonLocater.js +42 -42
  91. package/lib/cjs/SchemaKey.d.ts +110 -110
  92. package/lib/cjs/SchemaKey.js +219 -219
  93. package/lib/cjs/SchemaLoader.d.ts +23 -23
  94. package/lib/cjs/SchemaLoader.js +47 -47
  95. package/lib/cjs/SchemaPartVisitorDelegate.d.ts +274 -274
  96. package/lib/cjs/SchemaPartVisitorDelegate.js +212 -212
  97. package/lib/cjs/UnitConversion/Graph.d.ts +34 -34
  98. package/lib/cjs/UnitConversion/Graph.js +83 -83
  99. package/lib/cjs/UnitConversion/Parser.d.ts +8 -8
  100. package/lib/cjs/UnitConversion/Parser.js +42 -42
  101. package/lib/cjs/UnitConversion/UnitConversion.d.ts +45 -45
  102. package/lib/cjs/UnitConversion/UnitConversion.js +77 -77
  103. package/lib/cjs/UnitConversion/UnitConversion.js.map +1 -1
  104. package/lib/cjs/UnitConversion/UnitConverter.d.ts +39 -39
  105. package/lib/cjs/UnitConversion/UnitConverter.js +116 -116
  106. package/lib/cjs/UnitConversion/UnitTree.d.ts +43 -43
  107. package/lib/cjs/UnitConversion/UnitTree.js +169 -169
  108. package/lib/cjs/UnitProvider/SchemaUnitProvider.d.ts +77 -77
  109. package/lib/cjs/UnitProvider/SchemaUnitProvider.js +234 -234
  110. package/lib/cjs/Validation/SchemaWalker.d.ts +23 -23
  111. package/lib/cjs/Validation/SchemaWalker.js +53 -53
  112. package/lib/cjs/ecschema-metadata.d.ts +51 -51
  113. package/lib/cjs/ecschema-metadata.js +100 -96
  114. package/lib/cjs/ecschema-metadata.js.map +1 -1
  115. package/lib/cjs/utils/SchemaGraph.d.ts +25 -25
  116. package/lib/cjs/utils/SchemaGraph.js +65 -65
  117. package/package.json +8 -8
@@ -1,235 +1,235 @@
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
- return {
181
- name: unit.fullName,
182
- label: unit.label ?? "",
183
- phenomenon: unit.phenomenon ? unit.phenomenon.fullName : "",
184
- isValid: true,
185
- system: unit.unitSystem === undefined ? "" : unit.unitSystem.fullName,
186
- };
187
- }
188
- /**
189
- * Finds Unit by displayLabel and that it belongs to schemaName, phenomenon, and unitSystem if defined.
190
- * @internal
191
- */
192
- async findUnitByDisplayLabel(displayLabel, schemaName, phenomenon, unitSystem) {
193
- const schemaItems = this._context.getSchemaItems();
194
- let { value, done } = schemaItems.next();
195
- while (!done) {
196
- if (Unit_1.Unit.isUnit(value) && value.label?.toLowerCase() === displayLabel) {
197
- const currPhenomenon = await value.phenomenon;
198
- const currUnitSystem = await value.unitSystem;
199
- if (!schemaName || value.schema.name.toLowerCase() === schemaName)
200
- if (!phenomenon || (currPhenomenon && currPhenomenon.key.matchesFullName(phenomenon)))
201
- if (!unitSystem || (currUnitSystem && currUnitSystem.key.matchesFullName(unitSystem)))
202
- return value;
203
- }
204
- ({ value, done } = schemaItems.next());
205
- }
206
- throw new core_bentley_1.BentleyError(core_bentley_1.BentleyStatus.ERROR, "Cannot find unit with display label", () => {
207
- return { displayLabel };
208
- });
209
- }
210
- /**
211
- * Finds Unit by altDisplayLabel and that it belongs to schemaName, phenomenon, and unitSystem if defined.
212
- * @internal
213
- */
214
- async findUnitByAltDisplayLabel(altDisplayLabel, schemaName, phenomenon, unitSystem) {
215
- for (const entry of this._unitExtraData) {
216
- if (entry.altDisplayLabels && entry.altDisplayLabels.length > 0) {
217
- if (entry.altDisplayLabels.findIndex((ref) => ref.toLowerCase() === altDisplayLabel) !== -1) {
218
- // Found altDisplayLabel that matches label to find
219
- const unit = await this.findECUnitByName(entry.name);
220
- const foundPhenomenon = await unit.phenomenon;
221
- const foundUnitSystem = await unit.unitSystem;
222
- if (!schemaName || unit.schema.name.toLowerCase() === schemaName)
223
- if (!phenomenon || (foundPhenomenon && foundPhenomenon.key.matchesFullName(phenomenon)))
224
- if (!unitSystem || (foundUnitSystem && foundUnitSystem.key.matchesFullName(unitSystem)))
225
- return unit;
226
- }
227
- }
228
- }
229
- throw new core_bentley_1.BentleyError(core_bentley_1.BentleyStatus.ERROR, "Cannot find unit with alternate display label", () => {
230
- return { altDisplayLabel };
231
- });
232
- }
233
- }
234
- 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
+ return {
181
+ name: unit.fullName,
182
+ label: unit.label ?? "",
183
+ phenomenon: unit.phenomenon ? unit.phenomenon.fullName : "",
184
+ isValid: true,
185
+ system: unit.unitSystem === undefined ? "" : unit.unitSystem.fullName,
186
+ };
187
+ }
188
+ /**
189
+ * Finds Unit by displayLabel and that it belongs to schemaName, phenomenon, and unitSystem if defined.
190
+ * @internal
191
+ */
192
+ async findUnitByDisplayLabel(displayLabel, schemaName, phenomenon, unitSystem) {
193
+ const schemaItems = this._context.getSchemaItems();
194
+ let { value, done } = schemaItems.next();
195
+ while (!done) {
196
+ if (Unit_1.Unit.isUnit(value) && value.label?.toLowerCase() === displayLabel) {
197
+ const currPhenomenon = await value.phenomenon;
198
+ const currUnitSystem = await value.unitSystem;
199
+ if (!schemaName || value.schema.name.toLowerCase() === schemaName)
200
+ if (!phenomenon || (currPhenomenon && currPhenomenon.key.matchesFullName(phenomenon)))
201
+ if (!unitSystem || (currUnitSystem && currUnitSystem.key.matchesFullName(unitSystem)))
202
+ return value;
203
+ }
204
+ ({ value, done } = schemaItems.next());
205
+ }
206
+ throw new core_bentley_1.BentleyError(core_bentley_1.BentleyStatus.ERROR, "Cannot find unit with display label", () => {
207
+ return { displayLabel };
208
+ });
209
+ }
210
+ /**
211
+ * Finds Unit by altDisplayLabel and that it belongs to schemaName, phenomenon, and unitSystem if defined.
212
+ * @internal
213
+ */
214
+ async findUnitByAltDisplayLabel(altDisplayLabel, schemaName, phenomenon, unitSystem) {
215
+ for (const entry of this._unitExtraData) {
216
+ if (entry.altDisplayLabels && entry.altDisplayLabels.length > 0) {
217
+ if (entry.altDisplayLabels.findIndex((ref) => ref.toLowerCase() === altDisplayLabel) !== -1) {
218
+ // Found altDisplayLabel that matches label to find
219
+ const unit = await this.findECUnitByName(entry.name);
220
+ const foundPhenomenon = await unit.phenomenon;
221
+ const foundUnitSystem = await unit.unitSystem;
222
+ if (!schemaName || unit.schema.name.toLowerCase() === schemaName)
223
+ if (!phenomenon || (foundPhenomenon && foundPhenomenon.key.matchesFullName(phenomenon)))
224
+ if (!unitSystem || (foundUnitSystem && foundUnitSystem.key.matchesFullName(unitSystem)))
225
+ return unit;
226
+ }
227
+ }
228
+ }
229
+ throw new core_bentley_1.BentleyError(core_bentley_1.BentleyStatus.ERROR, "Cannot find unit with alternate display label", () => {
230
+ return { altDisplayLabel };
231
+ });
232
+ }
233
+ }
234
+ exports.SchemaUnitProvider = SchemaUnitProvider;
235
235
  //# sourceMappingURL=SchemaUnitProvider.js.map
@@ -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
@@ -1,54 +1,54 @@
1
- "use strict";
2
- /*---------------------------------------------------------------------------------------------
3
- * Copyright (c) Bentley Systems, Incorporated. All rights reserved.
4
- * See LICENSE.md in the project root for license terms and full copyright notice.
5
- *--------------------------------------------------------------------------------------------*/
6
- Object.defineProperty(exports, "__esModule", { value: true });
7
- exports.SchemaWalker = void 0;
8
- const ECObjects_1 = require("../ECObjects");
9
- const Class_1 = require("../Metadata/Class");
10
- const SchemaPartVisitorDelegate_1 = require("../SchemaPartVisitorDelegate");
11
- /**
12
- * The purpose of this class is to traverse a given schema, allowing clients to hook into
13
- * the traversal process via Visitors to allow for custom processing of the schema elements.
14
- * @internal
15
- */
16
- class SchemaWalker {
17
- /**
18
- * Initializes a new SchemaWalker instance.
19
- * @param visitor An ISchemaWalkerVisitor implementation whose methods will be called during schema traversal.
20
- */
21
- constructor(visitor) {
22
- this._visitorHelper = new SchemaPartVisitorDelegate_1.SchemaPartVisitorDelegate(visitor);
23
- }
24
- /**
25
- * Traverses the given Schema, calling ISchemaWalkerVisitor methods along the way.
26
- * @param schema The Schema to traverse.
27
- */
28
- async traverseSchema(schema) {
29
- this._schema = schema;
30
- await this._visitorHelper.visitSchema(schema);
31
- await this._visitorHelper.visitSchemaPart(schema);
32
- for (const item of this._schema.getItems())
33
- await this.traverseSchemaItem(item);
34
- return schema;
35
- }
36
- async traverseSchemaItem(schemaItem) {
37
- await this._visitorHelper.visitSchemaPart(schemaItem);
38
- if (Class_1.ECClass.isECClass(schemaItem))
39
- await this.traverseClass(schemaItem);
40
- }
41
- async traverseClass(ecClass) {
42
- if (ecClass.properties) {
43
- for (const property of ecClass.properties) {
44
- await this._visitorHelper.visitSchemaPart(property);
45
- }
46
- }
47
- if (ecClass.schemaItemType === ECObjects_1.SchemaItemType.RelationshipClass) {
48
- await this._visitorHelper.visitSchemaPart(ecClass.source);
49
- await this._visitorHelper.visitSchemaPart(ecClass.target);
50
- }
51
- }
52
- }
53
- exports.SchemaWalker = SchemaWalker;
1
+ "use strict";
2
+ /*---------------------------------------------------------------------------------------------
3
+ * Copyright (c) Bentley Systems, Incorporated. All rights reserved.
4
+ * See LICENSE.md in the project root for license terms and full copyright notice.
5
+ *--------------------------------------------------------------------------------------------*/
6
+ Object.defineProperty(exports, "__esModule", { value: true });
7
+ exports.SchemaWalker = void 0;
8
+ const ECObjects_1 = require("../ECObjects");
9
+ const Class_1 = require("../Metadata/Class");
10
+ const SchemaPartVisitorDelegate_1 = require("../SchemaPartVisitorDelegate");
11
+ /**
12
+ * The purpose of this class is to traverse a given schema, allowing clients to hook into
13
+ * the traversal process via Visitors to allow for custom processing of the schema elements.
14
+ * @internal
15
+ */
16
+ class SchemaWalker {
17
+ /**
18
+ * Initializes a new SchemaWalker instance.
19
+ * @param visitor An ISchemaWalkerVisitor implementation whose methods will be called during schema traversal.
20
+ */
21
+ constructor(visitor) {
22
+ this._visitorHelper = new SchemaPartVisitorDelegate_1.SchemaPartVisitorDelegate(visitor);
23
+ }
24
+ /**
25
+ * Traverses the given Schema, calling ISchemaWalkerVisitor methods along the way.
26
+ * @param schema The Schema to traverse.
27
+ */
28
+ async traverseSchema(schema) {
29
+ this._schema = schema;
30
+ await this._visitorHelper.visitSchema(schema);
31
+ await this._visitorHelper.visitSchemaPart(schema);
32
+ for (const item of this._schema.getItems())
33
+ await this.traverseSchemaItem(item);
34
+ return schema;
35
+ }
36
+ async traverseSchemaItem(schemaItem) {
37
+ await this._visitorHelper.visitSchemaPart(schemaItem);
38
+ if (Class_1.ECClass.isECClass(schemaItem))
39
+ await this.traverseClass(schemaItem);
40
+ }
41
+ async traverseClass(ecClass) {
42
+ if (ecClass.properties) {
43
+ for (const property of ecClass.properties) {
44
+ await this._visitorHelper.visitSchemaPart(property);
45
+ }
46
+ }
47
+ if (ecClass.schemaItemType === ECObjects_1.SchemaItemType.RelationshipClass) {
48
+ await this._visitorHelper.visitSchemaPart(ecClass.source);
49
+ await this._visitorHelper.visitSchemaPart(ecClass.target);
50
+ }
51
+ }
52
+ }
53
+ exports.SchemaWalker = SchemaWalker;
54
54
  //# sourceMappingURL=SchemaWalker.js.map