/
main.go
269 lines (222 loc) · 8.17 KB
/
main.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
package main
import (
"bytes"
"io/ioutil"
"log"
"net/http"
"time"
"encoding/json"
"fmt"
"os"
"text/template"
"gopkg.in/mgo.v2"
// "gopkg.in/mgo.v2/bson"
sparql "github.com/knakk/sparql"
)
const queries = `
# Comments are ignored, except those tagging a query.
# tag: maxage
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
prefix iodp: <http://data.oceandrilling.org/core/1/>
prefix foaf: <http://xmlns.com/foaf/0.1/>
prefix owl: <http://www.w3.org/2002/07/owl#>
prefix xsd: <http://www.w3.org/2001/XMLSchema#>
prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
SELECT (MAX(?age) AS ?maxage) (MIN(?age) AS ?minage)
WHERE {
?s iodp:leg "192" .
?s iodp:ma ?age .
}
# tag: query2
PREFIX chronos: <http://www.chronos.org/loc-schema#>
PREFIX geo: <http://www.w3.org/2003/01/geo/wgs84_pos#>
SELECT ?dataset ?ob ?age ?depth ?long ?lat
FROM <http://chronos.org/janusamp#>
WHERE {
?ob chronos:age ?age .
?ob chronos:depth ?depth .
?ob <http://purl.org/linked-data/cube#dataSet> ?dataset .
?dataset geo:long ?long .
?dataset geo:lat ?lat .
?dataset chronos:leg "{{.Leg}}" .
?dataset chronos:site "{{.Site}}" .
?dataset chronos:hole "{{.Hole}}" .
}
ORDER BY ?dataset DESC(?age)
LIMIT 1
# connection numeric age to name
# tag: csiro
prefix gts: <http://resource.geosciml.org/ontology/timescale/gts#>
prefix thors: <http://resource.geosciml.org/ontology/timescale/thors#>
prefix tm: <http://def.seegrid.csiro.au/isotc211/iso19108/2002/temporal>
PREFIX isc: <http://resource.geosciml.org/classifier/ics/ischart/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT *
WHERE {
?era gts:rank ?rank .
?era thors:begin/tm:temporalPosition/tm:value ?begin .
?era thors:begin/tm:temporalPosition/tm:frame <http://resource.geosciml.org/classifier/cgi/geologicage/ma> .
?era thors:end/tm:temporalPosition/tm:value ?end .
?era thors:end/tm:temporalPosition/tm:frame <http://resource.geosciml.org/classifier/cgi/geologicage/ma> .
?era rdfs:label ?name .
BIND ( "{{.}}"^^xsd:decimal AS ?targetAge )
FILTER ( ?targetAge > xsd:decimal(?end) )
FILTER ( ?targetAge < xsd:decimal(?begin) )
}
`
type CSIROStruct struct {
Head struct {
Vars []string `json:"vars"`
} `json:"head"`
Results struct {
Bindings []struct {
Begin struct {
Datatype string `json:"datatype"`
Type string `json:"type"`
Value string `json:"value"`
} `json:"begin"`
End struct {
Datatype string `json:"datatype"`
Type string `json:"type"`
Value string `json:"value"`
} `json:"end"`
Era struct {
Type string `json:"type"`
Value string `json:"value"`
} `json:"era"`
Name struct {
Type string `json:"type"`
Value string `json:"value"`
XML_lang string `json:"xml:lang"`
} `json:"name"`
Rank struct {
Type string `json:"type"`
Value string `json:"value"`
} `json:"rank"`
TargetAge struct {
Datatype string `json:"datatype"`
Type string `json:"type"`
Value string `json:"value"`
} `json:"targetAge"`
} `json:"bindings"`
} `json:"results"`
}
type Expedition struct {
Expedition string `json:"expedition"`
Site string `json:"site"`
Hole string `json:"hole"`
}
func main() {
// call mongo and lookup the redirection to use...
session, err := GetMongoCon()
if err != nil {
log.Println(err)
panic(err)
}
defer session.Close()
IndexCSVW(session)
// IndexSchema(session)
}
func IndexCSVW(session *mgo.Session) {
// Optional. Switch the session to a monotonic behavior.
csvw := session.DB("expedire").C("features")
// Find all Documents CSVW
var csvwDocs []Expedition
err := csvw.Find(nil).All(&csvwDocs)
if err != nil {
fmt.Printf("this is error %v \n", err)
}
// index some data
for _, item := range csvwDocs {
// fmt.Printf("Indexed item %d with Leg: %s\n", index, item.Expedition)
res := SPARQLCall(item.Expedition, item.Site, item.Hole, "query2", "http://opencoredata.org/sparql")
solutionsTest := res.Solutions() // map[string][]rdf.Term
// fmt.Println("res.Solutions():")
for _, i := range solutionsTest {
fmt.Printf("\n\nLeg %s Site %s Hole %s Age %s \n", item.Expedition, item.Site, item.Hole, i["age"])
CSIROHack(fmt.Sprint(i["age"]))
// csiroSolutions := csirores.Solutions()
// fmt.Println(csirores)
// for _, x := range csiroSolutions {
// fmt.Printf("TEST item %s \n", x)
// }
// results := CSIROCall(fmt.Sprint(i["age"]))
// fmt.Println(results)
}
}
// // Update Example for Mongo
// colQuerier := bson.M{"name": "Ale"}
// change := bson.M{"$set": bson.M{"phone": "+86 99 8888 7777", "timestamp": time.Now()}}
// err = c.Update(colQuerier, change)
// if err != nil {
// panic(err)
// }
}
func SPARQLCall(leg string, site string, hole string, query string, endpoint string) *sparql.Results {
repo, err := sparql.NewRepo(endpoint,
sparql.Timeout(time.Millisecond*15000),
)
if err != nil {
log.Printf("%s\n", err)
}
f := bytes.NewBufferString(queries)
bank := sparql.LoadBank(f)
q, err := bank.Prepare(query, struct{ Leg, Site, Hole string }{leg, site, hole})
if err != nil {
log.Printf("%s\n", err)
}
res, err := repo.Query(q)
if err != nil {
log.Printf("%s\n", err)
}
return res
}
func CSIROHack(age string) {
const url = "http://resource.geosciml.org/sparql/isc2014?query=PREFIX+isc%3A+%3Chttp%3A%2F%2Fresource.geosciml.org%2Fclassifier%2Fics%2Fischart%2F%3E%0D%0APREFIX+gts%3A+%3Chttp%3A%2F%2Fresource.geosciml.org%2Fontology%2Ftimescale%2Fgts%23%3E%0D%0APREFIX+thors%3A+%3Chttp%3A%2F%2Fresource.geosciml.org%2Fontology%2Ftimescale%2Fthors%23%3E%0D%0APREFIX+tm%3A+%3Chttp%3A%2F%2Fdef.seegrid.csiro.au%2Fisotc211%2Fiso19108%2F2002%2Ftemporal%23%3E%0D%0APREFIX+rdfs%3A+%3Chttp%3A%2F%2Fwww.w3.org%2F2000%2F01%2Frdf-schema%23%3E%0D%0A%0D%0ASELECT+%2A%0D%0AWHERE+%7B%0D%0A++++++++++++++++%3Fera+gts%3Arank+%3Frank+.%0D%0A+++++++++++++++++%3Fera+thors%3Abegin%2Ftm%3AtemporalPosition%2Ftm%3Avalue+%3Fbegin+.%0D%0A+++++++++++++++++%3Fera+thors%3Abegin%2Ftm%3AtemporalPosition%2Ftm%3Aframe+%3Chttp%3A%2F%2Fresource.geosciml.org%2Fclassifier%2Fcgi%2Fgeologicage%2Fma%3E+.%0D%0A+++++++++++++++++%3Fera+thors%3Aend%2Ftm%3AtemporalPosition%2Ftm%3Avalue+%3Fend+.%0D%0A+++++++++++++++++%3Fera+thors%3Aend%2Ftm%3AtemporalPosition%2Ftm%3Aframe+%3Chttp%3A%2F%2Fresource.geosciml.org%2Fclassifier%2Fcgi%2Fgeologicage%2Fma%3E+.%0D%0A++++++++++++++++%3Fera+rdfs%3Alabel+%3Fname+.%0D%0A+++++++++++++++++BIND+%28+%22{{.}}%22%5E%5Exsd%3Adecimal+AS+%3FtargetAge+%29%0D%0A+++++++++++++++++FILTER+%28+%3FtargetAge+%3E+xsd%3Adecimal%28%3Fend%29+%29%0D%0A+++++++++++++++++FILTER+%28+%3FtargetAge+%3C+xsd%3Adecimal%28%3Fbegin%29+%29%0D%0A%7D%0D%0A"
dt, err := template.New("RDF template").Parse(url)
if err != nil {
log.Printf("RDF template creation failed for hole data: %s", err)
}
var buff = bytes.NewBufferString("")
err = dt.Execute(buff, age)
if err != nil {
log.Printf("RDF template execution failed: %s", err)
}
req, _ := http.NewRequest("GET", string(buff.Bytes()), nil)
req.Header.Add("accept", "application/sparql-results+json")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := ioutil.ReadAll(res.Body)
csiroStruct := CSIROStruct{}
json.Unmarshal(body, &csiroStruct)
// loop on csiroStruct.Results.Bindings
for _, item := range csiroStruct.Results.Bindings {
fmt.Printf("Era: %s %s \n", item.Era.Type, item.Era.Value)
fmt.Printf("Name: %s %s \n", item.Name.Type, item.Name.Value)
}
// fmt.Println(string(body))
}
// Had problem with this due to likely some issues now resolved with SPARQL library
func CSIROCall(age string) *sparql.Results {
repo, err := sparql.NewRepo("http://resource.geosciml.org/sparql/isc2014",
sparql.Timeout(time.Millisecond*15000),
)
if err != nil {
log.Printf("%s\n", err)
}
f := bytes.NewBufferString(queries)
bank := sparql.LoadBank(f)
q, err := bank.Prepare("csiro", struct{ Age string }{age})
if err != nil {
log.Printf("%s\n", err)
}
res, err := repo.Query(q)
if err != nil {
log.Printf("%s\n", err)
}
return res
}
func GetMongoCon() (*mgo.Session, error) {
host := os.Getenv("MONGO_HOST")
return mgo.Dial(host)
}