|
|
|
@ -5,6 +5,8 @@
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import { ValidationError } from "../Validation/validate";
|
|
|
|
|
import { UnitCommitmentScenario } from "../fixtures";
|
|
|
|
|
import { ColumnSpec } from "../../components/Common/Forms/DataTable";
|
|
|
|
|
|
|
|
|
|
export const renameItemInObject = <T>(
|
|
|
|
|
oldName: string,
|
|
|
|
@ -37,3 +39,119 @@ export const generateUniqueName = (container: any, prefix: string): string => {
|
|
|
|
|
}
|
|
|
|
|
return name;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const parseNumber = (valueStr: string): [number, ValidationError | null] => {
|
|
|
|
|
const valueFloat = parseFloat(valueStr);
|
|
|
|
|
if (isNaN(valueFloat)) {
|
|
|
|
|
return [0, { message: `"${valueStr}" is not a valid number` }];
|
|
|
|
|
} else {
|
|
|
|
|
return [valueFloat, null];
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const changeStringData = (
|
|
|
|
|
field: string,
|
|
|
|
|
newValue: string,
|
|
|
|
|
container: { [key: string]: any },
|
|
|
|
|
): [{ [key: string]: any }, ValidationError | null] => {
|
|
|
|
|
return [
|
|
|
|
|
{
|
|
|
|
|
...container,
|
|
|
|
|
[field]: newValue,
|
|
|
|
|
},
|
|
|
|
|
null,
|
|
|
|
|
];
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const changeBusRefData = (
|
|
|
|
|
field: string,
|
|
|
|
|
newValue: string,
|
|
|
|
|
container: { [key: string]: any },
|
|
|
|
|
scenario: UnitCommitmentScenario,
|
|
|
|
|
): [{ [key: string]: any }, ValidationError | null] => {
|
|
|
|
|
if (!(newValue in scenario.Buses)) {
|
|
|
|
|
return [scenario, { message: `Bus "${newValue}" does not exist` }];
|
|
|
|
|
}
|
|
|
|
|
return changeStringData(field, newValue, container);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const changeNumberData = (
|
|
|
|
|
field: string,
|
|
|
|
|
newValueStr: string,
|
|
|
|
|
container: { [key: string]: any },
|
|
|
|
|
): [{ [key: string]: any }, ValidationError | null] => {
|
|
|
|
|
// Parse value
|
|
|
|
|
const [newValueFloat, err] = parseNumber(newValueStr);
|
|
|
|
|
if (err) return [container, err];
|
|
|
|
|
|
|
|
|
|
// Build the new object
|
|
|
|
|
return [
|
|
|
|
|
{
|
|
|
|
|
...container,
|
|
|
|
|
[field]: newValueFloat,
|
|
|
|
|
},
|
|
|
|
|
null,
|
|
|
|
|
];
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const changeNumberVecData = (
|
|
|
|
|
field: string,
|
|
|
|
|
time: string,
|
|
|
|
|
newValueStr: string,
|
|
|
|
|
container: { [key: string]: any },
|
|
|
|
|
scenario: UnitCommitmentScenario,
|
|
|
|
|
): [{ [key: string]: any }, ValidationError | null] => {
|
|
|
|
|
// Parse value
|
|
|
|
|
const [newValueFloat, err] = parseNumber(newValueStr);
|
|
|
|
|
if (err) return [container, err];
|
|
|
|
|
|
|
|
|
|
// Convert HH:MM to offset
|
|
|
|
|
const hours = parseInt(time.split(":")[0]!, 10);
|
|
|
|
|
const min = parseInt(time.split(":")[1]!, 10);
|
|
|
|
|
const idx = (hours * 60 + min) / scenario.Parameters["Time step (min)"];
|
|
|
|
|
|
|
|
|
|
// Build the new vector
|
|
|
|
|
const newVec = [...container[field]];
|
|
|
|
|
newVec[idx] = newValueFloat;
|
|
|
|
|
return [
|
|
|
|
|
{
|
|
|
|
|
...container,
|
|
|
|
|
[field]: newVec,
|
|
|
|
|
},
|
|
|
|
|
null,
|
|
|
|
|
];
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const changeData = (
|
|
|
|
|
field: string,
|
|
|
|
|
newValueStr: string,
|
|
|
|
|
container: { [key: string]: any },
|
|
|
|
|
colSpecs: ColumnSpec[],
|
|
|
|
|
scenario: UnitCommitmentScenario,
|
|
|
|
|
): [{ [key: string]: any }, ValidationError | null] => {
|
|
|
|
|
const match = field.match(/^([^0-9]+)(\d+:\d+)?$/);
|
|
|
|
|
const fieldName = match![1]!.trim();
|
|
|
|
|
const fieldTime = match![2];
|
|
|
|
|
for (const spec of colSpecs) {
|
|
|
|
|
if (spec.title !== fieldName) continue;
|
|
|
|
|
switch (spec.type) {
|
|
|
|
|
case "string":
|
|
|
|
|
return changeStringData(fieldName, newValueStr, container);
|
|
|
|
|
case "busRef":
|
|
|
|
|
return changeBusRefData(fieldName, newValueStr, container, scenario);
|
|
|
|
|
case "number":
|
|
|
|
|
return changeNumberData(fieldName, newValueStr, container);
|
|
|
|
|
case "number[]":
|
|
|
|
|
return changeNumberVecData(
|
|
|
|
|
fieldName,
|
|
|
|
|
fieldTime!,
|
|
|
|
|
newValueStr,
|
|
|
|
|
container,
|
|
|
|
|
scenario,
|
|
|
|
|
);
|
|
|
|
|
default:
|
|
|
|
|
throw Error(`Unknown type: ${spec.type}`);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
throw Error(`Unknown field: ${fieldName}`);
|
|
|
|
|
};
|
|
|
|
|