Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (c) 2011 Michael Ruflin, Andr´┐Ż Locher, Claudia von Bastian. This file is part of Tatool. Tatool is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. Tatool is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with Tatool. If not, see <http://www.gnu.org/licenses/>. /
 
 package ch.tatool.core.element.handler.timeout;
 
 import java.util.List;
 
 
Timeout handler that individually adapts itself to the user by reducing and increasing the timeout duration. The interval of adaptation is given by the minDuration and maxDuration values. A level change resets the timeout duration.

Author(s):
Andre Locher
 
 		implements AdaptiveTimeoutHandler {
 
 	Logger logger = LoggerFactory
Number of samples used to calculate the new timeout.
 
 	private int timerSampleSize = 5;

Minimum timer duration in milliseconds
 
 	private long minTimerDuration = 200;

Maximum timer duration in milliseconds
 
 	private long maxTimerDuration = 3000;

Percentile which will be used to calculate the new timer duration.
 
 	private double percentile = 0.75;

Factor by which to reduce/increase the average timeout.
 
 	private float factor = 0.9f;

Reset the duration at level-change
 
 	private boolean resetTimerDuration = true;
 
 		super();
 	}
 	
     public void processExecutionPhase(ExecutionContext context) {
     	super.processExecutionPhase(context);
     	switch (context.getPhase()) {
     	case :
     		initialize(context);
     		break;
     	}
     }

Initializes this aspect.
 
 	private void initialize(ExecutionContext event) {
 		Module module = event.getExecutionData().getModule();
 		// read the last timeout data from the module aspect properties, use
 		// default value if not provided
 		long timeoutDuration = getDefaultTimerDuration();
 		timeoutDuration = .getValue(modulethistimeoutDuration);
 		setDefaultTimerDuration(timeoutDuration);
 	}

Individually adapts the timer.
 
 	public void adaptTimeoutDuration(ExecutionContext event) {
 		long newDuration = 0;
 		newDuration = getSamplesDuration(event);
 		if (newDuration > ) {
 		} else if (newDuration < ) {
 		} else {
 			.setValue(event.getExecutionData().getModule(), thisnewDuration);
 			setDefaultTimerDuration(newDuration);
		}
	}

Gets the reaction time of the n last trials (n=maxSamples) and calculates the 3rd quartile

Returns:
the 3rd quartile of the n last trials reaction times
	public long getSamplesDuration(ExecutionContext event) {
		// find the trials that contain data about this handler
		List<Trialtrials = event.getDataService().getTrials(event.getExecutionData().getModule(), null,
		// find the n-percentile reaction time - use the current duration by
		// default
		long newDuration = getDefaultTimerDuration();
		List<LongtimeList = new ArrayList<Long>();
		long reactionTimeSum = 0;
		if (trials.size() > 0) {
			int samples = 0;
			// set the samples
			if (trials.size() < ) {
				samples = trials.size();
else {
				samples = ;
			}
			for (int i = 0; i < samples - 1; i++) {
				Trial t = trials.get(trials.size() - 1 - i);
				Long duration = .getValue(tthis);
				if (duration != null) {
					// use reactionTime or timeout if no reactionTime is present
					//Long duration = durationProperty.getValue(t, this);
					Long reactionTime = .getValue(tthis);
					if (reactionTime == null) {
						reactionTime = duration;
					}
					if (reactionTime <= 0) {
						reactionTime = duration;
					}
					reactionTimeSum += reactionTime;
					timeList.add(reactionTime);
				}
			}
		}
		// add the reaction time of the current trial
		long timeout = getDefaultTimerDuration();
		long reactionTime = getReactionTime();
		if (reactionTime <= 0) {
			reactionTime = timeout;
		}
		reactionTimeSum += Long.valueOf(reactionTime);
		timeList.add(Long.valueOf(reactionTime));
		// calculate the n-percentile reaction time
		Collections.sort(timeList);
		Long[] list = new Long[timeList.size()];
		newDuration = (longgetInterpolatedValue(timeList.toArray(list),
		return newDuration;
	}

Sets the duration of the timer to maxDuration
	public void resetTimeoutDuration(ExecutionContext event) {
	}

Reduces the timer by the factor f with a minimum of minDuration
		Module module = event.getExecutionData().getModule();
		long newDuration = 0;
		newDuration = (long) (((floatgetDefaultTimerDuration()) * );
		if (newDuration < ) {
			// set the minimal duration
else {
			// reduce the duration
			.setValue(modulethisnewDuration);
		}
	}

Increases the timer by the factor f with a maximum of maxDuration
		Module module = event.getExecutionData().getModule();
		long newDuration = 0;
		newDuration = (long) (((floatgetDefaultTimerDuration()) / );
		if (newDuration > ) {
			// set the maximal duration
else {
			// increase the duration
			.setValue(modulethisnewDuration);
		}
	}

Calculates the percentile of an array of longs.

Parameters:
m has to be an ordered list
Returns:
the value
	public double getInterpolatedValue(Long[] mdouble percentile) {
		double value = 0;
		if (m.length > 1) {
			if (percentile >= 1) {
				percentile = 0.99;
else if (percentile <= 0) {
				percentile = 0.01;
			}
			// get the indices that are used for this percentile
			double index = (doublem.length * percentile;
			int lowerIndex = (int) Math.floor((m.length * percentile));
			int upperIndex = (int) Math.ceil((m.length * percentile));
			double fraction = index - (doublelowerIndex;
			// linear interpolation
			value = m[lowerIndex - 1]
					+ (fraction * (m[upperIndex - 1] - m[lowerIndex - 1]));
else if (m.length == 1) {
			value = m[0];
else {
			value = 0;
		}
		return value;
	}

TODO: NOT USED ANYMORE?!
	public double getPercentile(Long[] mdouble percentile) {
		if (m.length == 1) {
			return m[0]; // return the only element
else if (m.length == 2) {
			return ((m[0] + m[1]) / 2.0); // return the average
else if (m.length == 3) {
			return m[1]; // return the middle element
else {
			// get the subscript of the percentile element
			int middle = (int) Math.floor((m.length * percentile));
			if (m.length % 2 == 1) {
				// Odd number -- return the middle one.
				return m[middle - 1];
else {
				// Even number of elements -- return average of middle two
				// Must cast the numbers to double before dividing.
				return ((m[middle - 1] + m[middle]) / 2.0);
			}
		}
	}
	public int getTimerSampleSize() {
	}
	public void setTimerSampleSize(int timerSampleSize) {
		this. = timerSampleSize;
	}
	public double getPercentile() {
		return ;
	}
	public void setPercentile(double percentile) {
		this. = percentile;
	}
	public float getFactor() {
		return ;
	}
	public void setFactor(float factor) {
		this. = factor;
	}
	public long getMinTimerDuration() {
	}
	public void setMinTimerDuration(long minTimerDuration) {
		this. = minTimerDuration;
	}
	public long getMaxTimerDuration() {
	}
	public void setMaxTimerDuration(long maxTimerDuration) {
		this. = maxTimerDuration;
	}
	public boolean isResetTimerDuration() {
	}
	public void setResetTimerDuration(boolean resetTimerDuration) {
		this. = resetTimerDuration;
	}
New to GrepCode? Check out our FAQ X