pddl-workspace
Version:
131 lines • 5.97 kB
JavaScript
"use strict";
/* --------------------------------------------------------------------------------------------
* 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.PlanHappeningsBuilder = void 0;
const FileInfo_1 = require("../FileInfo");
const HappeningsInfo_1 = require("../HappeningsInfo");
const DocumentPositionResolver_1 = require("../DocumentPositionResolver");
/**
* Builds the list of happenings while validating the sequence.
*/
class PlanHappeningsBuilder {
constructor(epsilon) {
this.epsilon = epsilon;
// all happenings parsed thus far
this.happenings = [];
// parsing problems to report
this.parsingProblems = [];
// plan makespan thus far
this.makespan = 0;
// open actions (action starts that were not matched with an end yet)
this.openActions = [];
this.happeningPattern = /^\s*((\d+|\d+\.\d+|\.\d+)\s*:)?\s*(start|end)?\s*\(([\w -]+)\)\s*(#\d+)?\s*(;.*)?\s*$/;
this.whiteSpacePattern = /^\s*(;.*)?\s*$/;
}
tryParseFile(fileText) {
fileText.split('\n')
.forEach((planLine, index) => {
if (!this.isWhiteSpace(planLine)) {
this.tryParse(planLine, index);
}
});
}
isWhiteSpace(planLine) {
this.whiteSpacePattern.lastIndex = 0;
return this.whiteSpacePattern.exec(planLine) !== null;
}
tryParse(planLine, lineIndex) {
const happening = this.parse(planLine, lineIndex);
if (happening) {
this.add(happening);
}
else {
this.parsingProblems.push(new FileInfo_1.ParsingProblem(`Invalid happening syntax: ${planLine}`, "error", DocumentPositionResolver_1.PddlRange.createFullLineRange(lineIndex !== null && lineIndex !== void 0 ? lineIndex : 0)));
}
}
/**
* Parses single line of plan text.
* @param planLine line of plan text
* @param lineIndex line number
*/
parse(planLine, lineIndex) {
this.happeningPattern.lastIndex = 0;
const group = this.happeningPattern.exec(planLine);
if (group) {
// this line is a valid plan happening
const time = group[2] ? parseFloat(group[2]) : this.getMakespan() + this.epsilon;
const type = this.parseType(group[3]);
const action = group[4];
const counter = group[5] ? parseInt(group[5].substring(1)) : 0;
return new HappeningsInfo_1.Happening(time, type, action, counter, lineIndex);
}
else {
return undefined;
}
}
add(happening) {
var _a, _b, _c;
switch (happening.getType()) {
case HappeningsInfo_1.HappeningType.START:
const alreadyExistingStart = this.openActions.concat(this.happenings).find(happening1 => happening1.getType() === HappeningsInfo_1.HappeningType.START
&& happening1.belongsTo(happening));
if (alreadyExistingStart) {
this.parsingProblems.push(new FileInfo_1.ParsingProblem(`A happening matching ${happening.toString()} is already in the plan. Increase the #N counter.`, "error", DocumentPositionResolver_1.PddlRange.createFullLineRange((_a = happening.lineIndex) !== null && _a !== void 0 ? _a : 0)));
}
this.openActions.push(happening);
break;
case HappeningsInfo_1.HappeningType.END:
// there must be an open start
const matchingStart = this.openActions.find(start => start.belongsTo(happening));
if (matchingStart) {
this.openActions.splice(this.openActions.indexOf(matchingStart), 1);
}
else {
this.parsingProblems.push(new FileInfo_1.ParsingProblem(`There is no start corresponding to ${happening.toString()}`, "error", DocumentPositionResolver_1.PddlRange.createFullLineRange((_b = happening.lineIndex) !== null && _b !== void 0 ? _b : 0)));
}
break;
}
// adjust the plan makespan
if (this.makespan < happening.getTime()) {
this.makespan = happening.getTime();
}
else if (this.makespan > happening.getTime()) {
this.parsingProblems.push(new FileInfo_1.ParsingProblem(`Time must not go backwards.`, "warning", DocumentPositionResolver_1.PddlRange.createFullLineRange((_c = happening.lineIndex) !== null && _c !== void 0 ? _c : 0)));
}
this.happenings.push(happening);
}
validateOpenQueueIsEmpty() {
const problems = this.openActions
.map(start => {
var _a;
return new FileInfo_1.ParsingProblem(`Missing end of ${start.toString()}`, "error", DocumentPositionResolver_1.PddlRange.createFullLineRange((_a = start.lineIndex) !== null && _a !== void 0 ? _a : 0));
});
this.parsingProblems.push(...problems);
}
getHappenings() {
return this.happenings;
}
getMakespan() {
return this.makespan;
}
getParsingProblems() {
return this.parsingProblems;
}
parseType(typeAsString) {
switch (typeAsString) {
case "start":
return HappeningsInfo_1.HappeningType.START;
case "end":
return HappeningsInfo_1.HappeningType.END;
case undefined:
return HappeningsInfo_1.HappeningType.INSTANTANEOUS;
default:
throw new Error(`Unexpected happening type: ${typeAsString}`);
}
}
}
exports.PlanHappeningsBuilder = PlanHappeningsBuilder;
//# sourceMappingURL=PlanHappeningsBuilder.js.map