Index: fmts/trunk/p/C0XF2N.m
===================================================================
--- fmts/trunk/p/C0XF2N.m	(revision 1279)
+++ fmts/trunk/p/C0XF2N.m	(revision 1294)
@@ -1,4 +1,4 @@
-C0XMAIN ; GPL - Fileman Triples entry point routine ;10/13/11  17:05
- ;;0.1;C0X;nopatch;noreleasedate;Build 1
+C0XF2N ; GPL - Fileman Triples entry point routine ;10/13/11  17:05
+ ;;0.1;C0X;nopatch;noreleasedate;Build 5
  ;Copyright 2011 George Lilly.  Licensed under the terms of the GNU
  ;General Public License See attached copy of the License.
@@ -33,9 +33,10 @@
  ; FOR THE DEFAULT TRIPLE STORE. USE OTHER VALUES FOR SUPPORTING ADDITIONAL
  ; TRIPLE STORES
+ I $D(@ZFARY) Q  ; ALREADY INITIALIZED
  S @ZFARY@("C0XTFN")=172.101 ; TRIPLES FILE NUMBER
  S @ZFARY@("C0XSFN")=172.201 ; TRIPLES STRINGS FILE NUMBER
  S @ZFARY@("C0XTN")=$NA(^C0X(101)) ; TRIPLES GLOBAL NAME
  S @ZFARY@("C0XSN")=$NA(^C0X(201)) ; STRING FILE GLOBAL NAME
- S @ZFARY@("C0XDIR")="/home/glilly/fmts/trunk/samples/qds/"
+ S @ZFARY@("C0XDIR")="/home/glilly/fmts/trunk/samples/"
  S @ZFARY@("BLKLOAD")=1 ; this file supports block load
  S @ZFARY@("FMTSSTYLE")="F2N" ; fileman style
@@ -71,11 +72,12 @@
  S C0XFN=Y
  D IMPORT(C0XFN,C0XDIR,,"C0XFARY")
- Q
- ;
-IMPORT(FNAME,FDIR,FURL,FARY) ; EXTRINSIC THAT READS A FILE FROM THE STANDARD 
+ K C0XFDA
+ Q
+ ;
+IMPORT(FNAME,INDIR,INURL,FARY) ; EXTRINSIC THAT READS A FILE FROM THE STANDARD 
  ; DIRECTORY, LOADS IT INTO THE TRIPLESTORE AS TEXT, AND RETURNS THE
  ; NODE NAME OF THE TEXT TRIPLE
- ; FDIR IS THE OPTIONAL DIRECTORY (DEFAUTS TO STANDARD DIR)
- ; FURL IS THE OPTIONAL URI FOR ACCESSING THE FILE FROM THE TRIPLE STORE
+ ; INDIR IS THE OPTIONAL DIRECTORY (DEFAUTS TO STANDARD DIR)
+ ; INURL IS THE OPTIONAL URI FOR ACCESSING THE FILE FROM THE TRIPLE STORE
  ; FARY IS THE OPTIONAL FILE ARRAY OF THE TRIPLE STORE TO USE
  I '$D(FARY) D  ;
@@ -84,8 +86,9 @@
  D USEFARY(FARY)
  N ZD,ZTMP
- I '$D(FDIR) S FDIR=C0XDIR ; DIRECTORY OF THE RDF FILE
- I '$D(FURL) D  ;
- . N ZN2 S ZN2=$TR(FNAME,".","_") ; REMOVE THE DOT FROM THE NAME
- . S FURL=FDIR_ZN2
+ I '$D(INDIR) S INDIR=C0XDIR ; DIRECTORY OF THE RDF FILE
+ I $G(INURL)="" D  ;
+ . ;N ZN2 S ZN2=$P(FNAME,".",1)_"_"_$P(FNAME,".",2) ; REMOVE THE DOT 
+ . ;S INURL=FDIR_ZN2
+ . S INURL=INDIR_FNAME
  N ZTMP
  S ZTMP=$NA(^TMP("C0X",$J,"FILEIN",1)) ; WHERE TO PUT THE INCOMING FILE
@@ -94,9 +97,12 @@
  W !,"STARTED: ",C0XSTART
  W !,"READING IN: ",FNAME
