@@ -29,13 +29,20 @@ function report (msg) {
29
29
++ errors ;
30
30
}
31
31
}
32
+ function jsonLdId ( t ) {
33
+ switch ( t . termType ) {
34
+ case "NamedNode" : return t . value ;
35
+ case "BlankNode" : return "_:" + t . value ;
36
+ default : throw Error ( `unknown termType in ${ JSON . stringify ( t ) } ` ) ;
37
+ }
38
+ }
32
39
33
40
var fs = require ( 'fs' ) ;
34
- var path = require ( "path" ) ;
41
+ var Path = require ( "path" ) ;
35
42
var N3 = require ( "n3" ) ;
36
- var parser = N3 . Parser ( { blankNodePrefix : "" } ) ;
43
+ var parser = new N3 . Parser ( { blankNodePrefix : "" } ) ;
37
44
var util = N3 . Util ;
38
- var store = N3 . Store ( ) ;
45
+ var store = new N3 . Store ( ) ;
39
46
//var json = fs.readFileSync(args[0]).toString();
40
47
41
48
var P = {
@@ -46,33 +53,36 @@ var P = {
46
53
"sx" : "https://shexspec.github.io/shexTest/ns#"
47
54
} ;
48
55
49
- var testDir = path . basename ( path . dirname ( path . resolve ( args [ 0 ] ) ) ) ;
56
+ var testDir = Path . basename ( Path . dirname ( Path . resolve ( args [ 0 ] ) ) ) ;
50
57
var basePath = "https://raw.githubusercontent.com/shexSpec/shexTest/master/" ;
51
58
var dirPath = basePath + testDir + '/' ;
59
+ function RelPath ( p ) {
60
+ return Path . relative ( dirPath , p ) ;
61
+ }
52
62
var apparentBase = dirPath + "manifest" ;
53
63
54
64
parser . parse (
55
65
"@base <" + apparentBase + "> .\n" +
56
66
fs . readFileSync ( args [ 0 ] , "utf8" ) ,
57
- function ( error , triple , prefixes ) {
67
+ function ( error , quad , prefixes ) {
58
68
if ( error ) {
59
69
error . message = "Error parsing " + args [ 0 ] + ": " + error . message ;
60
70
throw error ;
61
71
}
62
- if ( triple )
63
- store . addTriple ( triple )
72
+ if ( quad )
73
+ store . addQuad ( quad )
64
74
else
65
75
genText ( ) ;
66
76
} ) ;
67
77
68
78
/** expandCollection - N3.js utility to return an rdf collection's elements.
69
79
*/
70
80
function expandCollection ( h ) {
71
- if ( store . find ( h . object , " rdf: first", null ) . length ) {
81
+ if ( store . getQuads ( h . object , P . rdf + " first", null ) . length ) {
72
82
var ret = [ ] ;
73
- while ( h . object !== "http://www.w3.org/1999/02/22-rdf-syntax-ns#nil" ) {
74
- ret . push ( store . find ( h . object , " rdf: first", null ) [ 0 ] . object ) ;
75
- h = store . find ( h . object , " rdf: rest", null ) [ 0 ] ;
83
+ while ( h . object . value !== "http://www.w3.org/1999/02/22-rdf-syntax-ns#nil" ) {
84
+ ret . push ( store . getQuads ( h . object , P . rdf + " first", null ) [ 0 ] . object ) ;
85
+ h = store . getQuads ( h . object , P . rdf + " rest", null ) [ 0 ] ;
76
86
}
77
87
return ret ;
78
88
} else {
@@ -90,16 +100,14 @@ function genText () {
90
100
"@graph" : g
91
101
} ;
92
102
93
- store . addPrefixes ( P ) ;
94
-
95
- var manifest = store . find ( null , "rdf:type" , "mf:Manifest" ) [ 0 ] . subject ;
96
- var manifestComment = util . getLiteralValue ( store . find ( manifest , "rdfs:comment" , null ) [ 0 ] . object ) ;
103
+ var manifest = store . getQuads ( null , P . rdf + "type" , P . mf + "Manifest" ) [ 0 ] . subject ;
104
+ var manifestComment = store . getQuads ( manifest , P . rdfs + "comment" , null ) [ 0 ] . object . value ;
97
105
var entries = [ ] ;
98
106
var knownMissing = { } ; // record missing files.
99
- var head = store . find ( manifest , "mf: entries", null ) [ 0 ] . object ;
100
- while ( head !== P . rdf + "nil" ) {
101
- entries . push ( store . find ( head , " rdf: first", null ) [ 0 ] . object ) ;
102
- head = store . find ( head , " rdf: rest", null ) [ 0 ] . object ;
107
+ var head = store . getQuads ( manifest , P . mf + " entries", null ) [ 0 ] . object ;
108
+ while ( head . value !== P . rdf + "nil" ) {
109
+ entries . push ( store . getQuads ( head , P . rdf + " first", null ) [ 0 ] . object . value ) ;
110
+ head = store . getQuads ( head , P . rdf + " rest", null ) [ 0 ] . object ;
103
111
}
104
112
var unmatched = entries . reduce ( function ( ret , ent ) {
105
113
ret [ ent ] = true ;
@@ -113,15 +121,15 @@ function genText () {
113
121
"@id" : "" ,
114
122
"@type" : "mf:Manifest" ,
115
123
"rdfs:comment" : manifestComment ,
116
- "entries" : store . find ( null , " rdf: type", null ) . filter ( function ( t ) {
117
- var ret = expectedTypes . indexOf ( t . object ) !== - 1 ;
124
+ "entries" : store . getQuads ( null , P . rdf + " type", null ) . filter ( function ( t ) {
125
+ var ret = expectedTypes . indexOf ( t . object . value ) !== - 1 ;
118
126
if ( ret === false &&
119
- t . object !== P . mf + "Manifest" ) {
120
- report ( "test " + t . subject + " has unexpected type " + t . object ) ;
127
+ t . object . value !== P . mf + "Manifest" ) {
128
+ report ( "test " + t . subject . value + " has unexpected type " + t . object . value ) ;
121
129
}
122
130
return ret ;
123
131
} ) . map ( function ( t ) {
124
- return [ t . subject , t . object ] ;
132
+ return [ t . subject . value , t . object . value ] ;
125
133
} ) . filter ( function ( t ) {
126
134
var ret = entries . indexOf ( t [ 0 ] ) !== - 1 ;
127
135
if ( ret === false ) {
@@ -136,18 +144,18 @@ function genText () {
136
144
- 1 ;
137
145
} ) . map ( function ( st ) {
138
146
var s = st [ 0 ] , t = st [ 1 ] ;
139
- var testName = util . getLiteralValue ( store . find ( s , "mf: name", null ) [ 0 ] . object ) ;
140
- var testType = store . find ( s , " rdf: type", null ) [ 0 ] . object . replace ( P . sht , '' ) ;
147
+ var testName = store . getQuads ( s , P . mf + " name", null ) [ 0 ] . object . value ;
148
+ var testType = store . getQuads ( s , P . rdf + " type", null ) [ 0 ] . object . value . replace ( P . sht , '' ) ;
141
149
var expectedName = s . substr ( apparentBase . length + 1 ) ;
142
150
if ( WARN && testName !== expectedName ) {
143
151
report ( "expected label \"" + expectedName + "\" ; got \"" + testName + "\"" ) ;
144
152
}
145
- var actionTriples = store . find ( s , "mf: action", null ) ;
153
+ var actionTriples = store . getQuads ( s , P . mf + " action", null ) ;
146
154
function exists ( filename ) {
147
- var filepath = path . join ( __dirname , "../" + testDir + '/' + filename ) ;
155
+ var filepath = Path . join ( __dirname , "../" + testDir + '/' + filename ) ;
148
156
if ( WARN && ! fs . existsSync ( filepath ) && ! ( filepath in knownMissing ) ) {
149
- report ( "non-existent file: " + filepath . substr ( dirPath . length ) + " is missing " + path . relative ( process . cwd ( ) , filepath ) ) ;
150
- knownMissing [ filepath ] = path . relative ( process . cwd ( ) , filepath ) ;
157
+ report ( "non-existent file: " + RelPath ( filepath ) + " is missing " + Path . relative ( process . cwd ( ) , filepath ) ) ;
158
+ knownMissing [ filepath ] = Path . relative ( process . cwd ( ) , filepath ) ;
151
159
}
152
160
return filename ;
153
161
}
@@ -158,19 +166,23 @@ function genText () {
158
166
// Representation/Syntax/Structure tests
159
167
return [
160
168
// ["rdf" , "type" , function (v) { return v.substr(P.sht.length); }],
161
- [ s , "mf" , "name" , function ( v ) { return util . getLiteralValue ( v [ 0 ] ) ; } ] ,
169
+ [ s , "mf" , "name" , function ( v ) { return v [ 0 ] . value ; } ] ,
162
170
[ s , "sht" , "trait" , function ( v ) {
163
171
return v . map ( function ( x ) {
164
- return x . substr ( P . sht . length ) ; ;
165
- } ) ;
172
+ return x . value . substr ( P . sht . length ) ;
173
+ } ) . sort ( ) ;
166
174
} ] ,
167
- //[s, "rdfs" , "comment" , function (v) { return util.getLiteralValue(v[0]); }],
168
- [ s , "mf" , "status" , function ( v ) { return "mf:" + v [ 0 ] . substr ( P . mf . length ) ; } ] ,
169
- [ s , "sx" , "shex" , function ( v ) { return exists ( v [ 0 ] . substr ( dirPath . length ) ) ; } ] ,
170
- [ s , "sx" , "json" , function ( v ) { return exists ( v [ 0 ] . substr ( dirPath . length ) ) ; } ] ,
171
- [ s , "sx" , "ttl" , function ( v ) { return exists ( v [ 0 ] . substr ( dirPath . length ) ) ; } ] ,
175
+ //[s, "rdfs" , "comment" , function (v) { return (v[0].value; }],
176
+ [ s , "mf" , "status" , function ( v ) { return "mf:" + v [ 0 ] . value . substr ( P . mf . length ) ; } ] ,
177
+ [ s , "sx" , "shex" , function ( v ) { return exists ( RelPath ( v [ 0 ] . value ) ) ; } ] ,
178
+ [ s , "sx" , "json" , function ( v ) { return exists ( RelPath ( v [ 0 ] . value ) ) ; } ] ,
179
+ [ s , "sx" , "ttl" , function ( v ) { return exists ( RelPath ( v [ 0 ] . value ) ) ; } ] ,
180
+ [ s , "mf" , "startRow" , function ( v ) { return parseInt ( v [ 0 ] . value ) ; } ] ,
181
+ [ s , "mf" , "startColumn" , function ( v ) { return parseInt ( v [ 0 ] . value ) ; } ] ,
182
+ [ s , "mf" , "endRow" , function ( v ) { return parseInt ( v [ 0 ] . value ) ; } ] ,
183
+ [ s , "mf" , "endColumn" , function ( v ) { return parseInt ( v [ 0 ] . value ) ; } ] ,
172
184
] . reduce ( function ( ret , row ) {
173
- var found = store . findByIRI ( row [ 0 ] , P [ row [ 1 ] ] + row [ 2 ] , null ) . map ( expandCollection ) ;
185
+ var found = store . getQuads ( row [ 0 ] , P [ row [ 1 ] ] + row [ 2 ] , null ) . map ( expandCollection ) ;
174
186
var target = ret ;
175
187
if ( found . length )
176
188
target [ row [ 2 ] ] = row [ 3 ] ( found ) ;
@@ -180,44 +192,44 @@ function genText () {
180
192
var a = actionTriples [ 0 ] . object ;
181
193
return [
182
194
// ["rdf" , "type" , function (v) { return v.substr(P.sht.length); }],
183
- [ s , "mf" , "name" , function ( v ) { return util . getLiteralValue ( v [ 0 ] ) ; } ] ,
195
+ [ s , "mf" , "name" , function ( v ) { return v [ 0 ] . value ; } ] ,
184
196
[ s , "sht" , "trait" , function ( v ) {
185
197
return v . map ( function ( x ) {
186
- return x . substr ( P . sht . length ) ; ;
187
- } ) ;
198
+ return x . value . substr ( P . sht . length ) ; ;
199
+ } ) . sort ( ) ;
188
200
} ] ,
189
- [ s , "rdfs" , "comment" , function ( v ) { return util . getLiteralValue ( v [ 0 ] ) ; } ] ,
190
- [ s , "mf" , "status" , function ( v ) { return "mf:" + v [ 0 ] . substr ( P . mf . length ) ; } ] ,
191
- [ a , "sht" , "schema" , function ( v ) { return exists ( "../" + v [ 0 ] . substr ( basePath . length ) ) ; } ] , // could be more judicious in creating a relative path from an absolute path.
192
- [ a , "sht" , "shape" , function ( v ) { return v [ 0 ] . indexOf ( dirPath ) === 0 ? v [ 0 ] . substr ( dirPath . length ) : v [ 0 ] ; } ] ,
193
- [ a , "sht" , "data" , function ( v ) { return exists ( v [ 0 ] . substr ( dirPath . length ) ) ; } ] ,
194
- [ a , "sht" , "map" , function ( v ) { return exists ( v [ 0 ] . substr ( dirPath . length ) ) ; } ] ,
201
+ [ s , "rdfs" , "comment" , function ( v ) { return v [ 0 ] . value ; } ] ,
202
+ [ s , "mf" , "status" , function ( v ) { return "mf:" + v [ 0 ] . value . substr ( P . mf . length ) ; } ] ,
203
+ [ a , "sht" , "schema" , function ( v ) { return exists ( "../" + v [ 0 ] . value . substr ( basePath . length ) ) ; } ] , // could be more judicious in creating a relative path from an absolute path.
204
+ [ a , "sht" , "shape" , function ( v ) { return v [ 0 ] . value . indexOf ( dirPath ) === 0 ? RelPath ( v [ 0 ] . value ) : jsonLdId ( v [ 0 ] ) ; } ] ,
205
+ [ a , "sht" , "data" , function ( v ) { return exists ( RelPath ( v [ 0 ] . value ) ) ; } ] ,
206
+ [ a , "sht" , "map" , function ( v ) { return exists ( RelPath ( v [ 0 ] . value ) ) ; } ] ,
195
207
[ a , "sht" , "focus" , function ( v ) {
196
208
// Focus can be a literal
197
209
if ( util . isLiteral ( v [ 0 ] ) ) {
198
- var lang = util . getLiteralLanguage ( v [ 0 ] ) ;
199
- var dt = util . getLiteralType ( v [ 0 ] ) ;
200
- var res = { '@value' : util . getLiteralValue ( v [ 0 ] ) } ;
210
+ var lang = v [ 0 ] . language ;
211
+ var dt = v [ 0 ] . datatype . value ;
212
+ var res = { '@value' : v [ 0 ] . value } ;
201
213
if ( lang . length > 0 ) { res [ '@language' ] = lang }
202
214
if ( dt . length > 0 ) { res [ '@type' ] = dt }
203
215
return res ;
204
216
} else {
205
- return ( v [ 0 ] . indexOf ( dirPath ) === 0 ? v [ 0 ] . substr ( dirPath . length ) : v [ 0 ] ) ;
217
+ return ( v [ 0 ] . value . indexOf ( dirPath ) === 0 ? RelPath ( v [ 0 ] . value ) : jsonLdId ( v [ 0 ] ) ) ;
206
218
}
207
219
} ] ,
208
- [ a , "sht" , "semActs" , function ( v ) { return exists ( "../" + v [ 0 ] . substr ( basePath . length ) ) ; } ] , // could be more judicious in creating a relative path from an absolute path.
209
- [ a , "sht" , "shapeExterns" , function ( v ) { return exists ( "../" + v [ 0 ] . substr ( basePath . length ) ) ; } ] , // could be more judicious in creating a relative path from an absolute path.
210
- [ s , "mf" , "result" , function ( v ) { return exists ( v [ 0 ] . substr ( dirPath . length ) ) ; } ] ,
220
+ [ a , "sht" , "semActs" , function ( v ) { return exists ( "../" + v [ 0 ] . value . substr ( basePath . length ) ) ; } ] , // could be more judicious in creating a relative path from an absolute path.
221
+ [ a , "sht" , "shapeExterns" , function ( v ) { return exists ( "../" + v [ 0 ] . value . substr ( basePath . length ) ) ; } ] , // could be more judicious in creating a relative path from an absolute path.
222
+ [ s , "mf" , "result" , function ( v ) { return exists ( RelPath ( v [ 0 ] . value ) ) ; } ] ,
211
223
[ s , "mf" , "extensionResults" , function ( v ) {
212
224
return v [ 0 ] . map ( function ( x ) {
213
225
return {
214
- extension : store . find ( x , "mf: extension", null ) [ 0 ] . object ,
215
- prints : util . getLiteralValue ( store . find ( x , "mf: prints", null ) [ 0 ] . object )
226
+ extension : store . getQuads ( x , P . mf + " extension", null ) [ 0 ] . object . value ,
227
+ prints : store . getQuads ( x , P . mf + " prints", null ) [ 0 ] . object . value
216
228
} ;
217
229
} ) ;
218
230
} ]
219
231
] . reduce ( function ( ret , row ) {
220
- var found = store . findByIRI ( row [ 0 ] , P [ row [ 1 ] ] + row [ 2 ] , null ) . map ( expandCollection ) ;
232
+ var found = store . getQuads ( row [ 0 ] , P [ row [ 1 ] ] + row [ 2 ] , null ) . map ( expandCollection ) ;
221
233
var target = row [ 0 ] === s ? ret : row [ 0 ] === a ? ret . action : ret . extensionResults ;
222
234
if ( found . length )
223
235
target [ row [ 2 ] ] = row [ 3 ] ( found ) ;
@@ -231,9 +243,9 @@ function genText () {
231
243
}
232
244
if ( ! errors ) {
233
245
if ( OUTFILE ) {
234
- fs . writeFileSync ( OUTFILE , JSON . stringify ( ret , null , " " ) ) ;
246
+ fs . writeFileSync ( OUTFILE , JSON . stringify ( ret , null , " " ) + "\n" ) ;
235
247
} else {
236
- console . log ( JSON . stringify ( ret , null , " " ) ) ;
248
+ console . log ( JSON . stringify ( ret , null , " " ) + "\n" ) ;
237
249
}
238
250
process . exit ( 0 ) ;
239
251
} else {
0 commit comments