pddl-workspace
Version:
376 lines • 13.4 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.UnrecognizedStructure = exports.Job = exports.DurativeAction = exports.InstantAction = exports.Action = exports.PddlDomainConstruct = exports.DomainInfo = exports.TypeObjectMap = exports.TypeObjects = void 0;
const DirectionalGraph_1 = require("./utils/DirectionalGraph");
const serializationUtils_1 = require("./utils/serializationUtils");
const FileInfo_1 = require("./FileInfo");
const language_1 = require("./language");
const PddlSyntaxTree_1 = require("./parser/PddlSyntaxTree");
const DocumentPositionResolver_1 = require("./DocumentPositionResolver");
const PddlTokenizer_1 = require("./parser/PddlTokenizer");
/**
* Holds objects belonging to the same type.
*/
class TypeObjects {
constructor(type) {
this.type = type;
this.objects = new Set();
}
getObjects() {
return [...this.objects.keys()];
}
addObject(obj) {
this.objects.add(obj);
return this;
}
addAllObjects(objects) {
objects.forEach(o => this.addObject(o));
return this;
}
hasObjectCaseInsensitive(objectName) {
return [...this.objects.keys()]
.some(o => o.toLowerCase() === objectName.toLowerCase());
}
hasObject(objectName) {
return this.objects.has(objectName);
}
getObjectInstance(objectName) {
return new language_1.ObjectInstance(objectName, this.type);
}
}
exports.TypeObjects = TypeObjects;
class TypeObjectMap {
constructor() {
this.typeNameToTypeObjectMap = new Map();
this.objectNameToTypeObjectMap = new Map();
this.objectNameCaseInsensitiveToTypeObjectMap = new Map();
}
/**
* Re-hydrates de-serialized type-object map
* @param other de-serialized
*/
static clone(other) {
const map = new TypeObjectMap();
const otherMap = (0, serializationUtils_1.objToStrMap)(other);
otherMap.forEach((typeObjects) => {
map.addAll(typeObjects.type, typeObjects.objects);
});
return map;
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
toJSON(_key) {
return (0, serializationUtils_1.asSerializable)(this.typeNameToTypeObjectMap);
}
get length() {
return this.typeNameToTypeObjectMap.size;
}
merge(other) {
const mergedMap = new TypeObjectMap();
this.valuesArray().concat(other.valuesArray())
.forEach(typeObj => mergedMap.addAll(typeObj.type, typeObj.getObjects()));
return mergedMap;
}
/**
* Adds object to type.
* @param type type name (currently case insensitive)
* @param objectName object name (currently case sensitive)
*/
add(type, objectName) {
this._upsert(type, typeObjects => {
typeObjects.addObject(objectName);
// store map of object-to-type
this.objectNameCaseInsensitiveToTypeObjectMap.set(objectName.toLowerCase(), typeObjects);
this.objectNameToTypeObjectMap.set(objectName, typeObjects);
});
return this;
}
addAll(type, objects) {
this._upsert(type, typeObjects => {
typeObjects.addAllObjects(objects);
// store map of object-to-type
objects.forEach(objName => {
this.objectNameCaseInsensitiveToTypeObjectMap.set(objName.toLowerCase(), typeObjects);
this.objectNameToTypeObjectMap.set(objName, typeObjects);
});
});
return this;
}
_upsert(type, inserter) {
var _a;
const typeFound = (_a = this.getTypeCaseInsensitive(type)) !== null && _a !== void 0 ? _a : new TypeObjects(type);
inserter.apply(this, [typeFound]);
this.typeNameToTypeObjectMap.set(type.toLowerCase(), typeFound);
}
valuesArray() {
return [...this.typeNameToTypeObjectMap.values()];
}
/**
* Returns the `TypeObjects` structure for the lower-cased `type` supplied.
* @param type case-insensitive type name
*/
getTypeCaseInsensitive(type) {
return this.typeNameToTypeObjectMap.get(type.toLowerCase());
}
/**
* Returns the `TypeObjects` structure for a type that has the `objectName` supplied.
* @param objectName case-sensitive object name
*/
getTypeOf(objectName) {
return this.objectNameToTypeObjectMap.get(objectName);
}
/**
* Returns the `TypeObjects` structure for a type that has lower-cased `objectName` supplied.
* @param objectName case-insensitive object name
*/
getTypeOfCaseInsensitive(objectName) {
return this.objectNameCaseInsensitiveToTypeObjectMap.get(objectName.toLowerCase());
}
}
exports.TypeObjectMap = TypeObjectMap;
/**
* Domain file.
*/
class DomainInfo extends FileInfo_1.FileInfo {
constructor(fileUri, version, domainName, syntaxTree, positionResolver) {
super(fileUri, version, domainName, syntaxTree, positionResolver);
this.syntaxTree = syntaxTree;
this.predicates = [];
this.functions = [];
this.derived = [];
this.actions = [];
this.typeInheritance = new DirectionalGraph_1.DirectionalGraph();
this.typeLocations = new Map();
this.constants = new TypeObjectMap();
this.constraints = [];
this.TYPES_SECTION_START = "(:types";
}
/**
* Copy constructor for re-constructing the domain from a serialized form.
* @param domain de-serialized domain
*/
static clone(domain) {
const clonedDomain = new DomainInfo(domain.fileUri, Number.NaN, domain.name, PddlSyntaxTree_1.PddlSyntaxTree.EMPTY, new DocumentPositionResolver_1.SimpleDocumentPositionResolver(''));
clonedDomain.setActions(domain.actions.map(a => this.cloneAction(a)));
clonedDomain.setConstants(TypeObjectMap.clone(domain.constants));
clonedDomain.setConstraints(domain.constraints);
clonedDomain.setDerived(domain.derived);
domain.events && clonedDomain.setEvents(domain.events);
clonedDomain.setFunctions(domain.functions, domain.functionsNode);
clonedDomain.setPredicates(domain.predicates, domain.predicatesNode);
domain.processes && clonedDomain.setProcesses(domain.processes);
// todo: domain.requirements && clonedDomain.setRequirements(domain.getRequirements());
clonedDomain.setTypeInheritance(DirectionalGraph_1.DirectionalGraph.fromGraph(domain.typeInheritance), domain.typesNode);
return clonedDomain;
}
static cloneAction(action) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const actionAny = action;
if ('duration' in actionAny) {
return new DurativeAction(action.name, action.parameters, DocumentPositionResolver_1.PddlRange.createUnknown());
}
else {
return new InstantAction(action.name, action.parameters, DocumentPositionResolver_1.PddlRange.createUnknown());
}
}
getLanguage() {
return language_1.PddlLanguage.PDDL;
}
getPredicates() {
return this.predicates;
}
getPredicatesNode() {
return this.predicatesNode;
}
setPredicates(predicates, predicatesNode) {
this.predicates = predicates;
this.predicatesNode = predicatesNode;
}
injectPredicates(injection) {
this.predicates.push(...injection.variables);
this.getCompilations().add(injection);
}
getFunctions() {
return this.functions;
}
getFunctionsNode() {
return this.functionsNode;
}
setFunctions(functions, functionsNode) {
this.functions = functions;
this.functionsNode = functionsNode;
}
injectFunctions(injection) {
this.functions.push(...injection.variables);
this.getCompilations().add(injection);
}
getFunction(liftedVariableName) {
return this.functions
.find(variable => variable.name.toLocaleLowerCase() === liftedVariableName.toLocaleLowerCase());
}
getLiftedFunction(groundedVariable) {
return this.getFunction(groundedVariable.name);
}
getDerived() {
return this.derived;
}
setDerived(derived) {
this.derived = derived;
}
setActions(actions) {
this.actions = actions;
}
getActions() {
return this.actions;
}
getJobs() {
return this.actions.filter(a => a instanceof Job);
}
getStructures() {
const structures = new Array();
structures.push(...this.getActions());
const events = this.getEvents();
events && structures.push(...events);
const processes = this.getProcesses();
processes && structures.push(...processes);
const jobs = this.getJobs();
jobs && structures.push(...jobs);
return structures;
}
getTypeInheritance() {
return this.typeInheritance;
}
getTypesNode() {
return this.typesNode;
}
setTypeInheritance(typeInheritance, typesNode, positionResolver) {
this.typeInheritance = typeInheritance;
this.typesNode = typesNode;
if (typesNode && positionResolver) {
this.getTypes().forEach(typeName => {
const typeNode = typesNode.getFirstChild(PddlTokenizer_1.PddlTokenType.Other, new RegExp("^" + typeName + "$"));
if (typeNode) {
const range = new DocumentPositionResolver_1.PddlRange({
start: positionResolver.resolveToPosition(typeNode.getStart()),
end: positionResolver.resolveToPosition(typeNode.getEnd())
});
this.typeLocations.set(typeName, range);
}
});
}
}
setConstants(constants, constantsNode) {
this.constants = constants;
this.constantsNode = constantsNode;
}
getConstants() {
return this.constants;
}
getConstantsNode() {
return this.constantsNode;
}
getTypes() {
return this.typeInheritance.getVertices()
.filter(t => t.toLowerCase() !== "object");
}
getTypesInclObject() {
return this.typeInheritance.getVertices();
}
isDomain() {
return true;
}
getTypesInheritingFrom(type) {
return this.typeInheritance.getSubtreePointingTo(type);
}
getTypesInheritingFromPlusSelf(type) {
return [type].concat(this.typeInheritance.getSubtreePointingTo(type));
}
getEvents() {
return this.events;
}
setEvents(events) {
this.events = events;
}
getProcesses() {
return this.processes;
}
setProcesses(processes) {
this.processes = processes;
}
getConstraints() {
return this.constraints;
}
setConstraints(constraints) {
this.constraints = constraints;
}
getTypeLocation(type) {
return this.typeLocations.get(type);
}
}
exports.DomainInfo = DomainInfo;
class PddlDomainConstruct {
constructor(name, parameters, location) {
this.name = name;
this.parameters = parameters;
this.location = location;
this.documentation = []; // initialized lazily
}
getLocation() {
return this.location;
}
setDocumentation(documentation) {
this.documentation = documentation;
}
getDocumentation() {
return this.documentation;
}
getNameOrEmpty() {
var _a;
return (_a = this.name) !== null && _a !== void 0 ? _a : '';
}
}
exports.PddlDomainConstruct = PddlDomainConstruct;
class Action extends PddlDomainConstruct {
}
exports.Action = Action;
class InstantAction extends Action {
constructor(name, parameters, location, preCondition, effect) {
super(name, parameters, location);
this.preCondition = preCondition;
this.effect = effect;
}
isDurative() {
return false;
}
}
exports.InstantAction = InstantAction;
class DurativeAction extends Action {
constructor(name, parameters, location, actionNode, parametersNode, duration, condition, effect) {
super(name, parameters, location);
this.actionNode = actionNode;
this.parametersNode = parametersNode;
this.duration = duration;
this.condition = condition;
this.effect = effect;
}
isDurative() {
return true;
}
}
exports.DurativeAction = DurativeAction;
class Job extends DurativeAction {
constructor(name, parameters, location, actionNode, parametersNode, duration, condition, effect) {
super(name, parameters, location, actionNode, parametersNode, duration, condition, effect);
}
}
exports.Job = Job;
class UnrecognizedStructure extends PddlDomainConstruct {
constructor(range) {
super("unrecognized", [], range);
}
}
exports.UnrecognizedStructure = UnrecognizedStructure;
//# sourceMappingURL=DomainInfo.js.map