jalhyd
Version:
JaLHyd, a Javascript Library for Hydraulics
220 lines • 6.71 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ResultElement = void 0;
const internal_modules_1 = require("../internal_modules");
const internal_modules_2 = require("../internal_modules");
/**
* Calculation result for one iteration.
* May include value of the Nub's calculated parameter (if any), and/or other
* values (previously known as extraResults)
*/
class ResultElement {
/**
* @param v value of calculated parameter, or key-values map, or (usually error) Message
*/
constructor(v) {
this.log = new internal_modules_1.cLog();
this._values = {};
this._vCalcSymbol = ""; // default retrocompatible pseudo-symbol for cases like `new ResultElement(42);`
// detect first argument
if (typeof v === "number") {
this.vCalc = v;
}
else if (v instanceof internal_modules_2.Message) {
this.addMessage(v);
}
else if (v !== undefined) {
// assuming key-value map
this._values = v;
// assuming 1st value is vCalc
if (Object.keys(v).length > 0) {
this._vCalcSymbol = Object.keys(v)[0];
}
}
}
get parent() {
return this._parent;
}
set parent(r) {
this._parent = r;
this.log.parent = this.parent;
}
/**
* @returns value of the calculated parameter (main result); might be undefined,
* for ex. with SectionParametree
*/
get vCalc() {
return this._values[this._vCalcSymbol];
}
/**
* sets value of the calculated parameter (main result)
*/
set vCalc(r) {
this.setVCalc(r);
}
get vCalcSymbol() {
return this._vCalcSymbol;
}
setVCalc(r, keepSymbol = false) {
if (!keepSymbol && this.parent && this.parent.symbol) {
this._vCalcSymbol = this.parent.symbol;
}
this._values[this._vCalcSymbol] = r;
}
/**
* Hack to update the key of vCalc, when Result.symbol is updated
* after Equation() returned a ResultElement
*/
updateVCalcSymbol(s) {
if (!this.hasValues) {
throw new Error("updateVCalcSymbol() : values map is empty");
}
const tmp = this.vCalc;
delete this._values[this._vCalcSymbol];
this._vCalcSymbol = s;
this.setVCalc(tmp, true);
}
/**
* Sets vCalcSymbol back to the parent Nub's calculated parameter symbol
*/
resetVCalcSymbol() {
this.updateVCalcSymbol(this.parent.sourceNub.calculatedParam.symbol);
}
/**
* Adds given values map to the local values map; in case of key conflict,
* precedence goes to the given value
*/
mergeValues(values) {
this._values = Object.assign(Object.assign({}, this._values), values);
}
/**
* Returns an array of all the keys in the local values map,
* where vCalcSymbol is always in first position (used by
* GUI to iterate on displayable results)
*/
get keys() {
const keys = Object.keys(this._values);
// make sure vCalc symbol is always first
if (this._vCalcSymbol) { // sometimes empty (ex: SectionParametree)
const index = keys.indexOf(this._vCalcSymbol);
if (index > -1) {
keys.splice(index, 1);
}
keys.unshift(this._vCalcSymbol);
}
return keys;
}
/** Returns the whole key-value map */
get values() {
return this._values;
}
/**
* @returns the result value associated to the given symbol, or undefined if the given
* symbol was not found in the local values map
*/
getValue(symbol) {
return this._values[symbol];
}
get firstValue() {
return this._values[Object.keys(this._values)[0]];
}
count() {
return Object.keys(this._values).length;
}
/**
* @returns true if local values map is not empty, and local log has no error-level
* message (log still might have non-error level messages)
*/
get ok() {
return this.hasValues() && !this.hasErrorMessages();
}
/**
* @returns true if local values map has at least one key-value pair
*/
hasValues() {
return Object.keys(this._values).length > 0;
}
/**
* Adds a key-value pair to the values map
*/
addExtraResult(name, value) {
this._values[name] = value;
}
/**
* Returns a copy of the local key-values map, without vCalc (only "extra" results)
*/
get extraResults() {
const er = JSON.parse(JSON.stringify(this._values)); // clone
// remove vCalc
delete er[this._vCalcSymbol];
return er;
}
/**
* Removes all values that are not vCalc
*/
removeExtraResults() {
for (const k in this._values) {
if (k !== this._vCalcSymbol) {
delete this._values[k];
}
}
}
/**
* Removes all values
*/
removeValues() {
this._values = {};
}
toString() {
if (this.vCalc !== undefined) {
return String(this.vCalc);
}
return JSON.stringify(this);
}
// -------------- log related methods
/** adds a message to the global log */
addMessage(m) {
this.log.add(m);
}
/** @returns true if current log has at least one message */
hasLog() {
return this.log.messages.length > 0;
}
/**
* @returns true if at least one of the log messages has an error level,
* i.e. if calculation has failed
*/
hasErrorMessages() {
for (const m of this.log.messages) {
if (m.getSeverity() === internal_modules_2.MessageSeverity.ERROR) {
return true;
}
}
return false;
}
/** @returns true if at least one of the log messages has a warning level */
hasWarningMessages() {
for (const m of this.log.messages) {
if (m.getSeverity() === internal_modules_2.MessageSeverity.WARNING) {
return true;
}
}
return false;
}
hasMessage(code) {
for (const m of this.log.messages) {
if (m.code === code) {
return true;
}
}
return false;
}
/**
* compute error, warning, info count on all log messages
*/
logStats(stats) {
return internal_modules_1.cLog.messagesStats(this.log.messages, stats);
}
}
exports.ResultElement = ResultElement;
//# sourceMappingURL=resultelement.js.map