From e98ef2453efd3ff357a81ba3560aee34deb1a760 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fran=C3=A7ois=20Grand?= <francois.grand@inrae.fr>
Date: Tue, 11 Apr 2023 17:31:40 +0200
Subject: [PATCH] fix: parallel structures: remove results in case of
 submergence error

refs #302
---
 src/structure/parallel_structure.ts |  6 +++++
 src/util/result.ts                  | 38 ++++++++++++++++++++++++++++-
 src/util/resultelement.ts           |  7 ++++++
 3 files changed, 50 insertions(+), 1 deletion(-)

diff --git a/src/structure/parallel_structure.ts b/src/structure/parallel_structure.ts
index e0d2facd..682beb1c 100644
--- a/src/structure/parallel_structure.ts
+++ b/src/structure/parallel_structure.ts
@@ -82,6 +82,12 @@ export class ParallelStructure extends Nub {
             this.result.globalLog.addLog(st.result.log);
         }
 
+        // si une erreur d'ennoiement est survenue dans les structures, on annule les résultats
+
+        if (res.hasMessage(MessageCode.ERROR_STRUCTURE_SUBMERGENCE_LOWER_THAN, true)) {
+            res.removeValues();
+        }
+
         return res;
     }
 
diff --git a/src/util/result.ts b/src/util/result.ts
index a1cb07b4..2a8746ff 100644
--- a/src/util/result.ts
+++ b/src/util/result.ts
@@ -1,4 +1,4 @@
-import { JalhydObject } from "../internal_modules";
+import { JalhydObject, MessageCode } from "../internal_modules";
 import { Nub } from "../internal_modules";
 import { cLog } from "../internal_modules";
 import { Message, MessageSeverity } from "../internal_modules";
@@ -272,6 +272,33 @@ export class Result extends JalhydObject {
         return true;
     }
 
+    /**
+     * determine if a message is present in result
+     * @param code message code to find
+     * @param recurs if true, search into nub children
+     */
+    public hasMessage(code: MessageCode, recurs: boolean = false): boolean {
+        if (this._globalLog.contains(code)) {
+            return true;
+        }
+
+        for (const r of this._resultElements) {
+            if (r.log.contains(code)) {
+                return true;
+            }
+        }
+
+        if (recurs && this._sourceNub !== undefined) {
+            for (const n of this._sourceNub.getChildren()) {
+                if (n.result.hasMessage(code, true)) {
+                    return true;
+                }
+            }
+        }
+
+        return false;
+    }
+
     /**
      * compute log stats (# of error, warning, info) on all result element
      */
@@ -330,4 +357,13 @@ export class Result extends JalhydObject {
             re.removeExtraResults();
         }
     }
+
+    /**
+     * Removes all values in result elements
+     */
+    public removeValues() {
+        for (const re of this._resultElements) {
+            re.removeValues();
+        }
+    }
 }
diff --git a/src/util/resultelement.ts b/src/util/resultelement.ts
index 313e464a..f9f054cd 100644
--- a/src/util/resultelement.ts
+++ b/src/util/resultelement.ts
@@ -192,6 +192,13 @@ export class ResultElement {
         }
     }
 
+    /**
+     * Removes all values
+     */
+    public removeValues() {
+        this._values = {};
+    }
+
     public toString(): string {
         if (this.vCalc !== undefined) {
             return String(this.vCalc);
-- 
GitLab