pddl-workspace
Version:
52 lines • 2.74 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.PddlInheritanceParser = void 0;
const DirectionalGraph_1 = require("../utils/DirectionalGraph");
const DomainInfo_1 = require("../DomainInfo");
/**
* Planning type/object inheritance parser.
*/
class PddlInheritanceParser {
static parseInheritance(declarationText) {
// the inheritance graph is captured as a two dimensional array, where the first index is the types themselves, the second is the parent type they inherit from (PDDL supports multiple inheritance)
const inheritance = new DirectionalGraph_1.DirectionalGraph();
if (!declarationText) {
return inheritance;
}
// if there are root types that do not inherit from 'object', add the 'object' inheritance.
// it will make the following regex work
if (!declarationText.match(/-\s+\w[\w-]*\s*$/)) {
declarationText += ' - object';
}
const pattern = /(\w[\w-]*\s+)+-\s+\w[\w-]*/g;
let match;
while (match = pattern.exec(declarationText)) {
// is this a group with inheritance?
const fragments = match[0].split(/\s-/);
const parent = fragments.length > 1 ? fragments[1].trim() : undefined;
const children = fragments[0].trim().split(/\s+/g);
children.forEach(childType => inheritance.addEdge(childType, parent));
}
// connect orphan types to the 'object' type
const orphans = inheritance.getVertices()
.filter(v => { var _a, _b; return (_b = !((_a = inheritance.getVerticesWithEdgesFrom(v)) === null || _a === void 0 ? void 0 : _a.length)) !== null && _b !== void 0 ? _b : 0; })
.filter(orphan => orphan !== this.OBJECT);
orphans.forEach(orphan => inheritance.addEdge(orphan, this.OBJECT));
return inheritance;
}
static toTypeObjects(graph) {
const typeObjectMap = new DomainInfo_1.TypeObjectMap();
graph.getVertices().forEach(obj => {
var _a;
(_a = graph.getVerticesWithEdgesFrom(obj)) === null || _a === void 0 ? void 0 : _a.forEach(type => typeObjectMap.add(type, obj));
});
return typeObjectMap;
}
}
exports.PddlInheritanceParser = PddlInheritanceParser;
PddlInheritanceParser.OBJECT = 'object';
//# sourceMappingURL=PddlInheritanceParser.js.map