source: fmts/trunk/p/C0XF2N.m@ 1356

Last change on this file since 1356 was 1354, checked in by George Lilly, 13 years ago

experimenting with json out

File size: 18.6 KB
Line 
1C0XF2N ; GPL - Fileman Triples entry point routine ;10/13/11 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 ; This is based on C0XMAIN but experiments with a fast load for triples
23 ; that will write directly to the fileman global
24 ; The file 172.101 is a F2N design style for triples, which means
25 ; that it is a Flat file with no subfiles, all fields at the root
26 ; ... it is a "2" file solution which means all strings are stored in
27 ; ... strings file and pointed to by the triples file
28 ; ... it is an N file because it has generated Node IDs instead of
29 ; ... DINUM which would use the IEN for the Node ID.
30 ; gpl 11/04/2011
31 ;
32INITFARY(ZFARY) ; INITIALIZE FILE NUMBERS AND OTHER USEFUL THINGS
33 ; FOR THE DEFAULT TRIPLE STORE. USE OTHER VALUES FOR SUPPORTING ADDITIONAL
34 ; TRIPLE STORES
35 I $D(@ZFARY) Q ; ALREADY INITIALIZED
36 S @ZFARY@("C0XTFN")=172.101 ; TRIPLES FILE NUMBER
37 S @ZFARY@("C0XSFN")=172.201 ; TRIPLES STRINGS FILE NUMBER
38 S @ZFARY@("C0XTN")=$NA(^C0X(101)) ; TRIPLES GLOBAL NAME
39 S @ZFARY@("C0XSN")=$NA(^C0X(201)) ; STRING FILE GLOBAL NAME
40 S @ZFARY@("C0XDIR")="/home/glilly/snomed/"
41 S @ZFARY@("BLKLOAD")=1 ; this file supports block load
42 S @ZFARY@("FMTSSTYLE")="F2N" ; fileman style
43 S @ZFARY@("REPLYFMT")="JSON"
44 D USEFARY(ZFARY)
45 Q
46 ;
47USEFARY(ZFARY) ; INITIALIZES VARIABLES SAVED IN ARRAY ZFARY
48 N ZI S ZI=""
49 F S ZI=$O(@ZFARY@(ZI)) Q:ZI="" D
50 . ;N ZX
51 . S ZX="S "_ZI_"="""_@ZFARY@(ZI)_""""
52 . ;W !,ZX
53 . X ZX
54 Q
55 ;
56FILEIN ; INTERACTIVE ENTRY POINT FOR OPTION TO READ IN A FILE
57 I '$D(C0XFARY) D INITFARY("C0XFARY")
58 D USEFARY("C0XFARY")
59 S DIR(0)="F^3:240"
60 S DIR("A")="File Directory"
61 S DIR("B")=C0XDIR
62 D ^DIR
63 I Y="^" Q ;
64 S C0XDIR=Y
65 S C0XFARY("C0XDIR")=Y
66 S DIR(0)="F^3:240"
67 S DIR("A")="File Name"
68 I '$D(C0XFN) S DIR("B")="qds.rdf"
69 E S DIR("B")=C0XFN
70 D ^DIR
71 I Y="" Q ;
72 I Y="^" Q ;
73 S C0XFN=Y
74 D IMPORT(C0XFN,C0XDIR,,"C0XFARY")
75 K C0XFDA
76 Q
77 ;
78IMPORT(FNAME,INDIR,INURL,FARY) ; EXTRINSIC THAT READS A FILE FROM THE STANDARD
79 ; DIRECTORY, LOADS IT INTO THE TRIPLESTORE AS TEXT, AND RETURNS THE
80 ; NODE NAME OF THE TEXT TRIPLE
81 ; INDIR IS THE OPTIONAL DIRECTORY (DEFAUTS TO STANDARD DIR)
82 ; INURL IS THE OPTIONAL URI FOR ACCESSING THE FILE FROM THE TRIPLE STORE
83 ; FARY IS THE OPTIONAL FILE ARRAY OF THE TRIPLE STORE TO USE
84 I '$D(FARY) D ;
85 . D INITFARY("C0XFARY")
86 . S FARY="C0XFARY"
87 D USEFARY(FARY)
88 N ZD,ZTMP
89 I '$D(INDIR) S INDIR=C0XDIR ; DIRECTORY OF THE RDF FILE
90 I $G(INURL)="" D ;
91 . ;N ZN2 S ZN2=$P(FNAME,".",1)_"_"_$P(FNAME,".",2) ; REMOVE THE DOT
92 . ;S INURL=FDIR_ZN2
93 . S INURL=INDIR_FNAME
94 N ZTMP
95 S ZTMP=$NA(^TMP("C0X",$J,"FILEIN",1)) ; WHERE TO PUT THE INCOMING FILE
96 K @ZTMP ; MAKE SURE IT'S CLEAR
97 S C0XSTART=$$NOW^XLFDT
98 W !,"STARTED: ",C0XSTART
99 W !,"READING IN: ",FNAME
100 I '$$FILEREAD(ZTMP,INDIR,FNAME,4) D Q ; QUIT IF NO SUCCESS
101 . W !,"ERROR READING FILE: ",INDIR,FNAME
102 S ZRDF=$NA(^TMP("C0X",$J,"FILEIN")) ; WITHOUT THE SUBSCRIPT
103 W !,$O(@ZRDF@(""),-1)," LINES READ"
104 D INSRDF(ZRDF,INURL,FARY) ; IMPORT AND PROCESS THE RDF
105 K INURL
106 K C0XFDA
107 ;K ^TMP("MXMLDOM",$J)
108 Q
109 ;
110WGET(ZURL,FARY) ; GET FROM THE INTERNET AN RDF FILE AND INSERT IT
111 ;
112 I '$D(FARY) D ;
113 . D INITFARY("C0XFARY")
114 . S FARY="C0XFARY"
115 D USEFARY(FARY)
116 ;N ZLOC,ZTMP
117 K ZTMP
118 S ZLOC=$NA(^TMP("C0X","WGET",$J))
119 K @ZLOC
120 S C0XSTART=$$NOW^XLFDT
121 W !,"STARTED: ",C0XSTART
122 W !,"DOWNLOADING: ",ZURL
123 S OK=$$httpGET^%zewdGTM(ZURL,.ZTMP)
124 M @ZLOC=ZTMP
125 S C0XLINES=$O(@ZLOC@(""),-1)
126 W !,C0XLINES," LINES READ"
127 S C0XDLC=$$NOW^XLFDT ; DOWNLOAD COMPLETE
128 W !,"DOWNLOAD COMPLETE AT ",C0XDLC
129 S C0XDIFF=$$FMDIFF^XLFDT(C0XDLC,C0XSTART,2)
130 W !," ELAPSED TIME: ",C0XDIFF," SECONDS"
131 I C0XDIFF'=0 W !," APPROXIMATELY ",$P(C0XLINES/C0XDIFF,".")," LINES PER SEC"
132 D INSRDF(ZLOC,ZURL,FARY)
133 Q
134 ;
135INSRDF(ZRDF,ZNAME,FARY) ; INSERT AN RDF FILE INTO THE STORE AND PROCESS
136 ; ZRDF IS PASSED BY NAME
137 I '$D(FARY) D ;
138 . D INITFARY("C0XFARY")
139 . S FARY="C0XFARY"
140 D USEFARY(FARY)
141 S BATCNT=0 ; BATCH COUNTER
142 S BATMAX=10000 ; TRY BATCHES OF THIS SIZE
143 N ZGRAPH,ZSUBJECT
144 S ZGRAPH="_:G"_$$LKY9 ; RANDOM GRAPH NAME
145 S ZSUBJECT=$$ANONS ; RANDOM ANOYMOUS SUBJECT
146 D ADD(ZGRAPH,ZSUBJECT,"fmts:url",ZNAME,FARY)
147 N ZTXTNM
148 S ZTXTNM="_TXT_INCOMING_RDF_FILE_"_ZNAME_"_"_$$LKY9 ; NAME FOR TEXT NODE
149 D ADD(ZGRAPH,ZSUBJECT,"fmts:rdfSource",ZTXTNM,FARY)
150 D SWUPDIE(.C0XFDA) ; TRY IT OUT
151 K C0XCNT ;RESET FOR NEXT TIME
152 D STORETXT(ZRDF,ZTXTNM,FARY)
153 W !,"ADDED: ",ZGRAPH," ",ZSUBJECT," fmts:rdfSource ",ZTXTNM
154 D PROCESS(.G,ZRDF,ZNAME,ZGRAPH,FARY) ; PARSE AND INSERT THE RDF
155 Q
156 ;
157STORETXT(ZTXT,ZNAME,FARY) ; STORE TEXT IN THE TRIPLESTORE AT ZNAME
158 ;
159 I '$D(FARY) D ;
160 . D INITFARY("C0XFARY")
161 . S FARY="C0XFARY"
162 D USEFARY(FARY)
163 N ZIEN
164 S ZIEN=$$IENOF(ZNAME,FARY) ; GET THE IEN
165 D CLEAN^DILF
166 K ZERR
167 D WP^DIE(C0XSFN,ZIEN_",",1,,ZTXT,"ZERR")
168 I $D(ZERR) D ;
169 . ZWR ZERR
170 Q
171 ;
172GETTXT(ZRTN,ZNAME,FARY) ; RETURNS RDF SOURCE OR OTHER TEXT
173 ; ZRTN IS PASSED BY REFERENCE
174 I '$D(FARY) D ;
175 . D INITFARY("C0XFARY")
176 . S FARY="C0XFARY"
177 D USEFARY(FARY)
178 N ZIEN
179 S ZIEN=$$IENOF(ZNAME)
180 S OK=$$GET1^DIQ(C0XSFN,ZIEN_",",1,,"ZRTN")
181 Q
182 ;
183WHERETXT(ZNAME,FARY) ; EXTRINSIC WHICH RETURNS THE NAME OF THE GLOBAL
184 ; WHERE THE TEXT IS LOCATED. NAME IS THE NAME OF THE STRING
185 I '$D(FARY) D ;
186 . D INITFARY("C0XFARY")
187 . S FARY="C0XFARY"
188 D USEFARY(FARY)
189 N ZIEN
190 S ZIEN=$$IENOF(ZNAME)
191 Q $NA(@C0XSN@(ZIEN,1))
192 ;
193FILEREAD(ZINTMP,ZDIR,ZFNAME,ZLVL) ; READS A FILE INTO ZINTMP USING FTG^%ZISH
194 ; ZINTMP IS PASSED BY NAME AND INCLUDES THE NEW SUBSCRIPT
195 ; IE ^TMP("C0X","FILEIN",1)
196 ; ZLVL IN THIS CASE WOULD BE 3 INCREMENTING THE 1
197 ; EXTRINSIC WHICH RETURNS THE RESULT OF FTG^%ZISH
198 S OK=$$FTG^%ZISH(ZDIR,FNAME,ZINTMP,ZLVL)
199 Q OK
200 ;
201TESTPROC ; TEST PROCESS WITH EXISTING SMALL RDF FILE
202 S ZIN=$NA(^TMP("C0X",12226,"FILEIN"))
203 S ZGRAPH="/test/rdfFile"
204 S ZM="/test/rdfFile/meta"
205 D PROCESS(.G,ZIN,ZGRAPH,ZM)
206 Q
207 ;
208PROCESS(ZRTN,ZRDF,ZGRF,ZMETA,FARY) ; PROCESS AN INCOMING RDF FILE
209 ; ZRTN IS PASS BY REFERENCE AND RETURNS MESSAGES ABOUT THE PROCESSING
210 ; ZRDF IS PASSED BY NAME AND IS THE GLOBAL CONTAINING THE RDF FILE
211 ; ZGRF IS THE NAME OF THE GRAPH TO USE IN THE TRIPLE STORE FOR RESULTS
212 ; ZMETA IS OPTIONAL AND IS THE NAME OF THE GRAPH TO STORE METADATA
213 ;
214 I '$D(FARY) D ;
215 . D INITFARY("C0XFARY")
216 . S FARY="C0XFARY"
217 D USEFARY(FARY)
218 ;N BATCNT
219 ;N BATMAX
220 ; -- first parse the rdf file with the MXML parser
221 ;S C0XDOCID=$$PARSE^C0CNHIN(ZRDF,"C0XARRAY") ; PARSE WITH MXML
222 S C0XDLC2=$$NOW^XLFDT ; START OF PARSE
223 S C0XDOCID=$$EN^MXMLDOM(ZRDF,"W")
224 ;B
225 K @ZRDF ; DON'T NEED INPUT BUFFER ANYMORE
226 ; -- assign the MXLM dom global name to ZDOM
227 S ZDOM=$NA(^TMP("MXMLDOM",$J,C0XDOCID))
228 S C0XNODE=$O(@ZDOM@(""),-1)
229 W !,C0XNODE," XML NODES PARSED"
230 S C0XPRS=$$NOW^XLFDT ; PARSE COMPLETE
231 W !,"PARSE COMPLETE AT ",C0XPRS
232 S C0XDIFF=$$FMDIFF^XLFDT(C0XPRS,C0XDLC2,2)
233 W !," ELAPSED TIME: ",C0XDIFF," SECONDS"
234 I C0XDIFF'=0 D ;
235 . W !," APPROXIMATELY ",$P(C0XNODE/C0XDIFF,".")," NODES PER SECOND"
236 ; -- populate the metagraph to point to the graph with status unfinished
237 S METAS=$$ANONS ; GET AN ANONOMOUS RANDOM SUBJECT
238 I '$D(ZMETA) S ZMETA="_:G"_$$LKY9 ; RANDOM GRAPH NAME FOR METAGRAPH
239 D ADD(ZMETA,METAS,"fmts:about",ZGRF,FARY) ; POINT THE META TO THE GRAPH
240 D ADD(ZMETA,METAS,"fmts:status","unfinished",FARY) ; mark as unfinished
241 W !,"INSERTING GRAPH: ",ZGRF
242 ;S C0XDATE=$$FMDTOUTC^C0CUTIL($$NOW^XLFDT,"DT")
243 S C0XDATE=$$NOW^XLFDT
244 D ADD(ZMETA,METAS,"fmts:dateTime",C0XDATE,FARY)
245 D SWUPDIE(.C0XFDA) ; commit the metagraph changes to the triple store
246 ; --
247 ; -- pull out the vocabularies in the RDF statement. marked with xmlns:
248 ; -- put them in a local variable for quick reference
249 ; -- TODO: create a graph for vocabularies and validate incoming against it
250 ;
251 S C0XVOC=""
252 N ZI,ZJ,ZK S ZI=""
253 F S ZI=$O(@ZDOM@(1,"A",ZI)) Q:ZI="" D ; FOR EACH xmlns
254 . S ZVOC=$P(ZI,"xmlns:",2)
255 . I ZVOC'="" S C0XVOC(ZVOC)=$G(@ZDOM@(1,"A",ZI))
256 W !,"VOCABS:" ZWR C0XVOC
257 ;
258 ; -- look for children called rdf:Description. quit if none. not an rdf file
259 ;
260 S C0XTYPE("rdf:Description")=1
261 S C0XTYPE("owl:ObjectProperty")=1
262 S ZI=$O(@ZDOM@(1,"C",""))
263 I '$G(C0XTYPE(@ZDOM@(1,"C",ZI))) D Q ; not an rdf file
264 . W !,"Error. Not an RDF file. Cannot process."
265 . zwr ^TMP("MXMLDOM",$J,*)
266 ;
267 ; -- now process the rdf description children
268 ;
269 S ZI=""
270 S (C0XSUB,C0XPRE,C0XOBJ)="" ; INITIALIZE subject, object and predicate
271 F S ZI=$O(@ZDOM@(1,"C",ZI)) Q:ZI="" D ;
272 . ; -- we are skipping any child that is not rdf:Description
273 . ; -- TODO: check to see if this is right in general
274 . ;
275 . IF '$G(C0XTYPE(@ZDOM@(1,"C",ZI))) D Q ;
276 . . W !,"SKIPPING NODE: ",ZI
277 . ; -- now looking for the subject for the triples
278 . S ZX=$G(@ZDOM@(ZI,"A","rdf:about"))
279 . I ZX'="" D ; we have the subject
280 . . ;W " about: ",ZX
281 . . S C0XSUB=ZX
282 . E D ;
283 . . S ZX=$G(@ZDOM@(ZI,"A","rdf:nodeID")) ; node id is another style of subject
284 . . I ZX'="" D ;
285 . . . S C0XSUB=ZX
286 . I C0XSUB="" S C0XSUB=$$ANONS ; DEFAULT TO BLANK SUBJECT
287 . ;
288 . ; -- we now have the subject. the children of this node have the rest
289 . ;
290 . S ZJ="" ; for the children of the rdf:Description nodes
291 . F S ZJ=$O(@ZDOM@(ZI,"C",ZJ)) Q:ZJ="" D ; for each child
292 . . S C0XPRE=@ZDOM@(ZJ) ; the predicate without a prefix
293 . . S ZX=$G(@ZDOM@(ZJ,"A","xmlns")) ; name space
294 . . I ZX'="" S C0XPRE=ZX_C0XPRE ; add the namespace prefix
295 . . I C0XPRE[":" D ; expand using vocabulary
296 . . . N ZB,ZA
297 . . . S ZB=$P(C0XPRE,":",1)
298 . . . S ZA=$P(C0XPRE,":",2)
299 . . . I $G(C0XVOC(ZB))'="" D ;
300 . . . . S C0XPRE=C0XVOC(ZB)_ZA ; expanded
301 . . S ZY=$G(@ZDOM@(ZJ,"A","rdf:resource")) ; potential object
302 . . I ZY'="" D Q ;
303 . . . S C0XOBJ=ZY ; object
304 . . . D ADD(ZGRF,C0XSUB,C0XPRE,C0XOBJ) ; finally. our first real triple
305 . . ; -- this is an else because of the quit above
306 . . S ZX=$G(@ZDOM@(ZJ,"A","rdf:nodeID")) ; fishing for nodeId object
307 . . I ZX'="" D Q ; got one
308 . . . S C0XOBJ=ZX ; we are using the incoming nodeIDs as object/subject
309 . . . ; without change... this could be foolish .. look at it again later
310 . . . D ADD(ZGRF,C0XSUB,C0XPRE,C0XOBJ) ; go for it and add a node
311 . . S C0XOBJ=$G(@ZDOM@(ZJ,"T",1)) ; hopefully an object is here
312 . . I C0XOBJ="" D Q ; not a happy situation
313 . . . W !,"ERROR, NO OBJECT FOUND FOR NODE: ",ZJ
314 . . D ADD(ZGRF,C0XSUB,C0XPRE,C0XOBJ) ; go for it and add a node
315 S C0XTRP=$$NOW^XLFDT ; PARSE COMPLETE
316 W !,"TRIPLES COMPLETE AT ",C0XTRP
317 S C0XDIFF=$$FMDIFF^XLFDT(C0XTRP,C0XPRS,2)
318 W !," ELAPSED TIME: ",C0XDIFF," SECONDS"
319 I C0XDIFF'=0 D ;
320 . W !," APPROXIMATELY ",$P(C0XCNT/C0XDIFF,".")," TRIPLES PER SECOND"
321 W !,"INSERTING ",C0XCNT," TRIPLES"
322 I $D(C0XFDA) D ;
323 . I $G(BLKLOAD) D ;
324 . . D BULKLOAD(.C0XFDA)
325 . E D ;
326 . . D UPDIE(.C0XFDA) ; commit the updates to the file
327 ; next, mark the graph as finished
328 S C0XINS=$$NOW^XLFDT ; PARSE COMPLETE
329 W !,"INSERTION COMPLETE AT ",C0XPRS
330 S C0XDIFF=$$FMDIFF^XLFDT(C0XINS,C0XTRP,2)
331 W !," ELAPSED TIME: ",C0XDIFF," SECONDS"
332 I C0XDIFF'=0 W !," APPROXIMATELY ",$P(C0XCNT/C0XDIFF,".")," NODES PER SECOND"
333 S C0XEND=$$NOW^XLFDT
334 W !," ENDED AT: ",C0XEND
335 S C0XDIFF=$$FMDIFF^XLFDT(C0XEND,C0XSTART,2)
336 W !," ELAPSED TIME: ",C0XDIFF," SECONDS"
337 I C0XDIFF'=0 W !," APPROXIMATELY ",$P(C0XCNT/C0XDIFF,".")," TRIPLES PER SECOND"
338 Q
339 ;
340SHOW(ZN) ;
341 ZWR ^TMP("MXMLDOM",$J,1,ZN,*)
342 Q
343 ;
344ANONS() ; RETURNS AN ANONOMOUS SUBJECT
345 Q "_:S"_$$LKY9
346 ;
347NEWG(NGRAPH,NMETA) ; CREATES A NEW META GRAPH, MARKS IT AS UNFINISHED
348 ; THEN CREATES A NEW GRAPH AND POINTS THE METAGRAPH TO IT
349 ; NGRAPH AND NMETA ARE PASSED BY REFERENCE AND ARE THE RETURN
350 S NGRAPH="G"_$$LKY9
351 S NMETA=NGRAPH_"A"
352 Q
353 ;
354ADD(ZG,ZS,ZP,ZO,FARY) ; ADD A TRIPLE TO THE TRIPLESTORE. ALL VALUES ARE TEXT
355 ; THE FDA IS SET UP BUT THE FILES ARE NOT UPDATED. CALL UPDIE TO COMPLETE
356 I '$D(FARY) D ;
357 . D INITFARY("C0XFARY")
358 . S FARY="C0XFARY"
359 D USEFARY(FARY)
360 I '$D(C0XCNT) S C0XCNT=0
361 N ZNODE
362 S ZNODE="N"_$$LKY17
363 N ZNARY ; GET READY TO CALL IENOFA
364 S ZNARY("ZG",ZG)=""
365 S ZNARY("ZS",ZS)=""
366 S ZNARY("ZP",ZP)=""
367 S ZNARY("ZO",ZO)=""
368 D IENOFA(.ZIENS,.ZNARY,FARY) ; RESOLVE/ADD STRINGS
369 ;S ZGIEN=$$IENOF(ZG) ; LAYGO TO GET IEN
370 ;S ZSIEN=$$IENOF(ZS)
371 ;S ZPIEN=$$IENOF(ZP)
372 ;S ZOIEN=$$IENOF(ZO)
373 ;I $D(C0XFDA) D UPDIE ; ADD THE STRINGS IF NEEDED
374 S BATCNT=BATCNT+1
375 S C0XCNT=C0XCNT+1
376 I $G(BLKLOAD)=1 D ; we are using bulk load
377 . S C0XFDA(C0XTFN,BATCNT,.01)=ZNODE
378 . S C0XFDA(C0XTFN,BATCNT,.02)=$O(ZIENS("IEN","ZG",""))
379 . S C0XFDA(C0XTFN,BATCNT,.03)=$O(ZIENS("IEN","ZS",""))
380 . S C0XFDA(C0XTFN,BATCNT,.04)=$O(ZIENS("IEN","ZP",""))
381 . S C0XFDA(C0XTFN,BATCNT,.05)=$O(ZIENS("IEN","ZO",""))
382 E D ;
383 . S C0XFDA(C0XTFN,"?+"_BATCNT_",",.01)=ZNODE
384 . S C0XFDA(C0XTFN,"?+"_BATCNT_",",.02)=$O(ZIENS("IEN","ZG",""))
385 . S C0XFDA(C0XTFN,"?+"_BATCNT_",",.03)=$O(ZIENS("IEN","ZS",""))
386 . S C0XFDA(C0XTFN,"?+"_BATCNT_",",.04)=$O(ZIENS("IEN","ZP",""))
387 . S C0XFDA(C0XTFN,"?+"_BATCNT_",",.05)=$O(ZIENS("IEN","ZO",""))
388 I BATCNT=BATMAX D ; BATCH IS DONE
389 . I $G(BLKLOAD) D ; bulk load
390 . . D BULKLOAD(.C0XFDA) ; bulk load the batch
391 . E D ; no bulk load
392 . . D UPDIE(.C0XFDA)
393 . K C0XFDA
394 . S BATCNT=0 ; RESET COUNTER
395 ; REMEMBER TO CALL UPDIE WHEN YOU'RE DONE
396 Q
397 ;
398LKY9() ;EXTRINIC THAT RETURNS A RANDOM 9 DIGIT NUMBER. USED FOR GENERATING
399 ; UNIQUE NODE AND GRAPH NAMES
400 N ZN,ZI
401 S ZN=""
402 F ZI=1:1:9 D ;
403 . S ZN=ZN_$R(10)
404 Q ZN
405 ;
406LKY17() ;EXTRINIC THAT RETURNS A RANDOM 9 DIGIT NUMBER. USED FOR GENERATING
407 ; UNIQUE NODE AND GRAPH NAMES
408 N ZN,ZI
409 S ZN=""
410 F ZI=1:1:17 D ;
411 . S ZN=ZN_$R(10)
412 Q ZN
413 ;
414IENOF(ZSTRING,FARY) ; EXTRINSIC WHICH RETURNS THE IEN OF ZS IN THE STRINGS FILE
415 I '$D(FARY) D ;
416 . D INITFARY("C0XFARY")
417 . S FARY="C0XFARY"
418 N ZIEN
419 I $G(ZSTRING)="" Q "" ; NO STRING
420 S ZIEN=$O(@C0XSN@("B",ZSTRING,""))
421 I ZIEN="" D ;
422 . S C0XFDA2(C0XSFN,"+1,",.01)=ZSTRING
423 . D UPDIE(.C0XFDA2)
424 . S ZIEN=$O(@C0XSN@("B",ZSTRING,""))
425 . K C0XFDA2
426 Q ZIEN
427 ;
428IENOFA(ZOUTARY,ZINARY,FARY) ; RESOLVE STRINGS TO IEN IN STRINGS FILE
429 ; OR ADD THEM IF
430 ; MISSING. ZINARY AND ZOUTARY ARE PASSED BY REFERENCE
431 ; ZINARY LOOKS LIKE ZINARY("VAR","VAL")=""
432 ; RETURNS IN ZOUTARY OF THE FORM ZOUTARY("IEN","VAR",IEN)=""
433 I '$D(FARY) D ;
434 . D INITFARY("C0XFARY")
435 . S FARY="C0XFARY"
436 K ZOUTARY ; START WITH CLEAN RESULTS
437 K C0XFDA2 ; USE A SEPARATE FDA FOR THIS
438 N ZI S ZI=""
439 N ZV,ZIEN
440 N ZCNT S ZCNT=0
441 F S ZI=$O(ZINARY(ZI)) Q:ZI="" D ; LOOK FOR MISSING STRINGS
442 . S ZV=$O(ZINARY(ZI,""))
443 . I $O(@C0XSN@("B",ZV,""))="" D ;
444 . . S ZCNT=ZCNT+1
445 . . S C0XFDA2(C0XSFN,"+"_ZCNT_",",.01)=ZV
446 I $D(C0XFDA2) D ;
447 . D UPDIE(.C0XFDA2) ; ADD MISSING STRINGS
448 . K C0XFDA2 ; CLEAN UP
449 F S ZI=$O(ZINARY(ZI)) Q:ZI="" D ; NOT GET ALL IENS
450 . S ZV=$O(ZINARY(ZI,""))
451 . S ZIEN=$O(@C0XSN@("B",ZV,"")) ; THEY SHOULD BE THERE NOW
452 . I ZIEN="" D ;
453 . . W !,"ERROR ADDING STRING: ",ZV
454 . . B
455 . S ZOUTARY("IEN",ZI,ZIEN)=""
456 Q
457 ;
458BULKLOAD(ZBFDA) ; BULK LOADER FOR LOADING TRIPLES INTO FILE 172.101
459 ; USING GLOBAL SETS INSTEAD OF UPDATE^DIE
460 ; QUITS IF FILE IS NOT 172.101
461 ; EXPECTS AN FDA WITHOUT STRINGS FOR THE IENS, STARTING AT 1
462 ; QUITS IF FIRST ENTRY IS NOT IENS 1
463 ; ASSUMES THAT THE LAST IENS IS THE COUNT OF ENTRIES
464 ; ZBFDA IS PASSED BY REFERENCE
465 ;
466 ; -- reserves a block of iens from file 172.101 by locking the zero node
467 ; -- ^C0X(101,0) and adding the count of entries to piece 2 and 3
468 ; -- then unlocking to minimize the duration of the lock
469 ;
470 W !,"USING BULKLOAD"
471 I '$D(ZBFDA) Q ; EMPTY FDA
472 I $O(ZBFDA(""))'=172.101 Q ; WRONG FILE
473 N ZCNT,ZP3,ZP4
474 ; -- find the number of nodes to insert
475 S ZCNT=$O(ZBFDA(172.101,""),-1)
476 I ZCNT="" D Q ;
477 . W !,"ERROR IN BULK LOAD - INVALID NODE COUNT"
478 . B
479 ; -- lock the zero node and reserve a block of iens to insert
480 W !,"LOCKING ZERO NODE"
481 LOCK +^C0X(101,0)
482 S ZP3=$P(^C0X(101,0),U,3)
483 S ZP4=$P(^C0X(101,0),U,4)
484 S $P(^C0X(101,0),U,3)=ZP3+ZCNT+1
485 S $P(^C0X(101,0),U,4)=ZP4+ZCNT+1
486 LOCK -^C0X(101,0)
487 N ZI,ZN,ZG,ZS,ZP,ZO,ZIEN,ZBASE
488 S ZBASE=ZP3 ; the last ien in the file
489 W !,"ZERO NODE UNLOCKED, IENS RESERVED=",ZCNT
490 W !,$$NOW^XLFDT
491 S ZI=""
492 F S ZI=$O(ZBFDA(172.101,ZI)) Q:ZI="" D ;
493 . S ZN=$G(ZBFDA(172.101,ZI,.01)) ; node name
494 . I ZN="" D BLKERR Q ;
495 . S ZG=$G(ZBFDA(172.101,ZI,.02)) ; graph pointer
496 . I ZG="" D BLKERR Q ;
497 . S ZS=$G(ZBFDA(172.101,ZI,.03)) ; subject pointer
498 . I ZS="" D BLKERR Q ;
499 . S ZP=$G(ZBFDA(172.101,ZI,.04)) ; predicate pointer
500 . I ZP="" D BLKERR Q ;
501 . S ZO=$G(ZBFDA(172.101,ZI,.05)) ; object pointer
502 . I ZO="" D BLKERR Q ;
503 . S ZIEN=ZI+ZBASE ; the new ien
504 . S ^C0X(101,ZIEN,0)=ZN_U_ZG_U_ZS_U_ZP_U_ZO ; set the zero node
505 . D INDEX(ZIEN,ZN,ZG,ZS,ZP,ZO)
506 Q
507 ;
508INDEX(ZIEN,ZN,ZG,ZS,ZP,ZO) ; HARD SET THE INDEX FOR ONE ENTRY
509 S ^C0X(101,"B",ZN,ZIEN)="" ; the B index
510 S ^C0X(101,"G",ZG,ZIEN)="" ; the G for Graph index
511 S ^C0X(101,"SPO",ZS,ZP,ZO,ZIEN)=""
512 S ^C0X(101,"SOP",ZS,ZO,ZP,ZIEN)=""
513 S ^C0X(101,"OPS",ZO,ZP,ZS,ZIEN)=""
514 S ^C0X(101,"OSP",ZO,ZS,ZP,ZIEN)=""
515 S ^C0X(101,"PSO",ZP,ZS,ZO,ZIEN)=""
516 S ^C0X(101,"POS",ZP,ZO,ZS,ZIEN)=""
517 S ^C0X(101,"GOPS",ZG,ZO,ZP,ZS,ZIEN)=""
518 S ^C0X(101,"GOSP",ZG,ZO,ZS,ZP,ZIEN)=""
519 S ^C0X(101,"GPSO",ZG,ZP,ZS,ZO,ZIEN)=""
520 S ^C0X(101,"GPOS",ZG,ZP,ZO,ZS,ZIEN)=""
521 S ^C0X(101,"GSPO",ZG,ZS,ZP,ZO,ZIEN)=""
522 S ^C0X(101,"GSOP",ZG,ZS,ZO,ZP,ZIEN)=""
523 Q
524 ;
525REINDEX ; REINDEX THE ^C0X(101, TRIPLE STORE
526 K ^C0X(101,"B")
527 K ^C0X(101,"G")
528 K ^C0X(101,"SPO")
529 K ^C0X(101,"SOP")
530 K ^C0X(101,"OPS")
531 K ^C0X(101,"OSP")
532 K ^C0X(101,"PSO")
533 K ^C0X(101,"POS")
534 K ^C0X(101,"GOPS")
535 K ^C0X(101,"GOSP")
536 K ^C0X(101,"GPSO")
537 K ^C0X(101,"GPOS")
538 K ^C0X(101,"GSPO")
539 K ^C0X(101,"GSOP")
540 N ZIEN,ZZ
541 S ZIEN=0
542 F S ZIEN=$O(^C0X(101,ZIEN)) Q:+ZIEN=0 D ; FOR EACH NODE
543 . S ZZ=$G(^C0X(101,ZIEN,0))
544 . I ZZ="" D Q ;
545 . . W !,"ERROR REINDEXING NODE ",ZI
546 . S ZN=$P(ZZ,"^",1)
547 . S ZG=$P(ZZ,"^",2)
548 . S ZS=$P(ZZ,"^",3)
549 . S ZP=$P(ZZ,"^",4)
550 . S ZO=$P(ZZ,"^",5)
551 . D INDEX(ZIEN,ZN,ZG,ZS,ZP,ZO)
552 Q
553 ;
554BLKERR ;
555 W !,"ERROR IN BULK LOAD",! ZWR ZBFDA(ZI)
556 B
557 Q
558 ;
559SWUPDIE(ZFDA) ; SWITCH BETWEEN UPDIE AND BULKLOAD
560 . I $G(BLKLOAD) D ; bulk load
561 . . D BULKLOAD(.ZFDA) ; bulk load the batch
562 . E D ; no bulk load
563 . . D UPDIE(.ZFDA)
564 . K ZFDA
565 Q
566 ;
567UPDIE(ZFDA) ; INTERNAL ROUTINE TO CALL UPDATE^DIE AND CHECK FOR ERRORS
568 ; ZFDA IS PASSED BY REFERENCE
569 ;ZWR ZFDA
570 ;B
571 K ZERR
572 D CLEAN^DILF
573 D UPDATE^DIE("","ZFDA","","ZERR")
574 I $D(ZERR) S ZZERR=ZZERR ; ZZERR DOESN'T EXIST,
575 ; INVOKE THE ERROR TRAP IF TASKED
576 ;. W "ERROR",!
577 ;. ZWR ZERR
578 ;. B
579 K ZFDA
580 Q
581 ;
Note: See TracBrowser for help on using the repository browser.