-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreateTown.go
More file actions
145 lines (134 loc) · 3.63 KB
/
createTown.go
File metadata and controls
145 lines (134 loc) · 3.63 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
package main
import (
"encoding/json"
"io/ioutil"
"os"
"github.com/jmcvetta/neoism"
logging "github.com/op/go-logging"
)
var log = logging.MustGetLogger("example")
var format = logging.MustStringFormatter(
`%{color}%{time:15:04:05.000} %{shortfunc} ▶ %{level} %{id}%{color:reset} %{message}`,
)
func createTownFromJSON(fileName string) error {
backend1Leveled := logging.AddModuleLevel(logging.NewLogBackend(os.Stderr, "", 0))
backend1Leveled.SetLevel(logging.INFO, "")
logging.SetBackend(backend1Leveled, logging.NewBackendFormatter(logging.NewLogBackend(os.Stderr, "", 0), format))
if _, err := DB.CreateUniqueConstraint("BusLine", "name"); err != nil {
return err
}
if _, err := DB.CreateUniqueConstraint("BusStop", "id"); err != nil {
return err
}
town, err := getTown(fileName)
if err != nil {
return err
}
for _, busLine := range town.BusLines {
log.Noticef("Start registering line %s\n", busLine.Name)
if err := createBusLine(busLine); err != nil {
return err
}
if err := createBusStops(busLine); err != nil {
return err
}
}
return nil
}
func getTown(fileName string) (*jsonData, error) {
var out jsonData
file, err := os.Open(fileName)
if err != nil {
return nil, err
}
content, err := ioutil.ReadAll(file)
if err != nil {
return nil, err
}
if err := json.Unmarshal(content, &out); err != nil {
return nil, err
}
return &out, nil
}
func createBusLine(busLine *busLine) error {
query := `MERGE (b:BusLine {name: {name}, id: {id}, color: {color}, textColor: {textColor}})
ON CREATE
SET b:BusLine`
params := neoism.Props{
"name": busLine.Name,
"id": busLine.ID,
"color": busLine.Color,
"textColor": busLine.TextColor,
}
cq := neoism.CypherQuery{
Statement: query,
Parameters: params,
}
if err := DB.Cypher(&cq); err != nil {
return err
}
log.Noticef("Line %s created\n", busLine.Name)
return nil
}
func createBusStops(busLine *busLine) error {
for dir := 0; dir < len(busLine.Path); dir++ {
if busLine.Path[dir] == nil {
continue
}
log.Debugf("Start path %s (%d (on %d))\n", busLine.Path[dir].Dir, dir, len(busLine.Path))
for idStop := 0; idStop < len(busLine.Path[dir].Stops); idStop++ {
stop := busLine.Path[dir].Stops[idStop]
if err := createStop(stop); err != nil {
return err
}
if idStop+1 < len(busLine.Path[dir].Stops) {
stopNext := busLine.Path[dir].Stops[idStop+1]
if err := createStop(stopNext); err != nil {
return err
}
if err := createRelationBetweenTwoStops(stop, stopNext, busLine, dir); err != nil {
return err
}
}
}
}
return nil
}
func createStop(stop *stop) error {
query := `MERGE (b:BusStop {name: {name}, idStop: {id}, lat: {lat}, lon: {lon}})
ON CREATE
SET b:BusStop`
params := neoism.Props{
"name": stop.Name,
"id": stop.ID,
"lat": stop.Lat,
"lon": stop.Lon,
}
cq := neoism.CypherQuery{
Statement: query,
Parameters: params,
}
if err := DB.Cypher(&cq); err != nil {
return err
}
log.Infof("Stop %s created\n", stop.Name)
return nil
}
func createRelationBetweenTwoStops(startStop *stop, endStop *stop, busLine *busLine, dir int) error {
query := "MATCH (a:BusStop), (b:BusStop) WHERE a.idStop = {idStopA} AND b.idStop = {idStopB} CREATE (a)-[p:PATH {busLineID: {busLineID}, direction: {dir}}]->(b)"
params := neoism.Props{
"idStopA": startStop.ID,
"idStopB": endStop.ID,
"busLineID": busLine.ID,
"dir": dir,
}
cq := neoism.CypherQuery{
Statement: query,
Parameters: params,
}
if err := DB.Cypher(&cq); err != nil {
return err
}
log.Infof("Relationship between %s and %s of for line %s\n", startStop.Name, endStop.Name, busLine.Name)
return nil
}