pddl-workspace
Version:
263 lines • 8.98 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.TimedVariableValue = exports.ProblemInfo = exports.Metric = exports.MetricDirection = exports.SupplyDemand = exports.UnsupportedVariableValue = exports.VariableValue = void 0;
const FileInfo_1 = require("./FileInfo");
const PddlSyntaxTree_1 = require("./parser/PddlSyntaxTree");
const DocumentPositionResolver_1 = require("./DocumentPositionResolver");
const DomainInfo_1 = require("./DomainInfo");
const language_1 = require("./language");
/**
* Variable value initialization in the problem file.
*/
class VariableValue {
constructor(variableName, value) {
this.variableName = variableName;
this.value = value;
}
getVariableName() {
return this.variableName;
}
getValue() {
return this.value;
}
negate() {
return new VariableValue(this.variableName, !this.value);
}
get isSupported() {
return true;
}
}
exports.VariableValue = VariableValue;
class UnsupportedVariableValue extends VariableValue {
constructor(text) {
super(text, false);
}
negate() {
return this;
}
get isSupported() {
return false;
}
}
exports.UnsupportedVariableValue = UnsupportedVariableValue;
/**
* Supply-demand contract.
*/
class SupplyDemand {
constructor(name) {
this.name = name;
}
getName() {
return this.name;
}
}
exports.SupplyDemand = SupplyDemand;
var MetricDirection;
(function (MetricDirection) {
MetricDirection[MetricDirection["MINIMIZE"] = 0] = "MINIMIZE";
MetricDirection[MetricDirection["MAXIMIZE"] = 1] = "MAXIMIZE";
})(MetricDirection || (exports.MetricDirection = MetricDirection = {}));
class Metric {
constructor(direction, expression, location, documentation) {
this.direction = direction;
this.expression = expression;
this.location = location;
this.documentation = documentation;
}
getDirection() {
return this.direction;
}
getExpression() {
return this.expression;
}
getLocation() {
return this.location;
}
getDocumentation() {
return this.documentation;
}
}
exports.Metric = Metric;
/**
* Problem file.
*/
class ProblemInfo extends FileInfo_1.FileInfo {
constructor(fileUri, version, problemName, domainName, syntaxTree, positionResolver) {
super(fileUri, version, problemName, syntaxTree, positionResolver);
this.domainName = domainName;
this.syntaxTree = syntaxTree;
this.objects = new DomainInfo_1.TypeObjectMap();
this.inits = [];
this.supplyDemands = [];
this.constraints = [];
this.metrics = [];
}
/**
* Copy constructor for re-constructing the problem from a serialized form.
* @param problem de-serialized problem
* @param domainName domain name
*/
static clone(problem, domainName) {
const clonedProblem = new ProblemInfo(problem.fileUri, Number.NaN, problem.name, domainName !== null && domainName !== void 0 ? domainName : problem.domainName, PddlSyntaxTree_1.PddlSyntaxTree.EMPTY, new DocumentPositionResolver_1.SimpleDocumentPositionResolver(''));
clonedProblem.setConstraints(problem.constraints);
clonedProblem.setInits(problem.inits.map(i => TimedVariableValue.copy(i)));
clonedProblem.setMetrics(problem.metrics);
clonedProblem.setObjects(DomainInfo_1.TypeObjectMap.clone(problem.objects));
clonedProblem.setSupplyDemands(problem.supplyDemands);
return clonedProblem;
}
/**
* Copy constructor for deriving problem file with new initial state (preserving TILs after the `newStateTime`).
* @param origProblem original (de-serialized) problem
* @param newState new initial state
* @param newStateTime time at which the new state is effective
*/
static cloneWithInitStateAt(origProblem, newState, newStateTime = 0) {
const origProblemText = origProblem.getText();
const initNode = origProblem.syntaxTree.getDefineNodeOrThrow().getFirstOpenBracket(':init');
if (origProblem.getSupplyDemands().length) {
throw new Error("Problems with supply-demand are not supported.");
}
// some TILs from the original problem are still in the future
const futureTils = origProblem.getInits()
// filter original TILs occurring after `newStateTime`
.filter(til => til.getTime() > newStateTime);
const newStateWithTils = newState.concat(futureTils)
.map(til => TimedVariableValue.copy(til));
newStateWithTils.forEach(til => til.setTime(til.getTime() - newStateTime));
const newInit = newStateWithTils.map(tiv => tiv.toPddlString()).join('\n\t');
const newProblemText = origProblemText.substring(0, initNode.getStart())
+ '\n(:init\n' + newInit + '\n)'
+ origProblemText.substring(initNode.getEnd());
return newProblemText;
}
setPreParsingPreProcessor(preProcessor) {
this.preParsingPreProcessor = preProcessor;
}
getPreParsingPreProcessor() {
return this.preParsingPreProcessor;
}
getLanguage() {
return language_1.PddlLanguage.PDDL;
}
setObjects(objects) {
this.objects = objects;
}
getObjects(type) {
var _a, _b;
return (_b = ((_a = this.objects.getTypeCaseInsensitive(type)) === null || _a === void 0 ? void 0 : _a.getObjects())) !== null && _b !== void 0 ? _b : [];
}
getObjectsTypeMap() {
return this.objects;
}
/**
* Sets predicate/function initial values.
* @param inits initial values
*/
setInits(inits) {
this.inits = inits;
}
/**
* Returns variable initial values and time-initial literals/fluents.
*/
getInits() {
return this.inits;
}
setSupplyDemands(supplyDemands) {
this.supplyDemands = supplyDemands;
}
getSupplyDemands() {
return this.supplyDemands;
}
setConstraints(constraints) {
this.constraints = constraints;
}
getConstraints() {
return this.constraints;
}
setMetrics(metrics) {
this.metrics = metrics;
}
getMetrics() {
return this.metrics;
}
isProblem() {
return true;
}
}
exports.ProblemInfo = ProblemInfo;
/**
* Variable value effective from certain time, e.g. initialization of the variable in the problem file.
*/
class TimedVariableValue {
constructor(time, variableName, value, _isSupported = true) {
this.time = time;
this.variableName = variableName;
this.value = value;
this._isSupported = _isSupported;
}
static from(time, value) {
return new TimedVariableValue(time, value.getVariableName(), value.getValue(), value.isSupported);
}
/**
* Makes a deep copy of the supplied value and returns a new instance
* @param value value to copy from
*/
static copy(value) {
return new TimedVariableValue(value.time, value.variableName, value.value, value.isSupported);
}
get isSupported() {
return this._isSupported;
}
getTime() {
return this.time;
}
setTime(time) {
this.time = time;
}
getVariableName() {
return this.variableName;
}
getLiftedVariableName() {
return this.variableName.split(' ')[0];
}
getValue() {
return this.value;
}
/**
* Updates this value.
* @param newValue new value
*/
update(time, newValue) {
this.time = time;
this.value = newValue.getValue();
}
/**
* Determines whether the variable name and value are the same, ignoring the timestamp.
* @param other other timed variable value
*/
sameValue(other) {
return this.getVariableName() === other.getVariableName()
&& this.getValue() === other.getValue();
}
getVariableValue() {
return new VariableValue(this.variableName, this.value);
}
toString() {
return `${this.variableName}=${this.value} @ ${this.time}`;
}
toPddlString() {
const valuePddl = typeof this.value == "boolean" ?
this.value === true ? `(${this.variableName})` : `(not (${this.variableName}))` :
`(= (${this.variableName}) ${this.value})`;
return this.time <= 0 ?
valuePddl
: `(at ${this.time} ${valuePddl})`;
}
}
exports.TimedVariableValue = TimedVariableValue;
//# sourceMappingURL=ProblemInfo.js.map