From ae47d255fbd8adc75201f29f4b03d81b5296abb0 Mon Sep 17 00:00:00 2001
From: "francois.grand" <francois.grand@irstea.fr>
Date: Thu, 19 Apr 2018 15:08:38 +0200
Subject: [PATCH] =?UTF-8?q?=20#46=20ajout=20d'une=20classe=20StructureProp?=
 =?UTF-8?q?erties=20pour=20g=C3=A9rer=20les=20valeurs=20de=20StructureType?=
 =?UTF-8?q?=20et=20LoiDebit?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 spec/iterator/param_equation.spec.ts      |  3 +-
 spec/structure/parallel_structure.spec.ts |  3 +-
 spec/structure/structure_kivi.spec.ts     |  3 +-
 src/index.ts                              |  1 +
 src/nub_factory.ts                        |  3 +-
 src/structure/factory_structure.ts        | 42 ++-------------
 src/structure/structure_props.ts          | 63 +++++++++++++++++++++++
 7 files changed, 77 insertions(+), 41 deletions(-)
 create mode 100644 src/structure/structure_props.ts

diff --git a/spec/iterator/param_equation.spec.ts b/spec/iterator/param_equation.spec.ts
index 8a182a96..40660ada 100644
--- a/spec/iterator/param_equation.spec.ts
+++ b/spec/iterator/param_equation.spec.ts
@@ -10,7 +10,8 @@ import { ConduiteDistribParams, ConduiteDistrib } from "../../src/cond_distri";
 import { ParallelStructureParams } from "../../src/structure/parallel_structure_params";
 import { ParallelStructure } from "../../src/structure/parallel_structure";
 import { Structure } from "../../src/structure/structure";
