UG.java
8.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class UG {
String external;
String internal;
int[] adj;
float area;
int[] presc;
int[][] periods;
int[] wood;
int[] cork;
float mediumX;
float mediumY;
int time;
boolean valid;
String region;
String[] externalPresc;
static public int[] returnInsertable(Scanner myReader){
String data = myReader.nextLine();
String[] str_split = data.split(",", 0);
int size = str_split.length;
int[] toInsert = new int [size];
for(int i=0; i<size; i++) {
toInsert[i] = Integer.parseInt(str_split[i]);
}
return toInsert;
}
static public int[] returnInsertable2(Scanner myReader){
String data = myReader.nextLine();
String[] str_split = data.split(",", 0);
int size = str_split.length;
int[] toInsert = new int[size];
for(int i=0; i<size; i++) {
toInsert[i] = (int)Float.parseFloat(str_split[i]);
}
return toInsert;
}
static public int[] returnPeriods(String data){
String[] str_split = data.split(",", 0);
int size = str_split.length;
int[] toInsert = new int [size];
for(int i=0; i<size; i++) {
toInsert[i] = Integer.parseInt(str_split[i]);
}
return toInsert;
}
static public void fillArray(UG[] array, String dir, float X, float Y, int maxDistance) {
int index = 0;
try { // idea: pass a directory, which should contain files called
// graph_init, ugs_init, ...
File myObj = new File(dir + "/graph_init");
Scanner myReaderAdj = new Scanner(myObj);
File myObj2 = new File(dir + "/ugs_init");
Scanner myReaderPresc = new Scanner(myObj2);
File myObj3 = new File(dir + "/area_init");
Scanner myReaderArea = new Scanner(myObj3);
File myObj4 = new File(dir + "/period_init");
Scanner myReaderPeriod = new Scanner(myObj4);
File myObj5 = new File(dir + "/external_init");
Scanner myReaderExternal = new Scanner(myObj5);
File myObj6 = new File(dir + "/xvertex_init");
Scanner myReaderXVertex = new Scanner(myObj6);
File myObj7 = new File(dir + "/yvertex_init");
Scanner myReaderYVertex = new Scanner(myObj7);
File myObj8 = new File(dir + "/wood_init");
Scanner myReaderWood = new Scanner(myObj8);
File myObj9 = new File(dir + "/cork_init");
Scanner myReaderCork = new Scanner(myObj9);
File myObj10 = new File(dir + "/internal_init");
Scanner myReaderInternal = new Scanner(myObj10);
File myObj11 = new File(dir + "/region_init");
Scanner myReaderRegion = new Scanner(myObj11);
while (myReaderAdj.hasNextLine()) {
UG toInsert = new UG();
toInsert.mediumX = Float.parseFloat(myReaderXVertex.nextLine());
toInsert.mediumY = Float.parseFloat(myReaderYVertex.nextLine());
double d = Math.sqrt(Math.pow (toInsert.mediumX - X, 2) + Math.pow (toInsert.mediumY - Y, 2));
toInsert.region = myReaderRegion.nextLine();
if(d <= maxDistance){
toInsert.valid = true;
}
else{
toInsert.valid = false;
}
toInsert.adj = returnInsertable(myReaderAdj);
toInsert.presc = returnInsertable(myReaderPresc);
toInsert.external = myReaderExternal.nextLine();
toInsert.internal = myReaderInternal.nextLine();
toInsert.area = Float.parseFloat(myReaderArea.nextLine());
String data = myReaderPeriod.nextLine();
String[] str_split = data.split("/", 0);
int size = str_split.length;
toInsert.periods = new int[size][];
for(int i = 0; i < size; i++) {
toInsert.periods[i] = returnPeriods(str_split[i]);
}
toInsert.wood = returnInsertable2(myReaderWood);
toInsert.cork = returnInsertable2(myReaderCork);
array[index] = toInsert;
index++;
}
myReaderAdj.close();
myReaderPresc.close();
myReaderArea.close();
myReaderExternal.close();
myReaderXVertex.close();
myReaderYVertex.close();
myReaderWood.close();
myReaderCork.close();
myReaderRegion.close();
} catch (FileNotFoundException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
static public void fillArray(UG[] array, String dir, String region) {
int index = 0;
try { // idea: pass a directory, which should contain files called
// graph_init, ugs_init, ...
File myObj = new File(dir + "/graph_init");
Scanner myReaderAdj = new Scanner(myObj);
File myObj2 = new File(dir + "/ugs_init");
Scanner myReaderPresc = new Scanner(myObj2);
File myObj3 = new File(dir + "/area_init");
Scanner myReaderArea = new Scanner(myObj3);
File myObj4 = new File(dir + "/period_init");
Scanner myReaderPeriod = new Scanner(myObj4);
File myObj5 = new File(dir + "/external_init");
Scanner myReaderExternal = new Scanner(myObj5);
File myObj6 = new File(dir + "/xvertex_init");
Scanner myReaderXVertex = new Scanner(myObj6);
File myObj7 = new File(dir + "/yvertex_init");
Scanner myReaderYVertex = new Scanner(myObj7);
File myObj8 = new File(dir + "/wood_init");
Scanner myReaderWood = new Scanner(myObj8);
File myObj9 = new File(dir + "/cork_init");
Scanner myReaderCork = new Scanner(myObj9);
File myObj10 = new File(dir + "/internal_init");
Scanner myReaderInternal = new Scanner(myObj10);
File myObj11 = new File(dir + "/region_init");
Scanner myReaderRegion = new Scanner(myObj11);
while (myReaderAdj.hasNextLine()) {
UG toInsert = new UG();
toInsert.mediumX = Float.parseFloat(myReaderXVertex.nextLine());
toInsert.mediumY = Float.parseFloat(myReaderYVertex.nextLine());
toInsert.region = myReaderRegion.nextLine();
if(!region.equalsIgnoreCase(toInsert.region)){
toInsert.valid = true;
}
else{
toInsert.valid = false;
}
toInsert.adj = returnInsertable(myReaderAdj);
toInsert.presc = returnInsertable(myReaderPresc);
toInsert.external = myReaderExternal.nextLine();
toInsert.internal = myReaderInternal.nextLine();
toInsert.area = Float.parseFloat(myReaderArea.nextLine());
String data = myReaderPeriod.nextLine();
String[] str_split = data.split("/", 0);
int size = str_split.length;
toInsert.periods = new int[size][];
for(int i = 0; i < size; i++) {
toInsert.periods[i] = returnPeriods(str_split[i]);
}
toInsert.wood = returnInsertable2(myReaderWood);
toInsert.cork = returnInsertable2(myReaderCork);
array[index] = toInsert;
index++;
}
myReaderAdj.close();
myReaderPresc.close();
myReaderArea.close();
myReaderExternal.close();
myReaderXVertex.close();
myReaderYVertex.close();
myReaderWood.close();
myReaderCork.close();
myReaderRegion.close();
} catch (FileNotFoundException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
}