From 88a8b44bdef5f1cdb9413be6aa78aba08b996c79 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fran=C3=A7ois=20Grand?= <francois.grand@inrae.fr>
Date: Fri, 14 Oct 2022 17:15:20 +0200
Subject: [PATCH] refactor: set Nub.parent to private, add overridable setter
 (see Structure)

refs #328
---
 spec/verificateur/verificateur.spec.ts |  4 ++--
 src/nub.ts                             | 14 +++++++++++---
 src/pab/pab.ts                         |  2 +-
 src/prebarrage/pb_bassin.ts            |  8 +++++---
 src/prebarrage/pb_cloison.ts           |  8 +++++---
 src/structure/factory_structure.ts     |  2 +-
 src/structure/parallel_structure.ts    |  2 +-
 src/structure/structure.ts             | 10 ++++++++++
 8 files changed, 36 insertions(+), 14 deletions(-)

diff --git a/spec/verificateur/verificateur.spec.ts b/spec/verificateur/verificateur.spec.ts
index 82969208..bc4d21b4 100644
--- a/spec/verificateur/verificateur.spec.ts
+++ b/spec/verificateur/verificateur.spec.ts
@@ -28,7 +28,7 @@ function createPab(): Pab {
     const cl = Session.getInstance().createNub(
         new Props({ calcType: CalculatorType.Cloisons })
     ) as Cloisons;
-    cl.parent = pab;
+    cl.setParent(pab);
     pab.children.push(cl);
     pab.children[0].structures[0] = CreateStructure(LoiDebit.WeirSubmergedLarinier, cl);
     const dw = Session.getInstance().createNub(
@@ -237,7 +237,7 @@ describe("vérificateur de franchissement −", () => {
             pab.children[4].structures[0] = new StructureWeirVillemonte( // Échancrure (Villemonte 1957)
                 pab.children[4].structures[0].prms as RectangularStructureParams
             );
-            pab.children[4].structures[0].parent = pab.children[4];
+            pab.children[4].structures[0].setParent(pab.children[4]);
             pab.children[4].structures[0].getParameter("L").singleValue = 0.35;
             pab.prms.Z2.singleValue = 30.4; // évite une chute trop importante à la cloison 5
             // vérificateur
diff --git a/src/nub.ts b/src/nub.ts
index 73d7590e..91c1c242 100644
--- a/src/nub.ts
+++ b/src/nub.ts
@@ -76,7 +76,15 @@ export abstract class Nub extends ComputeNode implements IObservable {
     public dichoStartIntervalMaxSteps: number = 100;
 
     /** pointer to parent Nub */
-    public parent: Nub;
+    private _parent: Nub;
+
+    public get parent(): Nub {
+        return this._parent;
+    }
+
+    public setParent(p: Nub) {
+        this._parent = p;
+    }
 
     /**
      * Used by GUI for translation of the nub type in ngHyd (snackbar, ...).
@@ -727,7 +735,7 @@ export abstract class Nub extends ComputeNode implements IObservable {
             this._children.push(child);
         }
         // add reference to parent collection (this)
-        child.parent = this;
+        child.setParent(this);
         // postprocessing
         this.adjustChildParameters(child);
     }
@@ -1457,7 +1465,7 @@ export abstract class Nub extends ComputeNode implements IObservable {
             this.resetDefaultCalculatedParam();
         }
         // add reference to parent collection (this)
-        child.parent = this;
+        child.setParent(this);
     }
 
     /**
diff --git a/src/pab/pab.ts b/src/pab/pab.ts
index 5ef89546..811081b4 100644
--- a/src/pab/pab.ts
+++ b/src/pab/pab.ts
@@ -65,7 +65,7 @@ export class Pab extends FishPass {
     public set downWall(dw: CloisonAval) {
         this._downWall = dw;
         if (dw) { // might be undefined
-            dw.parent = this; // important
+            dw.setParent(this); // important
             // postprocessing
             this.adjustDownwallParameters(this.downWall);
         }
diff --git a/src/prebarrage/pb_bassin.ts b/src/prebarrage/pb_bassin.ts
index cac1c3cb..05022067 100644
--- a/src/prebarrage/pb_bassin.ts
+++ b/src/prebarrage/pb_bassin.ts
@@ -20,8 +20,6 @@ export class PbBassin extends Nub {
     /** Liste des cloisons aval */
     public cloisonsAval: PbCloison[];
 
-    public parent: PreBarrage;
-
     /** Débit transitant dans le bassin en m³/s */
     public Q: number;
 
@@ -86,6 +84,10 @@ export class PbBassin extends Nub {
         this.prms.ZF.calculability = ParamCalculability.FREE;
     }
 
+    public get parent(): PreBarrage {
+        return super.parent as PreBarrage;
+    }
+
     public CalcQ(): number {
         this.Q = 0;
         for(const c of this.cloisonsAmont) {
@@ -117,4 +119,4 @@ export class PbBassin extends Nub {
             order: String(this.order)
         });
     }
-}
\ No newline at end of file
+}
diff --git a/src/prebarrage/pb_cloison.ts b/src/prebarrage/pb_cloison.ts
index c4f19d28..07b39d44 100644
--- a/src/prebarrage/pb_cloison.ts
+++ b/src/prebarrage/pb_cloison.ts
@@ -9,9 +9,6 @@ import { Message, MessageCode } from "../util/message";
 
 export class PbCloison extends ParallelStructure {
 
-    /** pointer to parent Nub */
-    public parent: PreBarrage;
-
     constructor(bassinAmont: PbBassin, bassinAval: PbBassin, dbg: boolean = false, nullParams: boolean = false) {
         super(new ParallelStructureParams(0, 0, 0, nullParams), dbg);
         // prevent multiple CALC params in session file
@@ -24,6 +21,11 @@ export class PbCloison extends ParallelStructure {
         this._intlType = "Cloison";
     }
 
+    /** pointer to parent Nub */
+    public get parent(): PreBarrage {
+        return super.parent as PreBarrage;
+    }
+
     /** Bassin à l'amont de la cloison ou undefined pour condition limite amont */
     public get bassinAmont(): PbBassin {
         let basin: PbBassin;
diff --git a/src/structure/factory_structure.ts b/src/structure/factory_structure.ts
index f3bb70f8..f7e22968 100755
--- a/src/structure/factory_structure.ts
+++ b/src/structure/factory_structure.ts
@@ -223,7 +223,7 @@ export function CreateStructure(loiDebit: LoiDebit, parentNub?: ParallelStructur
 
     // set reference to parent
     if (parentNub) {
-        ret.parent = parentNub;
+        ret.setParent(parentNub);
         // Set Structure Type
         ret.properties.setPropValue("structureType", StructureProperties.findCompatibleStructure(loiDebit, parentNub));
     }
diff --git a/src/structure/parallel_structure.ts b/src/structure/parallel_structure.ts
index 1ab776cf..e8037fc9 100644
--- a/src/structure/parallel_structure.ts
+++ b/src/structure/parallel_structure.ts
@@ -29,7 +29,7 @@ export class ParallelStructure extends Nub {
     public set structures(structures: Structure[]) {
         this._children = structures;
         this._children.forEach((s) => {
-            s.parent = this;
+            s.setParent(this);
         });
     }
 
diff --git a/src/structure/structure.ts b/src/structure/structure.ts
index b9d3a0c9..8ed8981a 100644
--- a/src/structure/structure.ts
+++ b/src/structure/structure.ts
@@ -8,6 +8,7 @@ import { StructureParams } from "./structure_params";
 import { LoiDebit, StructureProperties } from "./structure_props";
 import { ParallelStructure } from "./parallel_structure";
 import { round } from "../base";
+import { Nub } from "../nub";
 
 /**
  * Flow mode: weir or orifice flow
@@ -139,6 +140,15 @@ export abstract class Structure extends ChildNub {
         // completes props with structureType and loiDebit
         this._loiDebit = ld;
         this._props.setPropValue("loiDebit", this._loiDebit);
+        this.updateStructureType();
+    }
+
+    public setParent(p: Nub): void {
+        super.setParent(p);
+        this.updateStructureType();
+    }
+
+    private updateStructureType() {
         this._props.setPropValue("structureType", StructureProperties.findCompatibleStructure(this._loiDebit, this.parent as ParallelStructure));
     }
 
-- 
GitLab