source: fmts/trunk/p/C0XGET1.m@ 1381

Last change on this file since 1381 was 1381, checked in by George Lilly, 12 years ago

added $$subject and $$object and LKY5 for randoms

File size: 13.2 KB
Line 
1C0XGET1 ; GPL - Fileman Triples entry point routine ;1/12/12 17:05
2 ;;0.1;C0X;nopatch;noreleasedate;Build 7
3 ;Copyright 2011 George Lilly. Licensed under the terms of the GNU
4 ;General Public License See attached copy of the License.
5 ;
6 ;This program is free software; you can redistribute it and/or modify
7 ;it under the terms of the GNU General Public License as published by
8 ;the Free Software Foundation; either version 2 of the License, or
9 ;(at your option) any later version.
10 ;
11 ;This program is distributed in the hope that it will be useful,
12 ;but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ;GNU General Public License for more details.
15 ;
16 ;You should have received a copy of the GNU General Public License along
17 ;with this program; if not, write to the Free Software Foundation, Inc.,
18 ;51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 ;
20 Q
21 ;
22LSSUBJ(RTN,ZSUBJ,C0XFARY) ; LIST NODES WITH SUBJECT ZSUBJ
23 ;
24 I '$D(C0XFARY) D INITFARY^C0XF2N("C0XFARY")
25 D USEFARY^C0XF2N("C0XFARY")
26 Q
27 ;
28GRAPHS(RTN,C0XFARY) ; LIST ALL GRAPHS
29 ;
30 I '$D(C0XFARY) D INITFARY^C0XF2N("C0XFARY")
31 D USEFARY^C0XF2N("C0XFARY")
32 N ZI S ZI=""
33 F S ZI=$O(@C0XTN@("G",ZI)) Q:ZI="" D ;
34 . S RTN(ZI,$$STR(ZI))=""
35 Q
36 ;
37STR(ZIN,C0XFARY) ; EXTRINSIC RETURNS A STRING
38 I '$D(C0XFARY) D INITFARY^C0XF2N("C0XFARY")
39 Q $$GET1^DIQ(C0XSFN,ZIN,.01,"E")
40 ;
41SPO(ZRTN,ZNODE,C0XFARY)
42 I '$D(C0XFARY) D INITFARY^C0XF2N("C0XFARY")
43 N ZI S ZI=$$NXT(.ZRTN)
44 S ZRTN(ZI,"S")=$$S(ZNODE)
45 S ZRTN(ZI,"P")=$$P(ZNODE)
46 S ZRTN(ZI,"O")=$$O(ZNODE)
47 Q
48 ;
49S(ZNODE,C0XFARY) ; EXTRINSIC RETURNING THE SUBJECT
50 Q $$STR($$GET1^DIQ(C0XTFN,ZNODE,.03,"I")) ;
51 ;
52P(ZNODE,C0XFARY) ; EXTRINSIC RETURNING THE PREDICATE
53 Q $$STR($$GET1^DIQ(C0XTFN,ZNODE,.04,"I")) ;
54 ;
55O(ZNODE,C0XFARY) ; EXTRINSIC RETURNING THE OBJECT
56 Q $$STR($$GET1^DIQ(C0XTFN,ZNODE,.05,"I")) ;
57 ;
58NXT(ZRTN) ;EXTRINSIC FOR THE NEXT NODE IN ARRAY ZRTN, PASSED BY REF
59 I '$D(ZRTN) S ZRTN=""
60 Q $O(ZRTN(""),-1)+1
61 ;
62SING(ZRTN,ZG) ; SUBJECTS IN GRAPH
63 ;
64 I '$D(C0XFARY) D INITFARY^C0XF2N("C0XFARY")
65 I '$D(ZRTN) S ZRTN=""
66 N ZI,ZN S ZI=""
67 F S ZI=$O(@C0XTN@("GSPO",ZG,ZI)) Q:ZI="" D ;
68 . S ZRTN($$NXT(.ZRTN),"S")=$$STR(ZI)
69 Q
70 ;
71qparse(qrtn,zquery) ; parses the query
72 ; want this to be able to handle the WHERE clause of SPARQL eventually
73 ;
74 n q1,q2,q3,qq
75 ;s qq=$tr(zquery," ","^")
76 s qq=query ; really want to remove whitespace here
77 s q1=$p(qq," ",1)
78 i q1["?" s q1=""
79 s q2=$p(qq," ",2)
80 i q2["?" s q2=""
81 s q3=$p(qq," ",3)
82 i q3["?" s q3=""
83 s qrtn(1)=q1_"^"_q2_"^"_q3 ; more lines to come later
84 q
85 ;
86getGraph(zrtn,zgrf,form) ; get all triples in graph zgrf
87 ; forms planned: "rdf" "json" "array" "turtle" "triples"
88 ; forms supported: "rdf" "json" "array"
89 I '$D(form) S form="rdf"
90 N ZIENS,ZTRIP
91 D TING^C0XF2N(.ZIENS,zgrf)
92 I '$D(ZIENS) Q ;
93 D ien2tary(.ZTRIP,"ZIENS")
94 I form="json" d jsonout(.zrtn,.ZTRIP) q ; what follows is else
95 i form="rdf" d rdfout^C0XRDF(.zrtn,.ZTRIP) q ;
96 i form="array" d arrayout^C0XGET1(.zrtn,.ZTRIP) q ;
97 W !,"Form not supported: ",form
98 Q
99 ;
100rpctrip(rtn,query,limit,offset) ; rpc to access triples with a query
101 ;
102 n zoff,zlim,zcount,zq
103 k rtn
104 i '$d(limit) s limit=250
105 i '$d(offset) s offset=0
106 d qparse(.zq,query) ; parse the query
107 n qsub,qpred,qobj,qtmp
108 W !,zq(1)
109 s qsub=$p(zq(1),"^",1)
110 s qpred=$p(zq(1),"^",2)
111 s qobj=$p(zq(1),"^",3)
112 d triples(.qtmp,qsub,qpred,qobj)
113 f zcount=offset+1:1:offset+limit q:'$d(qtmp(zcount)) d ;
114 . s rtn(zcount)=qtmp(zcount)
115 q
116 ;
117triples(triplertn,sub,pred,obj,graph,form,fary) ; returns triples
118 I '$D(fary) D ;
119 . D INITFARY^C0XF2N("C0XFARY")
120 . S fary="C0XFARY"
121 D USEFARY^C0XF2N(fary)
122 I '$D(form) S form="json"
123 k triplertn ; start with a clean return
124 n zsub,zpred,zobj,zgraph,tmprtn
125 s zsub=$$IENOF($$EXT^C0XUTIL($g(sub)),fary) ; ien of subject
126 s zpred=$$IENOF($$EXT^C0XUTIL($g(pred)),fary) ; ien of predicate
127 s zobj=$$IENOF($$EXT^C0XUTIL($g(obj)),fary) ; ien of object
128 s zgraph=$$IENOF($g(graph),fary) ; ien of graph
129 W !,"s:",zsub," p:",zpred," o:",zobj
130 d trip(.tmprtn,zsub,zpred,zobj,zgraph,fary)
131 d ien2tary(.zrary,"tmprtn") ; convert to triples
132 ;
133 i form="json" d jsonout(.triplertn,.zrary) q ; what follows is 'else'
134 i form="rdf" d rdfout^C0XRDF(.triplertn,.zrary) q ;
135 i form="array" d arrayout(.triplertn,.zrary) q ;
136 w !,"form not supported: ",form
137 q
138 ;
139subjects(listrtn,pred,obj,graph,form,fary) ; return list of subjects
140 d onelist("S",,$g(pred),$g(obj),$g(fary)) ;subjects
141 q
142 ;
143subject(pred,obj,graph,form,fary) ; extrinsic which returns the first
144 ; multiple of return from subjects - returns null if more than one
145 ; subjects(.G,sub,pred)
146 ; G("nodeID:1234") ==> "nodeID:1234"
147 n zin,zrtn
148 d subjects(.zin,$g(pred),$g(obj),$g(form),$g(fary))
149 s zrtn=$o(zin(""))
150 i $o(zin(zrtn))'="" s zrtn=""
151 q zrtn
152 ;
153preds(listrtn,sub,obj,graph,form,fary) ; return list of subjects
154 d onelist("P",$g(sub),,$g(obj),$g(fary)) ;subjects
155 q
156 ;
157objects(listrtn,sub,pred,graph,form,fary) ; return list of subjects
158 d onelist("O",$g(sub),$g(pred),"",$g(fary)) ;subjects
159 q
160 ;
161object(sub,pred,graph,form,fary) ; extrinsic which returns the first
162 ; multiple of return from objects - returns null if more than one
163 ; objects(.G,sub,pred)
164 ; G("location") ==> "location"
165 n zin,zrtn
166 d objects(.zin,$g(sub),$g(pred),$g(form),$g(fary))
167 s zrtn=$o(zin(""))
168 i $o(zin(zrtn))'="" s zrtn=""
169 q zrtn
170 ;
171onelist(zw,sub,pred,obj,fary) ; returns list
172 ; zw is S P or O depending on what should be returned
173 I $g(fary)="" D ;
174 . D INITFARY^C0XF2N("C0XFARY")
175 . S fary="C0XFARY"
176 D USEFARY^C0XF2N(fary)
177 I '$D(form) S form="json"
178 k listrtn ; start with a clean return
179 n zsub,zpred,zobj,zgraph,tmprtn
180 s zsub=$$IENOF($$EXT^C0XUTIL($g(sub)),fary) ; ien of sub
181 s zpred=$$IENOF($$EXT^C0XUTIL($g(pred)),fary) ; ien of pred
182 s zobj=$$IENOF($$EXT^C0XUTIL($g(obj)),fary) ; ien of obj
183 s zgraph=$$IENOF($g(graph),fary) ; ien of graph
184 W !,"s:",zsub," p:",zpred," o:",zobj
185 n c0xflag,zi,zx,zt
186 s zt=$na(^C0X(101)) ;
187 s c0xflag=$$mask(zsub,zpred,zobj) ; get mask flags
188 k tmprtn
189 n itbl,ii,ix
190 s ii=$s(zw="S":"SPO",zw="P":"POS",zw="O":"OSP") ; no constraint
191 s itbl("I000",ii)="d zip(.tmprtn,zt,zi)"
192 s ii=$s(zw="S":"OSP",zw="P":"OPS",zw="O":"OSP") ; obj constraint
193 s ix=$s(zw="O":"d just(zobj)",1:"d zip1(.tmprtn,zt,zi,zobj)")
194 s itbl("I001",ii)=ix
195 s ii=$s(zw="S":"PSO",zw="P":"POS",zw="O":"POS") ; pred constraint
196 s ix=$s(zw="P":"d just(zpred)",1:"d zip1(.tmprtn,zt,zi,zpred)")
197 s itbl("I010",ii)=ix
198 s ii=$s(zw="S":"POS",zw="P":"OPS",zw="O":"OSP") ; pred + obj constraint
199 s ix=$s(zw="S":"d zip2(.tmprtn,zt,zi,zpred,zobj)",zw="P":"d just(zpred)",zw="O":"d just(zobj)",1:"d just(zobj)")
200 s itbl("I011",ii)=ix
201 s ii=$s(zw="S":"SPO",zw="P":"SPO",zw="O":"SOP") ; sub constraint
202 s ix=$s(zw="S":"d just(zsub)",1:"d zip1(.tmprtn,zt,zi,zsub)")
203 s itbl("I100",ii)=ix
204 s ii=$s(zw="S":"SPO",zw="P":"SOP",zw="O":"OSP") ; sub + obj constraint
205 s ix=$s(zw="P":"d zip2(.tmprtn,zt,zi,zsub,zobj)",zw="S":"d just(zsub)",zw="O":"d just(zobj)",1:"d just(zobj)")
206 s itbl("I101",ii)=ix
207 s ii=$s(zw="S":"SPO",zw="P":"POS",zw="O":"SPO") ; sub + pred constraint
208 s ix=$s(zw="O":"d zip2(.tmprtn,zt,zi,zsub,zpred)",zw="S":"d just(zsub)",zw="P":"d just(zpred)",1:"d just(zsub)")
209 s itbl("I110",ii)=ix
210 s ii=$s(zw="S":"SPO",zw="P":"POS",zw="O":"OSP") ; sub + pred + obj constraint
211 s ix=$s(zw="O":"d just(zobj)",zw="S":"d just(zsub)",zw="P":"d just(zpred)",1:"d just(zsub)")
212 s itbl("I111",ii)=ix
213 ; end itbl definition
214 ;
215 s zi=$o(itbl(c0xflag,"")) ; find index to use
216 s zx=itbl(c0xflag,zi) ; executable instruction to run
217 ;i $g(ngraph)'="" s zi="G"_zi ; this is wrong.. don't do graphs yet
218 w !,c0xflag," ",zw," ",zt," ",zi," ",zx,!
219 ;zwr itbl
220 x zx
221 k listrtn
222 d strings(.listrtn,"tmprtn") ; convert pointer to strings
223 q
224 ;
225just(zin) ; add one element to tmprtn
226 s tmprtn(zin)=""
227 q
228 ;
229zip(zrtn,zt,zi) ; pull out just the first element of the index
230 ;
231 n zii s zii=""
232 f s zii=$o(@zt@(zi,zii)) q:zii="" d ;
233 . s zrtn(zii)=""
234 q
235 ;
236zip1(zrtn,zt,zi,zn) ; pull out just the first element of the index
237 ;
238 n zii s zii=""
239 f s zii=$o(@zt@(zi,zn,zii)) q:zii="" d ;
240 . s zrtn(zii)=""
241 q
242 ;
243zip2(zrtn,zt,zi,zn,zn1) ; pull out just the first element of the index
244 ;
245 n zii s zii=""
246 f s zii=$o(@zt@(zi,zn,zn1,zii)) q:zii="" d ;
247 . s zrtn(zii)=""
248 q
249 ;
250arrayout(rtn,zary) ; output an array of triples
251 ;
252 s zrsub=""
253 s zcnt=1
254 f s zrsub=$o(zary(zrsub)) q:zrsub="" d ; organized by subject
255 . s zzz=""
256 . f s zzz=$o(zary(zrsub,zzz)) q:zzz="" d ; pred and obj
257 . . s rtn(zcnt)=zrsub_"^"_zzz
258 . . s zcnt=zcnt+1
259 q
260 ;
261strings(zrary,zinary) ; convert pointers to strings
262 ;
263 k zrary
264 n zzz s zzz=""
265 f s zzz=$o(@zinary@(zzz)) q:zzz="" d ;
266 . n zs
267 . s zs=$$GET1^DIQ(C0XSFN,zzz_",",.01)
268 . q:zs=""
269 . s zrary(zs)=""
270 q
271 ;
272ien2tary(zrary,zinary) ; zinary is an array of iens passed by name
273 ; zrary is passed by reference and is return array of triples
274 ; format zrary(zsub,"zpred^zobj")=""
275 ;
276 k zrary ; start out clean
277 n zzz,zrsub,zrpred,zrobj,zgraph,zcnt
278 s zzz=""
279 f s zzz=$o(@zinary@(zzz)) q:zzz="" d ;
280 . s zrsub=$$GET1^DIQ(C0XTFN,zzz_",",.03,"E")
281 . s zrpred=$$GET1^DIQ(C0XTFN,zzz_",",.04,"E")
282 . s zrobj=$$GET1^DIQ(C0XTFN,zzz_",",.05,"E")
283 . s zrgraph=$$GET1^DIQ(C0XTFN,zzz_",",.02,"E")
284 . s zrary(zrsub,zrpred_"^"_zrobj)=""
285 q
286 ;
287jsonout(jout,zary) ;
288 d REPLYSTART^FMQLJSON("jout")
289 d LISTSTART^FMQLJSON("jout","results")
290 n zi s zi=""
291 f s zi=$o(zary(zi)) q:zi="" d ; for each subject
292 . n zii s zii=""
293 . D DICTSTART^FMQLJSON("jout",zi)
294 . f s zii=$o(zary(zi,zii)) q:zii="" d ; for each pred^obj pair
295 . . d DASSERT^FMQLJSON("jout",$p(zii,"^",1),$p(zii,"^",2))
296 . D DICTEND^FMQLJSON("jout")
297 d LISTEND^FMQLJSON("jout")
298 d REPLYEND^FMQLJSON("jout")
299 q
300 ;
301mask(zsub,zpred,zobj) ; function to return mask information
302 ; about the inputs ie I100 for just a subject and no pred or obj
303 n zf1,zf2,zf3,zflag
304 s zf1=$s($g(zsub)="":0,1:1)
305 s zf2=$s($g(zpred)="":0,1:1)
306 s zf3=$s($g(zobj)="":0,1:1)
307 s zflag="I"_zf1_zf2_zf3
308 q zflag
309 ;
310trip(triprtn,nsub,npred,nobj,ngraph,fary) ; returns triples iens
311 ; nsub,npred,nobj are all optional
312 ; graf is also optional, and will limit the search to a particular ngraph
313 ; fary is which triple store (not implemented yet)
314 n c0xflag,zi,zx,zt
315 s zt=$na(^C0X(101)) ;
316 s c0xflag=$$mask(nsub,npred,nobj) ; get mask flags
317 n itbl
318 s itbl("I000","SPO")="d do3(.triprtn,zt,zi)"
319 s itbl("I001","OSP")="d do2(.triprtn,zt,zi,nobj)"
320 s itbl("I010","PSO")="d do2(.triprtn,zt,zi,npred)"
321 s itbl("I011","POS")="d do1(.triprtn,zt,zi,npred,nobj)"
322 s itbl("I100","SPO")="d do2(.triprtn,zt,zi,nsub)"
323 s itbl("I101","SOP")="d do1(.triprtn,zt,zi,nsub,nobj)"
324 s itbl("I110","SPO")="d do1(.triprtn,zt,zi,nsub,npred)"
325 s itbl("I111","SPO")="d do0(.triprtn,zt,zi,nsub,npred,nobj)"
326 s zi=$o(itbl(c0xflag,""))
327 s zx=itbl(c0xflag,zi) ; executable instruction to run
328 i $g(ngraph)'="" s zi="G"_zi
329 w !,zx
330 x zx
331 q
332 ;
333do0(dortn,zt,zi,z1,z2,z3)
334 ; looking for only one triple
335 n zz
336 s zz=$o(@zt@(zi,z1,z2,z3,""))
337 i zz'="" s dortn(zz)=""
338 q
339 ;
340do1(dortn,zt,zi,z1,z2) ; have 2, looking for one
341 n zr,zx1
342 s zx1=""
343 f s zx1=$o(@zt@(zi,z1,z2,zx1)) q:zx1="" d ;
344 . s zr=$o(@zt@(zi,z1,z2,zx1,""))
345 . s dortn(zr)=""
346 q
347 ;
348do2(dortn,zt,zi,z1) ; have one, looking for 2
349 n zr,zx1,zx2
350 s (zx1,zx2)=""
351 f s zx1=$o(@zt@(zi,z1,zx1)) q:zx1="" d ;
352 . f s zx2=$o(@zt@(zi,z1,zx1,zx2)) q:zx2="" d ;
353 . . s zr=$o(@zt@(zi,z1,zx1,zx2,""))
354 . . s dortn(zr)=""
355 q
356 ;
357do3(dortn,zt,zi) ; have none, looking for three
358 n zr,zx1,zx2,zx3
359 s (zx1,zx2,zx3)=""
360 f s zx1=$o(@zt@(zi,zx1)) q:zx1="" d ;
361 . f s zx2=$o(@zt@(zi,zx1,zx2)) q:zx2="" d ;
362 . . f s zx3=$o(@zt@(zi,zx1,zx2,zx3)) q:zx3="" d ;
363 . . . s zr=$o(@zt@(zi,zx1,zx2,zx3,""))
364 . . . s dortn(zr)=""
365 q
366 ;
367IENOF(ZSTRING,FARY) ; EXTRINSIC WHICH RETURNS THE IEN OF ZS IN THE STRINGS FILE
368 I '$D(FARY) D ;
369 . D INITFARY^C0XF2N("C0XFARY")
370 . S FARY="C0XFARY"
371 N ZIEN
372 I $G(ZSTRING)="" Q "" ; NO STRING
373 S ZIEN=$O(@C0XSN@("B",ZSTRING,""))
374 Q ZIEN
375 ;
376IENOFA(ZOUTARY,INARY,FARY) ; RESOLVE STRINGS TO IEN IN STRINGS FILE
377 ; RETURNS IN ZOUTARY OF THE FORM ZOUTARY("IEN","VAR",IEN)=""
378 I '$D(FARY) D ;
379 . D INITFARY^C0XF2N("C0XFARY")
380 . S FARY="C0XFARY"
381 K ZOUTARY ; START WITH CLEAN RESULTS
382 K C0XFDA2 ; USE A SEPARATE FDA FOR THIS
383 I '$D(C0XVOC) D VOCINIT^C0XUTIL
384 N ZINARY
385 N ZI S ZI=""
386 F S ZI=$O(INARY(ZI)) Q:ZI="" D ;
387 . N ZK
388 . S ZK=$O(INARY(ZI,""))
389 . S ZINARY($$EXT^C0XUTIL(ZI),$$EXT^C0XUTIL(ZK))=""
390 N ZV,ZIEN
391 N ZCNT S ZCNT=0
392 F S ZI=$O(ZINARY(ZI)) Q:ZI="" D ; LOOK FOR MISSING STRINGS
393 . S ZV=$O(ZINARY(ZI,""))
394 . I ZV["^" S ZV=$TR(ZV,"^","|")
395 F S ZI=$O(ZINARY(ZI)) Q:ZI="" D ; NOW GET ALL IENS
396 . S ZV=$O(ZINARY(ZI,""))
397 . I ZV["^" S ZV=$TR(ZV,"^","|")
398 . S ZIEN=$O(@C0XSN@("B",ZV,"")) ; THEY SHOULD BE THERE NOW
399 . I ZIEN="" S ZOUTARY("IEN",ZI)=""
400 . E S ZOUTARY("IEN",ZI,ZIEN)=""
401 Q
402 ;
403output(zwhat,zfname,zdir) ; function to write an array to a host file
404 ; if zdir is ommitted, will output to the CCR directory
405 ; ^TMP("C0CCCR","ODIR")
406 ; if fname is ommitted, will output yyyy-mm-dd-hh-mm-ss-C0XOUT.out
407 ; zwhat is passed by name
408 ;
409 i '$d(zdir) s zdir=$G(^TMP("C0CCCR","ODIR"))
410 i '$d(zfname) d ;
411 . s zfname=$$FMTE^XLFDT($$NOW^XLFDT,7)
412 . s zfname=$tr(zfname,"/","-")
413 . s zfname=$tr(zfname,"@","-")
414 . s zfname=$tr(zfname,":","-")
415 . s zfname=zfname_".out"
416 i $e(zwhat,1,1)'="^" d ; not a global
417 . k ^TMP("C0XOUT",$J)
418 . m ^TMP("C0XOUT",$J)=@zwhat
419 . s zwhat=$na(^TMP("C0XOUT",$J,1))
420 n zout s zout=""
421 s zout=$$OUTPUT^C0CXPATH(zwhat,zfname,zdir)
422 K ^TMP("C0XOUT",$J)
423 Q zout
424 ;
Note: See TracBrowser for help on using the repository browser.