From 17222955edb1379ee1ce523f0d516b57b4592f11 Mon Sep 17 00:00:00 2001
From: "francois.grand" <francois.grand@irstea.fr>
Date: Fri, 6 Apr 2018 16:42:06 +0200
Subject: [PATCH] =?UTF-8?q?=20#46=20ajout=20d'un=20it=C3=A9rateur=20de=20p?=
 =?UTF-8?q?aram=C3=A8tres=20pour=20simplifier=20le=20code=20et=20g=C3=A9re?=
 =?UTF-8?q?r=20les=20structures=20parall=C3=A8les?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 spec/iterator/param_equation.spec.ts |  64 ++++++++----
 src/compute-node.ts                  |  28 ++++--
 src/param/params-equation.ts         | 140 ++++++++++++++++-----------
 src/structure/parallel_structure.ts  |   9 ++
 4 files changed, 157 insertions(+), 84 deletions(-)

diff --git a/spec/iterator/param_equation.spec.ts b/spec/iterator/param_equation.spec.ts
index b8b881f0..8a182a96 100644
--- a/spec/iterator/param_equation.spec.ts
+++ b/spec/iterator/param_equation.spec.ts
@@ -1,29 +1,53 @@
-/// <reference path="../../node_modules/@types/jasmine/index.d.ts" />
+/**
+ * IMPORTANT !
+ * Décommenter temporairement la ligne suivante (import { } from "./mock_jasmine")
+ * 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 { ConduiteDistribParams, ConduiteDistrib } from "../../src/cond_distri";
+import { ParallelStructureParams } from "../../src/structure/parallel_structure_params";
+import { ParallelStructure } from "../../src/structure/parallel_structure";
+import { Structure } from "../../src/structure/structure";
+import { CreateStructure, StructureType, LoiDebit } from "../../src/structure/factory_structure";
+import { IParamDefinitionIterator } from "../../src/param/params-equation";
 
-describe("iterator ConduiteDistribParams : ", () => {
-    it("test 1", () => {
-        const cdp: ConduiteDistribParams = new ConduiteDistribParams(1, 2, 3, 4, 5)
+function checkParams(pdi: IParamDefinitionIterator, symbols: string[], values: number[]) {
+    let n = 0;
+    for (const p of pdi) {
+        expect(p.symbol === symbols[n]);
+        if (n < values.length)
+            expect(p.v === values[n]);
+        n++;
+    }
+    expect(n === symbols.length).toBeTruthy();
+}
 
-        let n = 0;
-        for (let p of cdp) {
-            n++;
-            expect(p.v == n);
-        }
-        expect(n === 5);
+describe("iterator  : ", () => {
+    it("ConduiteDistribParams", () => {
+        const peq: ConduiteDistribParams = new ConduiteDistribParams(1, 2, 3, 4, 5);
+        const symbs = ["Q", "D", "J", "Lg", "Nu"];
+        const vals = [1, 2, 3, 4, 5];
+        checkParams(peq.iterator, symbs, vals);
     });
 
-    it("test 2", () => {
-        const cdp: ConduiteDistribParams = new ConduiteDistribParams(1, 2, 3, 4, 5)
+    it("ParallelStructureParams 1", () => {
+        const peq: ParallelStructureParams = new ParallelStructureParams(1, 2, 3);
+        const symbs = ["Q", "Z1", "Z2"];
+        const vals = [1, 2, 3];
+        checkParams(peq.iterator, symbs, vals);
+    });
 
-        const symbs = ["Q", "D", "J", "Lg", "Nu"];
-        let n = 0;
-        for (const p of cdp.iterator) {
-            expect(p.v == n + 1);
-            expect(p.symbol == symbs[n]);
-            n++;
-        }
-        expect(n === 5).toBeTruthy();
+    it("ParallelStructureParams 2", () => {
+        const psp: ParallelStructureParams = new ParallelStructureParams(1, 2, 3);
+        const pst = new ParallelStructure(psp);
+
+        const st: Structure = CreateStructure(StructureType.SeuilRectangulaire, LoiDebit.Cem88d);
+        pst.addStructure(st);
+
+        const symbs = ["Q", "Z1", "Z2", "Cd", "h1", "h2", "L", "Q", "W", "Z1", "Z2", "ZDV"];
+        const vals = [1, 2, 3];
+        checkParams(pst.parameterIterator, symbs, vals);
     });
 });
diff --git a/src/compute-node.ts b/src/compute-node.ts
index cb63fe3b..a3b2c4c9 100644
--- a/src/compute-node.ts
+++ b/src/compute-node.ts
@@ -1,5 +1,5 @@
 import { Debug } from "./base";
-import { ParamsEquation } from "./param/params-equation";
+import { ParamsEquation, IParamDefinitionIterator } from "./param/params-equation";
 import { ParamDefinition } from "./param/param-definition";
 import { ParamValueMode } from "./param/param-values";
 
@@ -49,23 +49,33 @@ export abstract class ComputeNode extends Debug {
     }
 
     public getParameter(name: string): ParamDefinition {
-        return this._prms.getParameter(name);
+        for (const p of this.parameterIterator)
+            if (p.symbol === name)
+                return p;
+        return undefined;
     }
 
     public getFirstAnalyticalParameter(): ParamDefinition {
-        return this._prms.getFirstAnalyticalParameter();
+        for (const p of this.parameterIterator)
+            if (p.isAnalytical())
+                return p;
+        return undefined;
     }
 
     public initParametersValueMode(computedParam: ParamDefinition, variatedParam?: ParamDefinition, variatedMode?: ParamValueMode) {
-        for (const k in this._prms.map) {
-            if (k == computedParam.symbol)
-                this._prms.map[k].paramValues.valueMode = ParamValueMode.CALCUL;
-            else if (variatedParam && k == variatedParam.symbol)
-                this._prms.map[k].paramValues.valueMode = variatedMode;
+        for (const p of this.parameterIterator) {
+            if (p.symbol === computedParam.symbol)
+                p.paramValues.valueMode = ParamValueMode.CALCUL;
+            else if (variatedParam && p.symbol == variatedParam.symbol)
+                p.paramValues.valueMode = variatedMode;
             else
-                this._prms.map[k].paramValues.valueMode = ParamValueMode.SINGLE;
+                p.paramValues.valueMode = ParamValueMode.SINGLE;
         }
     }
 
+    public get parameterIterator(): IParamDefinitionIterator {
+        return this._prms.iterator;
+    }
+
     protected abstract setParametersCalculability(): void;
 }
diff --git a/src/param/params-equation.ts b/src/param/params-equation.ts
index 94ea6bcd..e84f4525 100644
--- a/src/param/params-equation.ts
+++ b/src/param/params-equation.ts
@@ -2,6 +2,79 @@ import { MapIterator } from "../util/iterator"
 
 import { ParamDefinition } from "./param-definition";
 
+export interface IParamDefinitionIterator extends IterableIterator<ParamDefinition> {
+}
+
+/**
+ * itérateur sur les paramètres d'une seule instance de ParamsEquation
+ */
+export class ParamDefinitionIterator implements IParamDefinitionIterator {
+    private _mapIterator: MapIterator<ParamDefinition>;
+
+    constructor(_params: ParamsEquation) {
+        this._mapIterator = new MapIterator(_params.map);
+    }
+
+    public next(): IteratorResult<ParamDefinition> {
+        return this._mapIterator.next();
+    }
+
+    public [Symbol.iterator](): IterableIterator<ParamDefinition> {
+        return this;
+    }
+}
+
+/**
+ * itérateur sur les paramètres d'un tableau de de ParamsEquation
+ */
+export class ParamsEquationArrayIterator implements IParamDefinitionIterator {
+    private _paramsEqs: ParamsEquation[];
+
+    private _index: number = 0;
+
+    private _currentMapIterator: MapIterator<ParamDefinition>;
+
+    constructor(p: ParamsEquation[]) {
+        this._paramsEqs = p;
+    }
+
+    private nextIterator() {
+        if (this._index < this._paramsEqs.length)
+            this._currentMapIterator = new MapIterator(this._paramsEqs[this._index++].map);
+        else
+            this._currentMapIterator = undefined;
+    }
+
+    private get done(): IteratorResult<ParamDefinition> {
+        return {
+            done: true,
+            value: undefined
+        };
+    }
+
+    public next(): IteratorResult<ParamDefinition> {
+        if (this._currentMapIterator == undefined)
+            this.nextIterator();
+
+        if (this._currentMapIterator) {
+            var res = this._currentMapIterator.next();
+            if (res.done) {
+                this.nextIterator();
+                if (this._currentMapIterator)
+                    res = this._currentMapIterator.next();
+            }
+        }
+        else
+            res = this.done;
+
+        return res;
+    }
+
+    public [Symbol.iterator](): IterableIterator<ParamDefinition> {
+        return this;
+    }
+}
+
 /**
  * liste des paramètres d'une équation
  */
