KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > polepos > framework > Circuit


1 /*
2 This file is part of the PolePosition database benchmark
3 http://www.polepos.org
4
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public
16 License along with this program; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
18 MA 02111-1307, USA. */

19
20 package org.polepos.framework;
21
22 import java.lang.reflect.*;
23 import java.util.*;
24
25 /**
26  * a set of timed test cases that work against the same data
27  */

28 public abstract class Circuit{
29     
30     private final List<Lap> mLaps;
31     
32     private final TurnSetup[] mLapSetups;
33     
34     private final StopWatch watch;
35     
36     protected Circuit(){
37         watch = new StopWatch();
38         mLaps = new ArrayList<Lap>();
39         mLapSetups = TurnSetup.read(this);
40         addLaps();
41     }
42     
43     /**
44      * public official name for reporting
45      */

46     public final String JavaDoc name(){
47         String JavaDoc name = internalName();
48         return name.substring(0,1).toUpperCase() + name.substring(1);
49     }
50
51     /**
52      * internal name for BenchmarkSettings.properties
53      */

54     public final String JavaDoc internalName(){
55         String JavaDoc name = this.getClass().getName();
56         int pos = name.lastIndexOf(".");
57         return name.substring(pos + 1).toLowerCase();
58     }
59     
60     /**
61      * describes the intent of this circuit, what it wants to test
62      */

63     public abstract String JavaDoc description();
64
65     /**
66      * @return the driver class needed to run on this Circuit
67      */

68     public abstract Class JavaDoc requiredDriver();
69     
70     /**
71      * @return the methods that are intended to be run
72      */

73     protected abstract void addLaps();
74     
75     public void add(Lap lap){
76         mLaps.add(lap);
77     }
78     
79     /**
80      * setups are needed for reporting
81      */

82     public TurnSetup[] lapSetups(){
83         return mLapSetups;
84     }
85     
86     public List<Lap> laps() {
87         return Collections.unmodifiableList(mLaps);
88     }
89     
90     /**
91      * calling all the laps for all the lapSetups
92      */

93     public TurnResult[] race( Team team, Car car, Driver driver){
94         
95         TurnResult[] results = new TurnResult[ mLapSetups.length ];
96
97         int index = 0;
98         
99         for(TurnSetup setup : mLapSetups) {
100             
101             TurnResult result = new TurnResult();
102             results[index++] = result;
103             
104             try {
105                 driver.takeSeatIn(car, setup);
106             } catch (CarMotorFailureException e1) {
107                 e1.printStackTrace();
108                 break;
109             }
110             
111             
112             boolean first = true;
113             
114             for(Lap lap : mLaps) {
115                 
116                 
117                 Method method = null;
118             
119                 try {
120                     method = driver.getClass().getDeclaredMethod(lap.name(), (Class JavaDoc[])null);
121                 } catch (SecurityException JavaDoc e) {
122                     e.printStackTrace();
123                 } catch (NoSuchMethodException JavaDoc e) {
124                     e.printStackTrace();
125                 }
126                 
127                 
128                 if( ! lap.hot() ){
129                     if(first){
130                        first = false;
131                     }else{
132                         driver.backToPit();
133                     }
134                     
135                     try {
136                         driver.prepare();
137                     } catch (CarMotorFailureException e) {
138                         e.printStackTrace();
139                     }
140                 }
141                 
142                 
143                 watch.start();
144                 
145                 try {
146                     method.invoke(driver, (Object JavaDoc[])null);
147                 } catch (Exception JavaDoc e) {
148                     System.err.println("Exception on calling method " + method);
149                     e.printStackTrace();
150                 }
151                 
152                 watch.stop();
153                 
154                 if(lap.reportResult()){
155                     result.report(new Result(this, team, lap, setup, index, watch.millisEllapsed(), driver.checkSum()));
156                 }
157             }
158             
159             driver.backToPit();
160         }
161         return results;
162     }
163     
164 }
Free Books   Free Magazines  
Popular Tags