source: src/main/java/genius/cli/Runner.java@ 256

Last change on this file since 256 was 15, checked in by Wouter Pasman, 6 years ago

#7 moved version info to manifest. Build script creates the info and also uses it to attach the right version nrs to the zip and jar files

File size: 4.4 KB
Line 
1package genius.cli;
2
3import java.io.File;
4import java.io.IOException;
5import java.text.DateFormat;
6import java.text.SimpleDateFormat;
7import java.util.Date;
8import java.util.Scanner;
9
10import javax.xml.bind.JAXBException;
11
12import genius.AgentsInstaller;
13import genius.ProtocolsInstaller;
14import genius.core.config.MultilateralTournamentConfiguration;
15import genius.core.config.MultilateralTournamentsConfiguration;
16import genius.core.exceptions.InstantiateException;
17import genius.core.session.TournamentManager;
18import genius.domains.DomainInstaller;
19import genius.gui.About;
20import genius.gui.negosession.MultiPartyDataModel;
21import genius.gui.progress.MultipartyNegoEventLogger;
22
23/**
24 * Genius console entry point for running a
25 * {@link MultilateralTournamentsConfiguration}.
26 * <p>
27 * Use this entry point to run genius negotiations from an xml file.
28 * multilateraltournament.xml contains an example file.
29 */
30public class Runner {
31
32 /**
33 * Genius console entry point
34 * <p>
35 * Use this entry point to run genius negotiations from an xml file. Example
36 * xml file can be found at xml-runner/example.xml
37 * <p>
38 * When no arguments are given, input and output file are prompted at the
39 * console. Input file is expected to be an .xml file and output is expected
40 * to be an .csv file, although you are free to give different extensions.
41 *
42 * @param args
43 * 1st argument: input file or empty to be prompted. 2nd
44 * argument: output file or empty to be prompted
45 * @throws JAXBException
46 * @throws IOException
47 * @throws InstantiateException
48 */
49 public static void main(String[] args)
50 throws JAXBException, IOException, InstantiateException {
51
52 ProtocolsInstaller.run();
53 DomainInstaller.run();
54 AgentsInstaller.run();
55
56 // print welcome message
57 System.out.println(
58 "This is the Genius multilateral tournament runner command line tool");
59 System.out.println("Currently you are using using Genius "
60 + About.class.getPackage().getImplementationVersion());
61
62 // request input and output files
63 Scanner sc = new Scanner(System.in);
64 String input = requestInputFile(args, sc);
65 String output = requestOutputFile(args, sc);
66 sc.close();
67
68 // run xml configuration
69 MultilateralTournamentsConfiguration multiconfig = MultilateralTournamentsConfiguration
70 .load(new File(input));
71
72 // init data model, GUI, logger.
73 Integer numParties = multiconfig.getMaxNumNonMediators();
74 MultiPartyDataModel dataModel = new MultiPartyDataModel(numParties);
75 MultipartyNegoEventLogger myLogger = new MultipartyNegoEventLogger(
76 output, numParties, dataModel);
77 dataModel.addTableModelListener(myLogger);
78
79 for (MultilateralTournamentConfiguration config : multiconfig
80 .getTournaments()) {
81 System.out.println("Starting tournament " + config);
82 TournamentManager manager = new TournamentManager(config);
83 manager.addListener(dataModel);
84 manager.run(); // runs the manager thread in sync
85 }
86
87 System.out.println("Runner completed succesfully.");
88
89 }
90
91 /** Requests the input file from System.in (or from args[0] if defined) */
92 private static String requestInputFile(String[] args, Scanner sc) {
93 // init
94 File f = null;
95 String filename = null;
96
97 // if in args
98 if (args.length >= 1) {
99 System.out.println("Input file: " + args[0]);
100 return args[0];
101 }
102
103 // Request filename
104 System.out.print("Provide path to xml input file: ");
105 while (f == null || !f.exists()) {
106 if (f != null) {
107 System.out.println(
108 "Xml input file could not be found: " + f.getName());
109 System.out.print("Provide path to xml input file: ");
110 }
111 filename = sc.nextLine();
112 f = new File(filename);
113 }
114
115 // return filename
116 return filename;
117 }
118
119 /** Requests the output file from System.in (or from args[1] if defined) */
120 private static String requestOutputFile(String[] args, Scanner sc) {
121 // init
122 File f = null;
123 String filename = null;
124 DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd-HHmmss");
125 String defaultName = String.format("logs/Log-XmlRunner-%s.csv",
126 dateFormat.format(new Date()));
127
128 // if in args
129 if (args.length >= 2) {
130 System.out.println("Output file: " + args[1]);
131 return args[1];
132 }
133
134 // Request filename
135 System.out.print(String.format(
136 "Provide path to output logfile [default: %s]: ", defaultName));
137 while (f == null) {
138 filename = sc.nextLine();
139 if (filename.isEmpty()) {
140 filename = defaultName;
141 }
142 f = new File(filename);
143 }
144
145 // return filename
146 return filename;
147 }
148}
Note: See TracBrowser for help on using the repository browser.