-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpokemon.go
186 lines (157 loc) · 4.87 KB
/
pokemon.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
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
package main
import (
"bufio"
"fmt"
"log"
"os"
"strconv"
"strings"
"github.com/cayleygraph/cayley"
"github.com/cayleygraph/cayley/graph"
"github.com/cayleygraph/cayley/quad"
uuid "github.com/satori/go.uuid"
)
func Setup(dbFile *string) *cayley.Handle {
// Initialize the database
graph.InitQuadStore("bolt", *dbFile, nil)
// Open and use the database
store, err := cayley.NewGraph("bolt", *dbFile, nil)
if err != nil {
log.Fatalln(err)
}
return store
}
func LoadPokemons(store *cayley.Handle, csvFile *string) {
file, err := os.Open(*csvFile)
if err != nil {
log.Fatal(err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
s := strings.Split(scanner.Text(), ",")
id, err := strconv.Atoi(s[0])
if err != nil {
log.Fatal(err)
}
speciesId, err := strconv.Atoi(s[2])
if err != nil {
log.Fatal(err)
}
height, err := strconv.Atoi(s[3])
if err != nil {
log.Fatal(err)
}
baseExperience, err := strconv.Atoi(s[4])
if err != nil {
log.Fatal(err)
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
// IRI format: <https://my-domain.com/9f1dae45-6d98-11e6-871e-843a4b0f5a10>
uuid := quad.IRI("https://my-domain.com/" + uuid.NewV1().String())
store.AddQuad(quad.Make(uuid, quad.IRI("rdf:id"), id, nil))
store.AddQuad(quad.Make(uuid, quad.IRI("rdf:type"), "pokemon", nil))
store.AddQuad(quad.Make(uuid, quad.IRI("schema:name"), s[1], nil))
store.AddQuad(quad.Make(uuid, quad.IRI("rdf:species_id"), speciesId, nil))
store.AddQuad(quad.Make(uuid, quad.IRI("rdf:height"), height, nil))
store.AddQuad(quad.Make(uuid, quad.IRI("rdf:base_experience"), baseExperience, nil))
}
}
func UpdatePikachu(store *cayley.Handle) {
// find uuid of pikacho
p := cayley.StartPath(store).Has(quad.IRI("schema:name"), quad.String("pikacho"))
vals, err := p.Iterate(nil).AllValues(nil)
if err != nil {
log.Fatalln(err)
} else if len(vals) == 0 {
log.Fatalln("pikacho not found")
}
uuid := vals[0].Native().(quad.IRI)
// change pikacho to pikachu
t := cayley.NewTransaction()
t.RemoveQuad(quad.Make(uuid, quad.IRI("schema:name"), "pikacho", nil))
t.AddQuad(quad.Make(uuid, quad.IRI("schema:name"), "pikachu", nil))
err = store.ApplyTransaction(t)
if err != nil {
log.Fatalln(err)
}
}
func LoadEvolutions(store *cayley.Handle, csvFile *string) {
file, err := os.Open(*csvFile)
if err != nil {
log.Fatal("Error while opening evolutions csv file", err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
s := strings.Split(scanner.Text(), ",")
targetPokemon, err := strconv.Atoi(s[0])
if err != nil {
log.Fatal("Error converting string to int", err)
}
if s[3] == "" {
continue
}
sourcePokemon, err := strconv.Atoi(s[3])
if err != nil {
log.Fatal("Error converting string to int", err)
}
if err := scanner.Err(); err != nil {
log.Fatal("Error while scanning", err)
}
// find uuid of source and target
p := cayley.StartPath(store).Has(quad.IRI("rdf:id"), quad.Int(sourcePokemon))
vals, err := p.Iterate(nil).AllValues(nil)
if err != nil {
log.Fatalln(err)
} else if len(vals) == 0 {
log.Fatalln("source pokemon not found")
}
sourcePokemonUUID := vals[0].Native().(quad.IRI)
p = cayley.StartPath(store).Has(quad.IRI("rdf:id"), quad.Int(targetPokemon))
vals, err = p.Iterate(nil).AllValues(nil)
if err != nil {
log.Fatalln(err)
} else if len(vals) == 0 {
log.Fatalln("target pokemon not found")
}
targetPokemonUUID := vals[0].Native().(quad.IRI)
store.AddQuad(quad.Make(sourcePokemonUUID, quad.IRI("rdf:evolves_to"), targetPokemonUUID, nil))
}
}
func Print(store *cayley.Handle) {
// Now we create the path, to get to our data
p := cayley.StartPath(store).Has(quad.IRI("rdf:type"), quad.String("pokemon")).Out(quad.IRI("schema:name"))
it, _ := p.BuildIterator().Optimize()
it, _ = store.OptimizeIterator(it)
defer it.Close()
// While we have items
for it.Next() {
token := it.Result() // get a ref to a node
value := store.NameOf(token) // get the value in the node
nativeValue := quad.NativeOf(value) // this converts nquad values to normal Go type
fmt.Println(nativeValue) // print it!
}
if err := it.Err(); err != nil {
log.Fatalln(err)
}
}
func PrintEvolutions(store *cayley.Handle) {
// Now we create the path, to get to our data
p := cayley.StartPath(store).Out(quad.IRI("rdf:evolves_to")).Out(quad.IRI("rdf:evolves_to")).Out(quad.IRI("schema:name"))
it, _ := p.BuildIterator().Optimize()
it, _ = store.OptimizeIterator(it)
defer it.Close()
// While we have items
for it.Next() {
token := it.Result() // get a ref to a node
value := store.NameOf(token) // get the value in the node
nativeValue := quad.NativeOf(value) // this converts nquad values to normal Go type
fmt.Println(nativeValue) // print it!
}
if err := it.Err(); err != nil {
log.Fatalln(err)
}
}