- I '$$FILEREAD(ZTMP,FDIR,FNAME,4) D  Q  ; QUIT IF NO SUCCESS
- . W !,"ERROR READING FILE: ",FDIR,FNAME 
+ I '$$FILEREAD(ZTMP,INDIR,FNAME,4) D  Q  ; QUIT IF NO SUCCESS
+ . W !,"ERROR READING FILE: ",INDIR,FNAME 
  S ZRDF=$NA(^TMP("C0X",$J,"FILEIN")) ; WITHOUT THE SUBSCRIPT
  W !,$O(@ZRDF@(""),-1)," LINES READ"
- D INSRDF(ZRDF,FURL,FARY) ; IMPORT AND PROCESS THE RDF
+ D INSRDF(ZRDF,INURL,FARY) ; IMPORT AND PROCESS THE RDF
+ K INURL
+ K C0XFDA
+ K ^TMP("MXMLDOM",$J)
  Q
  ;
@@ -132,4 +138,6 @@
  . S FARY="C0XFARY"
  D USEFARY(FARY)
+ S BATCNT=0 ; BATCH COUNTER
+ S BATMAX=10000 ; TRY BATCHES OF THIS SIZE
  N ZGRAPH,ZSUBJECT
  S ZGRAPH="_:G"_$$LKY9 ; RANDOM GRAPH NAME
@@ -139,9 +147,9 @@
  S ZTXTNM="_TXT_INCOMING_RDF_FILE_"_ZNAME_"_"_$$LKY9 ; NAME FOR TEXT NODE
  D ADD(ZGRAPH,ZSUBJECT,"fmts:rdfSource",ZTXTNM,FARY)
- D UPDIE(.C0XFDA) ; TRY IT OUT
+ D SWUPDIE(.C0XFDA) ; TRY IT OUT
  K C0XCNT ;RESET FOR NEXT TIME
  D STORETXT(ZRDF,ZTXTNM,FARY)
  W !,"ADDED: ",ZGRAPH," ",ZSUBJECT," fmts:rdfSource ",ZTXTNM
- D PROCESS2(.G,ZRDF,ZNAME,ZGRAPH,FARY) ; PARSE AND INSERT THE RDF
+ D PROCESS(.G,ZRDF,ZNAME,ZGRAPH,FARY) ; PARSE AND INSERT THE RDF
  Q
  ;
@@ -207,10 +215,21 @@
  . S FARY="C0XFARY"
  D USEFARY(FARY)
+ ;N BATCNT
+ ;N BATMAX
  ; -- first parse the rdf file with the MXML parser
  ;S C0XDOCID=$$PARSE^C0CNHIN(ZRDF,"C0XARRAY") ; PARSE WITH MXML
+ S C0XDLC2=$$NOW^XLFDT ; START OF PARSE
  S C0XDOCID=$$EN^MXMLDOM(ZRDF,"W")
+ K @ZRDF ; DON'T NEED INPUT BUFFER ANYMORE
  ; -- assign the MXLM dom global name to ZDOM
  S ZDOM=$NA(^TMP("MXMLDOM",$J,C0XDOCID))
- W !,$O(@ZDOM@(""),-1)," XML NODES PARSED"
+ S C0XNODE=$O(@ZDOM@(""),-1)
+ W !,C0XNODE," XML NODES PARSED"
+ S C0XPRS=$$NOW^XLFDT ; PARSE COMPLETE
+ W !,"PARSE COMPLETE AT ",C0XPRS
+ S C0XDIFF=$$FMDIFF^XLFDT(C0XPRS,C0XDLC2,2)
+ W !," ELAPSED TIME: ",C0XDIFF," SECONDS"
+ I C0XDIFF'=0 D  ;
+ . W !," APPROXIMATELY ",$P(C0XNODE/C0XDIFF,".")," NODES PER SECOND"
  ; -- populate the metagraph to point to the graph with status unfinished
  S METAS=$$ANONS ; GET AN ANONOMOUS RANDOM SUBJECT
@@ -218,8 +237,9 @@
  D ADD(ZMETA,METAS,"fmts:about",ZGRF,FARY) ; POINT THE META TO THE GRAPH
  D ADD(ZMETA,METAS,"fmts:status","unfinished",FARY) ; mark as unfinished
+ W !,"INSERTING GRAPH: ",ZGRF
  ;S C0XDATE=$$FMDTOUTC^C0CUTIL($$NOW^XLFDT,"DT")
  S C0XDATE=$$NOW^XLFDT
  D ADD(ZMETA,METAS,"fmts:dateTime",C0XDATE,FARY)
