@@ -188,6 +188,47 @@ WOQLQuery.prototype._nuke_schema_element = function(elvar, graph) {
188
188
/**
189
189
* Experimental / Unstable API
190
190
*/
191
+ WOQLQuery . prototype . genEnum = function ( prop , cls , clslabel , clsdesc , graph ) {
192
+ clslabel = clslabel || cls
193
+ graph = this . _sg ( graph )
194
+
195
+ let add_class = new WOQLQuery ( )
196
+ . add_quad ( cls , "type" , "owl:Class" , graph )
197
+ . add_quad ( cls , "label" , clslabel , graph )
198
+ if ( clsdesc ) {
199
+ add_class . add_quad ( cls , "comment" , clsdesc , graph )
200
+ }
201
+
202
+ this . and (
203
+ new WOQLQuery ( ) . select ( "v:PV" ) . distinct ( "v:PV" ) . triple ( "v:A" , prop , "v:PV" ) ,
204
+ new WOQLQuery ( ) . idgen ( cls , [ "v:PV" ] , "v:ChoiceID" ) ,
205
+ add_class ,
206
+ new WOQLQuery ( ) . add_quad ( "v:ChoiceID" , "type" , cls , graph )
207
+ . add_quad ( "v:ChoiceID" , "label" , "v:PV" , graph )
208
+ )
209
+ return this
210
+ }
211
+
212
+ WOQLQuery . prototype . finishEnum = function ( client , cls , graph ) {
213
+ graph = this . _sg ( graph )
214
+ let q = new WOQLQuery ( ) . quad ( "v:ChoiceID" , "type" , cls , graph )
215
+ return client . query ( q ) . then ( ( results ) => {
216
+ let ids = [ ]
217
+ for ( var i = 0 ; i < results . bindings . length ; i ++ ) {
218
+ ids . push ( results . bindings [ i ] [ "ChoiceID" ] )
219
+ }
220
+ return client . query ( this . _oneOfList ( cls , ids , graph ) )
221
+ } )
222
+ }
223
+
224
+ WOQLQuery . prototype . makeEnum = function ( client , prop , cls , clslabel , clsdesc , graph ) {
225
+ client . query ( new WOQLQuery ( ) . genEnum ( prop , cls , clslabel , clsdesc , graph ) )
226
+ . then ( ( ) => new WOQLQuery ( ) . finishEnum ( client , cls , graph ) )
227
+ graph = this . _sg ( graph )
228
+ return new WOQLQuery ( ) . quad ( "v:Choice" , "type" , cls , graph )
229
+ }
230
+
231
+ //makeEnum(client(), "", "scm:DealType", "Deal Type", "The stage of the deal")
191
232
192
233
/**
193
234
* Generates a class representing a choice list - an enumerated list of specific options
@@ -235,6 +276,24 @@ WOQLQuery.prototype.generateChoiceList = function(cls, clslabel, clsdesc, choice
235
276
return this . and ( ...confs , oneof )
236
277
}
237
278
279
+ WOQLQuery . prototype . _oneOfList = function ( cls , ids , graph ) {
280
+ var listid = '_:' + ( cls . indexOf ( ':' ) == - 1 ? cls : cls . split ( ':' ) [ 1 ] )
281
+ var lastid = listid
282
+ graph = this . _sg ( graph )
283
+ let clist = [ ]
284
+ for ( var i = 0 ; i < ids . length ; i ++ ) {
285
+ var nextid = i < ids . length - 1 ? listid + '_' + i : 'rdf:nil'
286
+ clist . push ( new WOQLQuery ( ) . add_quad ( lastid , 'rdf:first' , new WOQLQuery ( ) . iri ( ids [ i ] ) , graph ) )
287
+ clist . push ( new WOQLQuery ( ) . add_quad ( lastid , 'rdf:rest' , new WOQLQuery ( ) . iri ( nextid ) , graph ) )
288
+ lastid = nextid
289
+ }
290
+ return this . and (
291
+ new WOQLQuery ( ) . add_quad ( cls , 'owl:oneOf' , new WOQLQuery ( ) . iri ( listid ) , graph ) ,
292
+ ...clist ,
293
+ )
294
+ }
295
+
296
+
238
297
WOQLQuery . prototype . deleteChoiceList = function ( classId , graph ) {
239
298
graph = this . _sg ( graph )
240
299
return new WOQLQuery ( ) . and (
0 commit comments