jalhyd
Version:
JaLHyd, a Javascript Library for Hydraulics
208 lines • 7.76 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.LinkedValue = void 0;
const internal_modules_1 = require("./internal_modules");
const internal_modules_2 = require("./internal_modules");
const internal_modules_3 = require("./internal_modules");
class LinkedValue {
constructor(nub, element, symbol, meta = {}, real) {
this._nub = nub;
this._element = element;
this._symbol = symbol;
this.meta = meta;
this._realNub = real;
}
get nub() { return this._nub; }
get element() { return this._element; }
get symbol() { return this._symbol; }
/**
* Returns true if targetted value is a ParamDefinition (in CALC mode or not),
* and not an extra result
*/
isParameter() {
return (this.element instanceof internal_modules_1.ParamDefinition);
}
/**
* Returns true if targetted value is a ParamDefinition in CALC mode
* (might not have any value yet)
*/
isResult() {
return (this.isParameter()
&& this.element.valueMode === internal_modules_2.ParamValueMode.CALCUL);
}
/**
* Returns true if targetted value is a ParamDefinition in CALC mode,
* or in LINK mode targetting a result (might not have any value yet)
*/
isCalculated() {
return (this.isResult()
|| this.isExtraResult()
|| (this.isParameter()
&& this.element.valueMode === internal_modules_2.ParamValueMode.LINK
&& this.element.referencedValue.isCalculated() // recursion
));
}
/**
* Returns true if targetted value is an extra result
* (might not have any value yet)
*/
isExtraResult() {
return (this.nub !== undefined
&& this.symbol !== undefined
&& !this.isParameter());
}
/**
* @returns true if given nub/symbol combination is targeted by "this"
*/
isTargetOf(nub, symbol) {
var _a;
if (this._symbol === symbol && (this._nub.uid === nub.uid || ((_a = this._realNub) === null || _a === void 0 ? void 0 : _a.uid) === nub.uid)) {
return true;
}
return false;
}
/**
* Returns true if v and the current objects have the same :
* - Nub UID
* - symbol
* - value type (Parameter / Result)
* (ignores metadata)
* @param v
*/
equals(v) {
return (v
&& (v.nub.uid === this.nub.uid)
&& (v.symbol === this.symbol)
&& ((v.element === undefined && this.element === undefined)
|| (v.element !== undefined
&& this.element !== undefined
&& v.element.constructor.name === this.element.constructor.name)));
}
/**
* Returs the ParamValues for the linked Parameter if any, or a
* fake ParamValues object if the targetted element is a Result
* or extra result.
* If target is a result and triggerChainComputation is true,
* triggers a chain computation to obtain the result
*/
getParamValues(triggerChainComputation = false) {
let ret;
// trigger computation ?
if ((this.isExtraResult() || this.isResult())
&& triggerChainComputation) {
this.nub.CalcSerie();
}
if (this.isParameter()) {
const targetParam = this.element;
// target might be in CALC mode
if (targetParam.valueMode === internal_modules_2.ParamValueMode.CALCUL) {
// if already computed, expose handmade fake param values for iterability
if (this.nub.result) {
if (!this._paramValues) {
this._paramValues = new internal_modules_3.ParamValues();
// populate
if (targetParam.hasMultipleValues) {
const multipleRes = this.nub.result.getCalculatedValues();
this._paramValues.setValues(multipleRes);
}
else {
const singleRes = this.nub.result.vCalc;
this._paramValues.setValues(singleRes);
}
}
// return local cache
ret = this._paramValues;
}
else {
throw new Error("LinkedValue.getParamValues() - result not available");
}
}
else {
// simple proxy to target parameter values; if target parameter is
// also a LINK, ParamDefinition.paramValues will recursively call
// LinkedValue.getParamValues() (this method)
ret = targetParam.paramValues;
}
}
else if (this.isExtraResult()) {
// is result available ?
if (this.nub.result) {
// expose handmade fake param values for iterability
if (!this._paramValues) {
this._paramValues = new internal_modules_3.ParamValues();
// populate
if (this.nub.resultHasMultipleValues()) {
const multipleExtraRes = this.getExtraResults(this.symbol);
this._paramValues.setValues(multipleExtraRes);
}
else {
const singleExtraRes = this.nub.result.getValue(this.symbol);
this._paramValues.setValues(singleExtraRes);
}
}
// return local cache
ret = this._paramValues;
}
else {
throw new Error("LinkedValue.getParamValues() - extra result not available");
}
}
else {
throw new Error("LinkedValue.getParamValues() - cannot determine nature of target");
}
return ret;
}
/**
* Returns the single value of this.paramValues; triggers a chain computation
* if required to obtain ParamValues
*/
getValue(triggerChainComputation = false) {
const pv = this.getParamValues(triggerChainComputation);
return pv.singleValue;
}
/**
* Returns true if
* - a parameter is targetted and it has multiple values
* - a result / extra result is targetted and it has more than 1 value
*/
hasMultipleValues() {
if (this.isParameter()) { // possibly in CALC mode, but ParamDefinition.hasMultipleValues takes care of it
return this.element.hasMultipleValues;
}
else if (this.isResult() || this.isExtraResult()) {
// guess if parent Nub has any variating parameter (linked or not)
return this.nub.resultHasMultipleValues();
}
return false;
}
/**
* Returns true if target value is available
*/
isDefined() {
if (this.isExtraResult()) {
return (this.nub.result !== undefined);
}
else {
return this.element.isDefined;
}
}
/**
* When invalidating a Nub's result, parameters pointing to this result should
* invalidate their proxy paramValues too
*/
invalidateParamValues() {
this._paramValues = undefined;
}
getExtraResults(symbol) {
const res = [];
for (const re of this.nub.result.resultElements) {
const er = re.getValue(symbol);
if (er !== undefined) {
res.push(er);
}
}
return res;
}
}
exports.LinkedValue = LinkedValue;
//# sourceMappingURL=linked-value.js.map