From 6c47c4f541fc39629f8ded4c5f92acd3489112d7 Mon Sep 17 00:00:00 2001
From: David Dorchies <david.dorchies@irstea.fr>
Date: Tue, 29 Sep 2020 11:43:43 +0200
Subject: [PATCH] Fix #261 - make QA linkable among PAB Cloisons

---
 spec/pab/pab.spec.ts | 28 +++++++++++++++++++++++++++-
 src/nub.ts           | 12 +++++++++++-
 src/pab/pab.ts       |  3 ++-
 3 files changed, 40 insertions(+), 3 deletions(-)

diff --git a/spec/pab/pab.spec.ts b/spec/pab/pab.spec.ts
index 08a5aa18..d238f8a3 100644
--- a/spec/pab/pab.spec.ts
+++ b/spec/pab/pab.spec.ts
@@ -1,4 +1,3 @@
-import { ParamValueMode, Session } from "../../src/index";
 import { CloisonAval } from "../../src/pab/cloison_aval";
 import { CloisonsAvalParams } from "../../src/pab/cloison_aval_params";
 import { Cloisons } from "../../src/pab/cloisons";
@@ -11,6 +10,8 @@ import { StructureVanLevVillemonte } from "../../src/structure/structure_vanlev_
 import { StructureWeirSubmergedLarinier } from "../../src/structure/structure_weir_submerged_larinier";
 import { StructureWeirVillemonte } from "../../src/structure/structure_weir_villemonte";
 import { MessageCode } from "../../src/util/message";
+import { ParamValueMode } from "../../src/param/param-value-mode";
+import { Session } from "../../src/session";
 
 const dbg: boolean = false;
 
@@ -282,4 +283,29 @@ describe("Class Pab: ", () => {
         });
     });
 
+    describe("linking QA", () => {
+
+        it("when there are more than 1 Cloison, QA should be linkable to its siblings", () => {
+            Session.getInstance().clear();
+            Session.getInstance().registerNub(pab);
+            const lv = Session.getInstance().getLinkableValues(pab.children[0].prms.QA);
+            expect(lv.length).toBe(13); // example PAB has 14 walls
+        });
+
+        it("when there are more than 1 Pab, QA should be linkable to other Pab's QAs", () => {
+            const pab2 = createPabTest();
+            Session.getInstance().clear();
+            // keep only one wall
+            for (let i=0; i < 13; i++) {
+                pab.deleteChild(13 - i);
+                pab2.deleteChild(13 - i);
+            }
+            expect(pab.children.length).toBe(1);
+            expect(pab2.children.length).toBe(1);
+            Session.getInstance().registerNub(pab);
+            Session.getInstance().registerNub(pab2);
+            const lv = Session.getInstance().getLinkableValues(pab.children[0].prms.QA);
+            expect(lv.length).toBe(1); // the only wall of the 2nd PAB
+        });
+    });
 });
diff --git a/src/nub.ts b/src/nub.ts
index 5e4011f4..5c32e1c4 100644
--- a/src/nub.ts
+++ b/src/nub.ts
@@ -789,7 +789,17 @@ export abstract class Nub extends ComputeNode implements IObservable {
         }
 
         // 3. children Nubs, except for PAB and MRC and PreBarrage
-        if (!(this instanceof Pab || this instanceof MacrorugoCompound || this._calcType === CalculatorType.PreBarrage)) {
+        if (
+            ! (this instanceof MacrorugoCompound)
+            && ( // meta-except, if source param in a PAB's Cloison (should only apply to QA)
+                ! (this instanceof Pab)
+                || (
+                    src.originNub instanceof Pab
+                    && src.symbol === "QA"
+                )
+            )
+            && this._calcType !== CalculatorType.PreBarrage
+        ) {
             for (const cn of this.getChildren()) {
                 res = res.concat(cn.getLinkableValues(src));
             }
diff --git a/src/pab/pab.ts b/src/pab/pab.ts
index 597d8263..716d7c81 100644
--- a/src/pab/pab.ts
+++ b/src/pab/pab.ts
@@ -1,5 +1,5 @@
 import { CalculatorType } from "../compute-node";
-import { Session } from "../index";
+import { Session, JalhydObject } from "../index";
 import { ParamValueMode } from "../index";
 import { Nub } from "../nub";
 import { ParamCalculability, ParamDefinition } from "../param/param-definition";
@@ -76,6 +76,7 @@ export class Pab extends FishPass {
         for (let i = 0; i < n; i++) {
             const cl: Cloisons =
                 Session.getInstance().unserialiseSingleNub(serialisedCloisonModel, false).nub as Cloisons;
+            cl.setUid(JalhydObject.nextUID); // prevent all Cloisons having the same UID
             const p = cl.prms;
             // Copy calculated param to the new Cloison #130
             cl.calculatedParam.singleValue = cloisonModel.calculatedParam.V;
-- 
GitLab