pddl-workspace
Version:
176 lines • 6.26 kB
JavaScript
;
/* --------------------------------------------------------------------------------------------
* Copyright (c) Jan Dolejsi 2020. 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.ObjectInstance = exports.Parameter = exports.Term = exports.Variable = exports.FileStatus = exports.toLanguageFromId = exports.PddlLanguage = exports.HAPPENINGS = exports.PLAN = exports.PDDL = void 0;
// Language ID of Domain and Problem files
exports.PDDL = 'pddl';
// Language ID of Plan files
exports.PLAN = 'plan';
// Language ID of Happenings files
exports.HAPPENINGS = 'happenings';
var PddlLanguage;
(function (PddlLanguage) {
// domain or problem
PddlLanguage[PddlLanguage["PDDL"] = 0] = "PDDL";
// plan (output of the planner)
PddlLanguage[PddlLanguage["PLAN"] = 1] = "PLAN";
// plan happenings sequence (instantaneous happenings)
PddlLanguage[PddlLanguage["HAPPENINGS"] = 2] = "HAPPENINGS";
})(PddlLanguage || (exports.PddlLanguage = PddlLanguage = {}));
const languageMap = new Map([
[exports.PDDL, PddlLanguage.PDDL],
[exports.PLAN, PddlLanguage.PLAN],
[exports.HAPPENINGS, PddlLanguage.HAPPENINGS]
]);
function toLanguageFromId(languageId) {
return languageMap.get(languageId);
}
exports.toLanguageFromId = toLanguageFromId;
/**
* Status of the file parsing.
*/
var FileStatus;
(function (FileStatus) {
/** File is parsed when the FileInfo object is created. */
FileStatus[FileStatus["Parsed"] = 0] = "Parsed";
/** File was parsed before, but was updated, but not re-parsed yet. */
FileStatus[FileStatus["Dirty"] = 1] = "Dirty";
/** Running external language-specific deep parser/validator. */
FileStatus[FileStatus["Validating"] = 2] = "Validating";
/** Finished running external language-specific deep parser/validator. */
FileStatus[FileStatus["Validated"] = 3] = "Validated";
})(FileStatus || (exports.FileStatus = FileStatus = {}));
/**
* State variable.
*/
class Variable {
/** @deprecated use one of the fromXyz methods or parseVariableDeclaration(fullName) function*/
constructor(declaredName, parameters = []) {
this.declaredName = declaredName;
this.parameters = parameters;
this.documentation = []; // initialized lazily
this.unit = ''; // initialized lazily
this.declaredNameWithoutTypes = declaredName.replace(/\s+-\s+[\w-_]+/gi, '');
this.name = declaredName.replace(/( .*)$/gi, '');
}
static from(name, terms) {
let fullName = name;
if (terms.length) {
terms.forEach(t => fullName += ' ' + t.toPddlString());
}
return new Variable(fullName, terms);
}
static fromGrounded(fullName) {
const fragments = fullName.split(/\s+/);
const name = fragments.shift();
if (!name) {
throw new Error(`Illegal grounded variable name: ${fullName}`);
}
const terms = fragments
.map(o => new ObjectInstance(o, "object"));
return new Variable(name, terms);
}
ground(objects) {
const objectNames = objects.map(o => o.name).join(" ");
if (this.parameters.length !== objects.length) {
throw new Error(`Invalid objects '${objectNames}' for function '${this.getFullName()}' with ${this.parameters.length} parameters.`);
}
let fullName = this.name;
if (objects) {
fullName += " " + objectNames;
}
return new Variable(fullName, objects);
}
bind(parameters) {
const paramDefs = parameters.map(o => o.toPddlString()).join(" ");
if (this.parameters.length !== parameters.length) {
throw new Error(`Invalid parameters '${paramDefs}' for function '${this.getFullName()}' with ${this.parameters.length} parameters.`);
}
let fullName = this.name;
if (parameters) {
fullName += " " + paramDefs;
}
return new Variable(fullName, parameters);
}
getFullName() {
return this.name + this.parameters.map(par => " " + par.toPddlString()).join('');
}
matchesShortNameCaseInsensitive(symbolName) {
return this.name.toLowerCase() === symbolName.toLowerCase();
}
isGrounded() {
return this.parameters.every(parameter => parameter.isGrounded());
}
setDocumentation(documentation) {
this.documentation = documentation;
const match = documentation.join('\n').match(/\[([^\]]*)\]/);
if (match) {
this.unit = match[1];
}
}
getDocumentation() {
return this.documentation;
}
getUnit() {
return this.unit;
}
setLocation(range) {
this.location = range;
}
getLocation() {
return this.location;
}
}
exports.Variable = Variable;
class Term {
constructor(type) {
this.type = type;
}
}
exports.Term = Term;
class Parameter extends Term {
constructor(name, type) {
super(type);
this.name = name;
}
object(objectName) {
return new ObjectInstance(objectName, this.type);
}
isGrounded() { return false; }
toPddlString() {
return `?${this.name} - ${this.type}`;
}
static createPddlString(...params) {
if (params.length === 0) {
return '';
}
let lastType = params[0].type;
const output = [];
const addType = (type) => output.push('- ' + type);
for (const p of params) {
if (p.type !== lastType) {
addType(lastType);
}
lastType = p.type;
output.push('?' + p.name);
}
addType(lastType);
return output.join(' ');
}
}
exports.Parameter = Parameter;
class ObjectInstance extends Term {
constructor(name, type) {
super(type);
this.name = name;
}
toPddlString() {
return this.name;
}
isGrounded() { return true; }
}
exports.ObjectInstance = ObjectInstance;
//# sourceMappingURL=language.js.map