From 4a7333b505b034b3034acc713272323d36dfa959 Mon Sep 17 00:00:00 2001
From: "mathias.chouet" <mathias.chouet@irstea.fr>
Date: Tue, 15 Jan 2019 16:17:39 +0100
Subject: [PATCH] =?UTF-8?q?Une=20Structure=20contient=20maintenant=20une?=
 =?UTF-8?q?=20r=C3=A9f=C3=A9rence=20vers=20son=20parent=20ParallelStructur?=
 =?UTF-8?q?e?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 src/nub_factory.ts                  |  1 -
 src/structure/cloisons.ts           |  2 +-
 src/structure/dever.ts              |  2 +-
 src/structure/parallel_structure.ts | 69 +++++++++++++++++------------
 src/structure/structure.ts          | 10 +++--
 5 files changed, 50 insertions(+), 34 deletions(-)

diff --git a/src/nub_factory.ts b/src/nub_factory.ts
index c5df10bb..25923b66 100644
--- a/src/nub_factory.ts
+++ b/src/nub_factory.ts
@@ -119,7 +119,6 @@ export class NubFactory {
     /**
      * déplace un SessionNub associé à un nub Structure d'une position vers la fin de la liste de structures
      * @param sn SessionNub à remplacer
-     * @param params propriété du nouveau SessionNub
      */
     public moveStructureNubDown(sn: SessionNub) {
         const psn = this.findParallelStructureWithNub(sn.nub);
diff --git a/src/structure/cloisons.ts b/src/structure/cloisons.ts
index 6571566f..1137494c 100644
--- a/src/structure/cloisons.ts
+++ b/src/structure/cloisons.ts
@@ -82,7 +82,7 @@ export class Cloisons extends ParallelStructure {
     }
 
     private updateKiviZRAM() {
-        for (const structure of this.structures) {
+        for (const structure of this._structures) {
             if (structure.prms instanceof StructureKiviParams) {
                 structure.prms.ZRAM.v = this.prms.Z1.v - this.prms.PB.v;
             }
diff --git a/src/structure/dever.ts b/src/structure/dever.ts
index 51fba377..df3b7d6a 100644
--- a/src/structure/dever.ts
+++ b/src/structure/dever.ts
@@ -60,7 +60,7 @@ export class Dever extends ParallelStructure {
      */
     private calcA(): number {
         let A: number = 0;
-        for (const st of this.structures) {
+        for (const st of this._structures) {
             A += st.calcA();
         }
         return A;
diff --git a/src/structure/parallel_structure.ts b/src/structure/parallel_structure.ts
index 697c782f..312b532d 100644
--- a/src/structure/parallel_structure.ts
+++ b/src/structure/parallel_structure.ts
@@ -27,7 +27,12 @@ interface IStructureVarCalc {
 export class ParallelStructure extends Nub {
 
     /** Tableau des structures hydrauliques en parallèle */
-    public structures: Structure[] = [];
+    protected _structures: Structure[] = [];
+
+    /** getter only makes it kind of readonly */
+    public get structures() {
+        return this._structures;
+    }
 
     /**
      * paramètres castés au bon type
@@ -55,7 +60,7 @@ export class ParallelStructure extends Nub {
     public get parameterIterator(): IParamDefinitionIterator {
         const prms: ParamsEquation[] = [];
         prms.push(this._prms);
-        for (const st of this.structures) {
+        for (const st of this._structures) {
             prms.push(st.parameters);
         }
         return new ParamsEquationArrayIterator(prms);
@@ -68,10 +73,12 @@ export class ParallelStructure extends Nub {
      */
     public addStructure(structure: Structure, after?: number) {
         if (after !== undefined) {
-            this.structures.splice(after + 1, 0, structure);
+            this._structures.splice(after + 1, 0, structure);
         } else {
-            this.structures.push(structure);
+            this._structures.push(structure);
         }
+        // add reference to parent collection (this)
+        structure.parent = this;
     }
 
     /**
@@ -80,18 +87,20 @@ export class ParallelStructure extends Nub {
      * @param structure nouvelle structure
      */
     public replaceStructure(index: number, structure: Structure) {
-        if (index > -1 && index < this.structures.length) {
-            this.structures[index] = structure;
+        if (index > -1 && index < this._structures.length) {
+            this._structures[index] = structure;
         } else {
             throw new Error(`ParallelStructure.replaceStructure invalid index ${index}`);
         }
+        // add reference to parent collection (this)
+        structure.parent = this;
     }
 
     /**
      * @return true si la structure donnée est dans la liste
      */
     public hasStructure(structure: Nub): boolean {
-        for (const s of this.structures) {
+        for (const s of this._structures) {
             if (s.uid === structure.uid) {
                 return true;
             }
@@ -104,11 +113,11 @@ export class ParallelStructure extends Nub {
      */
     public moveStructureUp(structure: Structure) {
         let i = 0;
-        for (const s of this.structures) {
+        for (const s of this._structures) {
             if (s.uid === structure.uid && i > 0) {
-                const t = this.structures[i - 1];
-                this.structures[i - 1] = this.structures[i];
-                this.structures[i] = t;
+                const t = this._structures[i - 1];
+                this._structures[i - 1] = this._structures[i];
+                this._structures[i] = t;
                 return;
             }
             i++;
@@ -120,11 +129,11 @@ export class ParallelStructure extends Nub {
      */
     public moveStructureDown(structure: Structure) {
         let i = 0;
-        for (const s of this.structures) {
-            if (s.uid === structure.uid && i < this.structures.length - 1) {
-                const t = this.structures[i];
-                this.structures[i] = this.structures[i + 1];
-                this.structures[i + 1] = t;
+        for (const s of this._structures) {
+            if (s.uid === structure.uid && i < this._structures.length - 1) {
+                const t = this._structures[i];
+                this._structures[i] = this._structures[i + 1];
+                this._structures[i + 1] = t;
                 return;
             }
             i++;
@@ -137,7 +146,11 @@ export class ParallelStructure extends Nub {
      */
     public deleteStructure(index: number) {
         if (index > -1) {
-            this.structures.splice(index, 1);
+            const removedStructures = this._structures.splice(index, 1);
+            if (removedStructures && removedStructures.length === 1) {
+                // remove reference to parent collection (this)
+                removedStructures[0].parent = undefined;
+            }
         } else {
             throw new Error("ParallelStructure.deleteStructure invalid index=" + index);
         }
@@ -159,17 +172,17 @@ export class ParallelStructure extends Nub {
      */
     public CalcQ(iExcept?: number): Result {
         if (iExcept !== undefined) {
-            if (iExcept < 0 || iExcept >= this.structures.length) {
+            if (iExcept < 0 || iExcept >= this._structures.length) {
                 throw new Error(
-                    "ParallelStructure.CalcQ iExcept not in [0;" + (this.structures.length - 1) + "]",
+                    "ParallelStructure.CalcQ iExcept not in [0;" + (this._structures.length - 1) + "]",
                 );
             }
         }
         const calcRes: Result = new Result(0);
         let qTot: number = 0;
-        for (let i = 0; i < this.structures.length; i++) {
+        for (let i = 0; i < this._structures.length; i++) {
             if (i !== iExcept) {
-                const res: Result = this.structures[i].Calc("Q");
+                const res: Result = this._structures[i].Calc("Q");
                 calcRes.resultElement.AddResultElementToExtra(res.resultElement, `ouvrage[${i}].Q`);
                 qTot += res.vCalc;
             }
@@ -204,7 +217,7 @@ export class ParallelStructure extends Nub {
                 // Suppression des extraResults : ils sont complétés plus bas pour chaque ouvrage
                 res.resultElement.extraResults = {};
                 if (res.ok) {
-                    this.structures[sVC.index].getParameter(sVC.prm).setValue(res.vCalc);
+                    this._structures[sVC.index].getParameter(sVC.prm).setValue(res.vCalc);
                 }
         }
         if (res.ok) {
@@ -225,13 +238,13 @@ export class ParallelStructure extends Nub {
         try {
             // analyse n.X
             const i: IStructureVarCalc = this.getStructureVarCalc(desc);
-            return this.structures[i.index].getParameter(i.prm);
+            return this._structures[i.index].getParameter(i.prm);
         } catch (e) {
             // desc n'est pas de la forme n.X
             try {
                 // analyse ouvrage[n].X
                 const i: IStructureVarCalc = this.getStructureVarCalc2(desc);
-                const res = this.structures[i.index].result;
+                const res = this._structures[i.index].result;
                 if (res === undefined) {
                     return undefined;  // pas de résultat calculé
                 }
@@ -276,7 +289,7 @@ export class ParallelStructure extends Nub {
         let res = super.getLinkableValues(src);
 
         let i = 0;
-        for (const s of this.structures) {
+        for (const s of this._structures) {
             // paramètres liables des Nub structures enfants
             const l = s.getLinkableValues(src, `${i}.`, true);
 
@@ -303,7 +316,7 @@ export class ParallelStructure extends Nub {
      * Mise à jour de Z1, Z2, h1 et h2 pour tous les ouvrages
      */
     protected updateStructuresH1H2() {
-        for (const structure of this.structures) {
+        for (const structure of this._structures) {
             structure.prms.Z1.v = this.prms.Z1.v;
             structure.prms.Z2.v = this.prms.Z2.v;
             structure.prms.update_h1h2();
@@ -351,10 +364,10 @@ export class ParallelStructure extends Nub {
      */
     private CalcStructPrm(sVC: IStructureVarCalc, rInit?: number, rPrec: number = 0.001): Result {
         // Le débit restant sur la structure en calcul est :
-        this.structures[sVC.index].prms.Q.setValue(this.prms.Q.v - this.CalcQ(sVC.index).vCalc);
+        this._structures[sVC.index].prms.Q.setValue(this.prms.Q.v - this.CalcQ(sVC.index).vCalc);
 
         // Calcul du paramètre de la structure en calcul
-        return this.structures[sVC.index].Calc(sVC.prm, rInit, rPrec);
+        return this._structures[sVC.index].Calc(sVC.prm, rInit, rPrec);
     }
 
 }
diff --git a/src/structure/structure.ts b/src/structure/structure.ts
index c3dfca92..75535e39 100644
--- a/src/structure/structure.ts
+++ b/src/structure/structure.ts
@@ -4,6 +4,7 @@ import { Message, MessageCode } from "../util/message";
 import { Result } from "../util/result";
 
 import { StructureParams } from "./structure_params";
+import { ParallelStructure } from "./parallel_structure";
 
 export { StructureParams };
 
@@ -16,7 +17,7 @@ export enum StructureFlowMode {
     /** Orifice flow */
     ORIFICE,
     /** Zéro flow */
-    NULL,
+    NULL
 }
 
 /**
@@ -30,7 +31,7 @@ export enum StructureFlowRegime {
     /** Submerged flow */
     SUBMERGED,
     /** Zéro flow */
-    NULL,
+    NULL
 }
 
 /**
@@ -42,7 +43,7 @@ export enum StructureJetType {
     /** Plongeant */
     PLONGEANT,
     /** De surface */
-    SURFACE,
+    SURFACE
 }
 
 /**
@@ -60,6 +61,9 @@ export abstract class Structure extends Nub {
     /** Constante utile : Racine de 2g */
     protected static readonly R2G: number = Math.sqrt(2 * 9.81);
 
+    /** pointer to collection of structures */
+    public parent: ParallelStructure;
+
     /** Peut-on calculer ZDV ? */
     protected _isZDVcalculable: boolean;
 
-- 
GitLab