-
Notifications
You must be signed in to change notification settings - Fork 0
/
term.go
354 lines (310 loc) · 12.4 KB
/
term.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
package main
import (
"fmt"
"html"
"io"
"strconv"
"strings"
rdf "github.com/iand/gordf"
)
func renderTerm(w io.StringWriter, c *Context, inline bool, brief bool, level int) {
if brief {
renderBrief(w, c, inline, brief, level)
return
}
if !inline {
w.WriteString("\n")
w.WriteString(fmt.Sprintf(`<h%d>%s</h%d>`, level, c.Label(true, false), level))
w.WriteString("\n")
}
c.SetDone(
LabellingProperties...,
)
c.SetDone(
rdf.IRI("http://www.w3.org/2000/01/rdf-schema#isDefinedBy"),
rdf.IRI("http://www.w3.org/1999/02/22-rdf-syntax-ns#type"),
)
w.WriteString(`<p class="termuri"><strong>URI:</strong> `)
writeLinkedIRI(w, c, c.Term.Value, false)
w.WriteString(`</p>`)
w.WriteString(`<p class="terminfo">`)
w.WriteString(html.EscapeString(c.Description()))
w.WriteString(`</p>`)
c.SetDone(GeneralDescribingProperties...)
if c.Object(rdf.IRI("http://purl.org/vocab/vann/usageNote")) {
w.WriteString("\n")
w.WriteString(fmt.Sprintf(`<h%d>Usage</h%d>`, level+1, level+1))
w.WriteString("\n")
for _, obj := range c.Objects(rdf.IRI("http://purl.org/vocab/vann/usageNote")) {
w.WriteString(`<div class="usagenote">`)
renderLiteral(w, c.New(obj), true, false)
w.WriteString(`</div>`)
}
c.SetDone(rdf.IRI("http://purl.org/vocab/vann/usageNote"))
}
isProperty := c.Type(rdf.IRI("http://www.w3.org/1999/02/22-rdf-syntax-ns#Property"))
if isProperty {
if c.Object(rdf.IRI("http://www.w3.org/2002/07/owl#SymmetricProperty"),
rdf.IRI("http://www.w3.org/2002/07/owl#TransitiveProperty"),
rdf.IRI("http://www.w3.org/2002/07/owl#FunctionalProperty"),
rdf.IRI("http://www.w3.org/2002/07/owl#InverseFunctionalProperty"),
rdf.IRI("http://www.w3.org/2000/01/rdf-schema#domain"),
rdf.IRI("http://www.w3.org/2000/01/rdf-schema#range"),
rdf.IRI("http://www.w3.org/2002/07/owl#inverseOf"),
rdf.IRI("http://www.w3.org/2000/01/rdf-schema#subPropertyOf"),
rdf.IRI("http://www.w3.org/2002/07/owl#equivalentProperty")) {
w.WriteString("\n")
w.WriteString(fmt.Sprintf(`<h%d>Semantics</h%d>`, level+1, level+1))
w.WriteString("\n")
w.WriteString(`<p class="termsemantics">`)
renderPropertySemantics(w, c)
w.WriteString(`</p>`)
}
c.SetDone(
rdf.IRI("http://www.w3.org/2002/07/owl#SymmetricProperty"),
rdf.IRI("http://www.w3.org/2002/07/owl#TransitiveProperty"),
rdf.IRI("http://www.w3.org/2002/07/owl#FunctionalProperty"),
rdf.IRI("http://www.w3.org/2002/07/owl#InverseFunctionalProperty"),
rdf.IRI("http://www.w3.org/2000/01/rdf-schema#domain"),
rdf.IRI("http://www.w3.org/2000/01/rdf-schema#range"),
rdf.IRI("http://www.w3.org/2002/07/owl#inverseOf"),
rdf.IRI("http://www.w3.org/2000/01/rdf-schema#subPropertyOf"),
rdf.IRI("http://www.w3.org/2002/07/owl#equivalentProperty"),
)
} else {
// Class
if c.Object(rdf.IRI("http://www.w3.org/2000/01/rdf-schema#subClassOf"),
rdf.IRI("http://www.w3.org/2002/07/owl#disjointWith"),
rdf.IRI("http://www.w3.org/2002/07/owl#equivalentClass"),
rdf.IRI("http://www.w3.org/2002/07/owl#complementOf"),
) {
w.WriteString("\n")
w.WriteString(fmt.Sprintf(`<h%d>Semantics</h%d>`, level+1, level+1))
w.WriteString("\n")
w.WriteString(`<p class="termsemantics">`)
renderClassSemantics(w, c)
w.WriteString(`</p>`)
}
c.SetDone(
rdf.IRI("http://www.w3.org/2000/01/rdf-schema#subClassOf"),
rdf.IRI("http://www.w3.org/2002/07/owl#disjointWith"),
rdf.IRI("http://www.w3.org/2002/07/owl#equivalentClass"),
rdf.IRI("http://www.w3.org/2002/07/owl#complementOf"),
)
}
if c.Object(rdf.IRI("http://purl.org/vocab/vann/example")) {
w.WriteString("\n")
w.WriteString(fmt.Sprintf(`<h%d id="sec-examples">Examples</h%d>`, level+1, level+1))
w.WriteString("\n")
for _, obj := range c.Objects(rdf.IRI("http://purl.org/vocab/vann/example")) {
example := c.New(obj)
if comment, exists := example.FirstLiteral(rdf.IRI("http://www.w3.org/2000/01/rdf-schema#comment"), ""); exists {
w.WriteString("\n")
w.WriteString(fmt.Sprintf(`<h%d>%s</h%d>`, level+2, html.EscapeString(example.Label(true, false)), level+2))
w.WriteString("\n")
renderLiteral(w, c.New(comment), false, false)
} else {
render(w, example, false, false, level+1)
}
}
c.SetDone(rdf.IRI("http://purl.org/vocab/vann/example"))
}
if c.Object(
rdf.IRI("http://www.w3.org/2004/02/skos/core#changeNote"),
rdf.IRI("http://www.w3.org/2004/02/skos/core#historyNote"),
rdf.IRI("http://www.w3.org/2003/06/sw-vocab-status/ns#term_status"),
rdf.IRI("http://purl.org/dc/terms/issued"),
) {
w.WriteString("\n")
w.WriteString(fmt.Sprintf(`<h%d id="sec-status">Status</h%d>`, level+1, level+1))
w.WriteString("\n")
var status string
if statusCode, exists := c.FirstLiteral(rdf.IRI("http://purl.org/dc/terms/issued")); exists {
switch statusCode.Value {
case "unstable":
status = "is deemed to be semantically unstable and is subject to its meaning being changed."
case "stable":
status = "is deemed to be semantically stable and its meaning should not change in the foreseable future."
case "testing":
status = "is undergoing testing to determine if it is semantically stable and its meaning may change in the foreseable future."
}
}
if status != "" {
w.WriteString(`<p class="termstatus">This term `)
w.WriteString(status)
w.WriteString(`</p>`)
}
renderHistory(w, c, false, false, level+1)
c.SetDone(
rdf.IRI("http://www.w3.org/2004/02/skos/core#changeNote"),
rdf.IRI("http://www.w3.org/2004/02/skos/core#historyNote"),
rdf.IRI("http://www.w3.org/2003/06/sw-vocab-status/ns#term_status"),
rdf.IRI("http://purl.org/dc/terms/issued"),
)
}
otherProperties := c.Properties(false)
if len(otherProperties) > 0 {
w.WriteString("\n")
w.WriteString(fmt.Sprintf(`<h%d id="sec-examples">Other Information</h%d>`, level+1, level+1))
w.WriteString("\n")
renderTable(w, c, false, false, level+1)
}
}
func writeRelationsProse(w io.StringWriter, c *Context, property rdf.Term, prefix string, suffix string, useDefiniteArticle bool, conjunction string, inverse bool, isClass bool) {
if (!inverse && !c.Object(property)) || (inverse && !c.Subject(property)) {
return
}
w.WriteString(prefix)
var terms []rdf.Term
if inverse {
terms = c.Subjects(property)
} else {
terms = c.Objects(property)
}
for i, obj := range terms {
// if ($index[$uri][$property][$i]['value'] != $uri) {
// $is_restriction = FALSE;
// $value = $index[$uri][$property][$i]['value'];
// if ( isset($index[$value][RDF_TYPE]) ) {
// for ($tmp = 0; $tmp < count($index[$value][RDF_TYPE]); $tmp++) {
// if ($index[$value][RDF_TYPE][$tmp]['value'] == 'http://www.w3.org/2002/07/owl#Restriction') {
// $is_restriction = TRUE;
// }
// }
// }
// if (! $is_restriction) {
// $values[] = $index[$uri][$property][$i];
// }
// }
if i > 0 {
if i < len(terms)-1 {
w.WriteString(", ")
} else {
w.WriteString(" " + conjunction + " ")
}
}
writeTerm(w, c.New(obj), useDefiniteArticle, isClass)
}
w.WriteString(suffix)
}
func writeTerm(w io.StringWriter, c *Context, useDefiniteArticle bool, isClass bool) {
if rdf.IsIRI(c.Term) {
writeLinkedIRI(w, c, "", useDefiniteArticle)
} else if rdf.IsBlank(c.Term) {
if isClass {
writeAnonymousClass(w, c, "")
} else {
// TODO
// writeAnonymousProperty(w, c.New(obj), "", useDefiniteArticle)
writeLinkedIRI(w, c, "", useDefiniteArticle)
}
} else {
renderLiteral(w, c, true, true)
}
}
func collectionMembers(c *Context) []rdf.Term {
members := []rdf.Term{}
if first, ok := c.FirstObject(rdf.IRI("http://www.w3.org/1999/02/22-rdf-syntax-ns#first")); ok {
members = append(members, first)
}
if rest, ok := c.FirstObject(rdf.IRI("http://www.w3.org/1999/02/22-rdf-syntax-ns#rest")); ok {
others := collectionMembers(c.New(rest))
if len(others) > 0 {
members = append(members, others...)
}
}
return members
}
func renderPropertySemantics(w io.StringWriter, c *Context) {
characteristics := []string{}
if c.Type(rdf.IRI("http://www.w3.org/2002/07/owl#SymmetricProperty")) {
characteristics = append(characteristics, "symmetrical")
}
if c.Type(rdf.IRI("http://www.w3.org/2002/07/owl#TransitiveProperty")) {
characteristics = append(characteristics, "transitive")
}
if c.Type(rdf.IRI("http://www.w3.org/2002/07/owl#FunctionalProperty")) {
characteristics = append(characteristics, "functional")
}
if c.Type(rdf.IRI("http://www.w3.org/2002/07/owl#InverseFunctionalProperty")) {
characteristics = append(characteristics, "inverse functional")
}
if len(characteristics) > 0 {
w.WriteString(`This property is `)
if len(characteristics) == 1 {
w.WriteString(characteristics[0])
} else {
w.WriteString(strings.Join(characteristics[:len(characteristics)-1], ", "))
w.WriteString(" and ")
w.WriteString(characteristics[len(characteristics)-1])
}
w.WriteString(`. `)
}
writeRelationsProse(w, c, rdf.IRI("http://www.w3.org/2000/01/rdf-schema#domain"), "Having this property implies being ", ". ", true, "and", false, true)
writeRelationsProse(w, c, rdf.IRI("http://www.w3.org/2000/01/rdf-schema#range"), "Every value of this property is ", ". ", true, "and", false, true)
if c.Object(rdf.IRI("http://www.w3.org/2002/07/owl#inverseOf")) {
if c.Object(rdf.IRI("http://www.w3.org/2000/01/rdf-schema#subPropertyOf")) {
writeRelationsProse(w, c, rdf.IRI("http://www.w3.org/2000/01/rdf-schema#subPropertyOf"), "It is a sub-property of ", " and ", false, "and", false, false)
} else {
w.WriteString(`It is `)
}
writeRelationsProse(w, c, rdf.IRI("http://www.w3.org/2002/07/owl#inverseOf"), "the inverse of ", "", false, "and", false, false)
} else {
writeRelationsProse(w, c, rdf.IRI("http://www.w3.org/2000/01/rdf-schema#subPropertyOf"), "It is a sub-property of ", ". ", false, "and", false, false)
}
writeRelationsProse(w, c, rdf.IRI("http://www.w3.org/2002/07/owl#equivalentProperty"), "It is equivalent to ", ". ", false, "and", false, false)
}
func renderClassSemantics(w io.StringWriter, c *Context) {
type restriction struct {
Type string
Amount string
Term rdf.Term
}
restrictions := []restriction{}
for _, obj := range c.Objects(rdf.IRI("http://www.w3.org/2000/01/rdf-schema#subClassOf")) {
class := c.New(obj)
if class.Type(rdf.IRI("http://www.w3.org/2002/07/owl#Restriction")) {
if prop, exists := class.FirstIRI(rdf.IRI("http://www.w3.org/2002/07/owl#onProperty")); exists {
if value, exists := class.FirstLiteral(rdf.IRI("http://www.w3.org/2002/07/owl#cardinality")); exists {
restrictions = append(restrictions, restriction{"exactly", value.Value, prop})
}
if value, exists := class.FirstLiteral(rdf.IRI("http://www.w3.org/2002/07/owl#minCardinality")); exists {
restrictions = append(restrictions, restriction{"at least", value.Value, prop})
}
if value, exists := class.FirstLiteral(rdf.IRI("http://www.w3.org/2002/07/owl#maxCardinality")); exists {
restrictions = append(restrictions, restriction{"at most", value.Value, prop})
}
}
}
}
if len(restrictions) > 0 {
w.WriteString("Every member of this class has ")
for i, r := range restrictions {
if i > 0 {
if i == len(restrictions)-1 {
w.WriteString(" and ")
} else {
w.WriteString(", ")
}
}
w.WriteString(r.Type)
w.WriteString(" ")
w.WriteString(r.Amount)
w.WriteString(" ")
writeLinkedIRI(w, c.New(r.Term), "", false)
w.WriteString(" propert")
if n, err := strconv.Atoi(r.Amount); err == nil && n != 1 {
w.WriteString("ies")
} else {
w.WriteString("y")
}
}
w.WriteString(". ")
}
writeRelationsProse(w, c, rdf.IRI("http://www.w3.org/2000/01/rdf-schema#subClassOf"), "Being a member of this class implies also being a member of ", ". ", false, "and", false, true)
writeRelationsProse(w, c, rdf.IRI("http://www.w3.org/2002/07/owl#disjointWith"), "No member of this class can also be a member of ", ". ", false, "or", false, true)
writeRelationsProse(w, c, rdf.IRI("http://www.w3.org/2000/01/rdf-schema#domain"), "Having ", " implies being a member of this class. ", false, "or", true, true)
writeRelationsProse(w, c, rdf.IRI("http://www.w3.org/2000/01/rdf-schema#range"), "Things are a member of this class if they are the value of ", ". ", false, "or", true, true)
writeRelationsProse(w, c, rdf.IRI("http://www.w3.org/2002/07/owl#equivalentClass"), "It is equivalent to ", ". ", false, "and", false, true)
writeRelationsProse(w, c, rdf.IRI("http://www.w3.org/2002/07/owl#complementOf"), "It is the complement of ", ". ", false, "and", false, true)
}