UNPKG

ai-planning-val

Version:

Javascript/typescript wrapper for VAL (AI Planning plan validation and evaluation tools from KCL Planning department and the planning community around the ICAPS conference).

190 lines 8.59 kB
"use strict"; /* -------------------------------------------------------------------------------------------- * Copyright (c) Jan Dolejsi. All rights reserved. * Licensed under the MIT License. See License.txt in the project root for license information. * ------------------------------------------------------------------------------------------ */ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Parser = exports.ParserExitCode = exports.ParserError = void 0; /* eslint-disable @typescript-eslint/no-use-before-define */ const process = __importStar(require("child_process")); const pddl_workspace_1 = require("pddl-workspace"); const ProblemPattern_1 = require("./ProblemPattern"); const valUtils_1 = require("./valUtils"); class ParserError extends Error { constructor(message, domain, problem) { super(message); this.message = message; this.domain = domain; this.problem = problem; } } exports.ParserError = ParserError; class ParserExitCode extends Error { constructor(message) { super(message); } } exports.ParserExitCode = ParserExitCode; /** PDDL domain and problem parser. */ class Parser { constructor(options) { this.options = options; } getSyntax() { return ["$(domain)", "$(problem)"]; } validate(domainInfo, problemInfo) { return __awaiter(this, void 0, void 0, function* () { const domainFilePath = valUtils_1.Util.toPddlFileSync(domainInfo.getCompiledText(), { prefix: "domain" }); const parsingProblems = new ParsingProblems(); if (!problemInfo) { const problemFilePath = valUtils_1.Util.toPddlFileSync(valUtils_1.PddlFactory.createEmptyProblem('dummy', domainInfo.name), { prefix: "problem" }); const context = { domain: domainInfo, problem: problemInfo, fileNameMap: new FileNameToUriMap(domainFilePath, domainInfo.fileUri) }; return yield this.validateProblem(domainFilePath, problemFilePath, context, parsingProblems); } else { const problemFilePath = valUtils_1.Util.toPddlFileSync(problemInfo.getCompiledText(), { prefix: "problem" }); const context = { domain: domainInfo, problem: problemInfo, fileNameMap: new FileNameToUriMap(domainFilePath, domainInfo.fileUri).addProblem(problemFilePath, problemInfo.fileUri) }; return yield this.validateProblem(domainFilePath, problemFilePath, context, parsingProblems); } }); } createPatternMatchers(context) { const filePaths = context.fileNameMap.getFilePaths(); return [new ProblemPattern_1.ProblemPattern(Parser.OUTPUT_PATTERN, filePaths)]; } processOutput(output, context, parsingProblems) { const distinctOutputs = []; const patterns = this.createPatternMatchers(context); patterns.forEach(pattern => { var _a; let match; while (match = pattern.regEx.exec(output)) { // only report each warning/error once if (distinctOutputs.includes(match[0])) { continue; } distinctOutputs.push(match[0]); const uri = context.fileNameMap.getUri(pattern.getFilePath(match)); if (!uri) { continue; } // this is not a file of interest const parsingProblem = new pddl_workspace_1.ParsingProblem(pattern.getMessage(match), pattern.getSeverity(match), pattern.getRange(match)); if (parsingProblems.has(uri)) { (_a = parsingProblems.get(uri)) === null || _a === void 0 ? void 0 : _a.push(parsingProblem); } else { parsingProblems.set(uri, [parsingProblem]); } } }); } validateProblem(domainFilePath, problemFilePath, context, parsingProblems) { return __awaiter(this, void 0, void 0, function* () { const syntaxFragments = this.getSyntax(); if (syntaxFragments.length < 1) { throw new Error('Parser syntax pattern should include $(domain) and $(problem) macros'); } const args = syntaxFragments .map(fragment => { return fragment .replace(/\$\(parser\)/i, "") // ignore '$(parser)' .replace(/\$\(domain\)/i, domainFilePath) .replace(/\$\(problem\)/i, problemFilePath); }); return yield this.runProcess(args, context, parsingProblems); }); } runProcess(args, context, parsingProblems) { return __awaiter(this, void 0, void 0, function* () { return new Promise((resolve, reject) => { var _a; const exePath = (_a = this.options.executablePath) !== null && _a !== void 0 ? _a : "Parser"; // console.log(`${exePath} ` + args.join(' ')); const child = process.spawn(exePath, args, { cwd: this.options.cwd }); let trailingLine = ''; child.stdout.on('data', output => { const outputString = trailingLine + output.toString("utf8"); this.processOutput(outputString, context, parsingProblems); trailingLine = outputString.substring(outputString.lastIndexOf('\n')); }); child.on("error", error => { if (!child.killed) { reject(error.message); } resolve(parsingProblems); }); child.on("close", (code, signal) => { if (code !== 0) { console.warn(`Parser exit code: ${code}, signal: ${signal}.`); } resolve(parsingProblems); }); }); }); } } exports.Parser = Parser; Parser.OUTPUT_PATTERN = `/^($(filePaths))\\s*:\\s*line\\s*:\\s*(\\d*)\\s*:\\s*(Error|Warning)\\s*:\\s*(.*)$/gmi/1,3,2,0,4`; class ParsingProblems extends pddl_workspace_1.utils.StringifyingMap { stringifyKey(key) { return key.toString(); } } class FileNameToUriMap { constructor(domainFilePath, uri) { this.map = new Map(); this.map.set(domainFilePath, uri); } addProblem(problemFilePath, uri) { this.map.set(problemFilePath, uri); return this; } getFilePaths() { return [...this.map.keys()]; } getUri(filePath) { return this.map.get(filePath); } } //# sourceMappingURL=Parser.js.map