001: /*
002: This file is part of the PolePosition database benchmark
003: http://www.polepos.org
004:
005: This program is free software; you can redistribute it and/or
006: modify it under the terms of the GNU General Public License
007: as published by the Free Software Foundation; either version 2
008: of the License, or (at your option) any later version.
009:
010: This program is distributed in the hope that it will be useful,
011: but WITHOUT ANY WARRANTY; without even the implied warranty of
012: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013: GNU General Public License for more details.
014:
015: You should have received a copy of the GNU General Public
016: License along with this program; if not, write to the Free
017: Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
018: MA 02111-1307, USA. */
019:
020: package org.polepos.framework;
021:
022: import java.lang.reflect.*;
023: import java.util.*;
024:
025: /**
026: * a set of timed test cases that work against the same data
027: */
028: public abstract class Circuit {
029:
030: private final List<Lap> mLaps;
031:
032: private final TurnSetup[] mLapSetups;
033:
034: private final StopWatch watch;
035:
036: protected Circuit() {
037: watch = new StopWatch();
038: mLaps = new ArrayList<Lap>();
039: mLapSetups = TurnSetup.read(this );
040: addLaps();
041: }
042:
043: /**
044: * public official name for reporting
045: */
046: public final String name() {
047: String name = internalName();
048: return name.substring(0, 1).toUpperCase() + name.substring(1);
049: }
050:
051: /**
052: * internal name for BenchmarkSettings.properties
053: */
054: public final String internalName() {
055: String name = this .getClass().getName();
056: int pos = name.lastIndexOf(".");
057: return name.substring(pos + 1).toLowerCase();
058: }
059:
060: /**
061: * describes the intent of this circuit, what it wants to test
062: */
063: public abstract String description();
064:
065: /**
066: * @return the driver class needed to run on this Circuit
067: */
068: public abstract Class requiredDriver();
069:
070: /**
071: * @return the methods that are intended to be run
072: */
073: protected abstract void addLaps();
074:
075: public void add(Lap lap) {
076: mLaps.add(lap);
077: }
078:
079: /**
080: * setups are needed for reporting
081: */
082: public TurnSetup[] lapSetups() {
083: return mLapSetups;
084: }
085:
086: public List<Lap> laps() {
087: return Collections.unmodifiableList(mLaps);
088: }
089:
090: /**
091: * calling all the laps for all the lapSetups
092: */
093: public TurnResult[] race(Team team, Car car, Driver driver) {
094:
095: TurnResult[] results = new TurnResult[mLapSetups.length];
096:
097: int index = 0;
098:
099: 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: boolean first = true;
112:
113: for (Lap lap : mLaps) {
114:
115: Method method = null;
116:
117: try {
118: method = driver.getClass().getDeclaredMethod(
119: lap.name(), (Class[]) null);
120: } catch (SecurityException e) {
121: e.printStackTrace();
122: } catch (NoSuchMethodException e) {
123: e.printStackTrace();
124: }
125:
126: if (!lap.hot()) {
127: if (first) {
128: first = false;
129: } else {
130: driver.backToPit();
131: }
132:
133: try {
134: driver.prepare();
135: } catch (CarMotorFailureException e) {
136: e.printStackTrace();
137: }
138: }
139:
140: watch.start();
141:
142: try {
143: method.invoke(driver, (Object[]) null);
144: } catch (Exception e) {
145: System.err.println("Exception on calling method "
146: + method);
147: e.printStackTrace();
148: }
149:
150: watch.stop();
151:
152: if (lap.reportResult()) {
153: result.report(new Result(this, team, lap, setup,
154: index, watch.millisEllapsed(), driver
155: .checkSum()));
156: }
157: }
158:
159: driver.backToPit();
160: }
161: return results;
162: }
163:
164: }
|