pddl-workspace
Version:
164 lines • 5.13 kB
JavaScript
;
/* --------------------------------------------------------------------------------------------
* 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.Division = exports.Product = exports.Subtraction = exports.Sum = exports.NAryExpression = exports.BinaryExpression = exports.NumericLiteral = exports.VariableExpression = exports.NumericExpression = exports.ValueMap = void 0;
const language_1 = require("./language");
class ValueMap extends Map {
constructor(...keyValues) {
super(ValueMap.toEntryPairs(keyValues));
}
static toEntryPairs(keyValues) {
if (keyValues.length % 2 === 1) {
throw new Error("Must supply an even number of arguments to eval context");
}
const entries = [];
for (let index = 0; index < keyValues.length; index += 2) {
const key = keyValues[index];
const value = keyValues[index + 1];
if (typeof (key) !== "string") {
throw new Error(`Expect string, but found ${key}`);
}
if (typeof (value) !== "number" && typeof (value) !== "boolean") {
throw new Error(`Expect number|boolean, but found ${value}`);
}
entries.push([key, value]);
}
return entries;
}
}
exports.ValueMap = ValueMap;
/**
* Numeric expression.
*/
class NumericExpression {
getVariableNames() {
return this.getVariables().map(v => v.getFullName());
}
}
exports.NumericExpression = NumericExpression;
class VariableExpression extends NumericExpression {
constructor(name) {
super();
this.name = name;
this.variable = language_1.Variable.fromGrounded(name);
}
getVariables() {
return [this.variable];
}
evaluate(context) {
const value = context.get(this.name);
if (typeof (value) === "number") {
return value;
}
else {
return undefined;
}
}
}
exports.VariableExpression = VariableExpression;
class NumericLiteral extends NumericExpression {
constructor(value) {
super();
this.value = value;
}
getVariables() {
return [];
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
evaluate(context) {
return this.value;
}
}
exports.NumericLiteral = NumericLiteral;
class BinaryExpression extends NumericExpression {
constructor(operator, left, right) {
super();
this.operator = operator;
this.left = left;
this.right = right;
}
getVariables() {
return [this.left, this.right]
.map(child => child.getVariables())
.reduce((prev, curr) => prev.concat(curr), []);
}
}
exports.BinaryExpression = BinaryExpression;
class NAryExpression extends NumericExpression {
constructor(operator, children) {
super();
this.operator = operator;
this.children = children;
}
getVariables() {
return this.children
.map(child => child.getVariables())
.reduce((prev, curr) => prev.concat(curr), []);
}
evaluate(context) {
return this.children
.map(child => child.evaluate(context))
.reduce(this.reduce, this.identity());
}
}
exports.NAryExpression = NAryExpression;
class Sum extends NAryExpression {
constructor(children) {
super("+", children);
}
reduce(left, right) {
if (left !== undefined && right !== undefined) {
return left + right;
}
return undefined;
}
identity() {
return 0;
}
}
exports.Sum = Sum;
class Subtraction extends BinaryExpression {
constructor(left, right) {
super("+", left, right);
}
evaluate(context) {
const leftValue = this.left.evaluate(context);
const rightValue = this.right.evaluate(context);
return leftValue !== undefined && rightValue !== undefined ?
leftValue - rightValue :
undefined;
}
}
exports.Subtraction = Subtraction;
class Product extends NAryExpression {
constructor(children) {
super("*", children);
}
reduce(left, right) {
if (left !== undefined && right !== undefined) {
return left * right;
}
return undefined;
}
identity() {
return 1;
}
}
exports.Product = Product;
class Division extends BinaryExpression {
constructor(left, right) {
super("/", left, right);
}
evaluate(context) {
const leftValue = this.left.evaluate(context);
const rightValue = this.right.evaluate(context);
return leftValue !== undefined && rightValue !== undefined ?
leftValue / rightValue :
undefined;
}
}
exports.Division = Division;
//# sourceMappingURL=NumericExpression.js.map