r4870: *** empty log message ***
authorKevin M. Rosenberg <kevin@rosenberg.net>
Wed, 7 May 2003 21:57:10 +0000 (21:57 +0000)
committerKevin M. Rosenberg <kevin@rosenberg.net>
Wed, 7 May 2003 21:57:10 +0000 (21:57 +0000)
Makefile [new file with mode: 0644]
class-support.lisp
create-sql.lisp [new file with mode: 0644]
data-structures.lisp
parse-2002.lisp
parse-common.lisp
parse-macros.lisp
run-tests.lisp [new file with mode: 0644]
sql-create.lisp [deleted file]
umlisp.asd

diff --git a/Makefile b/Makefile
new file mode 100644 (file)
index 0000000..dfa7a11
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,32 @@
+.PHONY: all clean test test-acl test-sbcl
+
+test-file:=`pwd`/run-tests.lisp
+all:
+
+clean:
+       @find . -type f -name "*.fasl*" -or -name "*.ufsl" -or -name "*.x86f" \
+         -or -name "*.fas" -or -name "*.pfsl" -or -name "*.dfsl" \
+         -or -name "*~" -or -name ".#*" -or -name "#*#" | xargs rm -f
+
+test: test-alisp
+
+test-alisp:
+       alisp8 -q -L $(test-file)
+
+test-mlisp:
+       mlisp -q -L $(test-file)
+
+test-sbcl:
+       sbcl --noinform --disable-debugger --userinit $(test-file)
+
+test-cmucl:
+       lisp -init $(test-file)
+
+test-lw:
+       lw-console -init $(test-file)
+
+test-scl: 
+       scl -init $(test-file)
+
+test-clisp: 
+       clisp -norc -q -i $(test-file)
index 4b88f846d2bc8b2820f0edd47646d619ea2778e6..f78ed7e02e84822bb0f2d97de7435796cf8ea74c 100644 (file)
@@ -2,12 +2,12 @@
 ;;;; *************************************************************************
 ;;;; FILE IDENTIFICATION
 ;;;;
-;;;; Name:          classes-support.lisp
-;;;; Purpose:       Class defintions for UMLisp
-;;;; Programmer:    Kevin M. Rosenberg
-;;;; Date Started:  Apr 2000
+;;;; Name:         classes-support.lisp
+;;;; Purpose:      Support for UMLisp classes
+;;;; Author:       Kevin M. Rosenberg
+;;;; Date Started: Apr 2000
 ;;;;
-;;;; $Id: class-support.lisp,v 1.3 2003/05/06 02:15:41 kevin Exp $
+;;;; $Id: class-support.lisp,v 1.4 2003/05/07 21:57:06 kevin Exp $
 ;;;;
 ;;;; This file, part of UMLisp, is
 ;;;;    Copyright (c) 2000-2002 by Kevin M. Rosenberg, M.D.
