From a4b5c3a49f776cb5206c31453920ab16ec787ea7 Mon Sep 17 00:00:00 2001 From: "francois.grand" <francois.grand@irstea.fr> Date: Wed, 6 Jun 2018 14:11:56 +0200 Subject: [PATCH] =?UTF-8?q?=20#45=20import=20de=20r=C3=A9sultat=20-=20clas?= =?UTF-8?q?se=20ParamValues=20:=20ajout=20de=20la=20notion=20de=20valeur?= =?UTF-8?q?=20courante=20(=5FcurrentValue)=20ind=C3=A9pendante=20du=20mode?= =?UTF-8?q?=20de=20valeur=20pour=20distinguer=20la=20valeur=20en=20mode=20?= =?UTF-8?q?SINGLE=20et=20la=20valeur=20actuelle=20quand=20on=20it=C3=A8re?= =?UTF-8?q?=20sur=20les=20valeurs=20possibles?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- spec/mock_jasmine.ts | 2 +- spec/value_ref/value_ref.spec.ts | 16 ++++++---------- src/param/param-base.ts | 4 +--- src/param/param-definition.ts | 22 +++++++++++++++++++++- src/param/param-values.ts | 26 ++++++++++++++++++++++++-- 5 files changed, 53 insertions(+), 17 deletions(-) diff --git a/spec/mock_jasmine.ts b/spec/mock_jasmine.ts index e96e1e8c..cfb6a397 100644 --- a/spec/mock_jasmine.ts +++ b/spec/mock_jasmine.ts @@ -83,7 +83,7 @@ class Expect { public toEqual(expected: any) { const res = this.actual === expected; if (!res) { - console.warn("Test 'to be equal to' failed"); + console.warn(`Test ${this.actual} 'to be equal to' ${expected} failed`); } return res; } diff --git a/spec/value_ref/value_ref.spec.ts b/spec/value_ref/value_ref.spec.ts index c9ba1ddd..82ed531e 100644 --- a/spec/value_ref/value_ref.spec.ts +++ b/spec/value_ref/value_ref.spec.ts @@ -4,7 +4,7 @@ * Pour exécuter ce code dans le débugger. * Faire de même avec le fichier test_func.ts */ -import { describe, expect, it, xdescribe, xit } from "../mock_jasmine"; +// import { describe, expect, it, xdescribe, xit } from "../mock_jasmine"; import { NubTest, NubTestParams } from "../nubtest"; import { precDigits } from "../test_config"; @@ -121,7 +121,7 @@ describe("référence d'un paramètre à un autre : ", () => { expect(nub1.Calc("C").vCalc).toBeCloseTo(3, precDigits); expect(nub2.Calc("A").vCalc).toBeCloseTo(0, precDigits); - //expect(nub2.Calc("B").vCalc).toBeCloseTo(3, precDigits); // échoue car l'écriture du paramètre esclave n'affecte pas la valeur maître + //expect(nub2.Calc("B").vCalc).toBeCloseTo(3, precDigits); // échoue car l'écriture du paramètre esclave (pendant la dichotomie) n'affecte pas la valeur maître; la relecture du paramètre esclave ne reflète pas la valeur écrite expect(nub2.Calc("C").vCalc).toBeCloseTo(3, precDigits); }); @@ -154,12 +154,12 @@ describe("référence d'un paramètre à un autre : ", () => { createEnv(); - prm1.C.v = 10; // valeur maître bidon + prm1.B.v = 5; prm1.C.paramValues.valueMode = ParamValueMode.CALCUL; prm2.A.v = 0; // valeur esclave (doit être masquée par la valeur maître, cad prm1.C, normalement 3) prm2.A.defineReference(nub1, "C"); - expect(prm2.A.v).toBeCloseTo(3, precDigits); + expect(prm2.A.v).toBeCloseTo(6, precDigits); }); it('test 2', () => { @@ -211,14 +211,10 @@ describe("référence d'un paramètre à un autre : ", () => { createEnv(); - const min = 1; - const max = 5; - const step = 1; - - const input = [1, 2, 3, 4, 5]; + const input = [2, 3, 4, 5, 6]; const pv = prm1.A.paramValues; pv.setValues(input); - prm2.A.v = 0; // valeur esclave bidon, doit être masquée par la valeur maître (cad prm1.A, normalement [1,2,3,4,5]) + prm2.A.v = 0; // valeur esclave bidon, doit être masquée par la valeur maître (cad prm1.A, normalement [2,3,4,5,6]) prm2.A.defineReference(nub1, "A"); const r: Result = nub2.CalcSerie(0.001, 0.1, "C"); diff --git a/src/param/param-base.ts b/src/param/param-base.ts index 7e21c631..6712c7ff 100644 --- a/src/param/param-base.ts +++ b/src/param/param-base.ts @@ -35,8 +35,6 @@ export class BaseParam extends JalhydObject implements IObjectReference { this._paramValues = new ParamValues(); this._paramValues.setSingleValue(val); - if (val !== undefined) - this._paramValues.valueMode = ParamValueMode.SINGLE; if (d instanceof ParamDomain) { this._domain = d; @@ -80,7 +78,7 @@ export class BaseParam extends JalhydObject implements IObjectReference { throw e; } - return this._paramValues.singleValue; + return this._paramValues.currentValue; } public setValue(val: number) { diff --git a/src/param/param-definition.ts b/src/param/param-definition.ts index 07088060..ef4f2f1b 100644 --- a/src/param/param-definition.ts +++ b/src/param/param-definition.ts @@ -45,7 +45,27 @@ export class ParamDefinition extends BaseParam { } get v(): number { - return super.getValue(); + if (this.isReferenceDefined) + switch (this.referencedParamValues.valueMode) { + case ParamValueMode.CALCUL: + const r = this.referencedResult; + if (r.nbResultElements == 1) + return r.resultElement.vCalc; + throw new Error(`il n'y a pas exactement un ResultElement dans le Result "${r.name}"`); + + default: + return this.referencedParamValues.currentValue; + } + else + switch (this.paramValues.valueMode) { + case ParamValueMode.SINGLE: + case ParamValueMode.LISTE: + case ParamValueMode.MINMAX: + return super.getValue(); + + default: + throw new Error(`mode de valeur ${ParamValueMode[this.paramValues.valueMode]} incorrect pour le paramètre`); + } } set v(val: number) { diff --git a/src/param/param-values.ts b/src/param/param-values.ts index 8486a6d4..9f6fb677 100644 --- a/src/param/param-values.ts +++ b/src/param/param-values.ts @@ -36,6 +36,11 @@ export class ParamValues implements IObjectReference { */ private _valueList: number[]; + /** + * valeur courante (éventuellement non définie) indépendemment du mode + */ + private _currentValue: DefinedNumber; + /** * itérateur courant */ @@ -48,6 +53,7 @@ export class ParamValues implements IObjectReference { constructor() { this._singleValue = new DefinedNumber(); + this._currentValue = new DefinedNumber(); this._valueRef = new ObjectReference(); } @@ -56,16 +62,19 @@ export class ParamValues implements IObjectReference { if (max == undefined) { this._valueMode = ParamValueMode.SINGLE; this._singleValue.value = o as number; + this._currentValue.value = o as number; } else { this._valueMode = ParamValueMode.MINMAX; this._minValue = o as number; this._maxValue = max; this._stepValue = step; + this._currentValue.undefine(); } } else if (Array.isArray(o)) { this._valueMode = ParamValueMode.LISTE; this._valueList = o; + this._currentValue.undefine(); } else throw new Error(`ParamValues.setValues() : appel invalide`); @@ -122,6 +131,18 @@ export class ParamValues implements IObjectReference { } } + /** + * valeur courante + */ + public get currentValue(): number { + if (this.isReferenceDefined) + return this._valueRef.referencedParamValues.currentValue; + return this._currentValue.value; + } + + /** + * valeur dans le mode SINGLE + */ public get singleValue(): number { if (this.isReferenceDefined) return this._valueRef.referencedParamValues.singleValue; @@ -135,6 +156,7 @@ export class ParamValues implements IObjectReference { public setSingleValue(v: number) { this._valueMode = ParamValueMode.SINGLE; this._singleValue.value = v; + this._currentValue.value = v; } public get isDefined() { @@ -253,8 +275,8 @@ export class ParamValues implements IObjectReference { * @return prochaine valeur à parcourir par l'itérateur courant */ public get next(): number { - this._singleValue.value = this._iterator.next().value; - return this._singleValue.value; + this._currentValue.value = this._iterator.next().value; + return this._currentValue.value; } // interface IObjectReference -- GitLab