From ba335df4e84072068f349ab81b7e4b58b9d621d5 Mon Sep 17 00:00:00 2001
From: "mathias.chouet" <mathias.chouet@irstea.fr>
Date: Tue, 19 Mar 2019 17:39:25 +0100
Subject: [PATCH] Easier access to results / extraResults values list

---
 src/util/result.ts          | 32 +++++++++++++++++++++------
 src/value_ref/object_ref.ts | 43 +++++++++++++++++++++++++++++++++----
 2 files changed, 65 insertions(+), 10 deletions(-)

diff --git a/src/util/result.ts b/src/util/result.ts
index 356b1c8c..15250857 100644
--- a/src/util/result.ts
+++ b/src/util/result.ts
@@ -100,9 +100,6 @@ export class Result extends JalhydObject implements INamedIterableValues {
         return this.resultElement.vCalc;
     }
 
-    /**
-     * @return le résultat de calcul du 1er ResultElement
-     */
     set vCalc(r: number) {
         this.resultElement.vCalc = r;
     }
@@ -176,6 +173,7 @@ export class Result extends JalhydObject implements INamedIterableValues {
         return Object.keys(r).length;
     }
 
+    // WTF
     public getExtraResult(name: string): any {
         const res = [];
         for (const r of this._resultElements) {
@@ -195,11 +193,35 @@ export class Result extends JalhydObject implements INamedIterableValues {
             default:
                 throw new Error(
                     "Result.getExtraResult() : il existe plusieurs ResultElement avec" +
-                    " un extratresult dont le nom est '" + name + "'",
+                    " un extraResult dont le nom est '" + name + "'",
                 );
         }
     }
 
+    /**
+     * Returns the list of calculated values for the parameter in CALC mode;
+     * if no parameter is variated, the list will contain only 1 value
+     */
+    public getCalculatedValues(): number[] {
+        return this.resultElements.map((re) => {
+            return re.vCalc;
+        });
+    }
+
+    /**
+     * Returns the list of extra results values for the given symbol;
+     * if no parameter is variated, the list will contain only 1 value
+     */
+    public getExtraResults(symbol: string): number[] {
+        const extraRes: number[] = [];
+        const ier = this.getIterableExtraResults(symbol);
+        ier.initValuesIterator(false);
+        for (const er of ier) {
+            extraRes.push(er);
+        }
+        return extraRes;
+    }
+
     /**
      * ajoute un message au journal
      */
@@ -292,11 +314,9 @@ export class Result extends JalhydObject implements INamedIterableValues {
                 res.addValue(er);
             }
         }
-
         if (found) {
             return res;
         }
-
         return undefined;
     }
 
diff --git a/src/value_ref/object_ref.ts b/src/value_ref/object_ref.ts
index 3db161ae..42c1ef9b 100644
--- a/src/value_ref/object_ref.ts
+++ b/src/value_ref/object_ref.ts
@@ -86,16 +86,38 @@ export class LinkedValue {
 
     /**
      * Returs the ParamValues for the linked Parameter if any, or a
-     * pseudo ParamValues object if the targetted element is a Result
+     * fake ParamValues object if the targetted element is a Result
      * or ExtraResults
      */
     public getParamValues(): ParamValues {
+        let ret: ParamValues;
+
         if (this.isParameter()) {
-            return (this.element as ParamDefinition).paramValues;
+            // simple proxy
+            ret = (this.element as ParamDefinition).paramValues;
+
+        } else if (this.isExtraResult()) {
+            // already computed ?
+            if (this.nub.result) {
+                // handmade fake param values
+                ret = new ParamValues(); // @TODO extend class ?
+                // console.log("setting fake PV for linked ER", this.symbol,
+                    // this.nub.result, this.nub.result.extraResults);
+                if (this.nub.resultHasMultipleValues()) {
+                    const multipleExtraRes = this.nub.result.getExtraResults(this.symbol);
+                    ret.setValues(multipleExtraRes);
+                } else {
+                    const singleExtraRes = this.nub.result.getExtraResult(this.symbol);
+                    ret.setSingleValue(singleExtraRes);
+                }
+            } else {
+                // not computed yet @TODO return undefined instead of throwing error ?
+                throw new Error("LinkedValue.getParamValues() - result not yet available");
+            }
         } else {
-            // @TODO implement
-            throw new Error("not implemented - get pseudo ParamValues for ExtraResults");
+            throw new Error("LinkedValue.getParamValues() - cannot determine nature of target");
         }
+        return ret;
     }
 
     /**
@@ -111,6 +133,19 @@ export class LinkedValue {
         return this.getParamValues().currentValue;
     }
 
+    /**
+     * Returns the current values array of this.paramValues; throws an error if
+     * this.paramValues is not defined
+     */
+    public getValues(): number[] {
+        if (! this.getParamValues().isDefined) {
+            const e = new Message(MessageCode.ERROR_PARAMDEF_LINKED_VALUE_UNDEFINED);
+            e.extraVar.symbol = this.symbol;
+            throw e;
+        }
+        return this.getParamValues().valueList;
+    }
+
     /**
      * Returns true if
      * - a parameter is targetted and it has multiple values
-- 
GitLab