-
Notifications
You must be signed in to change notification settings - Fork 0
/
locations.go
125 lines (111 loc) · 3.12 KB
/
locations.go
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
package entities
import (
"errors"
"fmt"
"strings"
)
var _ = fmt.Println // remove after test
func makeLocationTypeSet() map[string]struct{} {
a := map[string]struct{}{}
a["Building"] = struct{}{}
a["City"] = struct{}{}
a["County"] = struct{}{}
a["State"] = struct{}{}
a["Country"] = struct{}{}
return a
}
var LocationTypeSet = makeLocationTypeSet()
type Location struct {
Key *Key
LocationType string
Name string
Abbreviation string
GeoLocation *GeoLocation
Contains []*Location
Inside []*Location
}
func NewLocation(t string, n string) (*Location,error) {
p := new(Location)
p.Key = makeKey("Location")
_,ok := LocationTypeSet[t]
if !ok {
err := errors.New("Unknown location type: " + t)
return nil, err
}
p.LocationType = t
p.Name = n
return p, nil
}
func NewBuilding(n string) (*Location,error) {return NewLocation("Building", n)}
func NewCity(n string) (*Location,error) {return NewLocation("City", n)}
func NewCounty(n string) (*Location,error) {return NewLocation("County", n)}
func NewState(n string) (*Location,error) {return NewLocation("State", n)}
func NewCountry(n string) (*Location,error) {return NewLocation("Country", n)}
func (a *Location) Triples () [][3]string {
var t [][3]string
t = append(t, makeTriple(Triple{(*a).Key,"hasType","Location",nil}))
t = append(t, makeTriple(Triple{(*a).Key,"hasLocationType",(*a).LocationType,nil}))
t = append(t, makeTriple(Triple{(*a).Key,"hasName",(*a).Name,nil}))
if (*a).Abbreviation != "" {t = append(t, makeTriple(Triple{(*a).Key,"hasAbbreviation",(*a).Abbreviation,nil}))}
if (*a).GeoLocation != nil {t = append(t, makeTriple(Triple{(*a).Key,"hasGeoLocation","",(*a.GeoLocation).Key}))}
for _,cptr := range (*a).Contains {
t = append(t, makeTriple(Triple{(*a).Key,"hasContains","",cptr.Key}))
}
for _,cptr := range (*a).Inside {
t = append(t, makeTriple(Triple{(*a).Key,"hasInside","",cptr.Key}))
}
return t
}
func (a *Location) Row() []string {
var t []string
var co []string
var in []string
t = append(t, a.Key.s)
t = append(t, a.LocationType)
t = append(t, a.Name)
t = append(t, a.Abbreviation)
if a.GeoLocation != nil { t=append(t, a.GeoLocation.Key.s)}
for _,cptr := range (*a).Contains {
co = append(co, cptr.Key.s)
}
t = append(t,strings.Join(co,","))
for _,cptr := range (*a).Inside {
in = append(in, cptr.Key.s)
}
t = append(t,strings.Join(in,","))
return t
}
func FindLocationKey (kf *Key) int {
for i,a := range Locations {
if kf.s == a.Key.s {
return i
}
}
return -1
}
func AddLocationFact(a []string) {
key := new(Key)
key.s = a[0]
i := FindLocationKey(key)
switch a[1] {
case "LocationType":
Locations[i].LocationType = a[2]
case "Name":
Locations[i].Name = a[2]
case "Abbreviation":
Locations[i].Abbreviation = a[2]
case "GeoLocation":
key.s = a[2]
j := FindGeoLocationKey(key)
Locations[i].GeoLocation = GeoLocations[j]
case "Contains":
key.s = a[2]
j := FindLocationKey(key)
Locations[i].Contains = append(Locations[i].Contains,Locations[j])
case "Inside":
key.s = a[2]
j := FindPersonKey(key)
Locations[i].Inside = append(Locations[i].Inside,Locations[j])
}
}
var Locations []*Location