@@ -24,67 +97,27 @@ export abstract class ParamsEquation implements Iterable<ParamDefinition> {
     }
 
     public hasParameter(name: string): boolean {
-        for (const ps in this._paramMap) {
-            if (this._paramMap.hasOwnProperty(ps)) {
-                const p: ParamDefinition = this._paramMap[ps];
-                if (p.symbol === name) {
-                    return true;
-                }
-            }
-        }
-
+        for (const p of this)
+            if (p.symbol === name)
+                return true;
         return false;
     }
 
-    public getParameter(name: string): ParamDefinition {
-        for (const ps in this._paramMap) {
-            if (this._paramMap.hasOwnProperty(ps)) {
-                const p: ParamDefinition = this._paramMap[ps];
-                if (p.symbol === name) {
-                    return p;
-                }
-            }
-        }
-
-        throw new Error("ParamsEquation.getParameter() : invalid parameter name " + name);
-    }
-
-    public getFirstAnalyticalParameter(): ParamDefinition {
-        for (const ps in this._paramMap) {
-            if (this._paramMap.hasOwnProperty(ps)) {
-                const p: ParamDefinition = this._paramMap[ps];
-                if (p.isAnalytical()) {
-                    return p;
-                }
-            }
-        }
-        return undefined;
-    }
-
     public get map(): { [key: string]: ParamDefinition } {
         return this._paramMap;
     }
 
     public resetParametersCalculability() {
-        for (const ps in this._paramMap) {
-            if (this._paramMap.hasOwnProperty(ps)) {
-                const p: ParamDefinition = this._paramMap[ps];
-                p.calculability = undefined
-            }
-        }
+        for (const p of this)
+            p.calculability = undefined
     }
 
     public checkParametersCalculability() {
         const res = [];
 
-        for (const ps in this._paramMap) {
-            if (this._paramMap.hasOwnProperty(ps)) {
-                const p: ParamDefinition = this._paramMap[ps];
-                if (p.calculability === undefined) {
-                    res.push(p.symbol);
-                }
-            }
-        }
+        for (const p of this)
+            if (p.calculability === undefined)
+                res.push(p.symbol);
 
         if (res.length > 0) {
             throw new Error("Calculability of parameter(s) " + res.toString() + " has not been defined");
@@ -95,8 +128,8 @@ export abstract class ParamsEquation implements Iterable<ParamDefinition> {
         return this.iterator;
     }
 
-    public get iterator() {
-        return new MapIterator(this._paramMap);
+    public get iterator(): IParamDefinitionIterator {
+        return new ParamDefinitionIterator(this);
     }
 
     protected addParamDefinition(p: ParamDefinition) {
@@ -106,10 +139,7 @@ export abstract class ParamsEquation implements Iterable<ParamDefinition> {
     }
 
     protected addParamDefinitions(ps: ParamsEquation) {
-        for (const pi in ps._paramMap) {
-            if (ps._paramMap.hasOwnProperty(pi)) {
-                this.addParamDefinition(ps._paramMap[pi]);
-            }
-        }
+        for (const p of ps)
+            this.addParamDefinition(p);
     }
 }
diff --git a/src/structure/parallel_structure.ts b/src/structure/parallel_structure.ts
index 8d2b46ab..40259b82 100644
--- a/src/structure/parallel_structure.ts
+++ b/src/structure/parallel_structure.ts
@@ -4,6 +4,7 @@ import { Message } from "../util/message";
 import { Result } from "../util/result";
 import { ParallelStructureParams } from "./parallel_structure_params";
 import { Structure } from "./structure";
+import { IParamDefinitionIterator, ParamsEquation, ParamsEquationArrayIterator } from "../param/params-equation";
 
 /**
  * Interface pour mémoriser le n° d'ouvrage et le paramètre à calculer
@@ -46,6 +47,14 @@ export class ParallelStructure extends Nub {
         this.updateStructuresH1H2();
     }
 
+    public get parameterIterator(): IParamDefinitionIterator {
+        const prms: ParamsEquation[] = [];
+        prms.push(this._prms);
+        for (const st of this.structures)
+            prms.push(st.parameters);
+        return new ParamsEquationArrayIterator(prms);
+    }
+
     /**
      * Ajout d'une structure en parallèle
      * @param structure La structure à rajouter
-- 
GitLab