@itwin/ecschema-metadata 4.8.0-dev.9 → 4.9.0-dev.1

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 (251) hide show
  1. package/CHANGELOG.md +36 -1
  2. package/lib/cjs/Constants.js.map +1 -1
  3. package/lib/cjs/DelayedPromise.js.map +1 -1
  4. package/lib/cjs/Deserialization/AbstractParser.js.map +1 -1
  5. package/lib/cjs/Deserialization/Helper.js.map +1 -1
  6. package/lib/cjs/Deserialization/JsonParser.js.map +1 -1
  7. package/lib/cjs/Deserialization/JsonProps.d.ts +4 -0
  8. package/lib/cjs/Deserialization/JsonProps.d.ts.map +1 -1
  9. package/lib/cjs/Deserialization/JsonProps.js.map +1 -1
  10. package/lib/cjs/Deserialization/SchemaGraphUtil.js.map +1 -1
  11. package/lib/cjs/Deserialization/XmlParser.js.map +1 -1
  12. package/lib/cjs/Deserialization/XmlSerializationUtils.js.map +1 -1
  13. package/lib/cjs/ECName.js.map +1 -1
  14. package/lib/cjs/ECObjects.js.map +1 -1
  15. package/lib/cjs/Exception.js.map +1 -1
  16. package/lib/cjs/Interfaces.js.map +1 -1
  17. package/lib/cjs/Metadata/Class.d.ts +0 -6
  18. package/lib/cjs/Metadata/Class.d.ts.map +1 -1
  19. package/lib/cjs/Metadata/Class.js +0 -8
  20. package/lib/cjs/Metadata/Class.js.map +1 -1
  21. package/lib/cjs/Metadata/Constant.js.map +1 -1
  22. package/lib/cjs/Metadata/CustomAttribute.js.map +1 -1
  23. package/lib/cjs/Metadata/CustomAttributeClass.d.ts +7 -3
  24. package/lib/cjs/Metadata/CustomAttributeClass.d.ts.map +1 -1
  25. package/lib/cjs/Metadata/CustomAttributeClass.js +16 -10
  26. package/lib/cjs/Metadata/CustomAttributeClass.js.map +1 -1
  27. package/lib/cjs/Metadata/EntityClass.js.map +1 -1
  28. package/lib/cjs/Metadata/Enumeration.js.map +1 -1
  29. package/lib/cjs/Metadata/Format.js.map +1 -1
  30. package/lib/cjs/Metadata/InvertedUnit.js.map +1 -1
  31. package/lib/cjs/Metadata/KindOfQuantity.js.map +1 -1
  32. package/lib/cjs/Metadata/Mixin.js.map +1 -1
  33. package/lib/cjs/Metadata/OverrideFormat.d.ts.map +1 -1
  34. package/lib/cjs/Metadata/OverrideFormat.js +3 -1
  35. package/lib/cjs/Metadata/OverrideFormat.js.map +1 -1
  36. package/lib/cjs/Metadata/Phenomenon.js.map +1 -1
  37. package/lib/cjs/Metadata/Property.js.map +1 -1
  38. package/lib/cjs/Metadata/PropertyCategory.js.map +1 -1
  39. package/lib/cjs/Metadata/RelationshipClass.js.map +1 -1
  40. package/lib/cjs/Metadata/Schema.d.ts +14 -0
  41. package/lib/cjs/Metadata/Schema.d.ts.map +1 -1
  42. package/lib/cjs/Metadata/Schema.js +21 -0
  43. package/lib/cjs/Metadata/Schema.js.map +1 -1
  44. package/lib/cjs/Metadata/SchemaItem.d.ts +5 -0
  45. package/lib/cjs/Metadata/SchemaItem.d.ts.map +1 -1
  46. package/lib/cjs/Metadata/SchemaItem.js +7 -0
  47. package/lib/cjs/Metadata/SchemaItem.js.map +1 -1
  48. package/lib/cjs/Metadata/UnitSystem.js.map +1 -1
  49. package/lib/cjs/PropertyTypes.js.map +1 -1
  50. package/lib/cjs/SchemaJsonLocater.js.map +1 -1
  51. package/lib/cjs/SchemaKey.js.map +1 -1
  52. package/lib/cjs/SchemaLoader.js.map +1 -1
  53. package/lib/cjs/SchemaPartVisitorDelegate.js.map +1 -1
  54. package/lib/cjs/UnitConversion/Graph.js.map +1 -1
  55. package/lib/cjs/UnitConversion/Parser.js.map +1 -1
  56. package/lib/cjs/UnitConversion/UnitConversion.js.map +1 -1
  57. package/lib/cjs/UnitConversion/UnitConverter.js.map +1 -1
  58. package/lib/cjs/UnitConversion/UnitTree.js.map +1 -1
  59. package/lib/cjs/UnitProvider/SchemaUnitProvider.js.map +1 -1
  60. package/lib/cjs/Validation/SchemaWalker.js.map +1 -1
  61. package/lib/cjs/ecschema-metadata.js.map +1 -1
  62. package/lib/cjs/utils/SchemaGraph.js.map +1 -1
  63. package/lib/esm/Constants.d.ts +24 -0
  64. package/lib/esm/Constants.d.ts.map +1 -0
  65. package/lib/esm/Constants.js +30 -0
  66. package/lib/esm/Constants.js.map +1 -0
  67. package/lib/esm/Context.d.ts +219 -0
  68. package/lib/esm/Context.d.ts.map +1 -0
  69. package/lib/esm/Context.js +321 -0
  70. package/lib/esm/Context.js.map +1 -0
  71. package/lib/esm/DelayedPromise.d.ts +89 -0
  72. package/lib/esm/DelayedPromise.d.ts.map +1 -0
  73. package/lib/esm/DelayedPromise.js +88 -0
  74. package/lib/esm/DelayedPromise.js.map +1 -0
  75. package/lib/esm/Deserialization/AbstractParser.d.ts +46 -0
  76. package/lib/esm/Deserialization/AbstractParser.d.ts.map +1 -0
  77. package/lib/esm/Deserialization/AbstractParser.js +8 -0
  78. package/lib/esm/Deserialization/AbstractParser.js.map +1 -0
  79. package/lib/esm/Deserialization/Helper.d.ts +263 -0
  80. package/lib/esm/Deserialization/Helper.d.ts.map +1 -0
  81. package/lib/esm/Deserialization/Helper.js +871 -0
  82. package/lib/esm/Deserialization/Helper.js.map +1 -0
  83. package/lib/esm/Deserialization/JsonParser.d.ts +166 -0
  84. package/lib/esm/Deserialization/JsonParser.d.ts.map +1 -0
  85. package/lib/esm/Deserialization/JsonParser.js +677 -0
  86. package/lib/esm/Deserialization/JsonParser.js.map +1 -0
  87. package/lib/esm/Deserialization/JsonProps.d.ts +273 -0
  88. package/lib/esm/Deserialization/JsonProps.d.ts.map +1 -0
  89. package/lib/esm/Deserialization/JsonProps.js +9 -0
  90. package/lib/esm/Deserialization/JsonProps.js.map +1 -0
  91. package/lib/esm/Deserialization/SchemaGraphUtil.d.ts +35 -0
  92. package/lib/esm/Deserialization/SchemaGraphUtil.d.ts.map +1 -0
  93. package/lib/esm/Deserialization/SchemaGraphUtil.js +74 -0
  94. package/lib/esm/Deserialization/SchemaGraphUtil.js.map +1 -0
  95. package/lib/esm/Deserialization/XmlParser.d.ts +86 -0
  96. package/lib/esm/Deserialization/XmlParser.d.ts.map +1 -0
  97. package/lib/esm/Deserialization/XmlParser.js +970 -0
  98. package/lib/esm/Deserialization/XmlParser.js.map +1 -0
  99. package/lib/esm/Deserialization/XmlSerializationUtils.d.ts +55 -0
  100. package/lib/esm/Deserialization/XmlSerializationUtils.d.ts.map +1 -0
  101. package/lib/esm/Deserialization/XmlSerializationUtils.js +172 -0
  102. package/lib/esm/Deserialization/XmlSerializationUtils.js.map +1 -0
  103. package/lib/esm/ECName.d.ts +31 -0
  104. package/lib/esm/ECName.d.ts.map +1 -0
  105. package/lib/esm/ECName.js +82 -0
  106. package/lib/esm/ECName.js.map +1 -0
  107. package/lib/esm/ECObjects.d.ts +166 -0
  108. package/lib/esm/ECObjects.d.ts.map +1 -0
  109. package/lib/esm/ECObjects.js +422 -0
  110. package/lib/esm/ECObjects.js.map +1 -0
  111. package/lib/esm/Exception.d.ts +45 -0
  112. package/lib/esm/Exception.d.ts.map +1 -0
  113. package/lib/esm/Exception.js +82 -0
  114. package/lib/esm/Exception.js.map +1 -0
  115. package/lib/esm/Interfaces.d.ts +79 -0
  116. package/lib/esm/Interfaces.d.ts.map +1 -0
  117. package/lib/esm/Interfaces.js +9 -0
  118. package/lib/esm/Interfaces.js.map +1 -0
  119. package/lib/esm/Metadata/Class.d.ts +245 -0
  120. package/lib/esm/Metadata/Class.d.ts.map +1 -0
  121. package/lib/esm/Metadata/Class.js +552 -0
  122. package/lib/esm/Metadata/Class.js.map +1 -0
  123. package/lib/esm/Metadata/Constant.d.ts +65 -0
  124. package/lib/esm/Metadata/Constant.d.ts.map +1 -0
  125. package/lib/esm/Metadata/Constant.js +119 -0
  126. package/lib/esm/Metadata/Constant.js.map +1 -0
  127. package/lib/esm/Metadata/CustomAttribute.d.ts +24 -0
  128. package/lib/esm/Metadata/CustomAttribute.d.ts.map +1 -0
  129. package/lib/esm/Metadata/CustomAttribute.js +24 -0
  130. package/lib/esm/Metadata/CustomAttribute.js.map +1 -0
  131. package/lib/esm/Metadata/CustomAttributeClass.d.ts +44 -0
  132. package/lib/esm/Metadata/CustomAttributeClass.d.ts.map +1 -0
  133. package/lib/esm/Metadata/CustomAttributeClass.js +70 -0
  134. package/lib/esm/Metadata/CustomAttributeClass.js.map +1 -0
  135. package/lib/esm/Metadata/EntityClass.d.ts +78 -0
  136. package/lib/esm/Metadata/EntityClass.d.ts.map +1 -0
  137. package/lib/esm/Metadata/EntityClass.js +224 -0
  138. package/lib/esm/Metadata/EntityClass.js.map +1 -0
  139. package/lib/esm/Metadata/Enumeration.d.ts +87 -0
  140. package/lib/esm/Metadata/Enumeration.d.ts.map +1 -0
  141. package/lib/esm/Metadata/Enumeration.js +155 -0
  142. package/lib/esm/Metadata/Enumeration.js.map +1 -0
  143. package/lib/esm/Metadata/Format.d.ts +99 -0
  144. package/lib/esm/Metadata/Format.d.ts.map +1 -0
  145. package/lib/esm/Metadata/Format.js +238 -0
  146. package/lib/esm/Metadata/Format.js.map +1 -0
  147. package/lib/esm/Metadata/InvertedUnit.d.ts +50 -0
  148. package/lib/esm/Metadata/InvertedUnit.d.ts.map +1 -0
  149. package/lib/esm/Metadata/InvertedUnit.js +93 -0
  150. package/lib/esm/Metadata/InvertedUnit.js.map +1 -0
  151. package/lib/esm/Metadata/KindOfQuantity.d.ts +72 -0
  152. package/lib/esm/Metadata/KindOfQuantity.d.ts.map +1 -0
  153. package/lib/esm/Metadata/KindOfQuantity.js +229 -0
  154. package/lib/esm/Metadata/KindOfQuantity.js.map +1 -0
  155. package/lib/esm/Metadata/Mixin.d.ts +55 -0
  156. package/lib/esm/Metadata/Mixin.d.ts.map +1 -0
  157. package/lib/esm/Metadata/Mixin.js +109 -0
  158. package/lib/esm/Metadata/Mixin.js.map +1 -0
  159. package/lib/esm/Metadata/OverrideFormat.d.ts +68 -0
  160. package/lib/esm/Metadata/OverrideFormat.d.ts.map +1 -0
  161. package/lib/esm/Metadata/OverrideFormat.js +124 -0
  162. package/lib/esm/Metadata/OverrideFormat.js.map +1 -0
  163. package/lib/esm/Metadata/Phenomenon.d.ts +34 -0
  164. package/lib/esm/Metadata/Phenomenon.d.ts.map +1 -0
  165. package/lib/esm/Metadata/Phenomenon.js +55 -0
  166. package/lib/esm/Metadata/Phenomenon.js.map +1 -0
  167. package/lib/esm/Metadata/Property.d.ts +247 -0
  168. package/lib/esm/Metadata/Property.d.ts.map +1 -0
  169. package/lib/esm/Metadata/Property.js +565 -0
  170. package/lib/esm/Metadata/Property.js.map +1 -0
  171. package/lib/esm/Metadata/PropertyCategory.d.ts +40 -0
  172. package/lib/esm/Metadata/PropertyCategory.d.ts.map +1 -0
  173. package/lib/esm/Metadata/PropertyCategory.js +57 -0
  174. package/lib/esm/Metadata/PropertyCategory.js.map +1 -0
  175. package/lib/esm/Metadata/RelationshipClass.d.ts +170 -0
  176. package/lib/esm/Metadata/RelationshipClass.d.ts.map +1 -0
  177. package/lib/esm/Metadata/RelationshipClass.js +380 -0
  178. package/lib/esm/Metadata/RelationshipClass.js.map +1 -0
  179. package/lib/esm/Metadata/Schema.d.ts +330 -0
  180. package/lib/esm/Metadata/Schema.d.ts.map +1 -0
  181. package/lib/esm/Metadata/Schema.js +570 -0
  182. package/lib/esm/Metadata/Schema.js.map +1 -0
  183. package/lib/esm/Metadata/SchemaItem.d.ts +67 -0
  184. package/lib/esm/Metadata/SchemaItem.d.ts.map +1 -0
  185. package/lib/esm/Metadata/SchemaItem.js +140 -0
  186. package/lib/esm/Metadata/SchemaItem.js.map +1 -0
  187. package/lib/esm/Metadata/Unit.d.ts +77 -0
  188. package/lib/esm/Metadata/Unit.d.ts.map +1 -0
  189. package/lib/esm/Metadata/Unit.js +158 -0
  190. package/lib/esm/Metadata/Unit.js.map +1 -0
  191. package/lib/esm/Metadata/UnitSystem.d.ts +21 -0
  192. package/lib/esm/Metadata/UnitSystem.d.ts.map +1 -0
  193. package/lib/esm/Metadata/UnitSystem.js +25 -0
  194. package/lib/esm/Metadata/UnitSystem.js.map +1 -0
  195. package/lib/esm/PropertyTypes.d.ts +50 -0
  196. package/lib/esm/PropertyTypes.d.ts.map +1 -0
  197. package/lib/esm/PropertyTypes.js +88 -0
  198. package/lib/esm/PropertyTypes.js.map +1 -0
  199. package/lib/esm/SchemaJsonLocater.d.ts +42 -0
  200. package/lib/esm/SchemaJsonLocater.d.ts.map +1 -0
  201. package/lib/esm/SchemaJsonLocater.js +54 -0
  202. package/lib/esm/SchemaJsonLocater.js.map +1 -0
  203. package/lib/esm/SchemaKey.d.ts +111 -0
  204. package/lib/esm/SchemaKey.d.ts.map +1 -0
  205. package/lib/esm/SchemaKey.js +214 -0
  206. package/lib/esm/SchemaKey.js.map +1 -0
  207. package/lib/esm/SchemaLoader.d.ts +32 -0
  208. package/lib/esm/SchemaLoader.d.ts.map +1 -0
  209. package/lib/esm/SchemaLoader.js +53 -0
  210. package/lib/esm/SchemaLoader.js.map +1 -0
  211. package/lib/esm/SchemaPartVisitorDelegate.d.ts +275 -0
  212. package/lib/esm/SchemaPartVisitorDelegate.d.ts.map +1 -0
  213. package/lib/esm/SchemaPartVisitorDelegate.js +209 -0
  214. package/lib/esm/SchemaPartVisitorDelegate.js.map +1 -0
  215. package/lib/esm/UnitConversion/Graph.d.ts +35 -0
  216. package/lib/esm/UnitConversion/Graph.d.ts.map +1 -0
  217. package/lib/esm/UnitConversion/Graph.js +80 -0
  218. package/lib/esm/UnitConversion/Graph.js.map +1 -0
  219. package/lib/esm/UnitConversion/Parser.d.ts +9 -0
  220. package/lib/esm/UnitConversion/Parser.d.ts.map +1 -0
  221. package/lib/esm/UnitConversion/Parser.js +39 -0
  222. package/lib/esm/UnitConversion/Parser.js.map +1 -0
  223. package/lib/esm/UnitConversion/UnitConversion.d.ts +46 -0
  224. package/lib/esm/UnitConversion/UnitConversion.d.ts.map +1 -0
  225. package/lib/esm/UnitConversion/UnitConversion.js +74 -0
  226. package/lib/esm/UnitConversion/UnitConversion.js.map +1 -0
  227. package/lib/esm/UnitConversion/UnitConverter.d.ts +40 -0
  228. package/lib/esm/UnitConversion/UnitConverter.d.ts.map +1 -0
  229. package/lib/esm/UnitConversion/UnitConverter.js +113 -0
  230. package/lib/esm/UnitConversion/UnitConverter.js.map +1 -0
  231. package/lib/esm/UnitConversion/UnitTree.d.ts +44 -0
  232. package/lib/esm/UnitConversion/UnitTree.d.ts.map +1 -0
  233. package/lib/esm/UnitConversion/UnitTree.js +165 -0
  234. package/lib/esm/UnitConversion/UnitTree.js.map +1 -0
  235. package/lib/esm/UnitProvider/SchemaUnitProvider.d.ts +78 -0
  236. package/lib/esm/UnitProvider/SchemaUnitProvider.d.ts.map +1 -0
  237. package/lib/esm/UnitProvider/SchemaUnitProvider.js +231 -0
  238. package/lib/esm/UnitProvider/SchemaUnitProvider.js.map +1 -0
  239. package/lib/esm/Validation/SchemaWalker.d.ts +24 -0
  240. package/lib/esm/Validation/SchemaWalker.d.ts.map +1 -0
  241. package/lib/esm/Validation/SchemaWalker.js +50 -0
  242. package/lib/esm/Validation/SchemaWalker.js.map +1 -0
  243. package/lib/esm/ecschema-metadata.d.ts +52 -0
  244. package/lib/esm/ecschema-metadata.d.ts.map +1 -0
  245. package/lib/esm/ecschema-metadata.js +55 -0
  246. package/lib/esm/ecschema-metadata.js.map +1 -0
  247. package/lib/esm/utils/SchemaGraph.d.ts +44 -0
  248. package/lib/esm/utils/SchemaGraph.d.ts.map +1 -0
  249. package/lib/esm/utils/SchemaGraph.js +111 -0
  250. package/lib/esm/utils/SchemaGraph.js.map +1 -0
  251. package/package.json +9 -7