- D UPDIE(.C0XFDA) ; commit the metagraph changes to the triple store
+ D SWUPDIE(.C0XFDA) ; commit the metagraph changes to the triple store
  ; -- 
  ; -- pull out the vocabularies in the RDF statement. marked with xmlns:
@@ -288,118 +308,4 @@
  . . . W !,"ERROR, NO OBJECT FOUND FOR NODE: ",ZJ
  . . D ADD(ZGRF,C0XSUB,C0XPRE,C0XOBJ) ; go for it and add a node
- W !,"INSERTING ",C0XCNT," TRIPLES"
- D UPDIE(.C0XFDA) ; commit the updates to the file
- ; next, mark the graph as finished
- S C0XEND=$$NOW^XLFDT
- W !," ENDED AT: ",C0XEND
- S C0XDIFF=$$FMDIFF^XLFDT(C0XEND,C0XSTART,2)
- W !," ELAPSED TIME: ",C0XDIFF," SECONDS"
- I C0XDIFF'=0 D  ;
- . W !," APPROXIMATELY ",$P(C0XCNT/C0XDIFF,".")," TRIPLES PER SECOND"
- Q
- ;
-PROCESS2(ZRTN,ZRDF,ZGRF,ZMETA,FARY) ; PROCESS AN INCOMING RDF FILE
- ; ZRTN IS PASS BY REFERENCE AND RETURNS MESSAGES ABOUT THE PROCESSING
- ; ZRDF IS PASSED BY NAME AND IS THE GLOBAL CONTAINING THE RDF FILE
- ; ZGRF IS THE NAME OF THE GRAPH TO USE IN THE TRIPLE STORE FOR RESULTS
- ; ZMETA IS OPTIONAL AND IS THE NAME OF THE GRAPH TO STORE METADATA
- ;
- I '$D(FARY) D  ;
- . D INITFARY("C0XFARY")
- . S FARY="C0XFARY"
- D USEFARY(FARY)
- ;N BATCNT
- ;N BATMAX
- S BATCNT=0 ; BATCH COUNTER
- S BATMAX=10000 ; TRY BATCHES OF THIS SIZE
- ; -- first parse the rdf file with the MXML parser
- ;S C0XDOCID=$$PARSE^C0CNHIN(ZRDF,"C0XARRAY") ; PARSE WITH MXML
- S C0XDLC2=$$NOW^XLFDT ; START OF PARSE
- S C0XDOCID=$$EN^MXMLDOM(ZRDF,"W")
- ; -- assign the MXLM dom global name to ZDOM
- S ZDOM=$NA(^TMP("MXMLDOM",$J,C0XDOCID))
- S C0XNODE=$O(@ZDOM@(""),-1)
- W !,C0XNODE," XML NODES PARSED"
- S C0XPRS=$$NOW^XLFDT ; PARSE COMPLETE
- W !,"PARSE COMPLETE AT ",C0XPRS
- S C0XDIFF=$$FMDIFF^XLFDT(C0XPRS,C0XDLC2,2)
- W !," ELAPSED TIME: ",C0XDIFF," SECONDS"
- I C0XDIFF'=0 D  ;
- . W !," APPROXIMATELY ",$P(C0XNODE/C0XDIFF,".")," NODES PER SECOND"
- ; -- populate the metagraph to point to the graph with status unfinished
- S METAS=$$ANONS ; GET AN ANONOMOUS RANDOM SUBJECT
- I '$D(ZMETA) S ZMETA="_:G"_$$LKY9 ; RANDOM GRAPH NAME FOR METAGRAPH
- D ADD(ZMETA,METAS,"fmts:about",ZGRF,FARY) ; POINT THE META TO THE GRAPH
- D ADD(ZMETA,METAS,"fmts:status","unfinished",FARY) ; mark as unfinished
- ;S C0XDATE=$$FMDTOUTC^C0CUTIL($$NOW^XLFDT,"DT")
- S C0XDATE=$$NOW^XLFDT
- D ADD(ZMETA,METAS,"fmts:dateTime",C0XDATE,FARY)
- D UPDIE(.C0XFDA) ; commit the metagraph changes to the triple store
- ; -- 
- ; -- pull out the vocabularies in the RDF statement. marked with xmlns:
- ; -- put them in a local variable for quick reference
- ; -- TODO: create a graph for vocabularies and validate incoming against it
- ;
- S C0XVOC=""
- N ZI,ZJ,ZK S ZI=""
- F  S ZI=$O(@ZDOM@(1,"A",ZI)) Q:ZI=""  D  ; FOR EACH xmlns
- . S ZVOC=$P(ZI,"xmlns:",2)
- . I ZVOC'="" S C0XVOC(ZVOC)=$G(@ZDOM@(1,"A",ZI))
- ;W !,"VOCABS:" ZWR C0XVOC
- ;
- ; -- look for children called rdf:Description. quit if none. not an rdf file
- ;
- S ZI=$O(@ZDOM@(1,"C",""))
- I $G(@ZDOM@(1,"C",ZI))'="rdf:Description" D  Q  ; not an rdf file
- . W !,"Error. Not an RDF file. Cannot process."
- ;
- ; -- now process the rdf description children
- ;
- S ZI=""
- S (C0XSUB,C0XPRE,C0XOBJ)="" ; INITIALIZE subject, object and predicate
- F  S ZI=$O(@ZDOM@(1,"C",ZI)) Q:ZI=""  D  ;
- . ; -- we are skipping any child that is not rdf:Description
- . ; -- TODO: check to see if this is right in general
- . ;
- . IF $G(@ZDOM@(1,"C",ZI))'="rdf:Description" D  Q  ;
- . . W !,"SKIPPING NODE: ",ZI
- . ; -- now looking for the subject for the triples
- . S ZX=$G(@ZDOM@(ZI,"A","rdf:about"))
- . I ZX'="" D  ; we have the subject
- . . ;W " about: ",ZX
- . . S C0XSUB=ZX
- . E  D  ;
- . . S ZX=$G(@ZDOM@(ZI,"A","rdf:nodeID")) ; node id is another style of subject
- . . I ZX'="" D  ;
- . . . S C0XSUB=ZX
- . I C0XSUB="" S C0XSUB=$$ANONS ; DEFAULT TO BLANK SUBJECT
- . ; 
- . ; -- we now have the subject. the children of this node have the rest
- . ;
- . S ZJ="" ; for the children of the rdf:Description nodes
- . F  S ZJ=$O(@ZDOM@(ZI,"C",ZJ)) Q:ZJ=""  D  ; for each child
- . . S C0XPRE=@ZDOM@(ZJ) ; the predicate without a prefix
- . . S ZX=$G(@ZDOM@(ZJ,"A","xmlns")) ; name space
- . . I ZX'="" S C0XPRE=ZX_C0XPRE ; add the namespace prefix
- . . I C0XPRE[":" D  ; expand using vocabulary
- . . . N ZB,ZA
- . . . S ZB=$P(C0XPRE,":",1)
- . . . S ZA=$P(C0XPRE,":",2)
- . . . I $G(C0XVOC(ZB))'="" D  ;
- . . . . S C0XPRE=C0XVOC(ZB)_ZA ; expanded 
- . . S ZY=$G(@ZDOM@(ZJ,"A","rdf:resource")) ; potential object
- . . I ZY'="" D  Q ; 
- . . . S C0XOBJ=ZY ; object
- . . . D ADD2(ZGRF,C0XSUB,C0XPRE,C0XOBJ) ; finally. our first real triple
- . . ; -- this is an else because of the quit above
- . . S ZX=$G(@ZDOM@(ZJ,"A","rdf:nodeID")) ; fishing for nodeId object
- . . I ZX'="" D  Q  ; got one
- . . . S C0XOBJ=ZX ; we are using the incoming nodeIDs as object/subject 
- . . . ; without change... this could be foolish .. look at it again later
- . . . D ADD2(ZGRF,C0XSUB,C0XPRE,C0XOBJ) ; go for it and add a node
- . . S C0XOBJ=$G(@ZDOM@(ZJ,"T",1)) ; hopefully an object is here
- . . I C0XOBJ="" D  Q  ; not a happy situation
- . . . W !,"ERROR, NO OBJECT FOUND FOR NODE: ",ZJ
- . . D ADD2(ZGRF,C0XSUB,C0XPRE,C0XOBJ) ; go for it and add a node
  S C0XTRP=$$NOW^XLFDT ; PARSE COMPLETE
  W !,"TRIPLES COMPLETE AT ",C0XTRP
@@ -419,10 +325,10 @@
  S C0XDIFF=$$FMDIFF^XLFDT(C0XINS,C0XTRP,2)
  W !," ELAPSED TIME: ",C0XDIFF," SECONDS"
- W !," APPROXIMATELY ",$P(C0XCNT/C0XDIFF,".")," NODES PER SECOND"
+ I C0XDIFF'=0 W !," APPROXIMATELY ",$P(C0XCNT/C0XDIFF,".")," NODES PER SECOND"
  S C0XEND=$$NOW^XLFDT
  W !," ENDED AT: ",C0XEND
  S C0XDIFF=$$FMDIFF^XLFDT(C0XEND,C0XSTART,2)
  W !," ELAPSED TIME: ",C0XDIFF," SECONDS"
- W !," APPROXIMATELY ",$P(C0XCNT/C0XDIFF,".")," TRIPLES PER SECOND"
+ I C0XDIFF'=0 W !," APPROXIMATELY ",$P(C0XCNT/C0XDIFF,".")," TRIPLES PER SECOND"
  Q
  ;
@@ -432,5 +338,5 @@
  ;
 ANONS() ; RETURNS AN ANONOMOUS SUBJECT
- Q "_S:"_$$LKY9
+ Q "_:S"_$$LKY9
  ;
 NEWG(NGRAPH,NMETA) ; CREATES A NEW META GRAPH, MARKS IT AS UNFINISHED
@@ -442,33 +348,4 @@
  ;
 ADD(ZG,ZS,ZP,ZO,FARY) ; ADD A TRIPLE TO THE TRIPLESTORE. ALL VALUES ARE TEXT
- ; THE FDA IS SET UP BUT THE FILES ARE NOT UPDATED. CALL UPDIE TO COMPLETE
- I '$D(FARY) D  ;
- . D INITFARY("C0XFARY")
- . S FARY="C0XFARY"
- D USEFARY(FARY)
- I '$D(C0XCNT) S C0XCNT=0
- N ZNODE
- S ZNODE="N"_$$LKY17
- N ZNARY ; GET READY TO CALL IENOFA
- S ZNARY("ZG",ZG)=""
- S ZNARY("ZS",ZS)=""
- S ZNARY("ZP",ZP)=""
- S ZNARY("ZO",ZO)=""
- D IENOFA(.ZIENS,.ZNARY,FARY) ; RESOLVE/ADD STRINGS
- ;S ZGIEN=$$IENOF(ZG) ; LAYGO TO GET IEN
- ;S ZSIEN=$$IENOF(ZS)
- ;S ZPIEN=$$IENOF(ZP)
- ;S ZOIEN=$$IENOF(ZO)
- ;I $D(C0XFDA) D UPDIE ; ADD THE STRINGS IF NEEDED
- S C0XCNT=C0XCNT+1
- S C0XFDA(C0XTFN,"?+"_C0XCNT_",",.01)=ZNODE
- S C0XFDA(C0XTFN,"?+"_C0XCNT_",",.02)=$O(ZIENS("IEN","ZG",""))
- S C0XFDA(C0XTFN,"?+"_C0XCNT_",",.03)=$O(ZIENS("IEN","ZS",""))
- S C0XFDA(C0XTFN,"?+"_C0XCNT_",",.04)=$O(ZIENS("IEN","ZP",""))
- S C0XFDA(C0XTFN,"?+"_C0XCNT_",",.05)=$O(ZIENS("IEN","ZO",""))
- ; REMEMBER TO CALL UPDIE WHEN YOU'RE DONE
- Q
- ;
-ADD2(ZG,ZS,ZP,ZO,FARY) ; ADD A TRIPLE TO THE TRIPLESTORE. ALL VALUES ARE TEXT
  ; THE FDA IS SET UP BUT THE FILES ARE NOT UPDATED. CALL UPDIE TO COMPLETE
  I '$D(FARY) D  ;
@@ -637,4 +514,12 @@
  Q
  ;
+SWUPDIE(ZFDA) ; SWITCH BETWEEN UPDIE AND BULKLOAD
+ . I $G(BLKLOAD) D  ; bulk load
+ . . D BULKLOAD(.ZFDA) ; bulk load the batch
+ . E  D  ; no bulk load
+ . . D UPDIE(.ZFDA)
+ . K ZFDA
+ Q
+ ; 
 UPDIE(ZFDA) ; INTERNAL ROUTINE TO CALL UPDATE^DIE AND CHECK FOR ERRORS
  ; ZFDA IS PASSED BY REFERENCE
