1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10; Package: umlisp -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
5 ;;;; Name: parse-common.lisp
6 ;;;; Purpose: Common, stable parsing routines for UMLisp
7 ;;;; Author: Kevin M. Rosenberg
12 ;;;; This file, part of UMLisp, is
13 ;;;; Copyright (c) 2000-2006 by Kevin M. Rosenberg, M.D.
15 ;;;; UMLisp users are granted the rights to distribute and use this software
16 ;;;; as governed by the terms of the GNU General Public License.
17 ;;;; *************************************************************************
21 (defun ensure-ucols+ufiles (&optional (alwaysclear nil))
22 "Initialize all UMLS file and column structures if not already initialized"
24 (when (or alwaysclear (null *umls-files*))
25 (setf *umls-cols* nil *umls-files* nil)
28 (ensure-field-lengths))
30 (setf *umls-cols* nil *umls-files* nil)
35 (defun add-ucols (ucols)
36 "Adds a ucol or list of ucols to *umls-cols*. Returns input value."
37 (setq *umls-cols* (append (mklist ucols) *umls-cols*))
40 (defun add-ufiles (ufiles)
41 "Adds a ufile or list of ufiles to *umls-filess*. Returns input value."
42 (setq *umls-files* (append (mklist ufiles) *umls-files*))
45 (defun ufile-pathname (ufile &optional (extension ""))
46 "Return pathname for a umls filename with an optional extension"
47 (assert (typep ufile 'ufile))
48 (let* ((dirs (append (list (dir ufile))
49 (awhen (subdir ufile) (list it))))
50 (name-list (delimited-string-to-list (fil ufile) #\.))
51 (name (if (second name-list)
53 (concatenate 'string (first name-list) (or extension ""))))
54 (type (when (second name-list)
55 (concatenate 'string (second name-list) (or extension "")))))
57 (make-pathname :name name :type type
58 :directory (cons :relative dirs))
61 (defun umls-pathname (filename &optional (extension ""))
62 "Return pathname for a umls filename with an optional extension"
65 (let* ((name-list (delimited-string-to-list filename #\.))
66 (name (if (second name-list)
68 (concatenate 'string (first name-list) (or extension ""))))
69 (type (when (second name-list)
70 (concatenate 'string (second name-list) (or extension "")))))
72 (make-pathname :name name :type type)
73 (case (schar filename 0)
86 ;;; Find field lengths for LEX and NET files
88 (defun ensure-field-lengths ()
89 "Initial colstruct field lengths for files that don't have a measurement.
90 Currently, these are the LEX and NET files."
91 (dolist (length-list (ufiles-field-lengths (ufiles-to-measure)))
92 (destructuring-bind (filename fields-max fields-av) length-list
93 (let ((file (find-ufile filename)))
95 (error "Can't find ~A filename in ufiles" filename))
96 (unless (= (length fields-max) (length (fields file)))
98 "Number of file fields ~A not equal to field count in ufile ~S"
100 (dotimes (i (length (fields file)))
102 (let* ((field (nth i (fields file)))
103 (col (find-ucol field filename)))
105 (error "can't find column ~A" field))
106 (setf (cmax col) (aref fields-max i))
107 (setf (av col) (aref fields-av i))
108 (ensure-ucol-datatype col (datatype-for-colname (col col)))))))))
110 (defun ufiles-to-measure ()
111 "Returns a list of ufiles to measure"
112 (loop for ufile in *umls-files*
113 unless (or (char= #\M (schar (fil ufile) 0))
114 (char= #\m (schar (fil ufile) 0)))
118 (defun ufiles-field-lengths (ufiles)
119 "Returns a list of lists of containing (FILE MAX AV)"
120 (loop for ufile in ufiles collect (file-field-lengths ufile)))
122 (defun file-field-lengths (ufile)
123 "Returns a list of FILENAME MAX AV"
124 (declare (optimize (speed 3) (safety 0)))
125 (let (fields-max fields-av num-fields (count-lines 0))
126 (with-umls-ufile (line ufile)
128 (setq num-fields (length line))
129 (setq fields-max (make-array num-fields :element-type 'fixnum
131 (setq fields-av (make-array num-fields :element-type 'number
132 :initial-element 0)))
133 (dotimes (i num-fields)
135 (let* ((str (nth i line))
136 (len (length #-(and clisp unicode) str
137 #+(and clisp unicode)
138 (if *octet-sql-storage*
139 (ext:convert-string-to-bytes str charset:utf-8)
141 (incf (aref fields-av i) len)
142 (when (> len (aref fields-max i))
143 (setf (aref fields-max i) len))))
145 (dotimes (i num-fields)
146 (setf (aref fields-av i) (float (/ (aref fields-av i) count-lines))))
147 (list (fil ufile) fields-max fields-av)))
149 ;;; UMLS column/file functions
151 (defun find-ucol-of-colname (colname filename ucols)
152 "Returns list of umls-col structure for a column name and a filename"
153 (dolist (ucol ucols nil)
154 (when (and (string-equal filename (fil ucol))
155 (string-equal colname (col ucol)))
156 (return-from find-ucol-of-colname ucol))))
158 (defun ensure-col-in-columns (colname filename ucols)
159 (aif (find-ucol-of-colname colname filename ucols)
161 (add-ucols (make-ucol-for-column colname filename ucols))))
163 (defun make-ucol-for-column (colname filename ucols)
164 ;; try to find column name without a terminal digit
165 (let* ((len (length colname))
166 (last-digit? (digit-char-p (schar colname (1- len))))
167 (base-colname (if last-digit?
168 (subseq colname 0 (1- len))
170 (ucol (when last-digit?
171 (find-ucol-of-colname base-colname filename ucols))))
172 (when (and last-digit? (null ucol))
173 (error "Couldn't find a base column for col ~A in file ~A"
175 (copy-or-new-ucol colname filename ucol)))
177 (defun copy-or-new-ucol (colname filename ucol)
181 :col (copy-seq colname) :des (copy-seq (des ucol)) :ref (copy-seq (ref ucol))
182 :min (cmin ucol) :max (cmax ucol) :fil (copy-seq (fil ucol))
183 :sqltype (copy-seq (sqltype ucol)) :dty (copy-seq (dty ucol))
184 :parse-fun (parse-fun ucol) :quote-str (copy-seq (quote-str ucol))
185 :datatype (datatype ucol) :custom-value-fun (custom-value-fun ucol))
186 (make-empty-ucol colname filename)))
188 (defun ensure-compiled-fun (fun)
189 "Ensure that a function is compiled"
194 (if (compiled-function-p fun)
200 (defun make-ucol (col des ref min av max fil dty
201 &key (sqltype "VARCHAR") (parse-fun #'add-sql-quotes)
202 (quote-str "'") (custom-value-fun))
203 (let ((ucol (make-instance
205 :col col :des des :ref ref :min min :av av
206 :max (if (eql max 0) 1 max) ;; ensure at least one char wide
211 :parse-fun (ensure-compiled-fun parse-fun)
212 :custom-value-fun (ensure-compiled-fun custom-value-fun))))
213 (ensure-ucol-datatype ucol (datatype-for-colname col))
216 (defun make-empty-ucol (colname filename)
217 ;;(format "call in make-empty-ucol: ~A/~A" colname filename)
218 (make-ucol (copy-seq colname) "Unknown" "" nil nil nil filename nil))
220 (defun find-ucol (colname filename)
221 "Returns list of umls-col structure for a column name and a filename"
222 (ensure-col-in-columns colname filename *umls-cols*))
224 (defun find-ufile (filename)
225 "Returns umls-file structure for a filename"
226 (find-if #'(lambda (f) (string-equal filename (fil f))) *umls-files*))
228 (defun find-ucols-for-ufile (ufile)
229 "Returns list of umls-cols for a file structure"
230 (loop for colname in (fields ufile)
231 collect (find-ucol colname
233 (concatenate 'string (subdir ufile) "/" (fil ufile))
236 (defun umls-field-string-to-list (fmt)
237 "Converts a comma delimited list of fields into a list of field names. Will
238 append a unique number (starting at 2) onto a column name that is repeated in the list"
239 (let ((col-counts (make-hash-table :test 'equal)))
240 (loop for colname in (delimited-string-to-list (escape-column-name fmt) #\,)
242 (multiple-value-bind (value found) (gethash colname col-counts)
245 (incf (gethash colname col-counts))
246 (concatenate 'string colname (write-to-string (1+ value))))
248 (setf (gethash colname col-counts) 1)
251 (defun decompose-fil (fil)
253 (let ((pos (position #\/ fil)))
255 (values (subseq fil (1+ pos)) (subseq fil 0 pos))
259 (defun filename-to-tablename (file)
260 (let ((pos (search ".RRF" file)))
262 (setf file (subseq file 0 pos))))
263 (substitute #\_ #\. file))
265 (defun make-ufile (dir fil des cls rws bts fields)
266 (multiple-value-bind (file subdir) (decompose-fil fil)
267 (let ((ufile (make-instance 'ufile :dir dir :fil file :subdir subdir
269 :rws rws :bts bts :fields fields
270 :table (filename-to-tablename file))))
271 (setf (ucols ufile) (find-ucols-for-ufile ufile))
274 (defun datatype-for-colname (colname)
275 "Return datatype for column name"
276 (second (find colname +col-datatypes+ :key #'car :test #'string-equal)))
278 (defun canonicalize-column-type (type)
280 ((string-equal type "SMALLINT")
281 (case *umls-sql-type*
283 ((:postgresql :postgresql-socket) "INT2")
284 (:oracle "NUMBER(5,0)")
286 ((string-equal type "INTEGER")
287 (case *umls-sql-type*
289 ((:postgresql :postgresql-socket) "INT4")
290 (:oracle "NUMBER(9,0)")
292 ((string-equal type "BIGINT")
293 (case *umls-sql-type*
295 ((:postgresql :postgresql-socket) "INT8")
296 (:oracle "NUMBER(38,0)")
298 ((string-equal type "TEXT")
299 (case *umls-sql-type*
301 ((:postgresql :postgresql-socket) "TEXT")
302 (:oracle "VARCHAR2(3000)")
303 (t "VARCHAR(3000)")))
304 ((string-equal type "VARCHAR")
305 (case *umls-sql-type*
307 ((:postgresql :postgresql-socket) "VARCHAR")
310 ((string-equal type "NUMERIC")
311 (case *umls-sql-type*
313 ((:postgresql :postgresql-socket) "NUMERIC")
319 (defun ensure-ucol-datatype (col datatype)
320 "Add data type information to column"
321 (setf (datatype col) datatype)
323 (sql-u (setf (sqltype col) (canonicalize-column-type "INTEGER")
324 (parse-fun col) #'parse-ui
326 (sql-s (setf (sqltype col) (canonicalize-column-type "SMALLINT")
327 (parse-fun col) #'parse-integer
329 (sql-l (setf (sqltype col) (canonicalize-column-type "BIGINT")
330 (parse-fun col) #'parse-integer
332 (sql-i (setf (sqltype col) (canonicalize-column-type "INTEGER")
333 (parse-fun col) #'parse-integer
335 (sql-f (setf (sqltype col) (canonicalize-column-type "NUMERIC")
336 (parse-fun col) #'read-from-string
338 (t ; Default column type, optimized text storage
339 (setf (parse-fun col) #'add-sql-quotes
341 (when (and (cmax col) (av col))
342 (if (> (cmax col) 255)
343 (setf (sqltype col) (canonicalize-column-type "TEXT"))
344 (setf (sqltype col) (canonicalize-column-type "VARCHAR")))))))
346 (defun escape-column-name (name)
347 (substitute #\_ #\/ name))