-import { CreateStructure, StructureType, LoiDebit } from "../../src/structure/factory_structure";
+import { CreateStructure } from "../../src/structure/factory_structure";
+import { StructureType, LoiDebit } from "../../src/structure/structure_props";
 import { IParamDefinitionIterator } from "../../src/param/params-equation";
 
 function checkParams(pdi: IParamDefinitionIterator, symbols: string[], values: number[]) {
diff --git a/spec/structure/parallel_structure.spec.ts b/spec/structure/parallel_structure.spec.ts
index 1f2d7d9b..78f1ca3e 100644
--- a/spec/structure/parallel_structure.spec.ts
+++ b/spec/structure/parallel_structure.spec.ts
@@ -7,7 +7,8 @@
 // import { describe, expect, it, xdescribe, xit } from "../mock_jasmine";
 
 import { ParamCalculability } from "../../src/param/param-definition";
-import { CreateStructure, loiAdmissibles, LoiDebit, StructureType } from "../../src/structure/factory_structure";
+import { CreateStructure } from "../../src/structure/factory_structure";
+import { LoiDebit, StructureType, loiAdmissibles } from "../../src/structure/structure_props";
 import { ParallelStructure } from "../../src/structure/parallel_structure";
 import { ParallelStructureParams } from "../../src/structure/parallel_structure_params";
 import { Structure } from "../../src/structure/structure";
diff --git a/spec/structure/structure_kivi.spec.ts b/spec/structure/structure_kivi.spec.ts
index 1db9a2b5..1286b02b 100644
--- a/spec/structure/structure_kivi.spec.ts
+++ b/spec/structure/structure_kivi.spec.ts
@@ -7,7 +7,8 @@
 // import { describe, expect, it, xdescribe, xit } from "../mock_jasmine";
 
 import { StructureFlowMode, StructureFlowRegime, MessageCode } from "../../src";
-import { CreateStructure, LoiDebit, StructureType } from "../../src/structure/factory_structure";
+import { CreateStructure } from "../../src/structure/factory_structure";
+import { LoiDebit, StructureType } from "../../src/structure/structure_props";
 import { StructureKivi } from "../../src/structure/structure_kivi";
 import { StructureKiviParams } from "../../src/structure/structure_kivi_params";
 import { testStructure } from "./structure_test";
diff --git a/src/index.ts b/src/index.ts
index cf56f425..8d0ea149 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -36,4 +36,5 @@ export * from "./structure/parallel_structure_params";
 export * from "./structure/structure";
 export * from "./structure/structure_params";
 export * from "./structure/factory_structure";
+export * from "./structure/structure_props";
 export * from "./jalhyd_object";
diff --git a/src/nub_factory.ts b/src/nub_factory.ts
index 69ac91a6..2301efbb 100644
--- a/src/nub_factory.ts
+++ b/src/nub_factory.ts
@@ -14,7 +14,8 @@ import { RegimeUniforme } from "./regime_uniforme";
 import { CourbeRemousParams, MethodeResolution, CourbeRemous } from "./remous";
 import { PabDimensionParams, PabDimension } from "./pab/pab_dimension";
 import { PabPuissance, PabPuissanceParams } from "./pab/pab_puissance";
-import { CreateStructure, StructureType, LoiDebit } from "./structure/factory_structure";
+import { CreateStructure } from "./structure/factory_structure";
+import { StructureType, LoiDebit } from "./structure/structure_props";
 import { Structure } from "./structure/structure";
 import { ParallelStructure } from "./structure/parallel_structure";
 import { ParallelStructureParams } from "./structure/parallel_structure_params";
diff --git a/src/structure/factory_structure.ts b/src/structure/factory_structure.ts
index 787daa1f..abda575b 100644
--- a/src/structure/factory_structure.ts
+++ b/src/structure/factory_structure.ts
@@ -8,40 +8,7 @@ import { StructureKiviParams } from "./structure_kivi_params";
 import { StructureOrificeFree } from "./structure_orifice_free";
 import { StructureOrificeSubmerged } from "./structure_orifice_submerged";
 import { StructureWeirFree } from "./structure_weir_free";
-
-export enum StructureType {
-    SeuilRectangulaire, VanneRectangulaire
-    // VanneCirculaire,
-    // VanneTrapezoidale, SeuilTrapezoidal
-}
-
-export enum LoiDebit {
-    // loi de débit Déversoir / Orifice Cemagref 1988
-    Cem88d,
-    // loi de débit Déversoir / Vanne de fond Cemagref 1988
-    Cem88v,
-    // loi de débit Cunge 1980
-    Cunge80,
-    // loi de débit pour vanne dénoyée
-    OrificeFree,
-    // loi de débit pour vanne noyée
-    OrificeSubmerged,
-    // loi de débit pour seuil dénoyé
-    WeirFree,
-    // Loi Kindsvater-Carter et Villemonte
-    KIVI
-}
-
-export const loiAdmissibles: { [key: string]: LoiDebit[] } = {
-    SeuilRectangulaire: [
-        LoiDebit.Cem88d, LoiDebit.Cem88v, LoiDebit.Cunge80, LoiDebit.WeirFree,
-        LoiDebit.KIVI
-    ],
-    VanneRectangulaire: [
-        LoiDebit.Cem88d, LoiDebit.Cem88v, LoiDebit.Cunge80, LoiDebit.OrificeFree,
-        LoiDebit.OrificeSubmerged
-    ]
-};
+import { StructureType, LoiDebit, StructureProperties } from "./structure_props";
 
 export function CreateStructure(structureType: StructureType, loiDebit: LoiDebit, dbg: boolean = false): Structure {
     const rectStructPrms: RectangularStructureParams = new RectangularStructureParams(
@@ -57,14 +24,14 @@ export function CreateStructure(structureType: StructureType, loiDebit: LoiDebit
         case StructureType.VanneRectangulaire:
             rectStructPrms.W.v = 0.5;
             rectStructPrms.Cd.v = 0.6; // Cd pour une vanne rectangulaire
-            if (!(loiAdmissibles.VanneRectangulaire.includes(loiDebit))) {
+            if (!(StructureProperties.isCompatibleValues(StructureType.VanneRectangulaire, loiDebit))) {
                 throw new Error(
                     `la loi de débit ${LoiDebit[loiDebit]} n'est pas admissible pour les vannes rectangulaires`
                 );
             }
             break;
         case StructureType.SeuilRectangulaire:
-            if (!loiAdmissibles.SeuilRectangulaire.includes(loiDebit)) {
+            if (!(StructureProperties.isCompatibleValues(StructureType.SeuilRectangulaire, loiDebit))) {
                 throw new Error(
                     `la loi de débit ${LoiDebit[loiDebit]} n'est pas admissible pour les seuils rectangulaires`
                 );
@@ -96,6 +63,7 @@ export function CreateStructure(structureType: StructureType, loiDebit: LoiDebit
 
         case LoiDebit.WeirFree:
             return new StructureWeirFree(rectStructPrms, dbg);
+
         case LoiDebit.KIVI:
             const structKiviPrm: StructureKiviParams = new StructureKiviParams(
                 8.516, // Q
@@ -107,8 +75,8 @@ export function CreateStructure(structureType: StructureType, loiDebit: LoiDebit
                 0.001,  // béta
                 100);    // ZRAM : cote Radier Amont
             return new StructureKivi(structKiviPrm, dbg);
+
         default:
             throw new Error(`type de LoiDebit ${LoiDebit[loiDebit]} non pris en charge`);
-
     }
 }
diff --git a/src/structure/structure_props.ts b/src/structure/structure_props.ts
new file mode 100644
index 00000000..7c7d63a4
--- /dev/null
+++ b/src/structure/structure_props.ts
@@ -0,0 +1,63 @@
+export enum StructureType {
+    SeuilRectangulaire, VanneRectangulaire
+    // VanneCirculaire,
+    // VanneTrapezoidale, SeuilTrapezoidal
+}
+
+export enum LoiDebit {
+    // loi de débit Déversoir / Orifice Cemagref 1988
+    Cem88d,
+    // loi de débit Déversoir / Vanne de fond Cemagref 1988
+    Cem88v,
+    // loi de débit Cunge 1980
+    Cunge80,
+    // loi de débit pour vanne dénoyée
+    OrificeFree,
+    // loi de débit pour vanne noyée
+    OrificeSubmerged,
+    // loi de débit pour seuil dénoyé
+    WeirFree,
+    // Loi Kindsvater-Carter et Villemonte
+    KIVI
+}
+
+export const loiAdmissibles: { [key: string]: LoiDebit[] } = {
+    SeuilRectangulaire: [
+        LoiDebit.Cem88d, LoiDebit.Cem88v, LoiDebit.Cunge80, LoiDebit.WeirFree,
+        LoiDebit.KIVI
+    ],
+    VanneRectangulaire: [
+        LoiDebit.Cem88d, LoiDebit.Cem88v, LoiDebit.Cunge80, LoiDebit.OrificeFree,
+        LoiDebit.OrificeSubmerged
+    ]
+};
+
+export class StructureProperties {
+    /**
+     * @return true si les valeurs de StructureType et LoiDebit sont compatibles
+     */
+    public static isCompatibleValues(struct: StructureType, loi: LoiDebit): boolean {
+        return loiAdmissibles[StructureType[struct]].includes(loi);
+    }
+
+    /**
+     * @return la 1ère valeur de StructureType compatible avec la loi de débit
+     */
+    public static findCompatibleStructure(loi: LoiDebit): StructureType {
+        for (const st in loiAdmissibles) {
+            const lds: LoiDebit[] = loiAdmissibles[st];
+            for (const ld of lds)
+                if (ld === loi)
+                    return (<any>StructureType)[st];
+        }
+        return undefined;
+    }
+
+    /**
+     * @return la 1ère valeur de LoiDebit compatible avec le type de structure
+     */
+    public static findCompatibleLoiDebit(struct: StructureType): LoiDebit {
+        const sst: string = StructureType[struct];
+        return loiAdmissibles[sst][0];
+    }
+}
\ No newline at end of file
-- 
GitLab