@@ -0,0 +1,40 @@
1
+ import { SchemaContext } from "../Context";
2
+ import { UnitConversion } from "./UnitConversion";
3
+ /**
4
+ * Class constructed with SchemaContext and used to calculate [[UnitConversion]] between Units
5
+ * @internal
6
+ */
7
+ export declare class UnitConverter {
8
+ private readonly _context;
9
+ private _uGraph;
10
+ /**
11
+ * Create Converter context
12
+ * @param _context SchemaContext with contexts added to it.
13
+ */
14
+ constructor(_context: SchemaContext);
15
+ /**
16
+ * Find conversion between from and to units, formatted {schemaName}.{schemaItemName} or {schemaName}:{schemaItemName}
17
+ * @param fromUnit SchemaItem full name of source unit
18
+ * @param toUnit SchemaItem full name of target unit
19
+ * @returns [[UnitConversion]] converting fromUnit -> toUnit with a factor and an offset
20
+ * @throws Error if from and to Units' SchemaItem is not found in Schema or Schema prefix is not found in SchemaContext
21
+ * @throws Error if from and to Units do not belong to the same phenomenon
22
+ * @throws Error if definitions' SchemaItems cannot be found in its own or referenced Schemas
23
+ * @throws Error if base units of source and target unit do not match
24
+ */
25
+ calculateConversion(fromUnit: string, toUnit: string): Promise<UnitConversion>;
26
+ /**
27
+ * @param from Source unit converted from
28
+ * @param to Target unit converted to
29
+ * @internal
30
+ */
31
+ private processUnits;
32
+ /**
33
+ * Check if fromBaseUnits's base units and exponents matches toBaseUnits's
34
+ * @param fromBaseUnits Map of base units for source unit
35
+ * @param toBaseUnits Map of base units for target unit
36
+ * @internal
37
+ */
38
+ private checkBaseUnitsMatch;
39
+ }
40
+ //# sourceMappingURL=UnitConverter.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"UnitConverter.d.ts","sourceRoot":"","sources":["../../../src/UnitConversion/UnitConverter.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAK3C,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAGlD;;;GAGG;AACH,qBAAa,aAAa;IAOZ,OAAO,CAAC,QAAQ,CAAC,QAAQ;IANrC,OAAO,CAAC,OAAO,CAAY;IAE3B;;;OAGG;gBAC0B,QAAQ,EAAE,aAAa;IAIpD;;;;;;;;;OASG;IACU,mBAAmB,CAAC,QAAQ,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,cAAc,CAAC;IAqB3F;;;;OAIG;YACW,YAAY;IAiC1B;;;;;OAKG;IACH,OAAO,CAAC,mBAAmB;CA4B5B"}
@@ -0,0 +1,113 @@
1
+ /*---------------------------------------------------------------------------------------------
2
+ * Copyright (c) Bentley Systems, Incorporated. All rights reserved.
3
+ * See LICENSE.md in the project root for license terms and full copyright notice.
4
+ *--------------------------------------------------------------------------------------------*/
5
+ import { BentleyError, BentleyStatus } from "@itwin/core-bentley";
6
+ import { SchemaItem } from "../Metadata/SchemaItem";
7
+ import { Unit } from "../Metadata/Unit";
8
+ import { SchemaKey } from "../SchemaKey";
9
+ import { UnitConversion } from "./UnitConversion";
10
+ import { UnitGraph } from "./UnitTree";
11
+ /**
12
+ * Class constructed with SchemaContext and used to calculate [[UnitConversion]] between Units
13
+ * @internal
14
+ */
15
+ export class UnitConverter {
16
+ /**
17
+ * Create Converter context
18
+ * @param _context SchemaContext with contexts added to it.
19
+ */
20
+ constructor(_context) {
21
+ this._context = _context;
22
+ this._uGraph = new UnitGraph(this._context);
23
+ }
24
+ /**
25
+ * Find conversion between from and to units, formatted {schemaName}.{schemaItemName} or {schemaName}:{schemaItemName}
26
+ * @param fromUnit SchemaItem full name of source unit
27
+ * @param toUnit SchemaItem full name of target unit
28
+ * @returns [[UnitConversion]] converting fromUnit -> toUnit with a factor and an offset
29
+ * @throws Error if from and to Units' SchemaItem is not found in Schema or Schema prefix is not found in SchemaContext
30
+ * @throws Error if from and to Units do not belong to the same phenomenon
31
+ * @throws Error if definitions' SchemaItems cannot be found in its own or referenced Schemas
32
+ * @throws Error if base units of source and target unit do not match
33
+ */
34
+ async calculateConversion(fromUnit, toUnit) {
35
+ const [fromSchemaName, fromSchemaItemName] = SchemaItem.parseFullName(fromUnit);
36
+ const [toSchemaName, toSchemaItemName] = SchemaItem.parseFullName(toUnit);
37
+ const fromSchemaKey = new SchemaKey(fromSchemaName);
38
+ const toSchemaKey = new SchemaKey(toSchemaName);
39
+ const fromSchema = await this._context.getSchema(fromSchemaKey);
40
+ const toSchema = await this._context.getSchema(toSchemaKey);
41
+ if (!fromSchema || !toSchema) {
42
+ throw new BentleyError(BentleyStatus.ERROR, "Cannot find from's and/or to's schema", () => {
43
+ return { from: fromUnit, fromSchema: fromSchemaName, to: toUnit, toSchema: toSchemaName };
44
+ });
45
+ }
46
+ const from = await this._uGraph.resolveUnit(fromSchemaItemName, fromSchema);
47
+ const to = await this._uGraph.resolveUnit(toSchemaItemName, toSchema);
48
+ return this.processUnits(from, to);
49
+ }
50
+ /**
51
+ * @param from Source unit converted from
52
+ * @param to Target unit converted to
53
+ * @internal
54
+ */
55
+ async processUnits(from, to) {
56
+ if (from.key.matches(to.key))
57
+ return UnitConversion.identity;
58
+ const areCompatible = await Unit.areCompatible(from, to);
59
+ if (!areCompatible)
60
+ throw new BentleyError(BentleyStatus.ERROR, `Source and target units do not belong to same phenomenon`, () => {
61
+ return { from, to };
62
+ });
63
+ // Add nodes and subsequent children to graph
64
+ await this._uGraph.addUnit(from);
65
+ await this._uGraph.addUnit(to);
66
+ const fromBaseUnits = new Map();
67
+ const toBaseUnits = new Map();
68
+ // Calculate map of UnitConversions to get between from -> base
69
+ const fromMapStore = this._uGraph.reduce(from, fromBaseUnits);
70
+ // Calculate map of UnitConversions to get between base -> to
71
+ const toMapStore = this._uGraph.reduce(to, toBaseUnits);
72
+ if (!this.checkBaseUnitsMatch(fromBaseUnits, toBaseUnits))
73
+ throw new BentleyError(BentleyStatus.ERROR, `Source and target units do not have matching base units`, () => {
74
+ return { from, to };
75
+ });
76
+ // Final calculations to get singular UnitConversion between from -> to
77
+ const fromMap = fromMapStore.get(from.key.fullName) || UnitConversion.identity;
78
+ const toMap = toMapStore.get(to.key.fullName) || UnitConversion.identity;
79
+ const fromInverse = fromMap.inverse();
80
+ return fromInverse.compose(toMap);
81
+ }
82
+ /**
83
+ * Check if fromBaseUnits's base units and exponents matches toBaseUnits's
84
+ * @param fromBaseUnits Map of base units for source unit
85
+ * @param toBaseUnits Map of base units for target unit
86
+ * @internal
87
+ */
88
+ checkBaseUnitsMatch(fromBaseUnits, toBaseUnits) {
89
+ // Trim maps of "One" and value that equal zero as they do not affect the base units and calculations
90
+ for (const [key, value] of fromBaseUnits.entries()) {
91
+ const [, schemaItemName] = SchemaItem.parseFullName(key);
92
+ if (schemaItemName === "ONE" || value === 0) {
93
+ fromBaseUnits.delete(key);
94
+ }
95
+ }
96
+ for (const [key, value] of toBaseUnits.entries()) {
97
+ const [, schemaItemName] = SchemaItem.parseFullName(key);
98
+ if (schemaItemName === "ONE" || value === 0) {
99
+ toBaseUnits.delete(key);
100
+ }
101
+ }
102
+ if (fromBaseUnits.size !== toBaseUnits.size)
103
+ return false;
104
+ for (const key of fromBaseUnits.keys()) {
105
+ if (!toBaseUnits.has(key) || fromBaseUnits.get(key) !== toBaseUnits.get(key)) {
106
+ // Mismatching key or value
107
+ return false;
108
+ }
109
+ }
110
+ return true;
111
+ }
112
+ }
113
+ //# sourceMappingURL=UnitConverter.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"UnitConverter.js","sourceRoot":"","sources":["../../../src/UnitConversion/UnitConverter.ts"],"names":[],"mappings":"AAAA;;;+FAG+F;AAC/F,OAAO,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAGlE,OAAO,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAC;AACpD,OAAO,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAC;AACxC,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AACzC,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAClD,OAAO,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAEvC;;;GAGG;AACH,MAAM,OAAO,aAAa;IAGxB;;;OAGG;IACH,YAA6B,QAAuB;QAAvB,aAAQ,GAAR,QAAQ,CAAe;QAClD,IAAI,CAAC,OAAO,GAAG,IAAI,SAAS,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;IAC9C,CAAC;IAED;;;;;;;;;OASG;IACI,KAAK,CAAC,mBAAmB,CAAC,QAAgB,EAAE,MAAc;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AAC,OAAO,EAAE,CAAC;QACtC,OAAO,WAAW,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;IACpC,CAAC;IAED;;;;;OAKG;IACK,mBAAmB,CAAC,aAAkC,EAAE,WAAgC;QAC9F,qGAAqG;QACrG,KAAK,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,aAAa,CAAC,OAAO,EAAE,EAAE,CAAC;YACnD,MAAM,CAAC,EAAE,cAAc,CAAC,GAAG,UAAU,CAAC,aAAa,CAAC,GAAG,CAAC,CAAC;YACzD,IAAI,cAAc,KAAK,KAAK,IAAI,KAAK,KAAK,CAAC,EAAE,CAAC;gBAC5C,aAAa,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;YAC5B,CAAC;QACH,CAAC;QAED,KAAK,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,WAAW,CAAC,OAAO,EAAE,EAAE,CAAC;YACjD,MAAM,CAAC,EAAE,cAAc,CAAC,GAAG,UAAU,CAAC,aAAa,CAAC,GAAG,CAAC,CAAC;YACzD,IAAI,cAAc,KAAK,KAAK,IAAI,KAAK,KAAK,CAAC,EAAE,CAAC;gBAC5C,WAAW,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;YAC1B,CAAC;QACH,CAAC;QAED,IAAI,aAAa,CAAC,IAAI,KAAK,WAAW,CAAC,IAAI;YACzC,OAAO,KAAK,CAAC;QAEf,KAAK,MAAM,GAAG,IAAI,aAAa,CAAC,IAAI,EAAE,EAAE,CAAC;YACvC,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC,GAAG,CAAC,IAAI,aAAa,CAAC,GAAG,CAAC,GAAG,CAAC,KAAK,WAAW,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,CAAC;gBAC7E,2BAA2B;gBAC3B,OAAO,KAAK,CAAC;YACf,CAAC;QACH,CAAC;QAED,OAAO,IAAI,CAAC;IACd,CAAC;CACF","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 { SchemaContext } from \"../Context\";\r\nimport { Constant } from \"../Metadata/Constant\";\r\nimport { SchemaItem } from \"../Metadata/SchemaItem\";\r\nimport { Unit } from \"../Metadata/Unit\";\r\nimport { SchemaKey } from \"../SchemaKey\";\r\nimport { UnitConversion } from \"./UnitConversion\";\r\nimport { UnitGraph } from \"./UnitTree\";\r\n\r\n/**\r\n * Class constructed with SchemaContext and used to calculate [[UnitConversion]] between Units\r\n * @internal\r\n */\r\nexport class UnitConverter {\r\n private _uGraph: UnitGraph;\r\n\r\n /**\r\n * Create Converter context\r\n * @param _context SchemaContext with contexts added to it.\r\n */\r\n constructor(private readonly _context: SchemaContext) {\r\n this._uGraph = new UnitGraph(this._context);\r\n }\r\n\r\n /**\r\n * Find conversion between from and to units, formatted {schemaName}.{schemaItemName} or {schemaName}:{schemaItemName}\r\n * @param fromUnit SchemaItem full name of source unit\r\n * @param toUnit SchemaItem full name of target unit\r\n * @returns [[UnitConversion]] converting fromUnit -> toUnit with a factor and an offset\r\n * @throws Error if from and to Units' SchemaItem is not found in Schema or Schema prefix is not found in SchemaContext\r\n * @throws Error if from and to Units do not belong to the same phenomenon\r\n * @throws Error if definitions' SchemaItems cannot be found in its own or referenced Schemas\r\n * @throws Error if base units of source and target unit do not match\r\n */\r\n public async calculateConversion(fromUnit: string, toUnit: string): Promise<UnitConversion> {\r\n const [fromSchemaName, fromSchemaItemName] = SchemaItem.parseFullName(fromUnit);\r\n const [toSchemaName, toSchemaItemName] = SchemaItem.parseFullName(toUnit);\r\n const fromSchemaKey = new SchemaKey(fromSchemaName);\r\n const toSchemaKey = new SchemaKey(toSchemaName);\r\n\r\n const fromSchema = await this._context.getSchema(fromSchemaKey);\r\n const toSchema = await this._context.getSchema(toSchemaKey);\r\n\r\n if (!fromSchema || !toSchema) {\r\n throw new BentleyError(BentleyStatus.ERROR, \"Cannot find from's and/or to's schema\", () => {\r\n return { from: fromUnit, fromSchema: fromSchemaName, to: toUnit, toSchema: toSchemaName };\r\n });\r\n }\r\n\r\n const from = await this._uGraph.resolveUnit(fromSchemaItemName, fromSchema);\r\n const to = await this._uGraph.resolveUnit(toSchemaItemName, toSchema);\r\n\r\n return this.processUnits(from, to);\r\n }\r\n\r\n /**\r\n * @param from Source unit converted from\r\n * @param to Target unit converted to\r\n * @internal\r\n */\r\n private async processUnits(from: Unit | Constant, to: Unit | Constant): Promise<UnitConversion> {\r\n if (from.key.matches(to.key))\r\n return UnitConversion.identity;\r\n\r\n const areCompatible = await Unit.areCompatible(from as Unit, to as Unit);\r\n if (!areCompatible)\r\n throw new BentleyError(BentleyStatus.ERROR, `Source and target units do not belong to same phenomenon`, () => {\r\n return { from, to };\r\n });\r\n\r\n // Add nodes and subsequent children to graph\r\n await this._uGraph.addUnit(from);\r\n await this._uGraph.addUnit(to);\r\n\r\n const fromBaseUnits = new Map<string, number>();\r\n const toBaseUnits = new Map<string, number>();\r\n // Calculate map of UnitConversions to get between from -> base\r\n const fromMapStore = this._uGraph.reduce(from, fromBaseUnits);\r\n // Calculate map of UnitConversions to get between base -> to\r\n const toMapStore = this._uGraph.reduce(to, toBaseUnits);\r\n\r\n if (!this.checkBaseUnitsMatch(fromBaseUnits, toBaseUnits))\r\n throw new BentleyError(BentleyStatus.ERROR, `Source and target units do not have matching base units`, () => {\r\n return { from, to };\r\n });\r\n\r\n // Final calculations to get singular UnitConversion between from -> to\r\n const fromMap = fromMapStore.get(from.key.fullName) || UnitConversion.identity;\r\n const toMap = toMapStore.get(to.key.fullName) || UnitConversion.identity;\r\n const fromInverse = fromMap.inverse();\r\n return fromInverse.compose(toMap);\r\n }\r\n\r\n /**\r\n * Check if fromBaseUnits's base units and exponents matches toBaseUnits's\r\n * @param fromBaseUnits Map of base units for source unit\r\n * @param toBaseUnits Map of base units for target unit\r\n * @internal\r\n */\r\n private checkBaseUnitsMatch(fromBaseUnits: Map<string, number>, toBaseUnits: Map<string, number>): boolean {\r\n // Trim maps of \"One\" and value that equal zero as they do not affect the base units and calculations\r\n for (const [key, value] of fromBaseUnits.entries()) {\r\n const [, schemaItemName] = SchemaItem.parseFullName(key);\r\n if (schemaItemName === \"ONE\" || value === 0) {\r\n fromBaseUnits.delete(key);\r\n }\r\n }\r\n\r\n for (const [key, value] of toBaseUnits.entries()) {\r\n const [, schemaItemName] = SchemaItem.parseFullName(key);\r\n if (schemaItemName === \"ONE\" || value === 0) {\r\n toBaseUnits.delete(key);\r\n }\r\n }\r\n\r\n if (fromBaseUnits.size !== toBaseUnits.size)\r\n return false;\r\n\r\n for (const key of fromBaseUnits.keys()) {\r\n if (!toBaseUnits.has(key) || fromBaseUnits.get(key) !== toBaseUnits.get(key)) {\r\n // Mismatching key or value\r\n return false;\r\n }\r\n }\r\n\r\n return true;\r\n }\r\n}\r\n"]}
@@ -0,0 +1,44 @@
1
+ import { SchemaContext } from "../Context";
2
+ import { Constant } from "../Metadata/Constant";
3
+ import { Schema } from "../Metadata/Schema";
4
+ import { Unit } from "../Metadata/Unit";
5
+ import { UnitConversion } from "./UnitConversion";
6
+ import { Graph } from "./Graph";
7
+ /** @internal */
8
+ export declare class GraphUtils {
9
+ /**
10
+ * DFS traversal - Post order
11
+ * @param _graph Graph to traverse
12
+ * @param start Starting node
13
+ * @param keyFrom Get key from label
14
+ * @param op Reducing function
15
+ * @param initial Initial label
16
+ */
17
+ static dfsReduce<T>(_graph: Graph<Unit | Constant>, key: string, op: (previous: T, current: string) => T, initial: T, baseUnitsMap: Map<string, number>, accumulatedExponent: number): T;
18
+ }
19
+ /** @internal */
20
+ export declare class UnitGraph {
21
+ private _context;
22
+ private _graph;
23
+ constructor(_context: SchemaContext);
24
+ /**
25
+ * Tries to find the unit/constant given by name in currentSchema
26
+ * @param name SchemaItem name or parsed definition to find unit of; Could be {schemaName}:{schemaItemName} or {alias}:{schemaItemName} or {schemaItemName}
27
+ * @param currentSchema schema to find name in; name could also be in a referenced schema of current schema
28
+ */
29
+ resolveUnit(name: string, currentSchema: Schema): Promise<Unit | Constant>;
30
+ /**
31
+ * Adds unit and corresponding children to graph as well as edges between units
32
+ * @param unit Current unit to be added to graph
33
+ */
34
+ addUnit(unit: Unit | Constant): Promise<void>;
35
+ private isIdentity;
36
+ /**
37
+ * Reduce the tree to produce a single map
38
+ * @param unit Unit to be processed
39
+ * @param stopNodes The tree exploration should stop here
40
+ */
41
+ reduce(unit: Unit | Constant, baseUnitsMap: Map<string, number>): Map<string, UnitConversion>;
42
+ private reducingFunction;
43
+ }
44
+ //# sourceMappingURL=UnitTree.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"UnitTree.d.ts","sourceRoot":"","sources":["../../../src/UnitConversion/UnitTree.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAC3C,OAAO,EAAE,QAAQ,EAAE,MAAM,sBAAsB,CAAC;AAChD,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAE5C,OAAO,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAC;AAGxC,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAElD,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AAEhC,gBAAgB;AAChB,qBAAa,UAAU;IACrB;;;;;;;OAOG;WACW,SAAS,CAAC,CAAC,EAAE,MAAM,EAAE,KAAK,CAAC,IAAI,GAAG,QAAQ,CAAC,EAAE,GAAG,EAAE,MAAM,EAAE,EAAE,EAAE,CAAC,QAAQ,EAAE,CAAC,EAAE,OAAO,EAAE,MAAM,KAAK,CAAC,EAAE,OAAO,EAAE,CAAC,EAAE,YAAY,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,EAAE,mBAAmB,EAAE,MAAM,GAAG,CAAC;CAuBhM;AAED,gBAAgB;AAChB,qBAAa,SAAS;IAGR,OAAO,CAAC,QAAQ;IAF5B,OAAO,CAAC,MAAM,CAAgC;gBAE1B,QAAQ,EAAE,aAAa;IAI3C;;;;OAIG;IACU,WAAW,CAAC,IAAI,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,GAAG,QAAQ,CAAC;IAqDvF;;;OAGG;IACU,OAAO,CAAC,IAAI,EAAE,IAAI,GAAG,QAAQ,GAAG,OAAO,CAAC,IAAI,CAAC;IA8B1D,OAAO,CAAC,UAAU;IAIlB;;;;OAIG;IACI,MAAM,CAAC,IAAI,EAAE,IAAI,GAAG,QAAQ,EAAE,YAAY,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,GAAG,CAAC,MAAM,EAAE,cAAc,CAAC;IAOpG,OAAO,CAAC,gBAAgB;CAmBzB"}
@@ -0,0 +1,165 @@
1
+ /*---------------------------------------------------------------------------------------------
2
+ * Copyright (c) Bentley Systems, Incorporated. All rights reserved.
3
+ * See LICENSE.md in the project root for license terms and full copyright notice.
4
+ *--------------------------------------------------------------------------------------------*/
5
+ import { BentleyError, BentleyStatus } from "@itwin/core-bentley";
6
+ import { SchemaItem } from "../Metadata/SchemaItem";
7
+ import { SchemaItemKey, SchemaKey } from "../SchemaKey";
8
+ import { SchemaItemType } from "../ECObjects";
9
+ import { UnitConversion } from "./UnitConversion";
10
+ import { parseDefinition } from "./Parser";
11
+ import { Graph } from "./Graph";
12
+ /** @internal */
13
+ export class GraphUtils {
14
+ /**
15
+ * DFS traversal - Post order
16
+ * @param _graph Graph to traverse
17
+ * @param start Starting node
18
+ * @param keyFrom Get key from label
19
+ * @param op Reducing function
20
+ * @param initial Initial label
21
+ */
22
+ static dfsReduce(_graph, key, op, initial, baseUnitsMap, accumulatedExponent) {
23
+ const outEdges = _graph.outEdges(key);
24
+ let t = initial;
25
+ if (outEdges.length > 0) {
26
+ t = outEdges.reduce((p, edge) => {
27
+ const { v, w } = edge;
28
+ const edgeExponent = _graph.edge(v, w).exponent;
29
+ return GraphUtils.dfsReduce(_graph, edge.w, op, p, baseUnitsMap, accumulatedExponent * edgeExponent);
30
+ }, t);
31
+ }
32
+ else {
33
+ if (baseUnitsMap.has(key)) {
34
+ const oldExponent = baseUnitsMap.get(key);
35
+ baseUnitsMap.set(key, oldExponent + accumulatedExponent);
36
+ }
37
+ else {
38
+ baseUnitsMap.set(key, accumulatedExponent);
39
+ }
40
+ }
41
+ return op(t, key);
42
+ }
43
+ }
44
+ /** @internal */
45
+ export class UnitGraph {
46
+ constructor(_context) {
47
+ this._context = _context;
48
+ this._graph = new Graph();
49
+ this._graph.setGraph("Unit tree processor");
50
+ }
51
+ /**
52
+ * Tries to find the unit/constant given by name in currentSchema
53
+ * @param name SchemaItem name or parsed definition to find unit of; Could be {schemaName}:{schemaItemName} or {alias}:{schemaItemName} or {schemaItemName}
54
+ * @param currentSchema schema to find name in; name could also be in a referenced schema of current schema
55
+ */
56
+ async resolveUnit(name, currentSchema) {
57
+ let [schemaName] = SchemaItem.parseFullName(name);
58
+ const [, schemaItemName] = SchemaItem.parseFullName(name);
59
+ if (schemaName !== "") {
60
+ // Check if schemaName is schemaName or alias
61
+ const ref = currentSchema.getReferenceSync(schemaName);
62
+ const refName = currentSchema.getReferenceNameByAlias(schemaName);
63
+ if (ref) {
64
+ // Got schema by schemaName
65
+ schemaName = ref.name;
66
+ }
67
+ else if (refName) {
68
+ // Got schema by alias
69
+ schemaName = refName;
70
+ }
71
+ else {
72
+ // Didn't match any referenced schema, check if it is current schemaName or alias
73
+ if (schemaName === currentSchema.name || schemaName === currentSchema.alias)
74
+ schemaName = currentSchema.name;
75
+ }
76
+ // Create schema key with schema name
77
+ const schemaKey = new SchemaKey(schemaName);
78
+ // Get schema with schema key
79
+ const schema = await this._context.getSchema(schemaKey);
80
+ if (!schema) {
81
+ throw new BentleyError(BentleyStatus.ERROR, "Cannot find schema", () => {
82
+ return { schema: schemaName };
83
+ });
84
+ }
85
+ else {
86
+ // Set currentSchema to look up schemaItem to be whatever is prefixed in name
87
+ currentSchema = schema;
88
+ }
89
+ // Update name to not have prefix
90
+ name = schemaItemName;
91
+ }
92
+ // Create schema item key with name and schema
93
+ const itemKey = new SchemaItemKey(name, currentSchema.schemaKey);
94
+ // Get schema item with schema item key
95
+ const item = await this._context.getSchemaItem(itemKey);
96
+ if (!item)
97
+ throw new BentleyError(BentleyStatus.ERROR, "Cannot find schema item", () => {
98
+ return { item: name };
99
+ });
100
+ if (item.schemaItemType === SchemaItemType.Unit || item.schemaItemType === SchemaItemType.Constant)
101
+ return item;
102
+ throw new BentleyError(BentleyStatus.ERROR, "Item is neither a unit or a constant", () => {
103
+ return { itemType: item.key.fullName };
104
+ });
105
+ }
106
+ /**
107
+ * Adds unit and corresponding children to graph as well as edges between units
108
+ * @param unit Current unit to be added to graph
109
+ */
110
+ async addUnit(unit) {
111
+ if (this._graph.hasNode(unit.key.fullName))
112
+ return;
113
+ this._graph.setNode(unit.key.fullName, unit);
114
+ if (this.isIdentity(unit))
115
+ return;
116
+ const umap = parseDefinition(unit.definition);
117
+ const promiseArray = [];
118
+ for (const [key, value] of umap) {
119
+ promiseArray.push(this.resolveUnit(key, unit.schema).then((u) => [u, value]));
120
+ }
121
+ const resolved = await Promise.all(promiseArray);
122
+ const children = resolved.map(async ([u, def]) => {
123
+ await this.addUnit(u);
124
+ this._graph.setEdge(unit.key.fullName, u.key.fullName, {
125
+ exponent: def.exponent,
126
+ });
127
+ });
128
+ await Promise.all(children);
129
+ }
130
+ isIdentity(unit) {
131
+ return unit.definition === unit.name;
132
+ }
133
+ /**
134
+ * Reduce the tree to produce a single map
135
+ * @param unit Unit to be processed
136
+ * @param stopNodes The tree exploration should stop here
137
+ */
138
+ reduce(unit, baseUnitsMap) {
139
+ const unitFullName = unit.key.fullName;
140
+ const innerMapStore = new Map();
141
+ const outerMapStore = GraphUtils.dfsReduce(this._graph, unitFullName, (p, c) => this.reducingFunction(p, c), innerMapStore, baseUnitsMap, 1);
142
+ return outerMapStore;
143
+ }
144
+ reducingFunction(innermapStore, unitFullName) {
145
+ const outEdges = this._graph.outEdges(unitFullName);
146
+ if (outEdges) {
147
+ const cmap = outEdges.reduce((pm, e) => {
148
+ const { exponent } = this._graph.edge(e.v, e.w);
149
+ const stored = innermapStore.get(e.w);
150
+ const map = stored ? stored : UnitConversion.identity;
151
+ const emap = map.raise(exponent);
152
+ return pm ? pm.multiply(emap) : emap;
153
+ }, undefined);
154
+ const thisMap = this._graph.node(unitFullName) ? UnitConversion.from(this._graph.node(unitFullName)) : UnitConversion.identity;
155
+ const other = cmap || UnitConversion.identity;
156
+ const result = other.compose(thisMap);
157
+ innermapStore.set(unitFullName, result);
158
+ }
159
+ else {
160
+ innermapStore.set(unitFullName, UnitConversion.identity);
161
+ }
162
+ return innermapStore;
163
+ }
164
+ }
165
+ //# sourceMappingURL=UnitTree.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"UnitTree.js","sourceRoot":"","sources":["../../../src/UnitConversion/UnitTree.ts"],"names":[],"mappings":"AAAA;;;+FAG+F;AAC/F,OAAO,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAIlE,OAAO,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAC;AAEpD,OAAO,EAAE,aAAa,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AACxD,OAAO,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAC9C,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAClD,OAAO,EAAsB,eAAe,EAAE,MAAM,UAAU,CAAC;AAC/D,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AAEhC,gBAAgB;AAChB,MAAM,OAAO,UAAU;IACrB;;;;;;;OAOG;IACI,MAAM,CAAC,SAAS,CAAI,MAA8B,EAAE,GAAW,EAAE,EAAuC,EAAE,OAAU,EAAE,YAAiC,EAAE,mBAA2B;QACzL,MAAM,QAAQ,GAAG,MAAM,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------------------------------------------------------------------------------------------\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 { SchemaContext } from \"../Context\";\r\nimport { Constant } from \"../Metadata/Constant\";\r\nimport { Schema } from \"../Metadata/Schema\";\r\nimport { SchemaItem } from \"../Metadata/SchemaItem\";\r\nimport { Unit } from \"../Metadata/Unit\";\r\nimport { SchemaItemKey, SchemaKey } from \"../SchemaKey\";\r\nimport { SchemaItemType } from \"../ECObjects\";\r\nimport { UnitConversion } from \"./UnitConversion\";\r\nimport { DefinitionFragment, parseDefinition } from \"./Parser\";\r\nimport { Graph } from \"./Graph\";\r\n\r\n/** @internal */\r\nexport class GraphUtils {\r\n /**\r\n * DFS traversal - Post order\r\n * @param _graph Graph to traverse\r\n * @param start Starting node\r\n * @param keyFrom Get key from label\r\n * @param op Reducing function\r\n * @param initial Initial label\r\n */\r\n public static dfsReduce<T>(_graph: Graph<Unit | Constant>, key: string, op: (previous: T, current: string) => T, initial: T, baseUnitsMap: Map<string, number>, accumulatedExponent: number): T {\r\n const outEdges = _graph.outEdges(key);\r\n let t = initial;\r\n if (outEdges.length > 0) {\r\n t = outEdges.reduce<T>(\r\n (p, edge) => {\r\n const { v, w } = edge;\r\n const edgeExponent = _graph.edge(v, w).exponent;\r\n return GraphUtils.dfsReduce(_graph, edge.w, op, p, baseUnitsMap, accumulatedExponent * edgeExponent);\r\n },\r\n t,\r\n );\r\n } else {\r\n if (baseUnitsMap.has(key)) {\r\n const oldExponent = baseUnitsMap.get(key)!;\r\n baseUnitsMap.set(key, oldExponent + accumulatedExponent);\r\n } else {\r\n baseUnitsMap.set(key, accumulatedExponent);\r\n }\r\n }\r\n\r\n return op(t, key);\r\n }\r\n}\r\n\r\n/** @internal */\r\nexport class UnitGraph {\r\n private _graph = new Graph<Unit | Constant>();\r\n\r\n constructor(private _context: SchemaContext) {\r\n this._graph.setGraph(\"Unit tree processor\");\r\n }\r\n\r\n /**\r\n * Tries to find the unit/constant given by name in currentSchema\r\n * @param name SchemaItem name or parsed definition to find unit of; Could be {schemaName}:{schemaItemName} or {alias}:{schemaItemName} or {schemaItemName}\r\n * @param currentSchema schema to find name in; name could also be in a referenced schema of current schema\r\n */\r\n public async resolveUnit(name: string, currentSchema: Schema): Promise<Unit | Constant> {\r\n let [schemaName] = SchemaItem.parseFullName(name);\r\n const [, schemaItemName] = SchemaItem.parseFullName(name);\r\n\r\n if (schemaName !== \"\") {\r\n // Check if schemaName is schemaName or alias\r\n const ref = currentSchema.getReferenceSync(schemaName);\r\n const refName = currentSchema.getReferenceNameByAlias(schemaName);\r\n if (ref) {\r\n // Got schema by schemaName\r\n schemaName = ref.name;\r\n } else if (refName) {\r\n // Got schema by alias\r\n schemaName = refName;\r\n } else {\r\n // Didn't match any referenced schema, check if it is current schemaName or alias\r\n if (schemaName === currentSchema.name || schemaName === currentSchema.alias)\r\n schemaName = currentSchema.name;\r\n }\r\n\r\n // Create schema key with schema name\r\n const schemaKey = new SchemaKey(schemaName);\r\n // Get schema with schema key\r\n const schema = await this._context.getSchema(schemaKey);\r\n if (!schema) {\r\n throw new BentleyError(BentleyStatus.ERROR, \"Cannot find schema\", () => {\r\n return { schema: schemaName };\r\n });\r\n } else {\r\n // Set currentSchema to look up schemaItem to be whatever is prefixed in name\r\n currentSchema = schema;\r\n }\r\n // Update name to not have prefix\r\n name = schemaItemName;\r\n }\r\n\r\n // Create schema item key with name and schema\r\n const itemKey = new SchemaItemKey(name, currentSchema.schemaKey);\r\n // Get schema item with schema item key\r\n const item = await this._context.getSchemaItem(itemKey);\r\n if (!item)\r\n throw new BentleyError(BentleyStatus.ERROR, \"Cannot find schema item\", () => {\r\n return { item: name };\r\n });\r\n\r\n if (item.schemaItemType === SchemaItemType.Unit || item.schemaItemType === SchemaItemType.Constant)\r\n return item as Unit | Constant;\r\n\r\n throw new BentleyError(BentleyStatus.ERROR, \"Item is neither a unit or a constant\", () => {\r\n return { itemType: item.key.fullName };\r\n });\r\n }\r\n\r\n /**\r\n * Adds unit and corresponding children to graph as well as edges between units\r\n * @param unit Current unit to be added to graph\r\n */\r\n public async addUnit(unit: Unit | Constant): Promise<void> {\r\n if (this._graph.hasNode(unit.key.fullName))\r\n return;\r\n\r\n this._graph.setNode(unit.key.fullName, unit);\r\n if (this.isIdentity(unit))\r\n return;\r\n\r\n const umap = parseDefinition(unit.definition);\r\n\r\n const promiseArray: Promise<[Unit | Constant, DefinitionFragment]>[] = [];\r\n for (const [key, value] of umap) {\r\n promiseArray.push(\r\n this.resolveUnit(key, unit.schema).then((u) => [u, value]),\r\n );\r\n }\r\n const resolved = await Promise.all<[Unit | Constant, DefinitionFragment]>(\r\n promiseArray,\r\n );\r\n\r\n const children = resolved.map(async ([u, def]) => {\r\n await this.addUnit(u);\r\n this._graph.setEdge(unit.key.fullName, u.key.fullName, {\r\n exponent: def.exponent,\r\n });\r\n });\r\n\r\n await Promise.all(children);\r\n }\r\n\r\n private isIdentity(unit: Unit | Constant) {\r\n return unit.definition === unit.name;\r\n }\r\n\r\n /**\r\n * Reduce the tree to produce a single map\r\n * @param unit Unit to be processed\r\n * @param stopNodes The tree exploration should stop here\r\n */\r\n public reduce(unit: Unit | Constant, baseUnitsMap: Map<string, number>): Map<string, UnitConversion> {\r\n const unitFullName = unit.key.fullName;\r\n const innerMapStore = new Map<string, UnitConversion>();\r\n const outerMapStore = GraphUtils.dfsReduce(this._graph, unitFullName, (p, c) => this.reducingFunction(p, c), innerMapStore, baseUnitsMap, 1);\r\n return outerMapStore;\r\n }\r\n\r\n private reducingFunction(innermapStore: Map<string, UnitConversion>, unitFullName: string) {\r\n const outEdges = this._graph.outEdges(unitFullName);\r\n if (outEdges) {\r\n const cmap = outEdges.reduce<UnitConversion | undefined>((pm, e) => {\r\n const { exponent } = this._graph.edge(e.v, e.w);\r\n const stored = innermapStore.get(e.w);\r\n const map = stored ? stored : UnitConversion.identity;\r\n const emap = map.raise(exponent);\r\n return pm ? pm.multiply(emap) : emap;\r\n }, undefined);\r\n const thisMap = this._graph.node(unitFullName) ? UnitConversion.from(this._graph.node(unitFullName)) : UnitConversion.identity;\r\n const other = cmap || UnitConversion.identity;\r\n const result = other.compose(thisMap);\r\n innermapStore.set(unitFullName, result);\r\n } else {\r\n innermapStore.set(unitFullName, UnitConversion.identity);\r\n }\r\n return innermapStore;\r\n }\r\n}\r\n"]}
@@ -0,0 +1,78 @@
1
+ import { UnitConversionProps, UnitExtraData, UnitProps, UnitsProvider } from "@itwin/core-quantity";
2
+ import { ISchemaLocater } from "../Context";
3
+ /**
4
+ * Class used to find Units in SchemaContext by attributes such as Phenomenon and DisplayLabel.
5
+ * @beta
6
+ */
7
+ export declare class SchemaUnitProvider implements UnitsProvider {
8
+ private _unitExtraData;
9
+ private _unitConverter;
10
+ private _context;
11
+ /**
12
+ *
13
+ * @param contextOrLocater The SchemaContext or a different ISchemaLocater implementation used to retrieve the schema. The SchemaContext
14
+ * class implements the ISchemaLocater interface. If the provided locater is not a SchemaContext instance a new SchemaContext will be
15
+ * created and the locater will be added.
16
+ * @param _unitExtraData Additional data like alternate display label not found in Units Schema to match with Units; Defaults to empty array.
17
+ */
18
+ constructor(contextOrLocater: ISchemaLocater, _unitExtraData?: UnitExtraData[]);
19
+ /**
20
+ * Find unit in a schema that has unitName.
21
+ * @param unitName Full name of unit.
22
+ * @returns UnitProps interface from @itwin/core-quantity whose name matches unitName.
23
+ */
24
+ findUnitByName(unitName: string): Promise<UnitProps>;
25
+ /**
26
+ * Find all units in context that belongs to given phenomenon.
27
+ * @param phenomenon Full name of phenomenon.
28
+ * @returns Array of UnitProps (from @itwin/core-quantity) interface objects whose name matches phenomenon param.
29
+ */
30
+ getUnitsByFamily(phenomenon: string): Promise<Array<UnitProps>>;
31
+ /**
32
+ * Find alternate display labels associated with unitName, if any.
33
+ * @param unitName Full name of Unit.
34
+ */
35
+ getAlternateDisplayLabels(unitName: string): Array<string>;
36
+ /**
37
+ * Finds Unit by unitLabel, which could be a display label in the schema or alternate an display label defined in
38
+ * this._unitExtraData. If there are duplicates of the same display label in the context or teh same alternate display
39
+ * labels, specify schemaName, phenomenon, or unitSystem to get a specific unit.
40
+ *
41
+ * @param unitLabel Display label or alternate display label to query unit by.
42
+ * @param schemaName Ensure Unit with unitLabel belongs to Schema with schemaName.
43
+ * @param phenomenon Full name of phenomenon that Unit belongs to.
44
+ * @param unitSystem Full name of unitSystem that Unit belongs to.
45
+ * @returns The UnitProps interface from the @itwin/core-quantity package.
46
+ */
47
+ findUnit(unitLabel: string, schemaName?: string, phenomenon?: string, unitSystem?: string): Promise<UnitProps>;
48
+ /**
49
+ * Gets the @itwin/core-quantity UnitConversionProps for the given fromUnit and toUnit.
50
+ * @param fromUnit The UnitProps of the 'from' unit.
51
+ * @param toUnit The UnitProps of the 'to' unit.
52
+ * @returns The UnitConversionProps interface from the @itwin/core-quantity package.
53
+ */
54
+ getConversion(fromUnit: UnitProps, toUnit: UnitProps): Promise<UnitConversionProps>;
55
+ /**
56
+ * Find unit in a schema that has unitName.
57
+ * @param unitName Full name of unit.
58
+ * @returns Unit whose full name matches unitName.
59
+ */
60
+ private findECUnitByName;
61
+ /**
62
+ * Gets the @itwin/core UnitProps for the given Unit.
63
+ * @param unit The Unit to convert.
64
+ * @returns UnitProps interface from @itwin/core.
65
+ */
66
+ private getUnitsProps;
67
+ /**
68
+ * Finds Unit by displayLabel and that it belongs to schemaName, phenomenon, and unitSystem if defined.
69
+ * @internal
70
+ */
71
+ private findUnitByDisplayLabel;
72
+ /**
73
+ * Finds Unit by altDisplayLabel and that it belongs to schemaName, phenomenon, and unitSystem if defined.
74
+ * @internal
75
+ */
76
+ private findUnitByAltDisplayLabel;
77
+ }
78
+ //# sourceMappingURL=SchemaUnitProvider.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"SchemaUnitProvider.d.ts","sourceRoot":"","sources":["../../../src/UnitProvider/SchemaUnitProvider.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,mBAAmB,EAAE,aAAa,EAAE,SAAS,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC;AACpG,OAAO,EAAE,cAAc,EAAiB,MAAM,YAAY,CAAC;AAO3D;;;GAGG;AACH,qBAAa,kBAAmB,YAAW,aAAa;IAWR,OAAO,CAAC,cAAc;IAVpE,OAAO,CAAC,cAAc,CAAgB;IACtC,OAAO,CAAC,QAAQ,CAAgB;IAEhC;;;;;;OAMG;gBACS,gBAAgB,EAAE,cAAc,EAAU,cAAc,GAAE,aAAa,EAAO;IAU1F;;;;OAIG;IACU,cAAc,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,SAAS,CAAC;IAKjE;;;;OAIG;IACU,gBAAgB,CAAC,UAAU,EAAE,MAAM,GAAG,OAAO,CAAC,KAAK,CAAC,SAAS,CAAC,CAAC;IA0C5E;;;OAGG;IACI,yBAAyB,CAAC,QAAQ,EAAE,MAAM,GAAG,KAAK,CAAC,MAAM,CAAC;IAWjE;;;;;;;;;;OAUG;IACU,QAAQ,CAAC,SAAS,EAAE,MAAM,EAAE,UAAU,CAAC,EAAE,MAAM,EAAE,UAAU,CAAC,EAAE,MAAM,EAAE,UAAU,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,SAAS,CAAC;IAuB3H;;;;;OAKG;IACU,aAAa,CAAC,QAAQ,EAAE,SAAS,EAAE,MAAM,EAAE,SAAS,GAAG,OAAO,CAAC,mBAAmB,CAAC;IAQhG;;;;OAIG;YACW,gBAAgB;IA2B9B;;;;OAIG;IACH,OAAO,CAAC,aAAa;IAUrB;;;OAGG;YACW,sBAAsB;IAoBpC;;;OAGG;YACW,yBAAyB;CAoBxC"}