diff --git a/create-sql.lisp b/create-sql.lisp
new file mode 100644 (file)
index 0000000..e81440d
--- /dev/null
@@ -0,0 +1,311 @@
+;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10; Package: umlisp -*-
+;;;; *************************************************************************
+;;;; FILE IDENTIFICATION
+;;;;
+;;;; Name:          sql-create
+;;;; Purpose:       Create SQL database for UMLisp
+;;;; Author:        Kevin M. Rosenberg
+;;;; Date Started:  Apr 2000
+;;;;
+;;;; $Id: create-sql.lisp,v 1.1 2003/05/07 21:57:06 kevin Exp $
+;;;;
+;;;; This file, part of UMLisp, is
+;;;;    Copyright (c) 2000-2002 by Kevin M. Rosenberg, M.D.
+;;;;
+;;;; UMLisp users are granted the rights to distribute and use this software
+;;;; as governed by the terms of the GNU General Public License.
+;;;; *************************************************************************
+
+(in-package #:umlisp)
+
+(eval-when (:compile-toplevel)
+  (declaim (optimize (speed 3) (safety 1) (compilation-speed 0) (debug 3))))
+
+(defun create-table-cmd (file)
+  "Return sql command to create a table"
+  (let ((col-func 
+        (lambda (c) 
+          (let ((sqltype (sqltype c)))
+            (concatenate 'string
+                         (col c)
+                         " "
+                         (if (or (string-equal sqltype "VARCHAR")
+                                 (string-equal sqltype "CHAR"))
+                             (format nil "~a (~a)" sqltype (cmax c))
+                             sqltype))))))
+    (format nil "CREATE TABLE ~a (~{~a~^,~})" (table file)
+           (mapcar col-func (ucols file)))))
+
+(defun create-custom-table-cmd (tablename sql-cmd)
+  "Return SQL command to create a custom table"
+  (format nil "CREATE TABLE ~a AS ~a;" tablename sql-cmd))
+
+(defun insert-col-value (col value)
+  (if (null (parse-fun col)) 
+      value
+      (format nil "~A" (funcall (parse-fun col) value))))
+
+(defun insert-values-cmd (file values)
+  "Return sql insert command for a row of values"  
+  (let ((insert-func
+        (lambda (col value)
+          (let ((q (quotechar col)))
+            (concatenate 'string q (insert-col-value col value) q)))))
+    (format
+     nil "INSERT INTO ~a (~{~a~^,~}) VALUES (~A)"
+     (table file)
+     (fields file)
+     (concat-separated-strings
+      "," 
+      (mapcar insert-func (remove-custom-cols (ucols file)) values)
+      (custom-col-values (custom-ucols-for-file file) values t)))))
+
+
+(defun custom-col-value (col values doquote)
+  (let ((custom-value (funcall (custom-value-fun col) values)))
+    (if custom-value
+       (if doquote
+           (let ((q (quotechar col)))
+             (concatenate 'string q (escape-backslashes custom-value) q))
+           (escape-backslashes custom-value))
+       "")))
+
+(defun custom-col-values (ucols values doquote)
+  "Returns a list of string column values for SQL inserts for custom columns"
+  (loop for col in ucols collect (custom-col-value col values doquote)))
+
+(defun remove-custom-cols (cols)
+  "Remove custom cols from a list col umls-cols"
+  (remove-if #'custom-value-fun cols))
+
+(defun find-custom-cols-for-filename (filename)
+  (remove-if-not (lambda (x) (string-equal filename (car x))) +custom-cols+))
+
+(defun find-custom-col (filename col)
+  (find-if (lambda (x) (and (string-equal filename (car x))
+                           (string-equal col (cadr x)))) +custom-cols+))
+
+(defun custom-colnames-for-filename (filename)
+  (mapcar #'cadr (find-custom-cols-for-filename filename)))
+
+(defun custom-ucols-for-file (file)
+  (remove-if-not #'custom-value-fun (ucols file)))
+
+(defun noneng-lang-index-files ()
+  (remove-if-not
+   (lambda (f) (and (> (length (fil f)) 4)
+                   (string-equal (fil f) "MRXW." :end1 5) 
+                   (not (string-equal (fil f) "MRXW.ENG"))
+                   (not (string-equal (fil f) "MRXW.NONENG"))))
+   *umls-files*))
+
+;;; SQL Command Functions
+
+(defun create-index-cmd (colname tablename length)
+  "Return sql create index command"
+  (format nil "CREATE INDEX ~a ON ~a (~a ~a)"
+         (concatenate 'string tablename "_" colname "_X")
+         tablename colname
+         (if (integerp length) (format nil "(~d)" length) "")))
+
+(defun create-all-tables-cmdfile ()
+  "Return sql commands to create all tables. Not need for automated SQL import"
+  (mapcar (lambda (f) (format nil "~a~%~%" (create-table-cmd f))) *umls-files*))
+
+;; SQL Execution functions
+
+(defun sql-drop-tables (conn)
+  "SQL Databases: drop all tables"
+  (dolist (file *umls-files*)
+    (ignore-errors 
+      (sql-execute (format nil "DROP TABLE ~a" (table file)) conn))))
+
+(defun sql-create-tables (conn)
+  "SQL Databases: create all tables" 
+  (dolist (file *umls-files*)
+    (sql-execute (create-table-cmd file) conn)))
+
+(defun sql-create-custom-tables (conn)
+  "SQL Databases: create all custom tables"
+  (dolist (ct +custom-tables+)
+    (sql-execute (create-custom-table-cmd (car ct) (cadr ct)) conn)))
+  
+(defun sql-insert-values (conn file)
+  "SQL Databases: inserts all values for a file"  
+  (with-umls-file (line (fil file))
+    (sql-execute (insert-values-cmd file line) conn)))
+
+(defun sql-insert-all-values (conn)
+  "SQL Databases: inserts all values for all files"  
+  (dolist (file *umls-files*)
+    (sql-insert-values conn file)))
+
+(defun sql-create-indexes (conn &optional (indexes +index-cols+))
+  "SQL Databases: create all indexes"
+  (dolist (idx indexes)
+    (sql-execute (create-index-cmd (car idx) (cadr idx) (caddr idx)) conn))) 
+
+(defun make-usrl (conn)
+  (sql-execute "drop table if exists USRL" conn)
+  (sql-execute "create table USRL (sab varchar(80), srl integer)" conn)
+  (dolist (tuple (mutex-sql-query
+                 "select distinct SAB,SRL from MRSO order by SAB asc"))
+    (sql-execute (format nil "insert into USRL (sab,srl) values ('~a',~d)" 
+                        (car tuple) (ensure-integer (cadr tuple)))
+                conn)))
+
+(defun sql-create-special-tables (conn)
+  (make-usrl conn))
+
+(defun create-umls-db-by-insert ()
+  "SQL Databases: initializes entire database via SQL insert commands"
+  (ensure-init-umls)
+  (init-hash-table)
+  (with-sql-connection (conn)
+    (sql-drop-tables conn)
+    (sql-create-tables conn)
+    (sql-insert-all-values conn)
+    (sql-create-indexes conn)
+    (sql-create-custom-tables conn)
+    (sql-create-indexes conn +custom-index-cols+)
+    (sql-create-special-tables conn)))
+
+(defun create-umls-db (&optional (extension ".trans") 
+                      (copy-cmd #'mysql-copy-cmd))
+  "SQL Databases: initializes entire database via SQL copy commands. 
+This is much faster that using create-umls-db-insert."
+  (ensure-init-umls)
+  (init-hash-table)
+  (translate-all-files extension)
+  (with-sql-connection (conn)
+    (sql-drop-tables conn)
+    (sql-create-tables conn)
+    (dolist (file *umls-files*)
+      (sql-execute (funcall copy-cmd file extension) conn))
+    (sql-create-indexes conn)
+    (sql-create-custom-tables conn)
+    (sql-create-indexes conn +custom-index-cols+)
+    (sql-create-special-tables conn)))
+
+(defun translate-all-files (&optional (extension ".trans"))
+  "Copy translated files and return postgresql copy commands to import"
+  (make-noneng-index-file extension)
+  (dolist (f *umls-files*) (translate-umls-file f extension)))
+
+(defun translate-umls-file (file extension)
+  "Translate a umls file into a format suitable for sql copy cmd"
+  (translate-files file extension (list file)))
+
+(defun make-noneng-index-file (extension)
+  "Make non-english index file"
+  (translate-files (find-ufile "MRXW.NONENG")
+                  extension (noneng-lang-index-files)))
+
+(defun translate-files (out-ufile extension input-ufiles)
+  "Translate a umls file into a format suitable for sql copy cmd"
+  (let ((output-path (umls-pathname (fil out-ufile) extension)))
+    (if (probe-file output-path)
+       (format t "File ~A already exists: skipping~%" output-path)
+      (with-open-file (ostream output-path :direction :output)
+       (dolist (input-ufile input-ufiles)
+         (with-umls-file (line (fil input-ufile))
+           (translate-line out-ufile line ostream)
+           (princ #\newline ostream)))))))
+
+(defun translate-line (file line strm)
+  "Translate a single line for sql output"
+  (flet ((col-value (col value)
+          (if (eq (datatype col) 'sql-u)
+              (let ((ui (parse-ui value "")))
+                (if (stringp ui)
+                    ui
+                    (write-to-string ui)))
+              (escape-backslashes value))))
+    (print-separated-strings
+     strm "|" 
+     (mapcar #'col-value (remove-custom-cols (ucols file)) line)
+     (custom-col-values (custom-ucols-for-file file) line nil))))
+
+(defun pg-copy-cmd (file extension)
+  "Return postgresql copy statement for a file"  
+  (format
+   nil "COPY ~a FROM '~a' using delimiters '|' with null as ''"
+   (table file) (umls-pathname (fil file) extension)))
+
+(defun mysql-copy-cmd (file extension)
+  "Return mysql copy statement for a file"  
+  (format
+   nil
+   "LOAD DATA LOCAL INFILE \"~a\" INTO TABLE ~a FIELDS TERMINATED BY \"|\""
+   (umls-pathname (fil file) extension) (table file)))
+
+   
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;;;
+;;; Routines for analyzing cost of fixed size storage
+;;;
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+
+(defun umls-fixed-size-waste ()
+  "Display storage waste if using all fixed size storage"
+  (let ((totalwaste 0)
+       (totalunavoidable 0)
+       (totalavoidable 0)
+       (unavoidable '())
+       (avoidable '()))
+    (dolist (file *umls-files*)
+      (dolist (col (ucols file))
+       (let* ((avwaste (- (cmax col) (av col)))
+              (cwaste (* avwaste (rws file))))
+         (when (plusp cwaste)
+           (if (<= avwaste 6)
+               (progn
+                 (incf totalunavoidable cwaste)
+                 (push (list (fil file) (col col)
+                             avwaste cwaste)
+                       unavoidable))
+               (progn
+                 (incf totalavoidable cwaste)
+                 (push (list (fil file) (col col)
+                             avwaste cwaste)
+                       avoidable)))
+           (incf totalwaste cwaste)))))
+    (values totalwaste totalavoidable totalunavoidable
+           (nreverse avoidable) (nreverse unavoidable))))
+
+(defun display-waste ()
+  (ensure-init-umls)
+  (multiple-value-bind (tw ta tu al ul) (umls-fixed-size-waste)
+    (format t "Total waste: ~d~%" tw)
+    (format t "Total avoidable: ~d~%" ta)
+    (format t "Total unavoidable: ~d~%" tu)
+    (format t "Avoidable:~%")
+    (dolist (w al)
+      (format t "  (~a,~a): ~a,~a~%" (car w) (cadr w) (caddr w) (cadddr w)))
+    (format t "Unavoidable:~%")
+    (dolist (w ul)
+      (format t "  (~a,~a): ~a,~a~%" (car w) (cadr w) (caddr w) (cadddr w)))
+    ))
+
+(defun max-umls-field ()
+  "Return length of longest field"
+  (declare (optimize (speed 3) (space 0)))
+  (ensure-init-umls)
+  (let ((max 0))
+    (declare (fixnum max))
+    (dolist (ucol *umls-cols*)
+      (when (> (cmax ucol) max)
+       (setq max (cmax ucol))))
+    max))
+
+(defun max-umls-row ()
+  "Return length of longest row"
+  (declare (optimize (speed 3) (space 0)))
+  (ensure-init-umls)
+  (let ((rowsizes '()))
+    (dolist (file *umls-files*)
+      (let ((row 0))
+       (dolist (ucol (ucols file))
+         (incf row (1+ (cmax ucol))))
+       (push row rowsizes)))
+    (car (sort rowsizes #'>))))
index 3500ba419a002b41b74e28c10bc5fab8c7b3fb19..dfa28458383cb94597026ca5ea72e857093d49d4 100644 (file)
@@ -7,7 +7,7 @@
 ;;;; Author:        Kevin M. Rosenberg
 ;;;; Date Started:  Apr 2000
 ;;;;
-;;;; $Id: data-structures.lisp,v 1.9 2003/05/06 07:20:31 kevin Exp $
+;;;; $Id: data-structures.lisp,v 1.10 2003/05/07 21:57:06 kevin Exp $
 ;;;;
 ;;;; This file, part of UMLisp, is
 ;;;;    Copyright (c) 2000-2002 by Kevin M. Rosenberg, M.D.
@@ -81,7 +81,7 @@
    (sqltype :initarg :sqltype :accessor sqltype)
    (dty :initarg :dty :accessor dty :documentation "new in 2002: suggested SQL datatype")
    (parse-fun :initarg :parse-fun :accessor parse-fun)
-   (quotechar :initarg :quotechar :accessor quotechar)
+   (quote-str :initarg :quote-str :accessor quote-str)
    (datatype :initarg :datatype :accessor datatype)
    (custom-value-fun :initarg :custom-value-fun :accessor custom-value-fun))
   (:default-initargs :col nil :des nil :ref nil :min nil :av nil :max nil :fil nil
index 4024eb2d32a824d5646c16ea2d0a785a746a9353..726764fd63b112f4d5280b5115eab0ff0b3a16fe 100644 (file)
@@ -8,7 +8,7 @@
 ;;;; Author:        Kevin M. Rosenberg
 ;;;; Date Started:  Apr 2000
 ;;;;
-;;;; $Id: parse-2002.lisp,v 1.9 2003/05/06 07:55:15 kevin Exp $
+;;;; $Id: parse-2002.lisp,v 1.10 2003/05/07 21:57:06 kevin Exp $
 ;;;;
 ;;;; This file, part of UMLisp, is
 ;;;;    Copyright (c) 2000-2002 by Kevin M. Rosenberg, M.D.
 
 ;; File & Column functions
 
-(defun init-umls (&optional (alwaysclear nil))
-"Initialize all UMLS file and column structures if not already initialized"
-  (when (or alwaysclear (null *umls-files*))
-    (init-umls-cols)
-    (init-umls-files)
-    (init-field-lengths)))
+(defun gen-ucols ()
+  (add-ucols (gen-ucols-meta))
+  (add-ucols (gen-ucols-custom))
+  (add-ucols (gen-ucols-generic "LRFLD"))
+  (add-ucols (gen-ucols-generic "SRFLD")))
 
-(defun init-umls-cols ()
-  (setq *umls-cols* (append 
-                    (init-meta-cols)
-                    (init-custom-cols)
-                    (init-generic-cols "LRFLD")
-                    (init-generic-cols "SRFLD"))))
-
-(defun init-meta-cols ()
+(defun gen-ucols-meta ()
 "Initialize all umls columns"  
   (let ((cols '()))
     (with-umls-file (line "MRCOLS")
       (destructuring-bind (col des ref min av max fil dty) line
-       (let ((c (make-instance 'ucol
-                 :col col
-                 :des des
-                 :ref ref
-                 :min (parse-integer min)
-                 :av (read-from-string av)
-                 :max (parse-integer max)
-                 :fil fil
-                 :dty dty  ;; new in 2002 UMLS
-                 :sqltype "VARCHAR"    ; default data type
-                 :parse-fun #'add-sql-quotes
-                 :custom-value-fun nil
-                 :quotechar "'")))
-         (add-datatype-to-col c (datatype-for-col col))
-         (push c cols))))
+       (push (make-ucol col des ref (parse-integer min) (read-from-string av)
+                        (parse-integer max) fil dty)
+             cols)))
     (nreverse cols)))
 
-(defun init-custom-cols ()
+(defun gen-ucols-custom ()
 "Initialize umls columns for custom columns"  
-  (let ((cols '()))
-    (dolist (customcol +custom-cols+)
-      (let ((c (make-instance 'ucol
-                             :col (nth 1 customcol) :des "" :ref 0 :min 0 :max (nth 3 customcol)
-                             :av 0 :dty nil :fil (nth 0 customcol) :sqltype (nth 2 customcol)
-                             :parse-fun #'add-sql-quotes  :custom-value-fun (nth 4 customcol)
-                             :quotechar "'")))
-       (add-datatype-to-col c (datatype-for-col (nth 1 customcol)))
-       (push c cols)))
-    (nreverse cols)))
-
-(defun escape-column-name (name)
-  (substitute #\_ #\/ name))
+  (loop for customcol in +custom-cols+
+       collect
+       (make-ucol (nth 1 customcol) "" 0 0 0 (nth 3 customcol)
+                  (nth 0 customcol) nil :sqltype (nth 2 customcol))))
 
-(defun init-generic-cols (col-filename)
+(defun gen-ucols-generic (col-filename)
 "Initialize for generic (LEX/NET) columns"  
   (let ((cols '()))
     (with-umls-file (line col-filename)
       (destructuring-bind (nam des ref fil) line
        (setq nam (escape-column-name nam))
        (dolist (file (delimited-string-to-list fil #\,))
-         (let ((c (make-instance 'ucol       
-                 :col nam
-                 :des des
-                 :ref ref
-                 :min nil
-                 :av nil
-                 :max nil
-                 :fil file
-                 :dty nil
-                 :sqltype "VARCHAR"    ; default data type
-                 :parse-fun #'add-sql-quotes
-                 :custom-value-fun nil
-                 :quotechar "'")))
-           (add-datatype-to-col c (datatype-for-col nam))
-           (push c cols)))))
+         (push
+          (make-ucol nam des ref nil nil nil file nil)
+          cols))))
     (nreverse cols)))
 
-(defun init-umls-files ()
-  (setq *umls-files* (append
-                     (init-generic-files "MRFILES") 
-                     (init-generic-files "LRFIL") 
-                     (init-generic-files "SRFIL")))
-  ;; need to separate this since init-custom-files depends on *umls-files*
-  (setq *umls-files* (append *umls-files* (init-custom-files))))
 
+(defun gen-ufiles ()
+  (add-ufiles (gen-ufiles-generic "MRFILES"))
+  (add-ufiles (gen-ufiles-generic "LRFIL"))
+  (add-ufiles (gen-ufiles-generic "SRFIL"))
+  ;; needs to come last
+  (add-ufiles (gen-ufiles-custom)))
 
-(defun umls-field-string-to-list (fmt)
-  "Converts a comma delimited list of fields into a list of field names. Will
-append a unique number (starting at 2) onto a column name that is repeated in the list"
-  (let ((field-list (delimited-string-to-list (escape-column-name fmt) #\,))
-       (col-count (make-hash-table :test 'equal)))
-    (dotimes (i (length field-list))
-      (declare (fixnum i))
-      (let ((col (nth i field-list)))
-       (multiple-value-bind (key found) (gethash col col-count)
-         (if found
-             (let ((next-id (1+ key)))
-               (setf (nth i field-list) (concatenate 'string col (write-to-string next-id)))
-               (setf (gethash col col-count) next-id))
-           (setf (gethash col col-count) 1)))))
-    field-list))
-
-(defun init-generic-files (files-filename)
+                       
+(defun gen-ufiles-generic (files-filename)
 "Initialize all LEX file structures"  
   (let ((files '()))
-  (with-umls-file (line files-filename)
-    (destructuring-bind (fil des fmt cls rws bts) line
-      (let ((f (make-instance 'ufile 
-               :fil fil
-               :table (substitute #\_ #\. fil)
-               :des des
-               :fmt (escape-column-name fmt)
-               :cls (parse-integer cls)
-               :rws (parse-integer rws)
-               :bts (parse-integer bts)
-               :fields (concatenate 'list
-                         (umls-field-string-to-list fmt)
-                         (custom-colnames-for-filename fil)))))
-       (setf (ucols f) (ucols-for-ufile f))
-       (push f files))))
-  (nreverse files)))
-
-(defun init-custom-files ()
-  (let ((ffile (make-instance 'ufile
-                    :fil "MRXW.NONENG" :des "Custom NonEnglish Index" :table "MRXW_NONENG"
-                    :cls 5 :rws 0 :bts 0 :fields (fields (find-ufile "MRXW.ENG")))))
-    (setf (ucols ffile)
-      (ucols-for-ufile ffile))
-    (list ffile)))
-
-(defun datatype-for-col (colname)
-"Return datatype for column name"  
-  (car (cdr (find colname +col-datatypes+ :key #'car :test #'string-equal))))
+    (with-umls-file (line files-filename)
+      (destructuring-bind (fil des fmt cls rws bts) line
+       (push (make-ufile
+              fil des (substitute #\_ #\. fil) (parse-integer cls)
+              (parse-integer rws) (parse-integer bts)
+              (concatenate 'list (umls-field-string-to-list fmt)
+                           (custom-colnames-for-filename fil)))
+             files)))
+    (nreverse files)))
 
-(defun add-datatype-to-col (col datatype)
-"Add data type information to column"
-  (setf (datatype col) datatype)
-  (case datatype
-    (sql-u (setf (sqltype col) "INTEGER"
-                (parse-fun col) #'parse-ui
-                (quotechar col) ""))
-    (sql-s (setf (sqltype col) "SMALLINT" 
-                (parse-fun col) #'parse-integer
-                (quotechar col) ""))
-    (sql-l (setf (sqltype col) "BIGINT" 
-                (parse-fun col) #'parse-integer
-                (quotechar col) ""))
-    (sql-i (setf (sqltype col) "INTEGER" 
-                (parse-fun col) #'parse-integer
-                (quotechar col) ""))
-    (sql-f (setf (sqltype col) "NUMERIC" 
-                (parse-fun col) #'read-from-string
-                (quotechar col) ""))
-    (t                      ; Default column type, optimized text storage
-     (setf (parse-fun col) #'add-sql-quotes 
-          (quotechar col) "'")
-     (when (and (cmax col) (av col))
-       (if (> (cmax col) 255)
-          (setf (sqltype col) "TEXT")
-        (if (< (- (cmax col) (av col)) 4) 
-            (setf (sqltype col) "CHAR") ; if average bytes wasted < 4
-          (setf (sqltype col) "VARCHAR")))))))
+(defun gen-ufiles-custom ()
+  (make-ufile "MRXW.NONENG" "Custom NonEnglish Index" "MRXW_NONENG"
+             5 0 0 (fields (find-ufile "MRXW.ENG"))))
 
 
 
index 07e5cc9a3138ee8f0a021af7b125efb63c988ef5..d4f7922c8b6db814cd8baf86d8375a55779fdfa3 100644 (file)
@@ -7,7 +7,7 @@
 ;;;; Author:        Kevin M. Rosenberg
 ;;;; Date Started:  Apr 2000
 ;;;;
-;;;; $Id: parse-common.lisp,v 1.8 2003/05/06 07:44:07 kevin Exp $
+;;;; $Id: parse-common.lisp,v 1.9 2003/05/07 21:57:06 kevin Exp $
 ;;;;
 ;;;; This file, part of UMLisp, is
 ;;;;    Copyright (c) 2000-2002 by Kevin M. Rosenberg, M.D.
 (eval-when (:compile-toplevel)
   (declaim (optimize (speed 3) (safety 1) (compilation-speed 0) (debug 3))))
 
+(defun ensure-init-umls (&optional (alwaysclear nil))
+"Initialize all UMLS file and column structures if not already initialized"
+  (when (or alwaysclear (null *umls-files*))
+    (gen-ucols)
+    (gen-ufiles)
+    (ensure-field-lengths)))
+
+(defun add-ucols (ucols)
+  "Adds a ucol or list of ucols to *umls-cols*. Returns input value."
+  (setq *umls-cols* (append (mklist ucols) *umls-cols*))
+  ucols)
+
+(defun add-ufiles (ufiles)
+  "Adds a ufile or list of ufiles to *umls-filess*. Returns input value."
+  (setq *umls-files* (append (mklist ufiles) *umls-files*))
+  ufiles)
+
 (defun umls-pathname (filename &optional (extension ""))
-"Return pathname for a umls filename"
+"Return pathname for a umls filename with an optional extension"
   (etypecase filename
     (string
      (merge-pathnames 
 
 ;;; Find field lengths for LEX and NET files
 
-(defun file-field-lengths (files)
-  (let ((lengths '()))
-    (dolist (file files)
-      (setq file (fil file))
-      (let (max-field count-field num-fields (count-lines 0))
-       (with-umls-file (fields file)
-         (unless num-fields
-           (setq num-fields (length fields))
-           (setq max-field (make-array num-fields :element-type 'fixnum 
-                                       :initial-element 0))
-           (setq count-field (make-array num-fields :element-type 'number
-                                         :initial-element 0)))
-         (dotimes (i (length fields))
-           (declare (fixnum i))
-           (let ((len (length (nth i fields))))
-             (incf (aref count-field i) len)
-             (when (> len (aref max-field i))
-               (setf (aref max-field i) len))))
-         (incf count-lines))
-       (dotimes (i num-fields)
-         (setf (aref count-field i) (float (/ (aref count-field i) count-lines))))
-       (push (list file max-field count-field) lengths)))
-    (nreverse lengths)))
-
-(defun init-field-lengths ()
+(defun ensure-field-lengths ()
   "Initial colstruct field lengths for files that don't have a measurement.
 Currently, these are the LEX and NET files."
-  (let ((measure-files '()))
-    (dolist (file *umls-files*)
-      (let ((filename (fil file)))
-       (unless (or (char= #\M (char filename 0))
-                   (char= #\m (char filename 0)))
-         (push file measure-files))))
-    (let ((length-lists (file-field-lengths measure-files)))
-      (dolist (length-list length-lists)
-       (let* ((filename (car length-list))
-              (max-field (cadr length-list))
-              (av-field (caddr length-list))
-              (file (find-ufile filename)))
-         (when file
-           (if (/= (length max-field) (length (fields file)))
-               (format t "Warning: Number of file fields ~A doesn't match length of fields in file structure ~S" 
-                      max-field file)
-             (dotimes (i (max (length max-field) (length (fields file))))
-               (declare (fixnum i))
-               (let* ((field (nth i (fields file)))
-                      (col (find-ucol field filename)))
-                 (if col
-                     (progn
-                       (setf (cmax col) (aref max-field i))
-                       (setf (av col) (aref av-field i))
-                       (add-datatype-to-col col (datatype-for-col (col col))))
-                 (error "can't find column ~A" field)))))))))))
+  (dolist (length-list (ufiles-field-lengths (ufiles-to-measure)))
+    (destructuring-bind (filename fields-max fields-av) length-list
+      (let ((file (find-ufile filename)))
+       (unless file
+         (error "Can't find ~A filename in ufiles"))
+       (unless (= (length fields-max) (length (fields file)))
+         (error
+          "Number of file fields ~A not equal to field count in ufile ~S" 
+          fields-max file))
+       (dotimes (i (length (fields file)))
+         (declare (fixnum i))
+         (let* ((field (nth i (fields file)))
+                (col (find-ucol field filename)))
+           (unless col
+               (error "can't find column ~A" field))
+           (setf (cmax col) (aref fields-max i))
+           (setf (av col) (aref fields-av i))
+           (ensure-ucol-datatype col (datatype-for-colname (col col)))))))))
   
-
+(defun ufiles-to-measure ()
+  "Returns a list of ufiles to measure"
+  (loop for ufile in *umls-files*
+       unless (or (char= #\M (char (fil ufile) 0))
+                  (char= #\m (char (fil ufile) 0)))
+       collect ufile))
+    
+  
+(defun ufiles-field-lengths (ufiles)
+  "Returns a list of lists of containing (FILE MAX AV)"
+  (loop for ufile in ufiles collect (file-field-lengths (fil ufile))))
+
+(defun file-field-lengths (filename)
+  "Returns a list of FILENAME MAX AV"
+  (let (fields-max fields-av num-fields (count-lines 0))
+    (with-umls-file (line filename)
+      (unless num-fields
+       (setq num-fields (length line))
+       (setq fields-max (make-array num-fields :element-type 'fixnum 
+                                    :initial-element 0))
+       (setq fields-av (make-array num-fields :element-type 'number
+                                   :initial-element 0)))
+      (dotimes (i num-fields)
+       (declare (fixnum i))
+       (let ((len (length (nth i line))))
+         (incf (aref fields-av i) len)
+         (when (> len (aref fields-max i))
+           (setf (aref fields-max i) len))))
+      (incf count-lines))
+    (dotimes (i num-fields)
+      (setf (aref fields-av i) (float (/ (aref fields-av i) count-lines))))
+    (list filename fields-max fields-av)))
 
 ;;; UMLS column/file functions
 
-(defun find-col-in-columns (colname filename cols)
+(defun find-ucol-of-colname (colname filename ucols)
 "Returns list of umls-col structure for a column name and a filename"
-  (dolist (col cols)
-    (when (and (string-equal filename (fil col))
-              (string-equal colname (col col)))
-      (return-from find-col-in-columns col)))
-  nil)
-
-(defun find-or-make-col-in-columns (colname filename cols)
-  (let ((col (find-col-in-columns colname filename cols)))
-    (if col
-       col
-      ;; try to find column name without a terminal digit
-      (let* ((last-char (char colname (1- (length colname))))
-            (digit (- (char-code last-char) (char-code #\0))))
-       (if (and (>= digit 0) (<= digit 9))
-           (let ((base-colname (subseq colname 0 (1- (length colname)))))
-             (setq col (find-col-in-columns base-colname filename cols))
-             (if col
-                 (let ((new-col (make-instance 'ucol
-                                 :col (copy-seq colname)
-                                 :des (copy-seq (des col))
-                                 :ref (copy-seq (ref col))
-                                 :min (cmin col)
-                                 :max (cmax col)
-                                 :fil (copy-seq (fil col))
-                                 :sqltype (copy-seq (sqltype col))
-                                 :dty (copy-seq (dty col))
-                                 :parse-fun (parse-fun col)
-                                 :quotechar (copy-seq (quotechar col))
-                                 :datatype (datatype col)
-                                 :custom-value-fun (custom-value-fun col))))
-                   (push new-col *umls-cols*)
-                   new-col)
-               (error "Couldn't find a base column for col ~A in file ~A"
-                      colname filename)))
-         (let ((new-col (make-instance 'ucol
-                         :col (copy-seq colname)
-                         :des "Unknown"
-                         :ref ""
-                         :min nil
-                         :max nil
-                         :fil filename
-                         :sqltype "VARCHAR"
-                         :dty nil
-                         :parse-fun #'add-sql-quotes
-                         :quotechar "'"
-                         :datatype nil
-                         :custom-value-fun nil)))
-           (push new-col *umls-cols*)
-           new-col))))))
+  (dolist (ucol ucols nil)
+    (when (and (string-equal filename (fil ucol))
+              (string-equal colname (col ucol)))
+      (return-from find-ucol-of-colname ucol))))
+
+(defun ensure-col-in-columns (colname filename ucols)
+  (aif (find-ucol-of-colname colname filename ucols)
+       it
+       (add-ucols (make-ucol-for-column colname filename ucols))))
+
+(defun make-ucol-for-column (colname filename ucols)
+  ;; try to find column name without a terminal digit
+  (let* ((len (length colname))
+        (last-digit? (digit-char-p (char colname (1- len))))
+        (base-colname (if last-digit?
+                          (subseq colname 0 (1- len))
+                          colname))
+        (ucol (when last-digit?
+                (find-ucol-of-colname base-colname filename ucols))))
+    (when (and last-digit? (null ucol))
+      (error "Couldn't find a base column for col ~A in file ~A"
+            colname filename))
+    (copy-or-new-ucol colname filename ucol)))
+
+(defun copy-or-new-ucol (colname filename ucol)
+  (if ucol
+      (make-instance
+       'ucol
+       :col (copy-seq colname) :des (copy-seq (des ucol)) :ref (copy-seq (ref ucol))
+       :min (cmin ucol) :max (cmax ucol) :fil (copy-seq (fil ucol))
+       :sqltype (copy-seq (sqltype ucol)) :dty (copy-seq (dty ucol))
+       :parse-fun (parse-fun ucol) :quote-str (copy-seq (quote-str ucol))
+       :datatype (datatype ucol) :custom-value-fun (custom-value-fun ucol))
+      (make-empty-ucol colname filename)))
+
+(defun make-ucol (col des ref min av max fil dty
+                 &key (sqltype "VARCHAR") (parse-fun #'add-sql-quotes)
+                 (quote-str "'") (custom-value-fun))
+  (let ((ucol (make-instance
+              'ucol
+              :col col :des des :ref ref :min min :av av :max max :fil fil
+              :dty dty :sqltype sqltype :parse-fun parse-fun
+              :quote-str quote-str :custom-value-fun custom-value-fun)))
+    (ensure-ucol-datatype ucol (datatype-for-colname col))
+    ucol))
+
+(defun make-empty-ucol (colname filename)
+  (make-ucol (copy-seq colname) "Unknown" "" nil nil nil filename nil))
 
 (defun find-ucol (colname filename)
   "Returns list of umls-col structure for a column name and a filename"
-  (find-or-make-col-in-columns colname filename *umls-cols*))
+  (ensure-col-in-columns colname filename *umls-cols*))
 
 (defun find-ufile (filename)
   "Returns umls-file structure for a filename"  
-  (find-if (lambda (f) (string-equal filename (fil f))) *umls-files*))
-
-(defun ucols-for-ufile (file)
-  "Returns list of umls-cols for a file structure"  
-  (let ((filename (fil file)))
-    (mapcar (lambda (col) (find-ucol col filename))
-           (fields file))))
-
-
+  (find-if #'(lambda (f) (string-equal filename (fil f))) *umls-files*))
+
+(defun find-ucols-for-filename (filename)
+  "Returns list of umls-cols for a file structure"
+  (loop for colname in (fields (find-ufile filename))
+       collect (find-ucol colname filename)))
+
+(defun umls-field-string-to-list (fmt)
+  "Converts a comma delimited list of fields into a list of field names. Will
+append a unique number (starting at 2) onto a column name that is repeated in the list"
+  (let ((col-counts (make-hash-table :test 'equal)))
+    (loop for colname in (delimited-string-to-list (escape-column-name fmt) #\,)
+         collect
+         (multiple-value-bind (value found) (gethash col col-counts)
+           (cond
+             (found
+               (incf (gethash col col-counts))
+               (concatenate 'string colname (write-to-string (1+ value))))
+             (t
+              (setf (gethash col col-counts) 1)
+              colname))))))
+
+(defun make-ufile (fil des table cls rws bts fields)
+  (let ((ufile
+        (make-instance
+         'ufile :fil fil :des des :table table :cls cls :rws rws :bts bts
+         :fields fields)))
+    (setf (ucols ufile) (find-ucols-for-filename fil))
+    ufile))
+
+(defun datatype-for-colname (colname)
+"Return datatype for column name"  
+  (second (find colname +col-datatypes+ :key #'car :test #'string-equal)))
+
+(defun ensure-ucol-datatype (col datatype)
+"Add data type information to column"
+  (setf (datatype col) datatype)
+  (case datatype
+    (sql-u (setf (sqltype col) "INTEGER"
+                (parse-fun col) #'parse-ui
+                (quote-str col) ""))
+    (sql-s (setf (sqltype col) "SMALLINT" 
+                (parse-fun col) #'parse-integer
+                (quote-str col) ""))
+    (sql-l (setf (sqltype col) "BIGINT" 
+                (parse-fun col) #'parse-integer
+                (quote-str col) ""))
+    (sql-i (setf (sqltype col) "INTEGER" 
+                (parse-fun col) #'parse-integer
+                (quote-str col) ""))
+    (sql-f (setf (sqltype col) "NUMERIC" 
+                (parse-fun col) #'read-from-string
+                (quote-str col) ""))
+    (t                      ; Default column type, optimized text storage
+     (setf (parse-fun col) #'add-sql-quotes 
+          (quote-str col) "'")
+     (when (and (cmax col) (av col))
+       (if (> (cmax col) 255)
+          (setf (sqltype col) "TEXT")
+        (if (< (- (cmax col) (av col)) 4) 
+            (setf (sqltype col) "CHAR") ; if average bytes wasted < 4
+          (setf (sqltype col) "VARCHAR")))))))
+
+(defun escape-column-name (name)
+  (substitute #\_ #\/ name))
index fbc2c24029af037d69d37a6901d8e28ded945cfb..cd7e5017346c594ad5556015c4f890e01e2c27ce 100644 (file)
@@ -2,12 +2,12 @@
 ;;;; *************************************************************************
 ;;;; FILE IDENTIFICATION
 ;;;;
-;;;; Name:          parse-macros.lisp
-;;;; Purpose:       Macros for UMLS file parsing
-;;;; Programmer:    Kevin M. Rosenberg
-;;;; Date Started:  Apr 2000
+;;;; Name:         parse-macros.lisp
+;;;; Purpose:      Macros for UMLS file parsing
+;;;; Author:       Kevin M. Rosenberg
+;;;; Date Started: Apr 2000
 ;;;;
-;;;; $Id: parse-macros.lisp,v 1.6 2003/05/06 06:09:29 kevin Exp $
+;;;; $Id: parse-macros.lisp,v 1.7 2003/05/07 21:57:06 kevin Exp $
 ;;;;
 ;;;; This file, part of UMLisp, is
 ;;;;    Copyright (c) 2000-2002 by Kevin M. Rosenberg, M.D.
diff --git a/run-tests.lisp b/run-tests.lisp
new file mode 100644 (file)
index 0000000..c9bbd31
--- /dev/null
@@ -0,0 +1,27 @@
+(defpackage #:run-tests (:use #:cl))
+(in-package #:run-tests)
+
+(require 'rt)
+(require 'kmrcl)
+(require 'clsql-mysql)
+(require 'clsql)
+(require 'hyperobject)
+(load "umlisp.asd")
+(asdf:oos 'asdf:test-op 'umlisp)
+
+(defun quit (&optional (code 0))
+  "Function to exit the Lisp implementation. Copied from CLOCC's QUIT function."
+    #+allegro (excl:exit code)
+    #+clisp (#+lisp=cl ext:quit #-lisp=cl lisp:quit code)
+    #+(or cmu scl) (ext:quit code)
+    #+cormanlisp (win32:exitprocess code)
+    #+gcl (lisp:bye code)
+    #+lispworks (lw:quit :status code)
+    #+lucid (lcl:quit code)
+    #+sbcl (sb-ext:quit :unix-status (typecase code (number code) (null 0) (t 1)))
+    #+mcl (ccl:quit code)
+    #-(or allegro clisp cmu scl cormanlisp gcl lispworks lucid sbcl mcl)
+    (error 'not-implemented :proc (list 'quit code)))
+
+(quit)
+
diff --git a/sql-create.lisp b/sql-create.lisp
deleted file mode 100644 (file)
index 49f550e..0000000
+++ /dev/null
@@ -1,312 +0,0 @@
-;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10; Package: umlisp -*-
-;;;; *************************************************************************
-;;;; FILE IDENTIFICATION
-;;;;
-;;;; Name:          sql-create
-;;;; Purpose:       Create SQL database for UMLisp
-;;;; Author:        Kevin M. Rosenberg
-;;;; Date Started:  Apr 2000
-;;;;
-;;;; $Id: sql-create.lisp,v 1.21 2003/05/06 08:15:47 kevin Exp $
-;;;;
-;;;; This file, part of UMLisp, is
-;;;;    Copyright (c) 2000-2002 by Kevin M. Rosenberg, M.D.
-;;;;
-;;;; UMLisp users are granted the rights to distribute and use this software
-;;;; as governed by the terms of the GNU General Public License.
-;;;; *************************************************************************
-
-(in-package :umlisp)
-
-(eval-when (:compile-toplevel)
-  (declaim (optimize (speed 3) (safety 1) (compilation-speed 0) (debug 3))))
-
-(defun create-table-cmd (file)
-  "Return sql command to create a table"
-  (let ((col-func 
-        (lambda (c) 
-          (let ((sqltype (sqltype c)))
-            (concatenate 'string
-                         (col c)
-                         " "
-                         (if (or (string-equal sqltype "VARCHAR")
-                                 (string-equal sqltype "CHAR"))
-                             (format nil "~a (~a)" sqltype (cmax c))
-                             sqltype))))))
-    (format nil "CREATE TABLE ~a (~{~a~^,~})" (table file)
-           (mapcar col-func (ucols-for-ufile file)))))
-
-(defun create-custom-table-cmd (tablename sql-cmd)
-  "Return SQL command to create a custom table"
-  (format nil "CREATE TABLE ~a AS ~a;" tablename sql-cmd))
-
-(defun insert-col-value (col value)
-  (if (null (parse-fun col)) 
-      value
-      (format nil "~A" (funcall (parse-fun col) value))))
-
-(defun insert-values-cmd (file values)
-  "Return sql insert command for a row of values"  
-  (let ((insert-func
-        (lambda (col value)
-          (let ((q (quotechar col)))
-            (concatenate 'string q (insert-col-value col value) q)))))
-    (format
-     nil "INSERT INTO ~a (~{~a~^,~}) VALUES (~A)"
-     (table file)
-     (fields file)
-     (concat-separated-strings
-      "," 
-      (mapcar insert-func (remove-custom-cols (ucols file)) values)
-      (custom-col-values (custom-ucols-for-file file) values t)))))
-
-
-(defun custom-col-value (col values doquote)
-  (let ((custom-value (funcall (custom-value-fun col) values)))
-    (if custom-value
-       (if doquote
-           (let ((q (quotechar col)))
-             (concatenate 'string q (escape-backslashes custom-value) q))
-           (escape-backslashes custom-value))
-       "")))
-
-(defun custom-col-values (ucols values doquote)
-  "Returns a list of string column values for SQL inserts for custom columns"
-  (loop for col in ucols collect (custom-col-value col values doquote)))
-
-(defun remove-custom-cols (cols)
-  "Remove custom cols from a list col umls-cols"
-  (remove-if #'custom-value-fun cols))
-
-(defun find-custom-cols-for-filename (filename)
-  (remove-if-not (lambda (x) (string-equal filename (car x))) +custom-cols+))
-
-(defun find-custom-col (filename col)
-  (find-if (lambda (x) (and (string-equal filename (car x))
-                           (string-equal col (cadr x)))) +custom-cols+))
-
-(defun custom-colnames-for-filename (filename)
-  (mapcar #'cadr (find-custom-cols-for-filename filename)))
-
-(defun custom-ucols-for-file (file)
-  (remove-if-not #'custom-value-fun (ucols file)))
-
-(defun noneng-lang-index-files ()
-  (remove-if-not
-   (lambda (f) (and (> (length (fil f)) 4)
-                   (string-equal (fil f) "MRXW." :end1 5) 
-                   (not (string-equal (fil f) "MRXW.ENG"))
-                   (not (string-equal (fil f) "MRXW.NONENG"))))
-   *umls-files*))
-
-;;; SQL Command Functions
-
-(defun create-index-cmd (colname tablename length)
-  "Return sql create index command"
-  (format nil "CREATE INDEX ~a ON ~a (~a ~a)"
-         (concatenate 'string tablename "_" colname "_X")
-         tablename colname
-         (if (integerp length) (format nil "(~d)" length) "")))
-
-(defun create-all-tables-cmdfile ()
-  "Return sql commands to create all tables. Not need for automated SQL import"
-  (mapcar (lambda (f) (format nil "~a~%~%" (create-table-cmd f))) *umls-files*))
-
-;; SQL Execution functions
-
-(defun sql-drop-tables (conn)
-  "SQL Databases: drop all tables"
-  (dolist (file *umls-files*)
-    (ignore-errors 
-      (sql-execute (format nil "DROP TABLE ~a" (table file)) conn))))
-
-(defun sql-create-tables (conn)
-  "SQL Databases: create all tables" 
-  (dolist (file *umls-files*)
-    (sql-execute (create-table-cmd file) conn)))
-
-(defun sql-create-custom-tables (conn)
-  "SQL Databases: create all custom tables"
-  (dolist (ct +custom-tables+)
-    (sql-execute (create-custom-table-cmd (car ct) (cadr ct)) conn)))
-  
-(defun sql-insert-values (conn file)
-  "SQL Databases: inserts all values for a file"  
-  (with-umls-file (line (fil file))
-    (sql-execute (insert-values-cmd file line) conn)))
-
-(defun sql-insert-all-values (conn)
-  "SQL Databases: inserts all values for all files"  
-  (dolist (file *umls-files*)
-    (sql-insert-values conn file)))
-
-(defun sql-create-indexes (conn &optional (indexes +index-cols+))
-  "SQL Databases: create all indexes"
-  (dolist (idx indexes)
-    (sql-execute (create-index-cmd (car idx) (cadr idx) (caddr idx)) conn))) 
-
-(defun make-usrl (conn)
-  (sql-execute "drop table if exists USRL" conn)
-  (sql-execute "create table USRL (sab varchar(80), srl integer)" conn)
-  (dolist (tuple (mutex-sql-query
-                 "select distinct SAB,SRL from MRSO order by SAB asc"))
-    (sql-execute (format nil "insert into USRL (sab,srl) values ('~a',~d)" 
-                        (car tuple) (ensure-integer (cadr tuple)))
-                conn)))
-
-(defun sql-create-special-tables (conn)
-  (make-usrl conn))
-
-(defun create-umls-db-by-insert ()
-  "SQL Databases: initializes entire database via SQL insert commands"
-  (init-umls)
-  (init-hash-table)
-  (with-sql-connection (conn)
-    (sql-drop-tables conn)
-    (sql-create-tables conn)
-    (sql-insert-all-values conn)
-    (sql-create-indexes conn)
-    (sql-create-custom-tables conn)
-    (sql-create-indexes conn +custom-index-cols+)
-    (sql-create-special-tables conn)))
-
-(defun create-umls-db (&optional (extension ".trans") 
-                      (copy-cmd #'mysql-copy-cmd))
-  "SQL Databases: initializes entire database via SQL copy commands. 
-This is much faster that using create-umls-db-insert."
-  (init-umls)
-  (init-hash-table)
-  (translate-all-files extension)
-  (with-sql-connection (conn)
-    (sql-drop-tables conn)
-    (sql-create-tables conn)
-    (map 'nil 
-     #'(lambda (file) (sql-execute (funcall copy-cmd file extension) conn)) 
-     *umls-files*)
-    (sql-create-indexes conn)
-    (sql-create-custom-tables conn)
-    (sql-create-indexes conn +custom-index-cols+)
-    (sql-create-special-tables conn)))
-
-(defun translate-all-files (&optional (extension ".trans"))
-  "Copy translated files and return postgresql copy commands to import"
-  (make-noneng-index-file extension)
-  (dolist (f *umls-files*) (translate-umls-file f extension)))
-
-(defun translate-umls-file (file extension)
-  "Translate a umls file into a format suitable for sql copy cmd"
-  (translate-files file extension (list file)))
-
-(defun make-noneng-index-file (extension)
-  "Make non-english index file"
-  (translate-files (find-ufile "MRXW.NONENG")
-                  extension (noneng-lang-index-files)))
-
-(defun translate-files (out-ufile extension input-ufiles)
-  "Translate a umls file into a format suitable for sql copy cmd"
-  (let ((output-path (umls-pathname (fil out-ufile) extension)))
-    (if (probe-file output-path)
-       (format t "File ~A already exists: skipping~%" output-path)
-      (with-open-file (ostream output-path :direction :output)
-       (dolist (input-ufile input-ufiles)
-         (with-umls-file (line (fil input-ufile))
-           (translate-line out-ufile line ostream)
-           (princ #\newline ostream)))))))
-
-(defun pg-copy-cmd (file extension)
-  "Return postgresql copy statement for a file"  
-  (format
-   nil "COPY ~a FROM '~a' using delimiters '|' with null as ''"
-   (table file) (umls-pathname (fil file) extension)))
-
-(defun mysql-copy-cmd (file extension)
-  "Return mysql copy statement for a file"  
-  (format
-   nil
-   "LOAD DATA LOCAL INFILE \"~a\" INTO TABLE ~a FIELDS TERMINATED BY \"|\""
-   (umls-pathname (fil file) extension) (table file)))
-
-(defun col-value (col value)
-  (if (eq (datatype col) 'sql-u)
-      (let ((ui (parse-ui value "")))
-       (if (stringp ui)
-           ui
-         (write-to-string ui)))
-    (escape-backslashes value)))
-
-(defun translate-line (file line strm)
-  "Translate a single line for sql output"
-  (print-separated-strings
-   strm "|" 
-   (mapcar #'col-value (remove-custom-cols (ucols file)) line)
-   (custom-col-values (custom-ucols-for-file file) line nil)))
-   
-
-;;; Routines for analyzing cost of fixed size storage
-
-
-(defun umls-fixed-size-waste ()
-  "Display storage waste if using all fixed size storage"
-  (let ((totalwaste 0)
-       (totalunavoidable 0)
-       (totalavoidable 0)
-       (unavoidable '())
-       (avoidable '()))
-    (dolist (file *umls-files*)
-      (dolist (col (ucols file))
-       (let* ((avwaste (- (cmax col) (av col)))
-              (cwaste (* avwaste (rws file))))
-         (when (plusp cwaste)
-           (if (<= avwaste 6)
-               (progn
-                 (incf totalunavoidable cwaste)
-                 (push (list (fil file) (col col)
-                             avwaste cwaste)
-                       unavoidable))
-               (progn
-                 (incf totalavoidable cwaste)
-                 (push (list (fil file) (col col)
-                             avwaste cwaste)
-                       avoidable)))
-           (incf totalwaste cwaste)))))
-    (values totalwaste totalavoidable totalunavoidable
-           (nreverse avoidable) (nreverse unavoidable))))
-
-(defun display-waste ()
-  (unless *umls-files*
-    (init-umls))
-  (multiple-value-bind (tw ta tu al ul) (umls-fixed-size-waste)
-    (format t "Total waste: ~d~%" tw)
-    (format t "Total avoidable: ~d~%" ta)
-    (format t "Total unavoidable: ~d~%" tu)
-    (format t "Avoidable:~%")
-    (dolist (w al)
-      (format t "  (~a,~a): ~a,~a~%" (car w) (cadr w) (caddr w) (cadddr w)))
-    (format t "Unavoidable:~%")
-    (dolist (w ul)
-      (format t "  (~a,~a): ~a,~a~%" (car w) (cadr w) (caddr w) (cadddr w)))
-    ))
-
-(defun max-umls-field ()
-  "Return length of longest field"
-  (unless *umls-files*
-    (init-umls))
-  (let ((max 0))
-    (declare (fixnum max))
-    (dolist (col *umls-cols*)
-      (when (> (cmax col) max)
-       (setq max (cmax col))))
-    max))
-
-(defun max-umls-row ()
-  "Return length of longest row"
-  (unless *umls-files*
-    (init-umls))
-  (let ((rowsizes '()))
-    (dolist (file *umls-files*)
-      (let ((row 0)
-           (fields (ucols file)))
-       (dolist (field fields)
-         (incf row (1+ (cmax field))))
-       (push row rowsizes)))
-    (car (sort rowsizes #'>))))
index 1c8cb6a96e993807a796560eb9641ad96281cd81..2d81e582de48d8b919ab989feae2ac18def35d47 100644 (file)
@@ -7,7 +7,7 @@
 ;;;; Author:        Kevin M. Rosenberg
 ;;;; Date Started:  Apr 2000
 ;;;;
-;;;; $Id: umlisp.asd,v 1.16 2003/05/03 17:10:08 kevin Exp $
+;;;; $Id: umlisp.asd,v 1.17 2003/05/07 21:57:06 kevin Exp $
 ;;;;
 ;;;; This file, part of UMLisp, is
 ;;;;    Copyright (c) 2000-2002 by Kevin M. Rosenberg, M.D.
@@ -29,7 +29,7 @@
    (:file "parse-macros"  :depends-on ("sql"))
    (:file "parse-2002"  :depends-on ("parse-macros"))
    (:file "parse-common"  :depends-on ("parse-2002"))
-   (:file "sql-create" :depends-on ("parse-common"))
+   (:file "create-sql" :depends-on ("parse-common"))
    (:file "classes" :depends-on ("utils"))
    (:file "class-support" :depends-on ("classes"))
    (:file "sql-classes" :depends-on ("class-support" "sql"))