1 | /************************************************************************************ |
---|
2 | * Copyright (C) 2008 by Politehnica University of Bucharest and Rutgers University |
---|
3 | * All rights reserved. |
---|
4 | * Refer to LICENSE for terms and conditions of use. |
---|
5 | ***********************************************************************************/ |
---|
6 | package vnsim.gui.selector; |
---|
7 | |
---|
8 | import java.io.EOFException; |
---|
9 | import java.io.File; |
---|
10 | import java.io.FileInputStream; |
---|
11 | import java.io.FileOutputStream; |
---|
12 | import java.io.InputStream; |
---|
13 | import java.io.ObjectInputStream; |
---|
14 | import java.io.ObjectOutputStream; |
---|
15 | import java.util.Vector; |
---|
16 | import java.util.logging.*; |
---|
17 | |
---|
18 | import javax.swing.JOptionPane; |
---|
19 | |
---|
20 | import vnsim.map.object.Globals; |
---|
21 | import vnsim.map.object.Map; |
---|
22 | import vnsim.vehicular.scenarios.*; |
---|
23 | import vnsim.gui.MapLoader; |
---|
24 | import vnsim.gui.Utils; |
---|
25 | import vnsim.gui.Utils.ClassSystemView; |
---|
26 | |
---|
27 | public class ScenarioDesigner { |
---|
28 | |
---|
29 | /** Logger used by this class */ |
---|
30 | private static final transient Logger logger = Logger.getLogger("gui.selector.ScenarioDesigner"); |
---|
31 | |
---|
32 | public Vector<ScenarioMap> existingScenarios; |
---|
33 | |
---|
34 | public ScenarioSelection scenarioSelector; |
---|
35 | |
---|
36 | public ScenarioConfiguration scenarioConfigurator; |
---|
37 | |
---|
38 | Scenario finalConfiguration; |
---|
39 | |
---|
40 | public ScenarioDesigner(String fileIn) { |
---|
41 | ScenarioMap s; |
---|
42 | try { |
---|
43 | existingScenarios = new Vector<ScenarioMap>(); |
---|
44 | final ClassSystemView f1 = new ClassSystemView(fileIn); |
---|
45 | String[] files = f1.list(); |
---|
46 | File f = new File(System.getProperty("user.home") + File.separatorChar |
---|
47 | + fileIn.replace('/', File.separatorChar).replace('\\', File.separatorChar)); |
---|
48 | if (f.exists() && f.isDirectory()) { |
---|
49 | String[] f2 = f.list(); |
---|
50 | Vector<String> v = new Vector<String>(); |
---|
51 | if (files != null) { |
---|
52 | for (int i = 0; i < files.length; i++) { |
---|
53 | if (!v.contains(files[i])) { |
---|
54 | v.add(files[i]); |
---|
55 | } |
---|
56 | } |
---|
57 | } |
---|
58 | if (f2 != null) { |
---|
59 | for (int i = 0; i < f2.length; i++) { |
---|
60 | if (!v.contains(f2[i])) { |
---|
61 | v.add(f2[i]); |
---|
62 | } |
---|
63 | } |
---|
64 | } |
---|
65 | files = new String[v.size()]; |
---|
66 | files = v.toArray(files); |
---|
67 | } |
---|
68 | final String dir = f.getAbsolutePath(); |
---|
69 | if (files != null && files.length != 0) { |
---|
70 | for (int i = 0; i < files.length; i++) { |
---|
71 | ObjectInputStream ois = null; |
---|
72 | f = new File(dir + File.separatorChar + files[i]); |
---|
73 | if (f.isDirectory()) { |
---|
74 | continue; |
---|
75 | } |
---|
76 | if (f.exists() && f.isFile() && f.canRead()) { |
---|
77 | ois = new ObjectInputStream(new FileInputStream(f)); |
---|
78 | } else { |
---|
79 | try { |
---|
80 | InputStream in = Utils.getInstance().openStream( |
---|
81 | fileIn + (fileIn.endsWith("/") ? "" : "/") + files[i]); |
---|
82 | if (in == null) { |
---|
83 | continue; |
---|
84 | } |
---|
85 | ois = new ObjectInputStream(in); |
---|
86 | } catch (Throwable t) { |
---|
87 | logger.log(Level.SEVERE, "Error reading " + fileIn + "/" + files[i], t); |
---|
88 | System.exit(0); |
---|
89 | } |
---|
90 | } |
---|
91 | while (true) { |
---|
92 | try { |
---|
93 | s = (ScenarioMap) ois.readObject(); |
---|
94 | if (s != null) { |
---|
95 | existingScenarios.add(s); |
---|
96 | } |
---|
97 | } catch (EOFException e) { |
---|
98 | break; |
---|
99 | } catch (Exception e) { |
---|
100 | e.printStackTrace(); |
---|
101 | } |
---|
102 | } |
---|
103 | ois.close(); |
---|
104 | } |
---|
105 | } |
---|
106 | } catch (Exception e) { |
---|
107 | logger.log(Level.SEVERE, "Error reading ScenarioMaps", e); |
---|
108 | System.exit(0); |
---|
109 | } |
---|
110 | } |
---|
111 | |
---|
112 | public boolean getScenario() { |
---|
113 | if (MapLoader.listSmf.getSelectedIndex() == -1) { |
---|
114 | System.err.println("Selecteaza un scenariu gol!"); |
---|
115 | return false; |
---|
116 | } |
---|
117 | int id = -1; |
---|
118 | String s = ((String) MapLoader.listSmf.getSelectedValue()); |
---|
119 | String scenariu = s.substring(0, s.length() - 4); |
---|
120 | logger.info("Looking for "+scenariu); |
---|
121 | for (int i = 0; i < existingScenarios.size(); i++) { |
---|
122 | // System.out.println(existingScenarios.get(i).name+":"+scenariu); |
---|
123 | if (existingScenarios.get(i).name.equals(scenariu)) { |
---|
124 | id = i; |
---|
125 | break; |
---|
126 | } |
---|
127 | } |
---|
128 | logger.info("Found on id="+id); |
---|
129 | try { |
---|
130 | ObjectInputStream ois = null; |
---|
131 | String fn = existingScenarios.get(id).mapFileName; |
---|
132 | if (fn.startsWith(".")) |
---|
133 | fn = fn.substring(1); |
---|
134 | String dir = System.getProperty("user.home") + File.separatorChar |
---|
135 | + fn.replace('/', File.separatorChar).replace('\\', File.separatorChar); |
---|
136 | File f = new File(dir); |
---|
137 | if (f.exists() && f.isFile() && f.canRead()) { |
---|
138 | ois = new ObjectInputStream(new FileInputStream(f)); |
---|
139 | } else { |
---|
140 | ois = new ObjectInputStream(Utils.getInstance().openStream(fn.replace('\\', '/'))); |
---|
141 | } |
---|
142 | Globals.map = (Map) ois.readObject(); |
---|
143 | ois.close(); |
---|
144 | |
---|
145 | } catch (Exception ex) { |
---|
146 | ex.printStackTrace(); |
---|
147 | } |
---|
148 | System.err.println("?? " + existingScenarios.elementAt(id).exits.size()); |
---|
149 | this.scenarioConfigurator = ScenarioConfiguration.getInstance(this, this.existingScenarios.elementAt(id), null); |
---|
150 | if (this.scenarioConfigurator==null){ |
---|
151 | JOptionPane.showMessageDialog(null, "Create routes for the scenario first!"); |
---|
152 | return false; |
---|
153 | } |
---|
154 | this.scenarioConfigurator.setVisible(true); |
---|
155 | this.scenarioConfigurator.repaint(); |
---|
156 | return true; |
---|
157 | } |
---|
158 | |
---|
159 | public void afterExit() { |
---|
160 | finalConfiguration = this.scenarioConfigurator.getScenario(); |
---|
161 | setPreferences(finalConfiguration); |
---|
162 | this.scenarioConfigurator.dispose(); |
---|
163 | EntryScenario es; |
---|
164 | EntryExitScenario ees; |
---|
165 | int j, i, k; |
---|
166 | for (i = 0; i < finalConfiguration.entryScenarios.size(); i++) { |
---|
167 | es = finalConfiguration.entryScenarios.get(i); |
---|
168 | int sum1 = 0, sum2 = 0, tp = 0; |
---|
169 | for (j = 0; j < es.percentsDriverTypes.size(); j++) { |
---|
170 | sum1 = sum1 + es.percentsDriverTypes.get(j).intValue(); |
---|
171 | } |
---|
172 | for (j = 0; j < es.percentsDriverTypes.size() - 1; j++) { |
---|
173 | tp = es.percentsDriverTypes.get(j); |
---|
174 | if (tp > 0) { |
---|
175 | sum2 = ((tp * 100) / sum1) + sum2; |
---|
176 | es.percentsDriverTypes.set(j, new Integer((tp * 100) / sum1)); |
---|
177 | } |
---|
178 | } |
---|
179 | es.percentsDriverTypes.set(j, new Integer(100 - sum2)); |
---|
180 | sum1 = 0; |
---|
181 | sum2 = 0; |
---|
182 | tp = 0; |
---|
183 | for (j = 0; j < es.entryExits.size(); j++) { |
---|
184 | sum1 = sum1 + es.entryExits.elementAt(j).percentOfFlow; |
---|
185 | } |
---|
186 | for (j = 0; j < es.entryExits.size() - 1; j++) { |
---|
187 | tp = es.entryExits.elementAt(j).percentOfFlow; |
---|
188 | if (tp > 0) { |
---|
189 | sum2 = (int) ((double) (tp * 100) / sum1) + sum2; |
---|
190 | es.entryExits.elementAt(j).percentOfFlow = (int) ((double) (tp * 100) / sum1); |
---|
191 | } |
---|
192 | } |
---|
193 | if (j > 0) { |
---|
194 | es.entryExits.elementAt(j).percentOfFlow = 100 - sum2; |
---|
195 | } |
---|
196 | for (k = 0; k < es.entryExits.size(); k++) { |
---|
197 | ees = es.entryExits.elementAt(k); |
---|
198 | sum1 = 0; |
---|
199 | sum2 = 0; |
---|
200 | tp = 0; |
---|
201 | for (j = 0; j < ees.percentsEachRoute.size(); j++) { |
---|
202 | sum1 = sum1 + ees.percentsEachRoute.get(j).intValue(); |
---|
203 | } |
---|
204 | for (j = 0; j < ees.percentsEachRoute.size() - 1; j++) { |
---|
205 | tp = ees.percentsEachRoute.get(j); |
---|
206 | if (tp > 0) { |
---|
207 | sum2 = ((tp * 100) / sum1) + sum2; |
---|
208 | ees.percentsEachRoute.set(j, new Integer((tp * 100) / sum1)); |
---|
209 | } |
---|
210 | } |
---|
211 | if (ees.percentsEachRoute.size() > 0) { |
---|
212 | ees.percentsEachRoute.set((ees.percentsEachRoute.size() - 1), new Integer(100 - sum2)); |
---|
213 | } |
---|
214 | } |
---|
215 | } |
---|
216 | try { |
---|
217 | String dir = System.getProperty("user.home") + File.separatorChar + "maps" + File.separatorChar + "fsc"; |
---|
218 | (new File(dir)).mkdirs(); |
---|
219 | ObjectOutputStream outStr = new ObjectOutputStream(new FileOutputStream(dir + File.separatorChar |
---|
220 | + finalConfiguration.name + ".fsc")); |
---|
221 | outStr.writeObject(finalConfiguration); |
---|
222 | outStr.close(); |
---|
223 | /* |
---|
224 | * ObjectOutputStream outStr = new ObjectOutputStream(new |
---|
225 | * FileOutputStream(".//maps//fsc//" + finalConfiguration.name + |
---|
226 | * ".fsc")); outStr.writeObject(finalConfiguration); |
---|
227 | */ |
---|
228 | } catch (Exception e) { |
---|
229 | e.printStackTrace(); |
---|
230 | } |
---|
231 | MapLoader.show(true); |
---|
232 | } |
---|
233 | |
---|
234 | public void setPreferences(Scenario s) { |
---|
235 | |
---|
236 | } |
---|
237 | } |
---|