Skip to content

Commit

Permalink
Renaming/refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenthoms committed Oct 9, 2023
1 parent 3af6571 commit 739a2da
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 62 deletions.
10 changes: 5 additions & 5 deletions frontend/src/framework/Ensemble.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { EnsembleIdent } from "./EnsembleIdent";
import { EnsembleParameters, Parameter } from "./EnsembleParameters";
import { EnsembleParameterSet, Parameter } from "./EnsembleParameterSet";
import { EnsembleSensitivities, Sensitivity } from "./EnsembleSensitivities";

export class Ensemble {
private _ensembleIdent: EnsembleIdent;
private _caseName: string;
private _realizationsArr: number[];
private _parameters: EnsembleParameters;
private _parameterSet: EnsembleParameterSet;
private _sensitivities: EnsembleSensitivities | null;

constructor(
Expand All @@ -20,7 +20,7 @@ export class Ensemble {
this._ensembleIdent = new EnsembleIdent(caseUuid, ensembleName);
this._caseName = caseName;
this._realizationsArr = Array.from(realizationsArr).sort((a, b) => a - b);
this._parameters = new EnsembleParameters(parameterArr);
this._parameterSet = new EnsembleParameterSet(parameterArr);

this._sensitivities = null;
if (sensitivityArr && sensitivityArr.length > 0) {
Expand Down Expand Up @@ -64,8 +64,8 @@ export class Ensemble {
return this._realizationsArr[this._realizationsArr.length - 1];
}

getParameters(): EnsembleParameters {
return this._parameters;
getParameterSet(): EnsembleParameterSet {
return this._parameterSet;
}

getSensitivities(): EnsembleSensitivities | null {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { MinMax } from "@lib/utils/MinMax";

import { ParameterIdent } from "./ParameterIdent";

export enum ParameterType {
CONTINUOUS,
DISCRETE,
Expand Down Expand Up @@ -28,52 +30,7 @@ export type DiscreteParameter = {

export type Parameter = ContinuousParameter | DiscreteParameter;

export class ParameterIdent {
readonly name: string;
readonly groupName: string | null;

constructor(name: string, groupName: string | null) {
this.name = name;
this.groupName = groupName;
}

static fromNameAndGroup(name: string, groupName: string | null): ParameterIdent {
return new ParameterIdent(name, groupName);
}

static fromString(paramIdentString: string): ParameterIdent {
const parts = paramIdentString.split("~@@~");
if (parts.length === 1) {
return new ParameterIdent(parts[0], null);
}
if (parts.length === 2) {
return new ParameterIdent(parts[0], parts[1]);
}

throw new Error(`Invalid parameter ident string: ${paramIdentString}`);
}

toString(): string {
if (this.groupName) {
return `${this.name}~@@~${this.groupName}`;
} else {
return this.name;
}
}

equals(otherIdent: ParameterIdent | null): boolean {
if (!otherIdent) {
return false;
}
if (otherIdent === this) {
return true;
}

return this.name === otherIdent.name && this.groupName === otherIdent.groupName;
}
}

export class EnsembleParameters {
export class EnsembleParameterSet {
private _parameterArr: Parameter[];

constructor(parameterArr: Parameter[]) {
Expand Down
44 changes: 44 additions & 0 deletions frontend/src/framework/ParameterIdent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
export class ParameterIdent {
readonly name: string;
readonly groupName: string | null;

constructor(name: string, groupName: string | null) {
this.name = name;
this.groupName = groupName;
}

static fromNameAndGroup(name: string, groupName: string | null): ParameterIdent {
return new ParameterIdent(name, groupName);
}

static fromString(paramIdentString: string): ParameterIdent {
const parts = paramIdentString.split("~@@~");
if (parts.length === 1) {
return new ParameterIdent(parts[0], null);
}
if (parts.length === 2) {
return new ParameterIdent(parts[0], parts[1]);
}

throw new Error(`Invalid parameter ident string: ${paramIdentString}`);
}

toString(): string {
if (this.groupName) {
return `${this.name}~@@~${this.groupName}`;
} else {
return this.name;
}
}

equals(otherIdent: ParameterIdent | null): boolean {
if (!otherIdent) {
return false;
}
if (otherIdent === this) {
return true;
}

return this.name === otherIdent.name && this.groupName === otherIdent.groupName;
}
}
5 changes: 2 additions & 3 deletions frontend/src/framework/internal/EnsembleSetLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { QueryClient } from "@tanstack/react-query";

import { Ensemble } from "../Ensemble";
import { EnsembleIdent } from "../EnsembleIdent";
import { Parameter, ParameterType, ContinuousParameter, DiscreteParameter } from "../EnsembleParameters";
import { ContinuousParameter, DiscreteParameter, Parameter, ParameterType } from "../EnsembleParameterSet";
import { Sensitivity, SensitivityCase } from "../EnsembleSensitivities";
import { EnsembleSet } from "../EnsembleSet";

Expand Down Expand Up @@ -141,8 +141,7 @@ function buildParameterArrFromApiResponse(apiParameterArr: EnsembleParameter_api
values: apiPar.values as number[],
};
retParameterArr.push(retPar);
}
else {
} else {
const retPar: DiscreteParameter = {
type: ParameterType.DISCRETE,
name: apiPar.name,
Expand Down
14 changes: 6 additions & 8 deletions frontend/tests/unit-tests/EnsembleParameters.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EnsembleParameters, Parameter, ParameterIdent, ParameterType } from "@framework/EnsembleParameters";
import { EnsembleParameterSet, Parameter, ParameterIdent, ParameterType } from "@framework/EnsembleParameters";
import { MinMax } from "@lib/utils/MinMax";

// prettier-ignore
Expand All @@ -12,10 +12,9 @@ const PARAM_ARR: Parameter[] = [
{type: ParameterType.DISCRETE, name: "dparam_B", groupName: null, description: "descB", isConstant: false, realizations: [1,2,3], values: ["A", "B", "C"]},
];


describe("EnsembleParameters tests", () => {
test("Get list of parameter idents", () => {
const ensParams = new EnsembleParameters(PARAM_ARR);
const ensParams = new EnsembleParameterSet(PARAM_ARR);
{
const allIdents = ensParams.getParameterIdents(null);
expect(allIdents.length).toEqual(6);
Expand Down Expand Up @@ -43,7 +42,7 @@ describe("EnsembleParameters tests", () => {
});

test("Check for parameter existence", () => {
const ensParams = new EnsembleParameters(PARAM_ARR);
const ensParams = new EnsembleParameterSet(PARAM_ARR);

expect(ensParams.hasParameter(ParameterIdent.fromNameAndGroup("cparam_10", null))).toBe(true);
expect(ensParams.hasParameter(ParameterIdent.fromNameAndGroup("cparam_50", "grp1"))).toBe(true);
Expand All @@ -56,7 +55,7 @@ describe("EnsembleParameters tests", () => {
});

test("Get parameters", () => {
const ensParams = new EnsembleParameters(PARAM_ARR);
const ensParams = new EnsembleParameterSet(PARAM_ARR);
{
const par = ensParams.getParameter(ParameterIdent.fromNameAndGroup("cparam_10", null));
expect(par.type).toEqual(ParameterType.CONTINUOUS);
Expand All @@ -81,12 +80,12 @@ describe("EnsembleParameters tests", () => {
});

test("Check that getting non-existing parameter throws", () => {
const ensParams = new EnsembleParameters(PARAM_ARR);
const ensParams = new EnsembleParameterSet(PARAM_ARR);
expect(() => ensParams.getParameter(ParameterIdent.fromNameAndGroup("someBogusName", null))).toThrow();
});

test("Test getting min/max values for continuous parameter", () => {
const ensParams = new EnsembleParameters(PARAM_ARR);
const ensParams = new EnsembleParameterSet(PARAM_ARR);
{
const minMax = ensParams.getContinuousParameterMinMax(ParameterIdent.fromNameAndGroup("cparam_10", null));
expect(minMax).toEqual(new MinMax(11, 19));
Expand All @@ -98,7 +97,6 @@ describe("EnsembleParameters tests", () => {
});
});


describe("ParameterIdent tests", () => {
test("Conversion to/from string", () => {
{
Expand Down

0 comments on commit 739a2da

Please sign in to comment.