View Javadoc

1   package cz.cuni.amis.pogamut.ut2004.tournament.deathmatch;
2   
3   import java.io.File;
4   import java.util.Collections;
5   import java.util.Map;
6   import java.util.concurrent.Callable;
7   import java.util.logging.Level;
8   import java.util.logging.LogRecord;
9   
10  import org.apache.commons.io.FileUtils;
11  
12  import cz.cuni.amis.pogamut.base.utils.logging.ILogPublisher;
13  import cz.cuni.amis.pogamut.base.utils.logging.LogCategory;
14  import cz.cuni.amis.pogamut.ut2004.tournament.match.UT2004MatchExecutor;
15  import cz.cuni.amis.utils.NullCheck;
16  import cz.cuni.amis.utils.exception.PogamutException;
17  import cz.cuni.amis.utils.token.IToken;
18  
19  /**
20   * Class that performs the tournament as described by {@link UT2004DeathMatchTournamentConfig}.
21   * <p><p>
22   * THREAD-UNSAFE!
23   * 
24   * @author Jimmy
25   */
26  public class UT2004DeathMatchTournament implements Callable<Map<IToken, UT2004DeathMatchResult>>, Runnable {
27  
28  	private LogCategory log;
29  	
30  	private UT2004DeathMatchTournamentConfig config;
31  	
32  	private UT2004MatchExecutor<UT2004DeathMatch, UT2004DeathMatchResult> executor;
33  
34  	public UT2004DeathMatchTournament(UT2004DeathMatchTournamentConfig config, LogCategory log) {
35  		this.log = log;
36  		this.config = config;
37  		NullCheck.check(this.config, "config");
38  	}
39  	
40  	/**
41  	 * This map holds the results of respective matches. Immutable.
42  	 * @return
43  	 */
44  	public Map<IToken, UT2004DeathMatchResult> getResults() {
45  		if (executor == null) throw new PogamutException("There are no results available, you have to run tournament first!", this);
46  		return executor.getResults();
47  	}
48  
49  	/**
50  	 * If some match fails, the exception reported is stored within this map. Immutable.
51  	 * @return
52  	 */
53  	public Map<IToken, Throwable> getExceptions() {
54  		if (executor == null) throw new PogamutException("There are no results available, you have to run tournament first!", this);
55  		return executor.getExceptions();
56  	}
57  
58  	@Override
59  	public Map<IToken, UT2004DeathMatchResult> call() throws Exception {
60  		return executor.getResults();
61  	}
62  
63  	@Override
64  	public synchronized void run() {
65  		UT2004DeathMatchConfig[] configs = config.createMatcheConfigs();
66  		UT2004DeathMatch[] matches = new UT2004DeathMatch[configs.length];
67  		for (int i = 0; i < configs.length; ++i) {
68  			matches[i] = new UT2004DeathMatch(configs[i], new LogCategory(configs[i].getMatchId().getToken()));
69  			matches[i].getLog().addHandler(new ILogPublisher() {
70  
71  				@Override
72  				public void close() throws SecurityException {
73  					// TODO Auto-generated method stub
74  					
75  				}
76  
77  				@Override
78  				public void flush() {
79  					// TODO Auto-generated method stub
80  					
81  				}
82  
83  				@Override
84  				public void publish(LogRecord record) {
85  					if (UT2004DeathMatchTournament.this.log != null) {
86  						UT2004DeathMatchTournament.this.log.log(record);
87  					}
88  				}
89  				
90  			});			
91  		}
92  		executor = new UT2004MatchExecutor<UT2004DeathMatch, UT2004DeathMatchResult>(matches, log);
93  		executor.run();
94  	}
95  
96  	/**
97  	 * WARNING: this method will delete the whole directory where results are stored! IT WILL DELETE IT COMPLETELY!
98  	 * DO NOT USE IT ON A WHIM... be sure you're using separate directories for all matches.
99  	 */
100 	public void cleanUp() {
101 		if (log != null && log.isLoggable(Level.WARNING)) log.warning("Cleaning up! Deleting: " + getOutputPath().getAbsolutePath());
102 		FileUtils.deleteQuietly(getOutputPath());
103 	}
104 
105 	/**
106 	 * Returns directory where all tournament matches will be output.
107 	 * @return
108 	 */
109 	public File getOutputPath() {
110 		return new File(config.getOutputDir());
111 	}
112 }