From 347ed1235a50e0d2acfbefa9119081221a37bcf3 Mon Sep 17 00:00:00 2001 From: "francois.grand" <francois.grand@irstea.fr> Date: Thu, 12 Apr 2018 15:33:56 +0200 Subject: [PATCH] =?UTF-8?q?=20#46=20:=20ajout=20de=20la=20classe=20Session?= =?UTF-8?q?Nub=20qui=20regroupe=20un=20nub=20et=20son=20contexte=20de=20cr?= =?UTF-8?q?=C3=A9ation=20(type=20de=20calculette,=20de=20noeud,=20loi=20de?= =?UTF-8?q?=20d=C3=A9bit...)=20-=20NubFactory=20:=20gestion=20de=20la=20se?= =?UTF-8?q?ssion=20avec=20une=20liste=20de=20SessionNub?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/index.ts | 1 + src/nub_factory.ts | 34 +++++++++++++++++--- src/section/section_nub.ts | 3 +- src/session_nub.ts | 66 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 98 insertions(+), 6 deletions(-) create mode 100644 src/session_nub.ts diff --git a/src/index.ts b/src/index.ts index 92585173..cf56f425 100644 --- a/src/index.ts +++ b/src/index.ts @@ -7,6 +7,7 @@ export * from "./param/param-values"; export * from "./compute-node"; export * from "./nub"; export * from "./nub_factory"; +export * from "./session_nub"; export * from "./cond_distri"; export * from "./dichotomie"; export * from "./lechaptcalmon"; diff --git a/src/nub_factory.ts b/src/nub_factory.ts index 14ad9c6a..7a8409d8 100644 --- a/src/nub_factory.ts +++ b/src/nub_factory.ts @@ -1,5 +1,6 @@ import { ComputeNodeType, CalculatorType } from "./compute-node" import { Nub } from "./nub" +import { SessionNub, Props } from "./session_nub" import { ConduiteDistribParams, ConduiteDistrib } from "./cond_distri"; import { LechaptCalmonParams, LechaptCalmon } from "./lechaptcalmon"; @@ -24,7 +25,11 @@ export class NubFactory { private static _instance: NubFactory; // instance pour le pattern singleton - private constructor() { } + private _session: SessionNub[]; + + private constructor() { + this._session = []; + } public static getInstance() { if (NubFactory._instance == undefined) @@ -36,13 +41,34 @@ export class NubFactory { this._defaultPrecision = p; } + /** + * créé un Nub et l'ajoute à la session + */ + public createSessionNub(p: Props | {}): SessionNub { + const params = p instanceof Props ? p : new Props(p); + const nub = this.createNub(params); + const res = new SessionNub(nub, params); + this._session.push(res); + return res; + } + + public findSessionNub(params: Props | {}): SessionNub { + for (const n of this._session) + if (n.hasProperties(params)) + return n; + return undefined; + } + /** * créé un Nub * @param calcType type de Nub * @param nodeType sous type de Nub * @param params paramètres supplémentaires spécifiques */ - public createNub(calcType: CalculatorType, nodeType: ComputeNodeType, params?: any): Nub { + private createNub(params: Props): Nub { + const calcType: CalculatorType = params.getPropValue("calcType"); + const nodeType: ComputeNodeType = params.getPropValue("nodeType"); + switch (calcType) { case CalculatorType.ConduiteDistributrice: { @@ -113,8 +139,8 @@ export class NubFactory { } case CalculatorType.Structure: - const structType: StructureType = params.structureType; - const loiDebit: LoiDebit = params.loiDebit; + const structType: StructureType = params.getPropValue("structureType"); + const loiDebit: LoiDebit = params.getPropValue("loiDebit"); return CreateStructure(structType, loiDebit); case CalculatorType.ParallelStructure: diff --git a/src/section/section_nub.ts b/src/section/section_nub.ts index a9c09389..476a83bf 100644 --- a/src/section/section_nub.ts +++ b/src/section/section_nub.ts @@ -144,8 +144,7 @@ export class SectionParametree extends Nub { } private hasVariatedParameter(): boolean { - for (const k in this._prms.map) { - const p: ParamDefinition = this._prms.map[k]; + for (const p of this.parameterIterator) { switch (p.valueMode) { case ParamValueMode.LISTE: case ParamValueMode.MINMAX: diff --git a/src/session_nub.ts b/src/session_nub.ts new file mode 100644 index 00000000..55dd6d18 --- /dev/null +++ b/src/session_nub.ts @@ -0,0 +1,66 @@ +import { Nub } from "./nub"; + +/** + * gestion d'un ensemble de propriétés (clé/valeur) + */ +export class Props { + constructor(private _props: any = {}) { } + + public hasProperties(props: Props | {}): boolean { + const keys = Object.keys(this._props); + const p = props instanceof Props ? props.props : props; + + // if (keys.length != Object.keys(p).length) + // return false; + + for (const k of keys) + if (this._props[k] !== p[k]) + return false; + + return true; + } + + public getPropValue(key: string): any { + return this._props[key]; + } + + public setPropValue(key: string, val: any): any { + this._props[key] = val; + } + + public get props() { + return this._props; + } + + public clone(): Props { + const res = new Props(); + for (const k in this._props) + res._props[k] = this._props[k]; + return res; + } +} + +/** + * Nub utilisé dans une session + */ +export class SessionNub { + private _props: Props; + + constructor(private _nub: Nub, props: Props | {}) { + if (this._nub == undefined) + throw new Error(`NgNub.constructor() : argument invalide`); + + if (props instanceof Props) + this._props = props.clone(); + else + this._props = new Props(props); + } + + public get nub() { + return this._nub; + } + + public hasProperties(p: Props | {}): boolean { + return this._props.hasProperties(p); + } +} -- GitLab