Merge branch 'position-float'

This commit is contained in:
2025-09-24 13:38:50 +02:00
2 changed files with 50 additions and 7 deletions

View File

@@ -12,6 +12,7 @@ const outputUtils = require('./src/helper/outputUtils.js');
const logger = require('./src/helper/logger.js'); const logger = require('./src/helper/logger.js');
const validation = require('./src/helper/validationUtils.js'); const validation = require('./src/helper/validationUtils.js');
const configUtils = require('./src/helper/configUtils.js'); const configUtils = require('./src/helper/configUtils.js');
const assertions = require('./src/helper/assertionUtils.js')
// Domain-specific modules // Domain-specific modules
const { MeasurementContainer } = require('./src/measurements/index.js'); const { MeasurementContainer } = require('./src/measurements/index.js');
@@ -34,6 +35,7 @@ module.exports = {
configUtils, configUtils,
logger, logger,
validation, validation,
assertions,
MeasurementContainer, MeasurementContainer,
nrmse, nrmse,
state, state,

View File

@@ -88,11 +88,18 @@ class MeasurementContainer {
return this; return this;
} }
position(positionName) { position(positionValue) {
if (!this._currentVariant) { if (!this._currentVariant) {
throw new Error('Variant must be specified before position'); throw new Error('Variant must be specified before position');
} }
this._currentPosition = positionName;
// Turn string positions into numeric values
if (typeof positionValue == "string") {
positionValue = this._convertPositionStr2Num(positionValue);
}
this._currentPosition = positionValue;
return this; return this;
} }
@@ -243,10 +250,12 @@ class MeasurementContainer {
const savedPosition = this._currentPosition; const savedPosition = this._currentPosition;
// Get upstream and downstream measurements // Get upstream and downstream measurements
this._currentPosition = 'upstream'; const positions = this.getPositions();
this._currentPosition = Math.min(...positions);
const upstream = this.get(); const upstream = this.get();
this._currentPosition = 'downstream'; this._currentPosition = Math.max(...positions);
const downstream = this.get(); const downstream = this.get();
this._currentPosition = savedPosition; this._currentPosition = savedPosition;
@@ -319,7 +328,7 @@ class MeasurementContainer {
Object.keys(this.measurements[this._currentType]) : []; Object.keys(this.measurements[this._currentType]) : [];
} }
getPositions() { getPositions(asNumber = false) {
if (!this._currentType || !this._currentVariant) { if (!this._currentType || !this._currentVariant) {
throw new Error('Type and variant must be specified before listing positions'); throw new Error('Type and variant must be specified before listing positions');
} }
@@ -329,7 +338,11 @@ class MeasurementContainer {
return []; return [];
} }
return Object.keys(this.measurements[this._currentType][this._currentVariant]); if (asNumber) {
return Object.keys(this.measurements[this._currentType][this._currentVariant]);
}
return Object.keys(this.measurements[this._currentType][this._currentVariant]).map(this._convertPositionNum2Str);
} }
clear() { clear() {
@@ -404,6 +417,34 @@ class MeasurementContainer {
} }
} }
_convertPositionStr2Num(positionString) {
switch(positionString) {
case "atEquipment":
return 0;
case "upstream":
return Number.POSITIVE_INFINITY;
case "downstream":
return Number.NEGATIVE_INFINITY;
default:
this.logger.error(`Invalid positionVsParent provided: ${positionString}`);
return;
}
}
_convertPositionNum2Str(positionValue) {
if (positionValue == 0) {
return "atEquipment";
}
if (positionValue < 0) {
return "upstream";
}
if (positionValue > 0) {
return "downstream";
}
this.logger.error(`Invalid position provided: ${positionValue}`);
}
} }
module.exports = MeasurementContainer; module.exports = MeasurementContainer;