ifc-expressions
Version:
Parsing and evaluation of IFC expressions
141 lines (140 loc) • 4.61 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.LogicalValue = void 0;
const Types_js_1 = require("../type/Types.js");
const BooleanValue_js_1 = require("./BooleanValue.js");
class LogicalValue {
constructor(value) {
this.logicalValue = value;
}
static true() {
return this.LOGICAL_VALUE_TRUE;
}
static false() {
return this.LOGICAL_VALUE_FALSE;
}
static unknown() {
return this.LOGICAL_VALUE_UNKNOWN;
}
static of(value) {
switch (value) {
case this.UNKNOWN_VALUE:
return this.LOGICAL_VALUE_UNKNOWN;
case false:
return this.LOGICAL_VALUE_FALSE;
case true:
return this.LOGICAL_VALUE_TRUE;
}
throw new Error(`Cannot get logical value of ${value}`);
}
getValue() {
return this.logicalValue;
}
isTrue() {
return this.logicalValue === true;
}
isFalse() {
return this.logicalValue === false;
}
isUnknown() {
return this.logicalValue === "UNKNOWN";
}
static isUnknown(val) {
return val === "UNKNOWN";
}
static isLogical(val) {
return typeof val === "boolean" || this.isUnknown(val);
}
and(other) {
const otherValue = other.getValue();
if (this.logicalValue === false || otherValue === false) {
return LogicalValue.false();
}
if (this.isUnknown() || otherValue === LogicalValue.UNKNOWN_VALUE) {
return LogicalValue.unknown();
}
return LogicalValue.true();
}
or(other) {
const otherValue = other.getValue();
if (this.logicalValue === true || otherValue === true) {
return LogicalValue.true();
}
if (this.isUnknown() || otherValue === LogicalValue.UNKNOWN_VALUE) {
return LogicalValue.unknown();
}
return LogicalValue.false();
}
xor(other) {
const otherValue = other.getValue();
if (this.isUnknown() || otherValue === LogicalValue.UNKNOWN_VALUE) {
return LogicalValue.unknown();
}
return LogicalValue.of(this.logicalValue !== other.getValue());
}
implies(other) {
const otherValue = other.getValue();
if (otherValue === true) {
return LogicalValue.true();
}
if (this.isUnknown()) {
return LogicalValue.unknown();
}
if (this.isFalse()) {
return LogicalValue.true();
}
return LogicalValue.of(otherValue);
}
not() {
switch (this.logicalValue) {
case LogicalValue.UNKNOWN_VALUE:
return LogicalValue.LOGICAL_VALUE_UNKNOWN;
case true:
return LogicalValue.LOGICAL_VALUE_FALSE;
case false:
return LogicalValue.LOGICAL_VALUE_TRUE;
}
throw new Error(`Cannot invert logical value ${this.logicalValue}`);
}
equals(other) {
if (LogicalValue.isLogicalValueType(other)) {
return this.logicalValue === other.logicalValue;
}
else if (BooleanValue_js_1.BooleanValue.isBooleanValueType(other)) {
return this.logicalValue === other.booleanValue;
}
return false;
}
getType() {
return Types_js_1.Type.LOGICAL;
}
compareTo(other) {
switch (this.logicalValue) {
case true:
return other.isTrue() ? 0 : 1;
case false:
return other.isTrue() ? -1 : other.logicalValue === false ? 0 : 1;
case "UNKNOWN":
return other.isUnknown() ? 0 : -1;
}
throw new Error(`Unexpected logical value comparison: ${this.logicalValue} vs ${other.logicalValue}`);
}
toString() {
return this.logicalValue === LogicalValue.UNKNOWN_VALUE
? LogicalValue.UNKNOWN_VALUE
: this.logicalValue
? "TRUE"
: "FALSE";
}
static isLogicalValueType(arg) {
return ((typeof arg.logicalValue !== "undefined" &&
typeof arg.logicalValue === "boolean") ||
arg.logicalValue === LogicalValue.UNKNOWN_VALUE);
}
}
exports.LogicalValue = LogicalValue;
LogicalValue.UNKNOWN_VALUE = "UNKNOWN";
LogicalValue.LOGICAL_VALUE_TRUE = new LogicalValue(true);
LogicalValue.LOGICAL_VALUE_FALSE = new LogicalValue(false);
LogicalValue.LOGICAL_VALUE_UNKNOWN = new LogicalValue(LogicalValue.UNKNOWN_VALUE);
//# sourceMappingURL=LogicalValue.js.map