UNPKG

@itwin/ecschema-metadata

Version:

ECObjects core concepts in typescript

179 lines • 8.27 kB
"use strict"; /*--------------------------------------------------------------------------------------------- * Copyright (c) Bentley Systems, Incorporated. All rights reserved. * See LICENSE.md in the project root for license terms and full copyright notice. *--------------------------------------------------------------------------------------------*/ /** @packageDocumentation * @module Metadata */ Object.defineProperty(exports, "__esModule", { value: true }); exports.MutableUnit = exports.Unit = void 0; const DelayedPromise_1 = require("../DelayedPromise"); const XmlSerializationUtils_1 = require("../Deserialization/XmlSerializationUtils"); const ECObjects_1 = require("../ECObjects"); const Exception_1 = require("../Exception"); const Phenomenon_1 = require("./Phenomenon"); const SchemaItem_1 = require("./SchemaItem"); const UnitSystem_1 = require("./UnitSystem"); /** * An abstract class that adds the ability to define Units and everything that goes with them, within an ECSchema as a * first-class concept is to allow the iModel to not be dependent on any hard-coded Units * @public @preview */ class Unit extends SchemaItem_1.SchemaItem { schemaItemType = Unit.schemaItemType; /** @internal */ static get schemaItemType() { return ECObjects_1.SchemaItemType.Unit; } _phenomenon; _unitSystem; _definition; _numerator; _denominator; _offset; /** @internal */ constructor(schema, name) { super(schema, name); this._definition = ""; } get phenomenon() { return this._phenomenon; } get unitSystem() { return this._unitSystem; } get definition() { return this._definition; } get numerator() { return this._numerator ?? 1.0; } get offset() { return this._offset ?? 0.0; } get denominator() { return this._denominator ?? 1.0; } get hasNumerator() { return (this._numerator !== undefined); } get hasOffset() { return (this._offset !== undefined); } get hasDenominator() { return (this._denominator !== undefined); } /** * Returns true if a conversion can be calculated between the input units * @alpha */ static async areCompatible(unitA, unitB) { const unitAPhenomenon = await unitA.phenomenon; const unitBPhenomenon = await unitB.phenomenon; if (!unitAPhenomenon || !unitBPhenomenon || !unitAPhenomenon.key.matches(unitBPhenomenon.key)) return false; return true; } /** * Type guard to check if the SchemaItem is of type Unit. * @param item The SchemaItem to check. * @returns True if the item is a Unit, false otherwise. */ static isUnit(item) { if (item && item.schemaItemType === ECObjects_1.SchemaItemType.Unit) return true; return false; } /** * Save this Unit's properties to an object for serializing to JSON. * @param standalone Serialization includes only this object (as opposed to the full schema). * @param includeSchemaVersion Include the Schema's version information in the serialized object. */ toJSON(standalone = false, includeSchemaVersion = false) { const schemaJson = super.toJSON(standalone, includeSchemaVersion); schemaJson.phenomenon = this.phenomenon.fullName; schemaJson.unitSystem = this.unitSystem.fullName; schemaJson.definition = this.definition; if (this.hasNumerator) schemaJson.numerator = this.numerator; if (this.hasDenominator) schemaJson.denominator = this.denominator; if (this.hasOffset) schemaJson.offset = this.offset; return schemaJson; } /** @internal */ async toXml(schemaXml) { const itemElement = await super.toXml(schemaXml); const phenomenon = await this.phenomenon; if (undefined !== phenomenon) { const phenomenonName = XmlSerializationUtils_1.XmlSerializationUtils.createXmlTypedName(this.schema, phenomenon.schema, phenomenon.name); itemElement.setAttribute("phenomenon", phenomenonName); } const unitSystem = await this.unitSystem; if (undefined !== unitSystem) { const unitSystemName = XmlSerializationUtils_1.XmlSerializationUtils.createXmlTypedName(this.schema, unitSystem.schema, unitSystem.name); itemElement.setAttribute("unitSystem", unitSystemName); } itemElement.setAttribute("definition", this.definition); if (this.hasNumerator) itemElement.setAttribute("numerator", this.numerator.toString()); if (this.hasDenominator) itemElement.setAttribute("denominator", this.denominator.toString()); if (this.hasOffset) itemElement.setAttribute("offset", this.offset.toString()); return itemElement; } fromJSONSync(unitProps) { super.fromJSONSync(unitProps); const phenomenonSchemaItemKey = this.schema.getSchemaItemKey(unitProps.phenomenon); if (!phenomenonSchemaItemKey) throw new Exception_1.ECSchemaError(Exception_1.ECSchemaStatus.InvalidECJson, `Unable to locate the phenomenon ${unitProps.phenomenon}.`); this._phenomenon = new DelayedPromise_1.DelayedPromiseWithProps(phenomenonSchemaItemKey, async () => { const phenom = await this.schema.lookupItem(phenomenonSchemaItemKey, Phenomenon_1.Phenomenon); if (undefined === phenom) throw new Exception_1.ECSchemaError(Exception_1.ECSchemaStatus.InvalidECJson, `Unable to locate the phenomenon ${unitProps.phenomenon}.`); return phenom; }); const unitSystemSchemaItemKey = this.schema.getSchemaItemKey(unitProps.unitSystem); if (!unitSystemSchemaItemKey) throw new Exception_1.ECSchemaError(Exception_1.ECSchemaStatus.InvalidECJson, `Unable to locate the unitSystem ${unitProps.unitSystem}.`); this._unitSystem = new DelayedPromise_1.DelayedPromiseWithProps(unitSystemSchemaItemKey, async () => { const unitSystem = await this.schema.lookupItem(unitSystemSchemaItemKey, UnitSystem_1.UnitSystem); if (undefined === unitSystem) throw new Exception_1.ECSchemaError(Exception_1.ECSchemaStatus.InvalidECJson, `Unable to locate the unitSystem ${unitProps.unitSystem}.`); return unitSystem; }); if (this._definition !== "" && unitProps.definition.toLowerCase() !== this._definition.toLowerCase()) throw new Exception_1.ECSchemaError(Exception_1.ECSchemaStatus.InvalidECJson, `The Unit ${this.name} has an invalid 'definition' attribute.`); else if (this._definition === "") this._definition = unitProps.definition; if (undefined !== unitProps.numerator) { if (unitProps.numerator !== this._numerator) this._numerator = unitProps.numerator; } if (undefined !== unitProps.denominator) { if (unitProps.denominator !== this._denominator) this._denominator = unitProps.denominator; } if (undefined !== unitProps.offset) { if (unitProps.offset !== this._offset) this._offset = unitProps.offset; } } async fromJSON(unitProps) { this.fromJSONSync(unitProps); } /** @internal */ async setPhenomenon(phenomenon) { this._phenomenon = phenomenon; } /** @internal */ async setUnitSystem(unitSystem) { this._unitSystem = unitSystem; } /** @internal */ async setDefinition(definition) { this._definition = definition; } /** * Type assertion to check if the SchemaItem is of type Unit. * @param item The SchemaItem to check. * @returns The item cast to Unit if it is a Unit, undefined otherwise. * @internal */ static assertIsUnit(item) { if (!this.isUnit(item)) throw new Exception_1.ECSchemaError(Exception_1.ECSchemaStatus.InvalidSchemaItemType, `Expected '${ECObjects_1.SchemaItemType.Unit}' (Unit)`); } } exports.Unit = Unit; /** * @internal * An abstract class used for schema editing. */ class MutableUnit extends Unit { } exports.MutableUnit = MutableUnit; //# sourceMappingURL=Unit.js.map