-
Notifications
You must be signed in to change notification settings - Fork 5
/
GeoStore.java
150 lines (130 loc) · 6.14 KB
/
GeoStore.java
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
package org.genericsystem.geography.app;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Collections;
import java.util.HashMap;
import org.genericsystem.common.Generic;
import org.genericsystem.common.Root;
import org.genericsystem.geography.app.GeoStore.GeoScript;
import org.genericsystem.geography.model.AdministrativeTerritory;
import org.genericsystem.geography.model.AdministrativeTerritory.Adm1;
import org.genericsystem.geography.model.AdministrativeTerritory.Adm2;
import org.genericsystem.geography.model.AdministrativeTerritory.Adm3;
import org.genericsystem.geography.model.Building;
import org.genericsystem.geography.model.City;
import org.genericsystem.geography.model.Country;
import org.genericsystem.geography.model.Place;
import org.genericsystem.geography.model.Place.Latitude;
import org.genericsystem.geography.model.Place.Longitude;
import org.genericsystem.reactor.annotations.Children;
import org.genericsystem.reactor.annotations.DependsOnModel;
import org.genericsystem.reactor.annotations.DirectSelect;
import org.genericsystem.reactor.annotations.RunScript;
import org.genericsystem.reactor.annotations.Style;
import org.genericsystem.reactor.appserver.ApplicationServer;
import org.genericsystem.reactor.appserver.Script;
import org.genericsystem.reactor.gscomponents.AppHeader;
import org.genericsystem.reactor.gscomponents.DivWithTitle.TitledInstancesTable;
import org.genericsystem.reactor.gscomponents.Modal.ModalEditor;
import org.genericsystem.reactor.gscomponents.Monitor;
import org.genericsystem.reactor.gscomponents.Responsive;
import org.genericsystem.reactor.gscomponents.RootTagImpl;
@RunScript(GeoScript.class)
@DependsOnModel({ AdministrativeTerritory.class, Country.class, Place.class, City.class, Building.class })
@Style(name = "background-color", value = "#00afeb")
@Children({ ModalEditor.class, AppHeader.class, Responsive.class, Monitor.class })
@Children(path = Responsive.class, value = { TitledInstancesTable.class, TitledInstancesTable.class,
TitledInstancesTable.class, TitledInstancesTable.class, TitledInstancesTable.class })
@DirectSelect(path = { Responsive.class, TitledInstancesTable.class }, value = { Country.class, Adm1.class, Adm2.class,
Adm3.class, City.class })
public class GeoStore extends RootTagImpl {
public static void main(String[] mainArgs) {
ApplicationServer.startSimpleGenericApp(mainArgs, GeoStore.class, "/GeoApp");
}
public static void readFile(String filename, Root engine, Generic returnType, int n) {
try {
BufferedReader reader;
String line;
reader = new BufferedReader(new FileReader(filename));
while ((line = reader.readLine()) != null)
readLine(line, engine, returnType, n);
reader.close();
} catch (Exception e) {
e.printStackTrace();
}
engine.getCurrentCache().flush();
}
public static String componentCode(String[] parts, int n) {
String code = parts[0];
for (int i = 1; i < n - 1; i++)
code += "_" + parts[i];
return code;
}
private static HashMap<String, Generic> hm;
public static void readLine(String line, Root engine, Generic returnType, int n) {
String[] parts = line.split("\\|");
if ("FR".equals(parts[0])) {
System.out.println("@" + line);
Generic currentInstance;
String currentCode;
if (returnType.getBaseComponent() == null) {
currentCode = parts[parts.length - 2];
currentInstance = returnType.addInstance(parts[n]);
hm.put(currentCode, currentInstance);
} else {
String componentCode = componentCode(parts, n);
currentCode = componentCode + "_" + parts[n - 1];
Generic componentInstance = hm.get(componentCode);
if (componentInstance != null) {
// currentGeneric = returnType.addInstance(parts[parts.length - 1], componentGeneric);
String currentValue;
if (returnType == engine.find(City.class)) {
if ("44".equals(parts[2])) {
currentInstance = engine.getCurrentCache().buildAndPlug(null, returnType,
Collections.emptyList(), parts[n - 1],
Collections.singletonList(componentInstance));
Generic latInstance = engine.getCurrentCache().buildAndPlug(null,
engine.find(Latitude.class), Collections.emptyList(), parts[n],
Collections.singletonList(currentInstance));
Generic lonInstance = engine.getCurrentCache().buildAndPlug(null,
engine.find(Longitude.class), Collections.emptyList(), parts[n + 1],
Collections.singletonList(currentInstance));
hm.put(currentCode, currentInstance);
}
} else {
currentValue = parts[n];
currentInstance = engine.getCurrentCache().buildAndPlug(null, returnType,
Collections.emptyList(), parts[n], Collections.singletonList(componentInstance));
hm.put(currentCode, currentInstance);
}
}
}
}
}
public static class GeoScript implements Script {
@Override
public void run(Root engine) {
hm = new HashMap<>();
readFile("src/main/resources/countries.csv", engine, engine.find(Country.class), 1);
readFile("src/main/resources/adm1.csv", engine, engine.find(Adm1.class), 2);
readFile("src/main/resources/adm2.csv", engine, engine.find(Adm2.class), 3);
readFile("src/main/resources/adm3.csv", engine, engine.find(Adm3.class), 4);
readFile("src/main/resources/populated_fr.csv", engine, engine.find(City.class), 5);
// Generic castle = engine.find(Castle.class);
// System.out.println(castle);
//
// Generic cityInstance = engine.find(City.class).getInstance("Nantes", hm.get("FR_52_44_442"));
// System.out.println(cityInstance.info());
// Generic castleInstance = castle.addInstance("château des ducs de Bretagne", cityInstance);
// Generic latInstance = engine.getCurrentCache().buildAndPlug(null, engine.find(Latitude.class),
// Collections.emptyList(), 47.2162, Collections.singletonList(castleInstance));
// Generic lonInstance = engine.getCurrentCache().buildAndPlug(null, engine.find(Longitude.class),
// Collections.emptyList(), -1.5497, Collections.singletonList(castleInstance));
//
// engine.getCurrentCache().flush();
//
// System.out.println(castleInstance.info());
// System.out.println(engine.find(Castle.class).getInstances().stream().collect(Collectors.toList()));
}
}
}