UNPKG

consys

Version:

consys is a flexible tool to evaluate models using generic and readable constraints.

147 lines (146 loc) 4.81 kB
import ConstraintSystemPlugin from './ConstraintSystemPlugin'; import { ConstraintData, EvaluationFilter, FunctionType, Report, StatementType, StatisticsReport } from './Util'; /** * A constraint system with multiple constraints and custom functions, defined for specific model and state types. */ export default class ConstraintSystem<M, S> { private readonly constraints; private readonly functions; private evaluationData; private updatedFunctions; /** * For a given evaluation filter type, return the corresponding function * * @param evaluationFilter filter type * @private */ private static getFilterFunction; /** * Registers a plugin for this constraint system. * * @param plugin plugin to be registered */ registerPlugin(plugin: ConstraintSystemPlugin<M, S>): Promise<void>; /** * Adds a constraint to this constraint system. * * @param resource constraint data */ addConstraint(resource: ConstraintData): void; /** * Adds an array of constraints to this constraint system. * * @param resources constraint data */ addConstraints(resources: ConstraintData[]): void; /** * Adds a custom function to this constraint system. * * @param name name of the function * @param fun custom function */ addFunction(name: string, fun: FunctionType): void; /** * Adds a custom statement (function without any arguments) to this constraint system. * * @param name name of the statement * @param fun custom statement */ addStatement(name: string, fun: StatementType<M, S>): void; /** * Evaluates one or more models and a state given all registered constraints and functions. * * @param model model to be evaluated * @param state state to be evaluated * @param include optionally filter evaluations of reports */ evaluate(model: M | M[], state: S, include?: EvaluationFilter): Report<M, S>[]; /** * Helper function to add a custom function or statement. * * @param name name of the function or statement * @param fun custom function or statement * @private */ private addFun; /** * Filters evaluations of reports based on a given function. * * @param reports reports for which evaluations are filtered * @param evaluationFilter filter function * @private */ private filterReportEvaluation; /** * Evaluates multiple models and a single state. * * @param models models to be evaluated * @param state state to be evaluated * @private */ private evaluateMultiple; /** * Evaluates a single model and state. * * @param model model to be evaluated * @param state state to be evaluated * @private */ private evaluateSingle; /** * Accumulates the occurrences of keys in src to the keys in res. * * @param src src counts * @param res res counts * @private */ private static accumulateOccurrences; /** * Returns statistical data about the occurrences of model and state variables in consistent or inconsistent * constraints. This can be used to determine which variable has the most influence on the result. * * @param model model to be evaluated * @param state state to be evaluated */ evaluateStatistics(model: M, state: S): StatisticsReport; /** * Returns the number of constraints that are inconsistent with the given model and state. * * @param model model to be evaluated * @param state state to be evaluated */ getNumInconsistentConstraints(model: M, state: S): number; /** * Returns the number of constraints that are consistent with the given model and state. * * @param model model to be evaluated * @param state state to be evaluated */ getNumConsistentConstraints(model: M, state: S): number; /** * Returns the number of constraints that were added to this system. */ getNumConstraints(): number; /** * Updates the data used to evaluate constraints with the new model, state and functions. * * @param model new model * @param state new state * @private */ private updateEvaluationData; /** * Returns a converted message string by replacing function calls or model and state variables with their * actual current value. * * @param msgString message to be converted * @param model model * @param state state */ getMessage(msgString: string, model: M, state: S): string; /** * Returns all constraint data that was registered. * @private */ private getConstraintData; }