all files / src/ base-experiment.js

93.33% Statements 70/75
85.71% Branches 24/28
100% Functions 19/19
88.1% Lines 37/42
2 statements, 4 functions, 4 branches Ignored     
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108                                                                                  13×     12× 12× 12×   12×                 12× 12×         12× 12× 12× 12×     12×       12×       12×         12×        
import BehaviourMissingError from './errors/behaviour-missing-error';
import Observation from './observation';
import Result from './result';
 
export default class BaseExperiment {
    constructor(name, randomProvider, dateTimeProvider) {
        this._name = name;
        this._runIf = true;
        this._publishers = [];
 
        this.Control = null;//Internal
        this.Candidate = null;//Internal
        this._percentageEnabled = 0;
 
        this._randomProvider = randomProvider;
        this._dateTimeProvider = dateTimeProvider;
    }
 
 
    get percentageEnabled() {
        return this._percentageEnabled;
    }
 
    set percentageEnabled(value) {
        this._percentageEnabled = value;
    }
 
    get publishers() {
        return this._publishers;
    }
 
    set publishers(value) {
        this._publishers = value;
    }
 
    get name() {
        return this._name;
    }
 
    set name(value) {
        this._name = value;
    }
 
    runIf(runIf) {
        this._runIf = runIf;
    }
 
 
    run() {
        //console.log('[BASE EXPERIMENT] run');
 
        if (this.Control === null) {
            throw new BehaviourMissingError(this, "Control");
        }
 
        var result = new Result(this);
        var candidateObservation;
        var controlObservation;
 
        if (this._randomProvider.randomInt(0, 2) === 0) {
            controlObservation = this._getControlObservation();
 
            result.observations.push(controlObservation);
 
            candidateObservation = this._getCandidateObservation();
            Eif (candidateObservation !== null) {
                result.observations.push(candidateObservation);
            }
        }
        else {
            candidateObservation = this._getCandidateObservation();
            Eif (candidateObservation !== null) {
                result.observations.push(candidateObservation);
            }
            controlObservation = this._getControlObservation();
            result.observations.push(controlObservation);
        }
 
        this.__publish(Result);
        return controlObservation.result;
 
    }
 
    _getCandidateObservation() {
        let candidateObservation = null;
        Eif (this._experimentShouldRun()) {
            Eif (this._runIf === true) {
                candidateObservation = new Observation(this, "Candidate", this.Candidate, this._dateTimeProvider);
            }
        }
        return candidateObservation;
    }
 
    _getControlObservation() {
        return new Observation(this, "Control", this.Control, this._dateTimeProvider);
    }
 
    _experimentShouldRun() {
        return this._percentageEnabled > 0 && this._randomProvider.randomInt(0, 100) < this._percentageEnabled;
    }
 
    ///internal
    __publish(result) {
        for (let publisher in this._publishers) {
            publisher.publish(result);
        }
    }
}