pddl-workspace
Version:
69 lines • 3.2 kB
JavaScript
;
/* --------------------------------------------------------------------------------------------
* Copyright (c) Jan Dolejsi 2021. 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.NormalizingPddlPlanParser = void 0;
const PlanStep_1 = require("../PlanStep");
const PddlPlanBuilder_1 = require("./PddlPlanBuilder");
const PddlPlanParser_1 = require("./PddlPlanParser");
/** Parsers the plan lines, offsets the times (by epsilon, if applicable) and returns the lines normalized. */
class NormalizingPddlPlanParser {
constructor(epsilon) {
this.epsilon = epsilon;
this.makespan = 0;
this.timeOffset = 0;
this.firstLineParsed = false;
}
/**
* Parses and normalizes the plan text (typically for plan comparison).
* If the plan starts at time zero, it is shifted to time `epsilon`.
*
* @param origText original plan text
* @param endl platform-specific end-line characters
*/
normalize(origText, endl) {
const compare = function (step1, step2) {
if (step1.getStartTime() !== step2.getStartTime()) {
return step1.getStartTime() - step2.getStartTime();
}
else {
return step1.fullActionName.localeCompare(step2.fullActionName);
}
};
const planMeta = PddlPlanParser_1.PddlPlanParser.parsePlanMeta(origText);
const planParser = new PddlPlanParser_1.PddlPlanParser();
const planBuilder = new PddlPlanBuilder_1.PddlPlanBuilder(this.epsilon);
const normalizedText = PddlPlanParser_1.PddlPlanParser.getPlanMeta(planMeta.domainName, planMeta.problemName, endl)
+ `${endl}; Normalized plan:${endl}`
+ origText.split('\n')
.map((origLine, idx) => this.normalizeLine(origLine, idx, planParser, planBuilder))
.filter(step => step !== undefined)
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
.map(step => step)
.sort(compare)
.map(step => step.toPddl())
.join(endl);
return normalizedText;
}
normalizeLine(line, lineIdx, planParser, planBuilder) {
const planStep = planParser.parse(line, lineIdx, planBuilder);
if (!planStep) {
return undefined;
}
else {
// this line is a plan step
const time = planStep.getStartTime();
if (!this.firstLineParsed) {
if (time === 0) {
this.timeOffset = -this.epsilon;
}
this.firstLineParsed = true;
}
return new PlanStep_1.PlanStep(time - this.timeOffset, planStep.getFullActionName(), planStep.isDurative, planStep.getDuration(), planStep.lineIndex);
}
}
}
exports.NormalizingPddlPlanParser = NormalizingPddlPlanParser;
//# sourceMappingURL=NormalizingPddlPlanParser.js.map