@syntest/core
Version:
The common core of the SynTest Framework
144 lines • 5.9 kB
JavaScript
;
/*
* Copyright 2020-2021 Delft University of Technology and SynTest contributors
*
* This file is part of SynTest Framework - SynTest Core.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
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.ObjectiveManager = void 0;
const Archive_1 = require("../../Archive");
const ExceptionObjectiveFunction_1 = require("../ExceptionObjectiveFunction");
const crypto = require("crypto");
/**
* Manager that keeps track of which objectives have been covered and are still to be searched.
*
* @author Mitchell Olsthoorn
*/
class ObjectiveManager {
/**
* Constructor.
*
* @param runner Encoding runner
* @protected
*/
constructor(runner) {
this._archive = new Archive_1.Archive();
this._currentObjectives = new Set();
this._coveredObjectives = new Set();
this._uncoveredObjectives = new Set();
this._runner = runner;
}
/**
* Evaluate multiple encodings on the current objectives.
*
* @param encodings The encoding to evaluate
* @param budgetManager The budget manager to track the remaining budget
* @param terminationManager The termination trigger manager
*/
evaluateMany(encodings, budgetManager, terminationManager) {
return __awaiter(this, void 0, void 0, function* () {
for (const encoding of encodings) {
// If there is no budget left or a termination trigger has been triggered, stop evaluating
if (!budgetManager.hasBudgetLeft() || terminationManager.isTriggered())
break;
yield this.evaluateOne(encoding, budgetManager, terminationManager);
}
});
}
/**
* Evaluate one encoding on the current objectives.
*
* @param encoding The encoding to evaluate
* @param budgetManager The budget manager to track evaluation
* @param terminationManager The termination trigger manager
*/
evaluateOne(encoding, budgetManager,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
terminationManager) {
return __awaiter(this, void 0, void 0, function* () {
// Execute the encoding
const result = yield this._runner.execute(this._subject, encoding);
budgetManager.evaluation(encoding);
// Store the execution result in the encoding
encoding.setExecutionResult(result);
// For all current objectives
this._currentObjectives.forEach((objectiveFunction) => {
// Calculate and store the distance
const distance = objectiveFunction.calculateDistance(encoding);
encoding.setDistance(objectiveFunction, distance);
// Update the objectives
this._updateObjectives(objectiveFunction, encoding, distance);
});
// Create separate exception objective when an exception occurred in the execution
if (result.hasExceptions()) {
// TODO there must be a better way
// investigate error patterns somehow
const hash = crypto
.createHash("md5")
.update(result.getExceptions())
.digest("hex");
const numOfExceptions = this._archive
.getObjectives()
.filter((objective) => objective instanceof ExceptionObjectiveFunction_1.ExceptionObjectiveFunction)
.filter((objective) => objective.getIdentifier() === hash).length;
if (numOfExceptions === 0) {
// TODO this makes the archive become too large crashing the tool
this._archive.update(new ExceptionObjectiveFunction_1.ExceptionObjectiveFunction(this._subject, hash, result.getExceptions()), encoding);
}
}
});
}
/**
* Return the uncovered objectives.
*/
getUncoveredObjectives() {
return this._uncoveredObjectives;
}
/**
* Return the current objectives.
*/
getCurrentObjectives() {
return this._currentObjectives;
}
/**
* Return the covered objectives.
*/
getCoveredObjectives() {
return this._coveredObjectives;
}
/**
* Return the archive.
*/
getArchive() {
return this._archive;
}
/**
* Determines if there are objectives left to cover.
*/
hasObjectives() {
return this._currentObjectives.size > 0;
}
}
exports.ObjectiveManager = ObjectiveManager;
//# sourceMappingURL=ObjectiveManager.js.map