-
Notifications
You must be signed in to change notification settings - Fork 0
/
query.go
417 lines (358 loc) · 12.7 KB
/
query.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
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
package main
import (
//"encoding/json"
"fmt"
"github.com/jmcvetta/neoism"
"os"
"strconv"
)
type req struct {
Classes []Class
RequirementType string // Class, Credit, or Sequence
RequirementQuantity int // -1 for all otherwise a quantity
}
type adjSet struct {
From string `json:"r.From"`
To string `json:"r.To"`
}
type Relation struct {
Label string `json:"Label"`
}
type course struct {
ClassName string //department + number
Properties classProperties //see def below
}
type classProperties struct {
Department string
Number int
Credits int // number of credits for this class
Enables []string // classes this class enables
Prereqs []string // classes this class requires
Title string // the name of this class
Fulfills []string // the requirements this class fulfills
}
type Class struct {
Title string `json:"Title"`
Department string `json:"Department"`
Number int `json:"Number"`
Credits int `json:"Credits"`
}
var ClassMap map[string][]string
var Db *neoism.Database
func main1() {
var err error
Db, err = neoism.Connect("http://localhost:7474/db/data")
if err != nil {
panic(err)
}
var dept string
if len(os.Args) < 3 {
dept = "CSCI"
} else {
dept = os.Args[2]
}
class64, _ := strconv.ParseInt(os.Args[1], 10, 0)
class := int(class64)
createEnablesAndPrereqs()
//fmt.Println(ClassMap[dept+" "+strconv.FormatInt(class64, 10)+"Enables"])
prereqs(class, dept) //throw away to use class and dept so i can test.
//notTaken := prereqsNotTaken(class, dept, "eric")
//courseList := allCourseList()
//printClasses(notTaken)
//courseList = append(courseList, createCourse(141, "CSCI"))
//courseList = append(courseList, createCourse(145, "CSCI"))
//courseList = append(courseList, createCourse(241, "CSCI"))
// testMap := make(map[string]Class)
// testMap["test1"] = notTaken[0]
// testMap["test2"] = notTaken[1]
// jsonMap, _ := json.Marshal(testMap)
// fmt.Println(string(jsonMap))
degreeMap()
//json, _ := json.Marshal(struct{ Data []Class }{notTaken})
//fmt.Println("printing the json:")
//fmt.Println(string(json))
// fmt.Println("the prereq tree fo", class)
// fmt.Println()
// res := allPrereqs(class, dept)
// printPrereqTree(res)
// fmt.Println()
// var res1 []Class
// fmt.Println("prereqs of", class, "are:")
// res1 = prereqs(class, dept)
// printClasses(res1)
// fmt.Println()
// var res2 []Class
// fmt.Println(class, "is a prereq of:")
// res2 = enables(class, dept)
// printClasses(res2)
}
func degreeMap() map[string]req {
var cores []Class
cq := neoism.CypherQuery{
Statement: `match (c:Class)<-[r:FULFILLED_BY]-(core:Requirement{name: "CORE"})
return distinct c.title AS Title, c.department AS Department, c.number AS Number, c.credits AS Credits`,
Result: &cores,
}
Db.Cypher(&cq)
var electives []Class
cq1 := neoism.CypherQuery{
Statement: `match (c:Class)<-[r:FULFILLED_BY]-(core:Requirement{name: "ELECTIVES"})
return distinct c.title AS Title, c.department AS Department, c.number AS Number, c.credits AS Credits`,
Result: &electives,
}
Db.Cypher(&cq1)
// var physics []Class
// cq2 := neoism.CypherQuery{
// Statement: ` match (a:Class)<-[r]-(Requirement{name: "SCIENCE"}) where
// a.department = "PHYS" match (a)<-[*]-(end:Class)
// return distinct end.title AS Title, end.department AS Department, end.number AS Number end.credits AS Credits UNION match (a:Class)<-[r]-(Requirement{name: "SCIENCE"}) where
// a.department = "PHYS" return a.title AS Title, a.department AS Department, a.number AS Number a.credits AS Credits`
// Result: &physics
// }
// Db.Cypher(&cq2)
// var chemistry []Class
// cq3 := neoism.CypherQuery{
// Statement: ` match (a:Class)<-[r]-(Requirement{name: "SCIENCE"}) where
// a.department = "CHEM" match (a)<-[*]-(end:Class)
// return distinct end.title AS Title, end.department AS Department, end.number AS Number end.credits AS Credits UNION match (a:Class)<-[r]-(Requirement{name: "SCIENCE"}) where
// a.department = "CHEM" return a.title AS Title, a.department AS Department, a.number AS Number a.credits AS Credits`
// Result: &chemistry
// }
// Db.Cypher(&cq3)
// var biology []Class
// cq4 := neoism.CypherQuery{
// Statement: ` match (a:Class)<-[r]-(Requirement{name: "SCIENCE"}) where
// a.department = "BIOL" match (a)<-[*]-(end:Class)
// return distinct end.title AS Title, end.department AS Department, end.number AS Number end.credits AS Credits UNION match (a:Class)<-[r]-(Requirement{name: "SCIENCE"}) where
// a.department = "BIOL" return a.title AS Title, a.department AS Department, a.number AS Number a.credits AS Credits`
// Result: &biology
// }
// Db.Cypher(&cq4)
var sci []Class
cq5 := neoism.CypherQuery{
Statement: `match (a:Class)<-[r]-(Requirement{name: "SCIENCE"})
return a.title AS Title, a.department AS Department, a.number AS Number, a.credits AS Credits `,
Result: &sci,
}
Db.Cypher(&cq5)
degree := make(map[string]req)
var core req
core.Classes = cores
core.RequirementType = "Class"
core.RequirementQuantity = -1
degree["CORE"] = core
var elective req
elective.Classes = electives
elective.RequirementType = "Credit"
elective.RequirementQuantity = 16
degree["ELECTIVES"] = elective
var science req
science.Classes = sci
science.RequirementType = "Class"
science.RequirementQuantity = 1
degree["SCIENCE"] = science
return degree
}
func prereqsNotTaken(classNumber int, department string, student string) []Class {
var prereqs []Class
cq := neoism.CypherQuery{
Statement: `match (c:Class)<-[*]-(d:Class)
where c.department = {department} and c.number = {class}
return distinct d.title AS Title, d.department AS Department, d.number AS Number, d.credits AS Credits`,
Parameters: neoism.Props{"class": classNumber, "department": department},
Result: &prereqs,
}
Db.Cypher(&cq)
var taken []Class
cq1 := neoism.CypherQuery{
Statement: `match (c:Class)<-[*]-(d:Class) where c.department = {department}
and c.number = {class} match (d)<-[r1:TAKEN]-(s:Student) where s.name = {student}
return distinct d.title AS Title, d.department AS Department, d.number AS Number, d.credits AS Credits`,
Parameters: neoism.Props{"class": classNumber, "department": department, "student": student},
Result: &taken,
}
Db.Cypher(&cq1)
// fmt.Println("prereqs:")
// printClasses(prereqs)
// fmt.Println()
// fmt.Println("taken:")
// printClasses(taken)
// fmt.Println()
var notTaken []Class
for i := range prereqs {
prereq := prereqs[i]
count := false
for j := range taken {
class := taken[j]
if prereq.Title == class.Title {
count = true
}
}
if !count {
notTaken = append(notTaken, prereq)
}
}
return notTaken
}
//take the difference of the prereqs and the taken class lists
func allCourseList() []course {
var courseList []course
var classes []Class
cq := neoism.CypherQuery{
Statement: `
MATCH (d:Class) RETURN d.title AS Title, d.department AS Department, d.number AS Number, d.credits AS Credits`,
Result: &classes,
}
err := Db.Cypher(&cq)
if err != nil {
fmt.Println(err)
}
//printClasses(classes)
//fmt.Println(len(classes))
for i := range classes {
//fmt.Println(i)
class := classes[i]
//fmt.Println(class.Title, class.Number, class.Department, class.Credits)
courseList = append(courseList, createCourse(class.Number, class.Department))
}
return courseList
}
func createEnablesAndPrereqs() {
adj := getAdjSet()
ClassMap = make(map[string][]string)
for i := range adj {
relation := adj[i]
ClassMap[relation.From+"Enables"] = append(ClassMap[relation.From+"Enables"], relation.To)
ClassMap[relation.To+"Prereqs"] = append(ClassMap[relation.To+"Prereqs"], relation.From)
}
}
func getAdjSet() []adjSet {
var adj []adjSet
cq := neoism.CypherQuery{
Statement: ` match (a:Class)-[r:PREREQ_OF]->(b:Class) return distinct r.From, r.To `,
Result: &adj,
}
Db.Cypher(&cq)
return adj
}
func printClasses(res []Class) {
for i := range res {
r := res[i]
fmt.Println("\t- ", r.Title, r.Department, r.Number, r.Credits)
}
}
func printPrereqTree(res2 []Class) {
for i := range res2 {
r := res2[i]
resi := allPrereqs(r.Number, r.Department)
fmt.Println(r.Title, r.Department, r.Number, r.Credits)
printClasses(resi)
fmt.Println()
}
}
func prereqs(classNumber int, department string) []Class {
var res []Class
cq := neoism.CypherQuery{
Statement: `
MATCH (c:Class)<-[PREREQ_OF]-(d:Class)
WHERE c.number = {class} AND c.department = {department}
RETURN DISTINCT d.title AS Title, d.department AS Department, d.number AS Number, d.credits AS Credits `,
Parameters: neoism.Props{"class": classNumber, "department": department},
Result: &res,
}
Db.Cypher(&cq)
return res
}
func enables(classNumber int, department string) []Class {
var res []Class
cq := neoism.CypherQuery{
Statement: `
MATCH (c:Class)-[PREREQ_OF]->(d:Class)
WHERE c.number = {class} AND c.department = {department}
RETURN DISTINCT d.title AS Title, d.department AS Department, d.number AS Number, d.credits AS Credits `,
Parameters: neoism.Props{"class": classNumber, "department": department},
Result: &res,
}
Db.Cypher(&cq)
return res
}
func degreeRequirements(degreeName string, degreeType string) {
}
// for gurs, so passing ACGM, BCGM, etc...
func requirementsOf(requirementType string) {
}
func allPrereqs(classNumber int, department string) []Class {
var res []Class
cq := neoism.CypherQuery{
Statement: `
MATCH p = (a:Class)-[*]->(end)
WHERE end.number = {class} AND end.department = {department}
UNWIND EXTRACT(n IN nodes(p)| n ) AS d
WITH DISTINCT d WHERE d.number <> {class}
RETURN d.title AS Title, d.department AS Department, d.number AS Number, d.credits AS Credits
ORDER BY d.department AS Department, d.number AS Number`,
Parameters: neoism.Props{"class": classNumber, "department": department},
Result: &res,
}
Db.Cypher(&cq)
return res
}
func createCourse(classNumber int, department string) course {
var class []Class
//fmt.Println(classNumber, department)
cq := neoism.CypherQuery{
Statement: `Match (d:Class) WHERE d.number = {classNum}
AND d.department = {classDept}
return distinct d.title AS Title, d.department AS Department, d.number AS Number, d.credits AS Credits`,
Parameters: neoism.Props{"classNum": classNumber, "classDept": department},
Result: &class,
}
//fmt.Println(len(class))
//printClasses(class)
Db.Cypher(&cq)
//var enable []Class
//var prereq []Class
//enable = enables(classNumber, department)
//prereq = prereqs(classNumber, department)
//fmt.Println("prereqs of", classNumber, "are:")
//printClasses(enable)
//fmt.Println()
var enableString []string
var prereqString []string
// for i := range enable {
// class := enable[i]
// //fmt.Println(class.Title)
// enableString = append(enableString, class.Title)
// }
//fmt.Println("enableString:")
// for i := range enableString {
// fmt.Println(enableString[i])
// }
// for i := range prereq {
// class := prereq[i]
// prereqString = append(prereqString, class.Title)
// }
enableString = ClassMap[department+" "+strconv.FormatInt(int64(classNumber), 10)+"Enables"]
prereqString = ClassMap[department+" "+strconv.FormatInt(int64(classNumber), 10)+"Prereqs"]
// for i := range class {
// class1 := class[i]
// fmt.Println("class:", i, "title is:", class1.Title, "credits is:", class1.Credits)
// }
credits := class[0].Credits
title := class[0].Title
//fmt.Println("test", credits, title, class[0].Number, class[0].Department)
//still need to do fulfills, need to put those relationships into the database.
var fulfills []string
//making the classProperties struct.
//fmt.Println("department and classnumber", department, classNumber)
cP := classProperties{department, classNumber, credits, enableString, prereqString, title, fulfills}
//props, _ := json.Marshal(cP)
//fmt.Println(string(props))
className := fmt.Sprintf("%s %d", department, classNumber)
course := course{className, cP}
//courseJson, err := json.Marshal(course)
//if err != nil {
// fmt.Println(err)
//}
return course
}