ifc-expressions
Version:
Parsing and evaluation of IFC expressions
91 lines (90 loc) • 2.66 kB
JavaScript
import { Type } from "../type/Types.js";
import { LogicalValue } from "./LogicalValue.js";
export class BooleanValue {
constructor(value) {
this.booleanValue = value;
}
static of(value) {
if (value === true) {
return this.TRUE_VALUE;
}
else if (value === false) {
return this.FALSE_VALUE;
}
throw new Error(`Cannot get boolean value of ${value}`);
}
getValue() {
return this.booleanValue;
}
getType() {
return Type.BOOLEAN;
}
static true() {
return BooleanValue.of(true);
}
static false() {
return BooleanValue.of(false);
}
static isBoolean(val) {
return typeof val === "boolean";
}
isTrue() {
return this.booleanValue === true;
}
isFalse() {
return this.booleanValue === false;
}
compareTo(other) {
return this.booleanValue ? (other.booleanValue ? 0 : 1) : -1;
}
toString() {
return this.booleanValue ? "TRUE" : "FALSE";
}
static isBooleanValueType(arg) {
return typeof arg.booleanValue === "boolean";
}
and(other) {
if (other instanceof LogicalValue) {
return other.and(this);
}
return BooleanValue.of(this.booleanValue && other.booleanValue);
}
or(other) {
if (other instanceof LogicalValue) {
return other.or(this);
}
return BooleanValue.of(this.booleanValue || other.booleanValue);
}
xor(other) {
if (other instanceof LogicalValue) {
return other.xor(this);
}
return BooleanValue.of(this.booleanValue != other.booleanValue);
}
implies(other) {
if (other instanceof LogicalValue) {
if (other.isUnknown()) {
return (this.isFalse() ? LogicalValue.true() : LogicalValue.unknown());
}
else {
return LogicalValue.of(!this.booleanValue || other.getValue());
}
}
return BooleanValue.of(!this.booleanValue || other.booleanValue);
}
not() {
return this.isTrue() ? BooleanValue.false() : BooleanValue.true();
}
equals(other) {
if (LogicalValue.isLogicalValueType(other)) {
return this.booleanValue === other.getValue();
}
else if (BooleanValue.isBooleanValueType(other)) {
return this.booleanValue === other.booleanValue;
}
return false;
}
}
BooleanValue.TRUE_VALUE = new BooleanValue(true);
BooleanValue.FALSE_VALUE = new BooleanValue(false);
//# sourceMappingURL=BooleanValue.js.map