UNPKG

@syntest/core

Version:

The common core of the SynTest Framework

186 lines 7.62 kB
"use strict"; 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.PluginManager = void 0; class PluginManager { constructor() { this._listeners = new Map(); this._searchAlgorithms = new Map(); this._crossoverOperators = new Map(); this._samplers = new Map(); this._terminationTriggers = new Map(); this._userInterfaces = new Map(); } getListeners() { return [...this._listeners.keys()]; } getSearchAlgorithms() { return [...this._searchAlgorithms.keys()]; } getCrossoverOperators() { return [...this._crossoverOperators.keys()]; } getSamplers() { return [...this._samplers.keys()]; } getTerminationTriggers() { return [...this._terminationTriggers.keys()]; } getUserInterfaces() { return [...this._userInterfaces.keys()]; } getListener(name) { return this._listeners.get(name); } getSearchAlgorithm(name) { return this._searchAlgorithms.get(name); } getCrossoverOperator(name) { return this._crossoverOperators.get(name); } getSampler(name) { return this._samplers.get(name); } getTerminationTrigger(name) { return this._terminationTriggers.get(name); } getUserInterface(name) { return this._userInterfaces.get(name); } addPluginOptions(yargs) { return __awaiter(this, void 0, void 0, function* () { yargs = yield this._addPluginOptionsSpecific(yargs, this._listeners); yargs = yield this._addPluginOptionsSpecific(yargs, this._searchAlgorithms); yargs = yield this._addPluginOptionsSpecific(yargs, this._crossoverOperators); yargs = yield this._addPluginOptionsSpecific(yargs, this._samplers); yargs = yield this._addPluginOptionsSpecific(yargs, this._terminationTriggers); yargs = yield this._addPluginOptionsSpecific(yargs, this._userInterfaces); return yargs; }); } _addPluginOptionsSpecific(yargs, plugins) { return __awaiter(this, void 0, void 0, function* () { for (const plugin of plugins.values()) { if (plugin.getConfig) { const options = yield plugin.getConfig(); for (const option of options.keys()) { yargs = yargs.option(`${plugin.name}-${option}`, options.get(option)); } } } return yargs; }); } cleanup() { return __awaiter(this, void 0, void 0, function* () { yield this._cleanupSpecific(this._listeners); yield this._cleanupSpecific(this._searchAlgorithms); yield this._cleanupSpecific(this._crossoverOperators); yield this._cleanupSpecific(this._samplers); yield this._cleanupSpecific(this._terminationTriggers); yield this._cleanupSpecific(this._userInterfaces); }); } _cleanupSpecific(plugins) { return __awaiter(this, void 0, void 0, function* () { for (const plugin of plugins.values()) { if (plugin.cleanup) { yield plugin.cleanup(); } } }); } prepare() { return __awaiter(this, void 0, void 0, function* () { yield this._prepareSpecific(this._listeners); yield this._prepareSpecific(this._searchAlgorithms); yield this._prepareSpecific(this._crossoverOperators); yield this._prepareSpecific(this._samplers); yield this._prepareSpecific(this._terminationTriggers); yield this._prepareSpecific(this._userInterfaces); }); } _prepareSpecific(plugins) { return __awaiter(this, void 0, void 0, function* () { for (const plugin of plugins.values()) { if (plugin.prepare) { yield plugin.prepare(); } } }); } loadPlugin(pluginPath) { return __awaiter(this, void 0, void 0, function* () { var _a; try { const { plugin } = yield (_a = pluginPath, Promise.resolve().then(() => require(_a))); const pluginInstance = new plugin.default(); if (!pluginInstance.register) { throw new Error(`Could not load plugin\nPlugin has no register function\nPlugin: ${pluginPath}`); } pluginInstance.register(this); } catch (e) { console.trace(e); } }); } registerListener(plugin) { return __awaiter(this, void 0, void 0, function* () { if (this._listeners.has(plugin.name)) { throw new Error(`Plugin with name: ${plugin.name} is already registered as a listener plugin.`); } this._listeners.set(plugin.name, plugin); }); } registerSearchAlgorithm(plugin) { return __awaiter(this, void 0, void 0, function* () { if (this._searchAlgorithms.has(plugin.name)) { throw new Error(`Plugin with name: ${plugin.name} is already registered as a search algorithm plugin.`); } this._searchAlgorithms.set(plugin.name, plugin); }); } registerCrossover(plugin) { return __awaiter(this, void 0, void 0, function* () { if (this._crossoverOperators.has(plugin.name)) { throw new Error(`Plugin with name: ${plugin.name} is already registered as a crossover plugin.`); } this._crossoverOperators.set(plugin.name, plugin); }); } registerSampler(plugin) { return __awaiter(this, void 0, void 0, function* () { if (this._samplers.has(plugin.name)) { throw new Error(`Plugin with name: ${plugin.name} is already registered as a sampler plugin.`); } this._samplers.set(plugin.name, plugin); }); } registerTermination(plugin) { return __awaiter(this, void 0, void 0, function* () { if (this._terminationTriggers.has(plugin.name)) { throw new Error(`Plugin with name: ${plugin.name} is already registered as a termination trigger plugin.`); } this._terminationTriggers.set(plugin.name, plugin); }); } registerUserInterface(plugin) { return __awaiter(this, void 0, void 0, function* () { if (this._userInterfaces.has(plugin.name)) { throw new Error(`Plugin with name: ${plugin.name} is already registered as a user-interface plugin.`); } this._userInterfaces.set(plugin.name, plugin); }); } } exports.PluginManager = PluginManager; //# sourceMappingURL=PluginManager.js.map