| 1 | C0XGET1 ; 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 | ;
|
|---|
| 22 | LSSUBJ(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 | ;
|
|---|
| 28 | GRAPHS(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 | ;
|
|---|
| 37 | STR(ZIN,C0XFARY) ; EXTRINSIC RETURNS A STRING
|
|---|
| 38 | I '$D(C0XFARY) D INITFARY^C0XF2N("C0XFARY")
|
|---|
| 39 | Q $$GET1^DIQ(C0XSFN,ZIN,.01,"E")
|
|---|
| 40 | ;
|
|---|
| 41 | SPO(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 | ;
|
|---|
| 49 | S(ZNODE,C0XFARY) ; EXTRINSIC RETURNING THE SUBJECT
|
|---|
| 50 | Q $$STR($$GET1^DIQ(C0XTFN,ZNODE,.03,"I")) ;
|
|---|
| 51 | ;
|
|---|
| 52 | P(ZNODE,C0XFARY) ; EXTRINSIC RETURNING THE PREDICATE
|
|---|
| 53 | Q $$STR($$GET1^DIQ(C0XTFN,ZNODE,.04,"I")) ;
|
|---|
| 54 | ;
|
|---|
| 55 | O(ZNODE,C0XFARY) ; EXTRINSIC RETURNING THE OBJECT
|
|---|
| 56 | Q $$STR($$GET1^DIQ(C0XTFN,ZNODE,.05,"I")) ;
|
|---|
| 57 | ;
|
|---|
| 58 | NXT(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 | ;
|
|---|
| 62 | SING(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 | ;
|
|---|
| 71 | qparse(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 | ;
|
|---|
| 86 | getGraph(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 | ;
|
|---|
| 100 | rpctrip(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 | ;
|
|---|
| 117 | triples(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^C0XF2N($$EXT^C0XUTIL($g(sub)),fary) ; ien of subject
|
|---|
| 126 | s zpred=$$IENOF^C0XF2N($$EXT^C0XUTIL($g(pred)),fary) ; ien of predicate
|
|---|
| 127 | s zobj=$$IENOF^C0XF2N($$EXT^C0XUTIL($g(obj)),fary) ; ien of object
|
|---|
| 128 | s zgraph=$$IENOF^C0XF2N($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 | ;
|
|---|
| 139 | arrayout(rtn,zary) ; output an array of triples
|
|---|
| 140 | ;
|
|---|
| 141 | s zrsub=""
|
|---|
| 142 | s zcnt=1
|
|---|
| 143 | f s zrsub=$o(zary(zrsub)) q:zrsub="" d ; organized by subject
|
|---|
| 144 | . s zzz=""
|
|---|
| 145 | . f s zzz=$o(zary(zrsub,zzz)) q:zzz="" d ; pred and obj
|
|---|
| 146 | . . s rtn(zcnt)=zrsub_"^"_zzz
|
|---|
| 147 | . . s zcnt=zcnt+1
|
|---|
| 148 | q
|
|---|
| 149 | ;
|
|---|
| 150 | ien2tary(zrary,zinary) ; zinary is an array of iens passed by name
|
|---|
| 151 | ; zrary is passed by reference and is return array of triples
|
|---|
| 152 | ; format zrary(zsub,"zpred^zobj")=""
|
|---|
| 153 | ;
|
|---|
| 154 | k zrary ; start out clean
|
|---|
| 155 | n zzz,zrsub,zrpred,zrobj,zgraph,zcnt
|
|---|
| 156 | s zzz=""
|
|---|
| 157 | f s zzz=$o(@zinary@(zzz)) q:zzz="" d ;
|
|---|
| 158 | . s zrsub=$$GET1^DIQ(C0XTFN,zzz_",",.03,"E")
|
|---|
| 159 | . s zrpred=$$GET1^DIQ(C0XTFN,zzz_",",.04,"E")
|
|---|
| 160 | . s zrobj=$$GET1^DIQ(C0XTFN,zzz_",",.05,"E")
|
|---|
| 161 | . s zrgraph=$$GET1^DIQ(C0XTFN,zzz_",",.02,"E")
|
|---|
| 162 | . s zrary(zrsub,zrpred_"^"_zrobj)=""
|
|---|
| 163 | q
|
|---|
| 164 | ;
|
|---|
| 165 | jsonout(jout,zary) ;
|
|---|
| 166 | d REPLYSTART^FMQLJSON("jout")
|
|---|
| 167 | d LISTSTART^FMQLJSON("jout","results")
|
|---|
| 168 | n zi s zi=""
|
|---|
| 169 | f s zi=$o(zary(zi)) q:zi="" d ; for each subject
|
|---|
| 170 | . n zii s zii=""
|
|---|
| 171 | . D DICTSTART^FMQLJSON("jout",zi)
|
|---|
| 172 | . f s zii=$o(zary(zi,zii)) q:zii="" d ; for each pred^obj pair
|
|---|
| 173 | . . d DASSERT^FMQLJSON("jout",$p(zii,"^",1),$p(zii,"^",2))
|
|---|
| 174 | . D DICTEND^FMQLJSON("jout")
|
|---|
| 175 | d LISTEND^FMQLJSON("jout")
|
|---|
| 176 | d REPLYEND^FMQLJSON("jout")
|
|---|
| 177 | q
|
|---|
| 178 | ;
|
|---|
| 179 | trip(triprtn,nsub,npred,nobj,ngraph,fary) ; returns triples iens
|
|---|
| 180 | ; nsub,npred,nobj are all optional
|
|---|
| 181 | ; graf is also optional, and will limit the search to a particular ngraph
|
|---|
| 182 | ; fary is which triple store (not implemented yet)
|
|---|
| 183 | n c0xflag,zi,zx,zt
|
|---|
| 184 | s zt=$na(^C0X(101)) ;
|
|---|
| 185 | n zf1,zf2,zf3
|
|---|
| 186 | s zf1=$s($g(nsub)="":0,1:1)
|
|---|
| 187 | s zf2=$s($g(npred)="":0,1:1)
|
|---|
| 188 | s zf3=$s($g(nobj)="":0,1:1)
|
|---|
| 189 | s c0xflag="I"_zf1_zf2_zf3
|
|---|
| 190 | n itbl
|
|---|
| 191 | s itbl("I000","SPO")="d do3(.triprtn,zt,zi)"
|
|---|
| 192 | s itbl("I001","OSP")="d do2(.triprtn,zt,zi,nobj)"
|
|---|
| 193 | s itbl("I010","PSO")="d do2(.triprtn,zt,zi,npred)"
|
|---|
| 194 | s itbl("I011","POS")="d do1(.triprtn,zt,zi,npred,nobj)"
|
|---|
| 195 | s itbl("I100","SPO")="d do2(.triprtn,zt,zi,nsub)"
|
|---|
| 196 | s itbl("I101","SOP")="d do1(.triprtn,zt,zi,nsub,nobj)"
|
|---|
| 197 | s itbl("I110","SPO")="d do1(.triprtn,zt,zi,nsub,npred)"
|
|---|
| 198 | s itbl("I111","SPO")="d do0(.triprtn,zt,zi,nsub,npred,nobj)"
|
|---|
| 199 | s zi=$o(itbl(c0xflag,""))
|
|---|
| 200 | s zx=itbl(c0xflag,zi) ; executable instruction to run
|
|---|
| 201 | i $g(ngraph)'="" s zi="G"_zi
|
|---|
| 202 | w !,zx
|
|---|
| 203 | x zx
|
|---|
| 204 | q
|
|---|
| 205 | ;
|
|---|
| 206 | do0(dortn,zt,zi,z1,z2,z3)
|
|---|
| 207 | ; looking for only one triple
|
|---|
| 208 | n zz
|
|---|
| 209 | s zz=$o(@zt@(zi,z1,z2,z3,""))
|
|---|
| 210 | i zz'="" s dortn(zz)=""
|
|---|
| 211 | q
|
|---|
| 212 | ;
|
|---|
| 213 | do1(dortn,zt,zi,z1,z2) ; have 2, looking for one
|
|---|
| 214 | n zr,zx1
|
|---|
| 215 | s zx1=""
|
|---|
| 216 | f s zx1=$o(@zt@(zi,z1,z2,zx1)) q:zx1="" d ;
|
|---|
| 217 | . s zr=$o(@zt@(zi,z1,z2,zx1,""))
|
|---|
| 218 | . s dortn(zr)=""
|
|---|
| 219 | q
|
|---|
| 220 | ;
|
|---|
| 221 | do2(dortn,zt,zi,z1) ; have one, looking for 2
|
|---|
| 222 | n zr,zx1,zx2
|
|---|
| 223 | s (zx1,zx2)=""
|
|---|
| 224 | f s zx1=$o(@zt@(zi,z1,zx1)) q:zx1="" d ;
|
|---|
| 225 | . f s zx2=$o(@zt@(zi,z1,zx1,zx2)) q:zx2="" d ;
|
|---|
| 226 | . . s zr=$o(@zt@(zi,z1,zx1,zx2,""))
|
|---|
| 227 | . . s dortn(zr)=""
|
|---|
| 228 | q
|
|---|
| 229 | ;
|
|---|
| 230 | do3(dortn,zt,zi) ; have none, looking for three
|
|---|
| 231 | n zr,zx1,zx2,zx3
|
|---|
| 232 | s (zx1,zx2,zx3)=""
|
|---|
| 233 | f s zx1=$o(@zt@(zi,zx1)) q:zx1="" d ;
|
|---|
| 234 | . f s zx2=$o(@zt@(zi,zx1,zx2)) q:zx2="" d ;
|
|---|
| 235 | . . f s zx3=$o(@zt@(zi,zx1,zx2,zx3)) q:zx3="" d ;
|
|---|
| 236 | . . . s zr=$o(@zt@(zi,zx1,zx2,zx3,""))
|
|---|
| 237 | . . . s dortn(zr)=""
|
|---|
| 238 | q
|
|---|
| 239 | ;
|
|---|
| 240 | output(zwhat,zfname,zdir) ; function to write an array to a host file
|
|---|
| 241 | ; if zdir is ommitted, will output to the CCR directory
|
|---|
| 242 | ; ^TMP("C0CCCR","ODIR")
|
|---|
| 243 | ; if fname is ommitted, will output yyyy-mm-dd-hh-mm-ss-C0XOUT.out
|
|---|
| 244 | ; zwhat is passed by name
|
|---|
| 245 | ;
|
|---|
| 246 | i '$d(zdir) s zdir=$G(^TMP("C0CCCR","ODIR"))
|
|---|
| 247 | i '$d(zfname) d ;
|
|---|
| 248 | . s zfname=$$FMTE^XLFDT($$NOW^XLFDT,7)
|
|---|
| 249 | . s zfname=$tr(zfname,"/","-")
|
|---|
| 250 | . s zfname=$tr(zfname,"@","-")
|
|---|
| 251 | . s zfname=$tr(zfname,":","-")
|
|---|
| 252 | . s zfname=zfname_".out"
|
|---|
| 253 | i $e(zwhat,1,1)'="^" d ; not a global
|
|---|
| 254 | . k ^TMP("C0XOUT",$J)
|
|---|
| 255 | . m ^TMP("C0XOUT",$J)=@zwhat
|
|---|
| 256 | . s zwhat=$na(^TMP("C0XOUT",$J,1))
|
|---|
| 257 | n zout s zout=""
|
|---|
| 258 | s zout=$$OUTPUT^C0CXPATH(zwhat,zfname,zdir)
|
|---|
| 259 | K ^TMP("C0XOUT",$J)
|
|---|
| 260 | Q zout
|
|---|
| 261 | ; |
|---|