pddl-workspace
Version:
81 lines • 3.86 kB
JavaScript
;
/* --------------------------------------------------------------------------------------------
* Copyright (c) Jan Dolejsi. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
* ------------------------------------------------------------------------------------------ */
Object.defineProperty(exports, "__esModule", { value: true });
exports.PlanValuesParser = void 0;
class StateValues {
constructor(time) {
this.time = time;
this.values = new Map();
}
setValue(functionName, value) {
this.values.set(functionName, value);
}
getValue(functionName) {
var _a;
return (_a = this.values.get(functionName)) !== null && _a !== void 0 ? _a : Number.NaN;
}
}
class PlanValuesParser {
constructor(steps, functions, actionFunctionValues) {
this.steps = steps;
this.functions = functions;
this.stateValues = [];
if (steps.length !== actionFunctionValues.length) {
throw new Error("Plan steps and action values do not correspond.");
}
steps.forEach((planStep, idx) => {
var _a;
const planStepFunctionValues = actionFunctionValues[idx].split(',');
if (!this.describesInstantaneousAction(planStepFunctionValues)
&& !this.describesDurativeAction(planStepFunctionValues)) {
throw new Error("Wrong number of values on in the output: " + actionFunctionValues[idx]);
}
if (planStep.fullActionName !== planStepFunctionValues[0]) {
throw new Error("Action name does not match the one in the plan: " + actionFunctionValues[idx]);
}
this.addState(planStep.getStartTime(), planStepFunctionValues.slice(1, 1 + functions.length));
if (this.describesDurativeAction(planStepFunctionValues)) {
this.addState(planStep.getStartTime() + ((_a = planStep.getDuration()) !== null && _a !== void 0 ? _a : 1e-3), planStepFunctionValues.slice(1 + functions.length));
}
});
this.stateValues = this.stateValues.sort((s1, s2) => s1.time - s2.time);
}
describesInstantaneousAction(planStepFunctionValues) {
return this.compareCsvTermsToFunctionCount(planStepFunctionValues, 1);
}
describesDurativeAction(planStepFunctionValues) {
return this.compareCsvTermsToFunctionCount(planStepFunctionValues, 2);
}
compareCsvTermsToFunctionCount(planStepFunctionValues, multiple) {
return planStepFunctionValues.length === 1 + multiple * this.functions.length;
}
addState(time, values) {
const state = new StateValues(time);
if (values.length !== this.functions.length) {
throw new Error(`Expecting number of values (${values}) to match number of functions ${this.functions.length}.`);
}
for (let index = 0; index < values.length; index++) {
const valueAsString = values[index];
const value = parseFloat(valueAsString);
state.setValue(this.functions[index].getFullName(), value);
}
this.stateValues.push(state);
}
getSingleFunctionValues(functionName) {
return this.stateValues.map(state => [state.time, state.getValue(functionName)]);
}
getValues(functionName) {
const functions = this.functions.filter(f => f.name === functionName);
return this.stateValues.map(state => this.getStateValues(functions, state));
}
getStateValues(groundedFunctions, state) {
const values = [state.time];
groundedFunctions.forEach(function1 => values.push(state.getValue(function1.getFullName())));
return values;
}
}
exports.PlanValuesParser = PlanValuesParser;
//# sourceMappingURL=PlanValuesParser.js.map