001/** 002 * Copyright (c) 2011, The University of Southampton and the individual contributors. 003 * All rights reserved. 004 * 005 * Redistribution and use in source and binary forms, with or without modification, 006 * are permitted provided that the following conditions are met: 007 * 008 * * Redistributions of source code must retain the above copyright notice, 009 * this list of conditions and the following disclaimer. 010 * 011 * * Redistributions in binary form must reproduce the above copyright notice, 012 * this list of conditions and the following disclaimer in the documentation 013 * and/or other materials provided with the distribution. 014 * 015 * * Neither the name of the University of Southampton nor the names of its 016 * contributors may be used to endorse or promote products derived from this 017 * software without specific prior written permission. 018 * 019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 020 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 021 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 022 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 023 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 026 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 028 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 029 */ 030package org.openimaj.experiment; 031 032import java.io.IOException; 033import java.util.Map; 034import java.util.Set; 035 036import org.apache.commons.math.stat.descriptive.SummaryStatistics; 037import org.openimaj.citation.ReferenceListener; 038import org.openimaj.citation.annotation.Reference; 039import org.openimaj.experiment.agent.ExperimentAgent; 040import org.openimaj.experiment.agent.TimeTracker; 041import org.openimaj.experiment.annotations.Time; 042 043/** 044 * Support for running {@link RunnableExperiment}s and automatically creating 045 * and populating the context of the experiments. 046 * <p> 047 * <b>Usage notes:</b> Much of the collection of data for building the 048 * {@link ExperimentContext} works through dynamic byte code augmentation 049 * performed by the {@link ExperimentAgent}. On most JVMs the agent will be 050 * loaded dynamically at runtime on the first call to a method on the 051 * {@link ExperimentAgent} class. However, the Java byte code for a class can 052 * only be augmented <b>before</b> the class is loaded, so it is important 053 * that the {@link ExperimentRunner} is used before any classes for the experiment 054 * are used for the first time. If this is not possible, you can manually 055 * initialise the {@link ExperimentAgent} by calling {@link ExperimentAgent#initialise()} 056 * at the earliest possible point in your code (i.e. the first line of a main method). 057 * Also, bear in mind that your main class (and its superclasses) will not be passed 058 * to the agent for augmentation as they will already be loaded. 059 * <p> 060 * <b>Implementation notes:</b> The {@link ExperimentRunner} can only run a 061 * single experiment at a time. This is because global static objects and variables 062 * must be used to track the state of a running experiment. It is however safe for an 063 * experiment to make use of multiple threads for the experiments execution. 064 * 065 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk) 066 * 067 */ 068public class ExperimentRunner { 069 static { 070 try { 071 ExperimentAgent.initialise(); 072 } catch (IOException e) { 073 throw new RuntimeException(e); 074 } 075 } 076 077 private ExperimentRunner() {}; 078 079 /** 080 * Run an experiment, filling in the context of the experiment as 081 * it runs. 082 * 083 * @param experiment the experiment to run 084 * @return the experiments context 085 */ 086 public static synchronized ExperimentContext runExperiment(RunnableExperiment experiment) { 087 return InternalRunner.runExperiment(experiment); 088 } 089 090 /** 091 * Inner class the does the work. This is used so that the 092 * class will be transformed and the @Time annotations processed. 093 * 094 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk) 095 */ 096 private static class InternalRunner { 097 public static synchronized ExperimentContext runExperiment(RunnableExperiment experiment) { 098 Set<Reference> oldRefs = ReferenceListener.reset(); 099 Map<String, SummaryStatistics> oldTimes = TimeTracker.reset(); 100 101 ExperimentContext context = new ExperimentContext(experiment); 102 103 runSetup(experiment); 104 runPerform(experiment); 105 runFinish(experiment, context); 106 107 context.lock(); 108 109 ReferenceListener.addReferences(oldRefs); 110 TimeTracker.addMissing(oldTimes); 111 112 return context; 113 } 114 115 @Time(identifier = "Setup Experiment") 116 protected static void runSetup(RunnableExperiment experiment) { 117 experiment.setup(); 118 } 119 120 @Time(identifier = "Perform Experiment") 121 protected static void runPerform(RunnableExperiment experiment) { 122 experiment.perform(); 123 } 124 125 @Time(identifier = "Finish Experiment") 126 protected static void runFinish(RunnableExperiment experiment, ExperimentContext context) { 127 experiment.finish(context); 128 } 129 } 130}