Commit 8c6c58b4 authored by Grand Francois's avatar Grand Francois
Browse files

ajout des classes DefinedValue, DefinedString, DefinedBoolean, DefinedNumber et NumericalString

Showing with 172 additions and 23 deletions
+172 -23
...@@ -5,3 +5,5 @@ export * from './cond_distri'; ...@@ -5,3 +5,5 @@ export * from './cond_distri';
export * from './dichotomie'; export * from './dichotomie';
export * from './lechaptcalmon'; export * from './lechaptcalmon';
export * from './regime_uniforme'; export * from './regime_uniforme';
export * from './util/definedvalue';
export * from './util/numericalstring';
import { Debug, UndefinedError } from './base'; import { Debug, UndefinedError } from './base';
import { DefinedNumber } from './util/definedvalue';
/** /**
* domaine de définition du paramètre * domaine de définition du paramètre
...@@ -93,7 +94,7 @@ export enum ParamCalculability { ...@@ -93,7 +94,7 @@ export enum ParamCalculability {
/** /**
* définition d'un paramètre * définition d'un paramètre
*/ */
export class ParamDefinition { export class ParamDefinition extends DefinedNumber {
/** /**
* symbole * symbole
*/ */
...@@ -117,10 +118,14 @@ export class ParamDefinition { ...@@ -117,10 +118,14 @@ export class ParamDefinition {
/** /**
* valeur du paramètre * valeur du paramètre
*/ */
private _value: number; //private _value: number;
// private static _idGen: number = 0; // A VIRER
// private _id: number; // A VIRER
// constructor(s: string, d: ParamDomain, c: ParamCalculability, val: number = undefined) { // constructor(s: string, d: ParamDomain, c: ParamCalculability, val: number = undefined) {
constructor(s: string, d: any, val: number = undefined) { constructor(s: string, d: any, val: number = undefined) {
super(val);
this._symbol = s; this._symbol = s;
if (d instanceof ParamDomain) if (d instanceof ParamDomain)
...@@ -130,9 +135,16 @@ export class ParamDefinition { ...@@ -130,9 +135,16 @@ export class ParamDefinition {
this._calc = undefined; this._calc = undefined;
this.checkValue(val); this.checkValue(val);
this._value = val; // this._value = val;
// this._id = ParamDefinition._idGen++; // A VIRER
// console.log("constructor param " + this._symbol + " id=" + this._id); // A VIRER
} }
// get id(): number {
// return this._id;
// }
/** /**
* getter symbole * getter symbole
*/ */
...@@ -180,22 +192,30 @@ export class ParamDefinition { ...@@ -180,22 +192,30 @@ export class ParamDefinition {
* gestion de la valeur * gestion de la valeur
*/ */
get v(): number { get v(): number {
if (this._value == undefined) // if (this._value == undefined)
if (!this.isDefined)
throw new UndefinedError("value of '" + this._symbol + "' parameter is not defined"); throw new UndefinedError("value of '" + this._symbol + "' parameter is not defined");
return this._value; // return this._value;
return this.getValue();
} }
set v(val: number) { set v(val: number) {
// if (val == undefined)
// console.log("warning : setting parameter '" + this._symbol + "' to undefined value (use undefine() instead)");
if (this.calculability == ParamCalculability.NONE) if (this.calculability == ParamCalculability.NONE)
throw "value of '" + this._symbol + "' parameter cannot be changed"; throw "value of '" + this._symbol + "' parameter cannot be changed";
this.checkValue(val); this.checkValue(val);
this._value = val; // this._value = val;
} this.setValue(val);
get uncheckedValue() { // console.log("setting param " + this._symbol + " id=" + this._id + " to " + val); // A VIRER
return this._value;
} }
// get uncheckedValue() {
// return this._value;
// }
checkValue(v: number) { checkValue(v: number) {
let sDomain = ParamDomainValue[this._domain.domain]; let sDomain = ParamDomainValue[this._domain.domain];
...@@ -230,15 +250,15 @@ export class ParamDefinition { ...@@ -230,15 +250,15 @@ export class ParamDefinition {
} }
} }
isDefined(): boolean { // isDefined(): boolean {
return this._value != undefined; // return this._value != undefined;
} // }
undefine() { // undefine() {
this._value = undefined; // this._value = undefined;
// this._savedValue = undefined; // // this._savedValue = undefined;
} // }
/** /**
* gestion du cache * gestion du cache
...@@ -260,11 +280,11 @@ export class ParamDefinition { ...@@ -260,11 +280,11 @@ export class ParamDefinition {
} }
*/ */
toString(): string { // toString(): string {
if (this.isDefined()) // if (this.isDefined())
return "" + this._value; // return "" + this._value;
return "undefined"; // return "undefined";
} // }
} }
......
...@@ -132,7 +132,7 @@ export class cSnCirc extends acSection { ...@@ -132,7 +132,7 @@ export class cSnCirc extends acSection {
} }
this.debug("circ.Calc_B() : PAS débordement"); this.debug("circ.Calc_B() : PAS débordement");
if (this.prms.D.isDefined() && this.prms.Y.isDefined()) { if (this.prms.D.isDefined && this.prms.Y.isDefined) {
//return this.prms.D.v * Math.sin(this.Calc("Alpha")); //return this.prms.D.v * Math.sin(this.Calc("Alpha"));
let res = this.prms.D.v * Math.sin(this.Calc("Alpha")); let res = this.prms.D.v * Math.sin(this.Calc("Alpha"));
this.debug("circ.Calc_B() : res=" + res); this.debug("circ.Calc_B() : res=" + res);
......
...@@ -249,7 +249,7 @@ export abstract class acSection extends ComputeNode { ...@@ -249,7 +249,7 @@ export abstract class acSection extends ComputeNode {
this.debug("in Calc(" + sDonnee + ", rY=" + rY + ") old " + sDonnee + "=" + this.arCalc[sDonnee]); this.debug("in Calc(" + sDonnee + ", rY=" + rY + ") old " + sDonnee + "=" + this.arCalc[sDonnee]);
this.debug("this.Y=" + this.prms.Y.toString()); this.debug("this.Y=" + this.prms.Y.toString());
if (rY != undefined && (!this.prms.Y.isDefined() || rY != this.prms.Y.v)) { if (rY != undefined && (!this.prms.Y.isDefined || rY != this.prms.Y.v)) {
this.prms.Y.v = rY; this.prms.Y.v = rY;
// On efface toutes les données dépendantes de Y pour forcer le calcul // On efface toutes les données dépendantes de Y pour forcer le calcul
this.Reset(false); this.Reset(false);
......
export class DefinedValue<T> {
private _value: T;
constructor(v: T = undefined) {
this.value = v;
}
get isDefined() {
return this._value != undefined;
}
undefine() {
this._value = undefined;
}
get uncheckedValue() {
return this._value;
}
/**
* fonction nécessitée par le fait qu'on ne peut pas appeller les accesseurs d'un parent
* ou que si l'un des 2 accesseurs est surchargé, il faut surcharger l'autre
* (et donc pour pouvoir éventuellement appeler l'accesseur parent)
*/
protected getValue(): T {
if (this._value == undefined)
throw "undefined value";
return this._value;
}
/**
* fonction nécessitée par le fait qu'on ne peut pas appeller les accesseurs d'un parent
* ou que si l'un des 2 accesseurs est surchargé, il faut surcharger l'autre
* (et donc pour pouvoir éventuellement appeler l'accesseur parent)
*/
protected setValue(v: T) {
this._value = v;
}
get value() {
return this.getValue();
}
set value(v: T) {
this.setValue(v);
}
toString(): string {
if (this.isDefined)
return String(this._value);
return "undefined";
}
}
export class DefinedBoolean extends DefinedValue<boolean>{
}
export class DefinedNumber extends DefinedValue<number>{
}
export class DefinedString extends DefinedValue<string>{
constructor(v: any = undefined) {
super(v == undefined ? v : String(v));
}
}
import { DefinedValue, DefinedBoolean, DefinedString } from "./definedvalue";
export class NumericalString extends DefinedString {
private _isNumericalFlag: DefinedBoolean;
constructor(s: any = undefined) {
super(s);
}
private get isNumericalFlag(): DefinedBoolean {
if (this._isNumericalFlag == undefined)
this._isNumericalFlag = new DefinedBoolean();
return this._isNumericalFlag;
}
private updateNumericalFlag() {
if (!this.isNumericalFlag.isDefined) {
this.isNumericalFlag.value = false;
if (this.isDefined) {
if (typeof this.value === "string") {
this.isNumericalFlag.value = String(this.value).trim() !== "" && !isNaN(+this.value)
}
}
}
}
get isNumerical(): boolean {
this.updateNumericalFlag();
return this.isNumericalFlag.value;
}
/**
* nécessaire car si on surcharge un des accesseurs, il faut surcharger les 2 (merci Microsoft)
*/
get value() {
return this.getValue();
}
set value(v: string) {
this.isNumericalFlag.undefine();
this.setValue(v);
// this.updateNumericalFlag();
}
get numericalValue(): number {
if (!this.isNumerical)
throw "invalid NumericString '" + this.uncheckedValue + "' value";
return +this.value;
}
get uncheckValueString(): string {
if (this.isDefined)
return this.uncheckedValue;
return "";
}
toString(): string {
return super.toString() + (this.isNumerical ? " [numerical]" : " [NOT numerical]");
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment