From c7524b9cd7631b94729a20aac13c069ab7988233 Mon Sep 17 00:00:00 2001 From: "Kevin M. Rosenberg" Date: Fri, 2 May 2003 21:49:19 +0000 Subject: [PATCH 1/1] r4742: *** empty log message *** --- sql-classes.lisp | 441 ++++++++++++++++++++--------------------------- utils.lisp | 20 ++- 2 files changed, 203 insertions(+), 258 deletions(-) diff --git a/sql-classes.lisp b/sql-classes.lisp index e3804d6..59efde5 100644 --- a/sql-classes.lisp +++ b/sql-classes.lisp @@ -4,10 +4,10 @@ ;;;; ;;;; Name: sql-classes.lisp ;;;; Purpose: Routines for reading UMLS objects from SQL database -;;;; Programmer: Kevin M. Rosenberg +;;;; Author: Kevin M. Rosenberg ;;;; Date Started: Apr 2000 ;;;; -;;;; $Id: sql-classes.lisp,v 1.17 2003/05/02 21:24:19 kevin Exp $ +;;;; $Id: sql-classes.lisp,v 1.18 2003/05/02 21:49:19 kevin Exp $ ;;;; ;;;; This file, part of UMLisp, is ;;;; Copyright (c) 2000-2002 by Kevin M. Rosenberg, M.D. @@ -26,43 +26,26 @@ (defun current-srl! (srl) (setq *current-srl* srl)) -;;; Object lookups - -;;; Lookup functions for uterms,ustr in ucons - -(defun find-uterm-in-ucon (ucon lui) - (find lui (s#term ucon) :key #'lui :test 'equal)) - -(defun find-ustr-in-uterm (uterm sui) - (find sui (s#str uterm) :key #'sui :test 'equal)) - -(defun find-ustr-in-ucon (ucon sui) - (let ((found-ustr nil)) - (dolist (uterm (s#term ucon)) - (unless found-ustr - (dolist (ustr (s#str uterm)) - (unless found-ustr - (when (string-equal sui (sui ustr)) - (setq found-ustr ustr)))))) - found-ustr)) (defmacro with-umlisp-query ((table fields srl where-name where-value - &key (lrlname "KCUILRL") distinct single terminal like) + &key (lrlname "KCUILRL") distinct single + terminal like) &body body) (let ((query (gensym))) `(unless (and ,where-name (not ,where-value)) - (let ((,query (umlisp-query ,table ,fields ,srl ,where-name ,where-value - :lrlname ,lrlname :single ,single :distinct ,distinct - :terminal ,terminal :like ,like))) - (if ,single - (let ((tuple (car ,query))) - (when tuple - ,@body)) - (loop - for tuple in ,query - collect - ,@body)))))) - + (let ((,query (umlisp-query ,table ,fields ,srl ,where-name ,where-value + :lrlname ,lrlname :single ,single :distinct ,distinct + :terminal ,terminal :like ,like))) + (if ,single + (let ((tuple (car ,query))) + (when tuple + (destructuring-bind ,fields tuple + ,@body))) + (loop + for tuple in ,query collect + (destructuring-bind ,fields tuple + ,@body))))))) + (defun umlisp-query (table fields srl where-name where-value &key (lrlname "KCUILRL") single distinct terminal like) "Query the UMLisp database. Return a list of umlisp objects whose name @@ -98,57 +81,53 @@ is OBJNAME from TABLE where WHERE-NAME field = WHERE-VALUE with FIELDS" (defun find-ucon-cui (cui &key (srl *current-srl*)) "Find ucon for a cui" - (with-umlisp-query ('mrcon '(kpfstr kcuilrl) srl 'cui (parse-cui cui) :single t) + (with-umlisp-query ('mrcon (kpfstr kcuilrl) srl 'cui (parse-cui cui) :single t) (make-instance 'ucon :cui (parse-cui cui) - :pfstr (car tuple) - :lrl (ensure-integer (cadr tuple))))) + :pfstr kpfstr2 + :lrl (ensure-integer kcuilrl)))) (defun find-ucon-cui-sans-pfstr (cui &key (srl *current-srl*)) "Find ucon for a cui" - (with-umlisp-query ('mrcon '(kcuilrl) srl 'cui (parse-cui cui) :single t) + (with-umlisp-query ('mrcon (kcuilrl) srl 'cui (parse-cui cui) :single t) (make-instance 'ucon :cui (parse-cui cui) - :lrl (ensure-integer (car tuple)) + :lrl (ensure-integer kcuilrl) :pfstr nil))) (defun find-pfstr-cui (cui &key (srl *current-srl*)) "Find preferred string for a cui" - (with-umlisp-query ('mrcon '(kpfstr) srl 'cui (parse-cui cui) :single t) - (car tuple))) + (with-umlisp-query ('mrcon (kpfstr) srl 'cui (parse-cui cui) :single t) + kpfstr)) (defun find-ucon-lui (lui &key (srl *current-srl*)) "Find list of ucon for lui" - (with-umlisp-query ('mrcon '(cui kpfstr kcuilrl) srl 'lui (parse-lui lui) :distinct t) - (destructuring-bind (cui pfstr lrl) tuple - (make-instance 'ucon :cui (ensure-integer cui) - :pfstr pfstr - :lrl (ensure-integer lrl))))) + (with-umlisp-query ('mrcon (cui kpfstr kcuilrl) srl 'lui (parse-lui lui) :distinct t) + (make-instance 'ucon :cui (ensure-integer cui) + :pfstr kpfstr + :lrl (ensure-integer lrl)))) (defun find-ucon-sui (sui &key (srl *current-srl*)) "Find list of ucon for sui" - (with-umlisp-query ('mrcon '(cui kpfstr kcuilrl) srl 'sui (parse-sui sui) :distinct t) - (destructuring-bind (cui pfstr lrl) tuple - (make-instance 'ucon :cui (ensure-integer cui) - :pfstr pfstr - :lrl (ensure-integer lrl))))) + (with-umlisp-query ('mrcon (cui kpfstr kcuilrl) srl 'sui (parse-sui sui) :distinct t) + (make-instance 'ucon :cui (ensure-integer cui) + :pfstr kpfstr + :lrl (ensure-integer kcuilrl))))) (defun find-ucon-cuisui (cui sui &key (srl *current-srl*)) "Find ucon for cui/sui" (when (and cui sui) - (with-umlisp-query ('mrcon '(cui kpfstr kcuilrl) srl 'kcuisui + (with-umlisp-query ('mrcon (cui kpfstr kcuilrl) srl 'kcuisui (make-cuisui (parse-cui cui) (parse-sui sui))) - (destructuring-bind (cui pfstr lrl) tuple - (make-instance 'ucon :cui (ensure-integer cui) - :pfstr pfstr - :lrl (ensure-integer lrl)))))) + (make-instance 'ucon :cui (ensure-integer cui) + :pfstr kpfstr + :lrl (ensure-integer kcuilrl))))) (defun find-ucon-str (str &key (srl *current-srl*)) "Find ucon that are exact match for str" - (with-umlisp-query ('mrcon '(cui kpfstr kcuilrl) srl 'str str :distinct t) - (destructuring-bind (cui pfstr lrl) tuple - (make-instance 'ucon :cui (ensure-integer cui) - :pfstr pfstr - :lrl (ensure-integer lrl))))) + (with-umlisp-query ('mrcon (cui kpfstr kcuilrl) srl 'str str :distinct t) + (make-instance 'ucon :cui (ensure-integer cui) + :pfstr kpfstr + :lrl (ensure-integer kcuilrl)))) (defun find-ucon-all (&key (srl *current-srl*)) "Return list of all ucon's" @@ -178,35 +157,30 @@ is OBJNAME from TABLE where WHERE-NAME field = WHERE-VALUE with FIELDS" (defun find-udef-cui (cui &key (srl *current-srl*)) "Return a list of udefs for cui" - (with-umlisp-query ('mrdef '(sab def) srl 'cui (parse-cui cui) :lrlname "KSRL") - (destructuring-bind (sab def) tuple - (make-instance 'udef :sab sab :def def)))) + (with-umlisp-query ('mrdef (sab def) srl 'cui (parse-cui cui) :lrlname "KSRL") + (make-instance 'udef :sab sab :def def))) (defun find-usty-cui (cui &key (srl *current-srl*)) "Return a list of usty for cui" - (with-umlisp-query ('mrsty '(tui sty) srl 'cui (parse-cui cui) :lrlname "KLRL") - (destructuring-bind (tui sty) tuple - (make-instance 'usty :tui (ensure-integer tui) :sty sty)))) + (with-umlisp-query ('mrsty (tui sty) srl 'cui (parse-cui cui) :lrlname "KLRL") + (make-instance 'usty :tui (ensure-integer tui) :sty sty))) (defun find-usty-word (word &key (srl *current-srl*)) "Return a list of usty that match word" - (with-umlisp-query ('mrsty '(tui sty) srl 'sty word :lrlname 'klrl :like t :distinct t) - (destructuring-bind (tui sty) tuple - (make-instance 'usty :tui (ensure-integer tui) :sty sty)))) + (with-umlisp-query ('mrsty (tui sty) srl 'sty word :lrlname 'klrl :like t :distinct t) + (make-instance 'usty :tui (ensure-integer tui) :sty sty))) (defun find-urel-cui (cui &key (srl *current-srl*)) "Return a list of urel for cui" - (with-umlisp-query ('mrrel '(rel cui2 rela sab sl mg kpfstr2) srl 'cui1 (parse-cui cui) :lrlname "KSRL") - (destructuring-bind (rel cui2 rela sab sl mg pfstr2) tuple - (make-instance 'urel :cui1 (parse-cui cui) :rel rel :cui2 (ensure-integer cui2) :rela rela - :sab sab :sl sl :mg mg :pfstr2 pfstr2)))) + (with-umlisp-query ('mrrel (rel cui2 rela sab sl mg kpfstr2) srl 'cui1 (parse-cui cui) :lrlname "KSRL") + (make-instance 'urel :cui1 (parse-cui cui) :rel rel :cui2 (ensure-integer cui2) :rela rela + :sab sab :sl sl :mg mg :pfstr2 kpfstr2))) (defun find-urel-cui2 (cui2 &key (srl *current-srl*)) "Return a list of urel for cui2" - (with-umlisp-query ('mrrel '(rel cui1 rela sab sl mg kpfstr2) srl 'cui2 (parse-cui cui2) :lrlname "KSRL") - (destructuring-bind (rel cui1 rela sab sl mg pfstr2) tuple - (make-instance 'urel :cui2 (parse-cui cui2) :rel rel :cui1 (ensure-integer cui1) :rela rela - :sab sab :sl sl :mg mg :pfstr2 pfstr2)))) + (with-umlisp-query ('mrrel (rel cui1 rela sab sl mg kpfstr2) srl 'cui2 (parse-cui cui2) :lrlname "KSRL") + (make-instance 'urel :cui2 (parse-cui cui2) :rel rel :cui1 (ensure-integer cui1) :rela rela + :sab sab :sl sl :mg mg :pfstr2 kpfstr2)))) (defun find-ucon-rel-cui2 (cui2 &key (srl *current-srl*)) (mapcar @@ -215,23 +189,21 @@ is OBJNAME from TABLE where WHERE-NAME field = WHERE-VALUE with FIELDS" (defun find-ucoc-cui (cui &key (srl *current-srl*)) "Return a list of ucoc for cui" - (with-umlisp-query ('mrcoc '(cui2 soc cot cof coa kpfstr2) srl 'cui1 (parse-cui cui) + (with-umlisp-query ('mrcoc (cui2 soc cot cof coa kpfstr2) srl 'cui1 (parse-cui cui) :lrlname "KSRL" :terminal "order by COF asc") - (destructuring-bind (cui2 soc cot cof coa pfstr2) tuple - (setq cui2 (ensure-integer cui2)) - (when (zerop cui2) (setq cui2 nil)) - (make-instance 'ucoc :cui1 (parse-cui cui) :cui2 (ensure-integer cui2) :soc soc :cot cot - :cof (ensure-integer cof) :coa coa :pfstr2 pfstr2)))) + (setq cui2 (ensure-integer cui2)) + (when (zerop cui2) (setq cui2 nil)) + (make-instance 'ucoc :cui1 (parse-cui cui) :cui2 (ensure-integer cui2) :soc soc :cot cot + :cof (ensure-integer cof) :coa coa :pfstr2 kpfstr2))) (defun find-ucoc-cui2 (cui2 &key (srl *current-srl*)) "Return a list of ucoc for cui2" - (with-umlisp-query ('mrcoc '(cui1 soc cot cof coa kpfstr2) srl 'cui2 (parse-cui cui2) + (with-umlisp-query ('mrcoc (cui1 soc cot cof coa kpfstr2) srl 'cui2 (parse-cui cui2) :lrlname "KSRL" :terminal "order by COF asc") - (destructuring-bind (cui1 soc cot cof coa pfstr2) tuple - (setq cui2 (ensure-integer cui2)) - (when (zerop cui2) (setq cui2 nil)) - (make-instance 'ucoc :cui1 (ensure-integer cui1) :cui2 (parse-cui cui2) :soc soc :cot cot - :cof (ensure-integer cof) :coa coa :pfstr2 pfstr2)))) + (setq cui2 (ensure-integer cui2)) + (when (zerop cui2) (setq cui2 nil)) + (make-instance 'ucoc :cui1 (ensure-integer cui1) :cui2 (parse-cui cui2) :soc soc :cot cot + :cof (ensure-integer cof) :coa coa :pfstr2 kpfstr2))) (defun find-ucon-coc-cui2 (cui2 &key (srl *current-srl*)) "List of ucon with co-occurance cui2" @@ -241,10 +213,9 @@ is OBJNAME from TABLE where WHERE-NAME field = WHERE-VALUE with FIELDS" (defun find-ulo-cui (cui &key (srl *current-srl*)) "Return a list of ulo for cui" - (with-umlisp-query ('mrlo '(isn fr un sui sna soui) srl 'cui (parse-cui cui) :lrlname "KLRL") - (destructuring-bind (isn fr un sui sna soui) tuple - (make-instance 'ulo :isn isn :fr (ensure-integer fr) :un un :sui (ensure-integer sui) :sna sna - :soui soui)))) + (with-umlisp-query ('mrlo (isn fr un sui sna soui) srl 'cui (parse-cui cui) :lrlname "KLRL") + (make-instance 'ulo :isn isn :fr (ensure-integer fr) :un un :sui (ensure-integer sui) :sna sna + :soui soui))) (defgeneric suistr (lo)) (defmethod suistr ((lo ulo)) @@ -253,59 +224,52 @@ is OBJNAME from TABLE where WHERE-NAME field = WHERE-VALUE with FIELDS" (defun find-uatx-cui (cui &key (srl *current-srl*)) "Return a list of uatx for cui" - (with-umlisp-query ('mratx '(sab rel atx) srl 'cui (parse-cui cui) :lrlname 'ksrl) - (destructuring-bind (sab rel atx) tuple - (make-instance 'uatx :sab sab :rel rel :atx atx)))) + (with-umlisp-query ('mratx (sab rel atx) srl 'cui (parse-cui cui) :lrlname 'ksrl) + (make-instance 'uatx :sab sab :rel rel :atx atx))) (defun find-uterm-cui (cui &key (srl *current-srl*)) "Return a list of uterm for cui" - (with-umlisp-query ('mrcon '(lui lat ts kluilrl) srl 'cui (parse-cui cui) :lrlname 'kluilrl + (with-umlisp-query ('mrcon (lui lat ts kluilrl) srl 'cui (parse-cui cui) :lrlname 'kluilrl :distinct t) - (destructuring-bind (lui lat ts lrl) tuple - (make-instance 'uterm :lui (ensure-integer lui) :cui (parse-cui cui) - :lat lat :ts ts :lrl (ensure-integer lrl))))) + (make-instance 'uterm :lui (ensure-integer lui) :cui (parse-cui cui) + :lat lat :ts ts :lrl (ensure-integer lrl)))) (defun find-uterm-lui (lui &key (srl *current-srl*)) "Return a list of uterm for lui" - (with-umlisp-query ('mrcon '(cui lat ts kluilrl) srl 'lui (parse-lui lui) + (with-umlisp-query ('mrcon (cui lat ts kluilrl) srl 'lui (parse-lui lui) :lrlname 'kluilrl :distinct t) - (destructuring-bind (cui lat ts lrl) tuple - (make-instance 'uterm :cui (ensure-integer cui) :lui (parse-lui lui) - :lat lat :ts ts :lrl (ensure-integer lrl))))) + (make-instance 'uterm :cui (ensure-integer cui) :lui (parse-lui lui) + :lat lat :ts ts :lrl (ensure-integer kluilrl)))) (defun find-uterm-cuilui (cui lui &key (srl *current-srl*)) "Return single uterm for cui/lui" - (with-umlisp-query ('mrcon '(lat ts kluilrl) srl 'kcuilui + (with-umlisp-query ('mrcon (lat ts kluilrl) srl 'kcuilui (make-cuilui (parse-cui cui) (parse-lui lui)) :lrlname 'kluilrl :single t) - (destructuring-bind (lat ts lrl) tuple - (make-instance 'uterm :cui cui :lui lui :lat lat :ts ts :lrl (ensure-integer lrl))))) + (make-instance 'uterm :cui cui :lui lui :lat lat :ts ts :lrl (ensure-integer kluilrl))))) (defun find-ustr-cuilui (cui lui &key (srl *current-srl*)) "Return a list of ustr for cui/lui" - (with-umlisp-query ('mrcon '(sui stt str lrl) srl 'kcuilui (make-cuilui cui lui) :lrlname 'lrl) - (destructuring-bind (sui stt str lrl) tuple - (make-instance 'ustr :sui (ensure-integer sui) :cui cui :lui lui - :cuisui (make-cuisui cui sui) :stt stt :str str - :lrl (ensure-integer lrl))))) + (with-umlisp-query ('mrcon (sui stt str lrl) srl 'kcuilui (make-cuilui cui lui) :lrlname 'lrl) + (make-instance 'ustr :sui (ensure-integer sui) :cui cui :lui lui + :cuisui (make-cuisui cui sui) :stt stt :str str + :lrl (ensure-integer lrl)))) (defun find-ustr-cuisui (cui sui &key (srl *current-srl*)) "Return the single ustr for cuisui" - (with-umlisp-query ('mrcon '(lui stt str lrl) srl 'kcuisui (make-cuisui cui sui) :lrlname 'lrl :single t) - (destructuring-bind (lui stt str lrl) tuple - (make-instance 'ustr :sui sui :cui cui :cuisui (make-cuisui cui sui) - :lui (ensure-integer lui) :stt stt :str str :lrl (ensure-integer lrl))))) + (with-umlisp-query ('mrcon (lui stt str lrl) srl 'kcuisui (make-cuisui cui sui) :lrlname 'lrl :single t) + (make-instance 'ustr :sui sui :cui cui :cuisui (make-cuisui cui sui) + :lui (ensure-integer lui) :stt stt :str str :lrl (ensure-integer lrl)))) (defun find-ustr-sui (sui &key (srl *current-srl*)) "Return the list of ustr for sui" - (with-umlisp-query ('mrcon '(cui lui stt str lrl) srl 'sui (parse-sui sui) :lrlname 'lrl) - (destructuring-bind (cui lui stt str lrl) tuple - (make-instance 'ustr :sui sui :cui cui :stt stt :str str - :cuisui (make-cuisui (ensure-integer cui) (parse-sui sui)) - :lui (ensure-integer lui) - :lrl (ensure-integer lrl))))) + (with-umlisp-query ('mrcon (cui lui stt str lrl) srl 'sui (parse-sui sui) :lrlname 'lrl) + (make-instance 'ustr :sui sui :cui cui :stt stt :str str + :cuisui (make-cuisui (ensure-integer cui) (parse-sui sui)) + :lui (ensure-integer lui) + :lrl (ensure-integer lrl)))) (defun find-ustr-sab (sab &key (srl *current-srl*)) "Return the list of ustr for sab" @@ -342,28 +306,26 @@ is OBJNAME from TABLE where WHERE-NAME field = WHERE-VALUE with FIELDS" (car tuple))) (defun find-uso-cuisui (cui sui &key (srl *current-srl*)) - (with-umlisp-query ('mrso '(sab code srl tty) srl 'kcuisui (make-cuisui cui sui) :lrlname 'srl) - (destructuring-bind (sab code srl tty) tuple - (make-instance 'uso :sab sab :code code :srl srl :tty tty)))) + (with-umlisp-query ('mrso (sab code srl tty) srl 'kcuisui (make-cuisui cui sui) :lrlname 'srl) + (make-instance 'uso :sab sab :code code :srl srl :tty tty))) (defun find-ucxt-cuisui (cui sui &key (srl *current-srl*)) - (with-umlisp-query ('mrcxt '(sab code cxn cxl rnk cxs cui2 hcd rela cx) srl 'kcuisui + (with-umlisp-query ('mrcxt (sab code cxn cxl rnk cxs cui2 hcd rela cx) srl 'kcuisui (make-cuisui cui sui) :lrlname 'ksrl) - (destructuring-bind (sab code cxn cxl rnk cxs cui2 hcd rela xc) tuple - (make-instance 'ucxt :sab sab :code code - :cxn (ensure-integer cxn) - :cxl cxl :cxs cxs :hcd hcd :rela rela :xc xc - :rnk (ensure-integer rnk) - :cui2 (ensure-integer cui2))))) + (make-instance 'ucxt :sab sab :code code + :cxn (ensure-integer cxn) + :cxl cxl :cxs cxs :hcd hcd :rela rela :xc xc + :rnk (ensure-integer rnk) + :cui2 (ensure-integer cui2)))) (defun find-usat-ui (cui &optional (lui nil) (sui nil) &key (srl *current-srl*)) (let ((ls (format nil "select CODE,ATN,SAB,ATV from MRSAT where "))) (cond - (sui (string-append ls (format nil "KCUISUI=~d" (make-cuisui cui sui)))) - (lui (string-append ls (format nil "KCUILUI=~d and sui=0" (make-cuilui cui lui)))) - (t (string-append ls (format nil "cui=~d and lui=0 and sui=0" cui)))) + (sui (string-append ls (format nil "KCUISUI=~d" (make-cuisui cui sui)))) + (lui (string-append ls (format nil "KCUILUI=~d and sui=0" (make-cuilui cui lui)))) + (t (string-append ls (format nil "cui=~d and lui=0 and sui=0" cui)))) (when srl - (string-append ls (format nil " and KSRL <= ~d" srl))) + (string-append ls (format nil " and KSRL <= ~d" srl))) (let ((usats '())) (dolist (tuple (mutex-sql-query ls)) (destructuring-bind (code atn sab atv) tuple @@ -374,46 +336,42 @@ is OBJNAME from TABLE where WHERE-NAME field = WHERE-VALUE with FIELDS" (defun find-usty-tui (tui) "Find usty for tui" - (with-umlisp-query ('mrsty '(sty) nil 'tui (parse-tui tui) :single t) - (make-instance 'usty :tui (parse-tui tui) :sty (car tuple)))) + (with-umlisp-query ('mrsty (sty) nil 'tui (parse-tui tui) :single t) + (make-instance 'usty :tui (parse-tui tui) :sty sty))) (defun find-usty-sty (sty) "Find usty for a sty" - (with-umlisp-query ('mrsty '(tui) nil 'sty sty :single t) - (make-instance 'usty :tui (ensure-integer (car tuple)) :sty sty))) + (with-umlisp-query ('mrsty (tui) nil 'sty sty :single t) + (make-instance 'usty :tui (ensure-integer tui) :sty sty))) (defun find-usty-all () "Return list of usty's for all semantic types" (with-umlisp-query ('mrsty '(tui) nil nil nil :distinct t) - (find-usty-tui (car tuple)))) + (find-usty-tui tui))) (defun find-usab-all () "Find usab for a key" - (with-umlisp-query ('mrsab '(vcui rcui vsab rsab son sf sver mstart mend imeta rmeta slc scc srl tfr cfr cxty ttyl atnl lat cenc curver sabin) nil nil nil) - (destructuring-bind - (vcui rcui vsab rsab son sf sver mstart mend imeta rmeta slc scc srl tfr cfr cxty ttyl atnl lat cenc curver sabin) tuple - (make-instance 'usab :vcui (ensure-integer vcui) - :rcui (ensure-integer rcui) - :vsab vsab :rsab rsab :son son :sf sf :sver sver :mstart mstart - :mend mend :imeta imeta :rmeta rmeta :slc slc :scc scc - :srl (ensure-integer srl) - :tfr (ensure-integer tfr) :cfr (ensure-integer cfr) - :cxty cxty :ttyl ttyl :atnl atnl :lat lat :cenc cenc - :curver curver :sabin sabin)))) + (with-umlisp-query ('mrsab (vcui rcui vsab rsab son sf sver mstart mend imeta rmeta slc scc srl tfr cfr cxty ttyl atnl lat cenc curver sabin) nil nil nil) + (make-instance 'usab :vcui (ensure-integer vcui) + :rcui (ensure-integer rcui) + :vsab vsab :rsab rsab :son son :sf sf :sver sver :mstart mstart + :mend mend :imeta imeta :rmeta rmeta :slc slc :scc scc + :srl (ensure-integer srl) + :tfr (ensure-integer tfr) :cfr (ensure-integer cfr) + :cxty cxty :ttyl ttyl :atnl atnl :lat lat :cenc cenc + :curver curver :sabin sabin))) (defun find-usab-by-key (key-name key) "Find usab for a key" - (with-umlisp-query ('mrsab '(vcui rcui vsab rsab son sf sver mstart mend imeta rmeta slc scc srl tfr cfr cxty ttyl atnl lat cenc curver sabin) nil key-name key :single t) - (destructuring-bind - (vcui rcui vsab rsab son sf sver mstart mend imeta rmeta slc scc srl tfr cfr cxty ttyl atnl lat cenc curver sabin) tuple - (make-instance 'usab :vcui (ensure-integer vcui) - :rcui (ensure-integer rcui) - :vsab vsab :rsab rsab :son son :sf sf :sver sver :mstart mstart - :mend mend :imeta imeta :rmeta rmeta :slc slc :scc scc - :srl (ensure-integer srl) - :tfr (ensure-integer tfr) :cfr (ensure-integer cfr) - :cxty cxty :ttyl ttyl :atnl atnl :lat lat :cenc cenc - :curver curver :sabin sabin)))) + (with-umlisp-query ('mrsab (vcui rcui vsab rsab son sf sver mstart mend imeta rmeta slc scc srl tfr cfr cxty ttyl atnl lat cenc curver sabin) nil key-name key :single t) + (make-instance 'usab :vcui (ensure-integer vcui) + :rcui (ensure-integer rcui) + :vsab vsab :rsab rsab :son son :sf sf :sver sver :mstart mstart + :mend mend :imeta imeta :rmeta rmeta :slc slc :scc scc + :srl (ensure-integer srl) + :tfr (ensure-integer tfr) :cfr (ensure-integer cfr) + :cxty cxty :ttyl ttyl :atnl atnl :lat lat :cenc cenc + :curver curver :sabin sabin))) (defun find-usab-rsab (rsab) "Find usab for rsab" @@ -430,54 +388,39 @@ is OBJNAME from TABLE where WHERE-NAME field = WHERE-VALUE with FIELDS" (defun find-ucon-tui (tui &key (srl *current-srl*)) "Find list of ucon for tui" - (with-umlisp-query ('mrsty '(cui) srl 'tui (parse-tui tui) :lrlname 'klrl :terminal "order by cui desc") - (find-ucon-cui (ensure-integer (car tuple)) :srl srl))) + (with-umlisp-query ('mrsty (cui) srl 'tui (parse-tui tui) :lrlname 'klrl :terminal "order by cui desc") + (find-ucon-cui (ensure-integer cui) :srl srl))) (defun find-ucon-word (word &key (srl *current-srl*) (like nil)) "Return list of ucons that match word. Optionally, use SQL's LIKE syntax" - (let ((ucons '()) - (ls (format nil "select distinct cui from MRXW_ENG where wd~A'~A'" - (if like " LIKE " "=") - word))) - (when srl (string-append ls (format nil " and KLRL <= ~d" srl))) - (string-append ls " order by cui desc") - (dolist (tuple (mutex-sql-query ls)) - (push (find-ucon-cui (car tuple) :srl srl) ucons)) - ucons)) + (with-umlisp-query ('mrxw_eng (cui) srl 'wd word :like like :distinct t + :lrlname 'klrl :terminal "order by cui desc") + (find-ucon-cui cui :srl srl))) (defun find-ucon-normalized-word (word &key (srl *current-srl*) (like nil)) "Return list of ucons that match word, optionally use SQL's LIKE syntax" - (let ((ucons '()) - (ls (format nil "select distinct cui from MRXNW_ENG where nwd~A'~A'" - (if like " LIKE " "=") - word))) - (when srl (string-append ls (format nil " and KLRL <= ~d" srl))) - (string-append ls " order by cui desc") - (dolist (tuple (mutex-sql-query ls)) - (push (find-ucon-cui (car tuple) :srl srl) ucons)) - ucons)) + (with-umlisp-query ('mrxnw_eng (cui) srl 'nwd word :like like :distinct t + :lrlname 'klrl :terminal "order by cui desc") + (find-ucon-cui cui :srl srl))) (defun find-ustr-word (word &key (srl *current-srl*)) "Return list of ustrs that match word" - (with-umlisp-query ('mrxw_eng '(cui sui) srl 'wd word + (with-umlisp-query ('mrxw_eng (cui sui) srl 'wd word :lrlname 'klrl :terminal "order by cui desc, sui desc") - (destructuring-bind (cui sui) tuple - (find-ustr-cuisui (ensure-integer cui) (ensure-integer sui) :srl srl)))) + (find-ustr-cuisui (ensure-integer cui) (ensure-integer sui) :srl srl))) (defun find-ustr-normalized-word (word &key (srl *current-srl*)) "Return list of ustrs that match word" - (with-umlisp-query ('mrxnw_eng '(cui sui) srl 'nwd word :lrlname 'klrl + (with-umlisp-query ('mrxnw_eng (cui sui) srl 'nwd word :lrlname 'klrl :terminal "order by cui desc, sui desc") - (destructuring-bind (cui sui) tuple - (find-ustr-cuisui (ensure-integer cui) (ensure-integer sui) ;srl srl)))) + (find-ustr-cuisui (ensure-integer cui) (ensure-integer sui) :srl srl))) ;; Special tables (defun find-usrl-all () - (with-umlisp-query ('usrl '(sab srl) nil nil nil :terminal "order by sab desc") - (destructuring-bind (sab srl) tuple - (make-instance 'usrl :sab sab :srl (ensure-integer srl)) usrls))) + (with-umlisp-query ('usrl (sab srl) nil nil nil :terminal "order by sab desc") + (make-instance 'usrl :sab sab :srl (ensure-integer srl)) usrls)) ;;; Multiword lookup and score functions @@ -552,112 +495,96 @@ is OBJNAME from TABLE where WHERE-NAME field = WHERE-VALUE with FIELDS" ;;; LEX SQL functions (defun find-lexterm-eui (eui) - (with-umlisp-query ('lrwd '(wrd) nil 'eui eui :single t) - (make-instance 'lexterm :eui eui :wrd (car tuple)))) + (with-umlisp-query ('lrwd (wrd) nil 'eui eui :single t) + (make-instance 'lexterm :eui eui :wrd wrd))) (defun find-lexterm-word (wrd) - (with-umlisp-query ('lrwd '(eui) nil 'wrd wrd) - (make-instance 'lexterm :eui (ensure-integer (car tuple)) + (with-umlisp-query ('lrwd (eui) nil 'wrd wrd) + (make-instance 'lexterm :eui (ensure-integer eui) :wrd (copy-seq wrd)))) ;; LEX SQL Read functions (defun find-labr-eui (eui) - (with-umlisp-query ('lrabr '(bas abr eui2 bas2) nil 'eui eui) - (destructuring-bind (bas abr eui2 bas2) tuple - (make-instance 'labr :eui eui :bas bas :abr abr :bas2 bas2 - :eui2 (ensure-integer eui2))))) + (with-umlisp-query ('lrabr (bas abr eui2 bas2) nil 'eui eui) + (make-instance 'labr :eui eui :bas bas :abr abr :bas2 bas2 + :eui2 (ensure-integer eui2)))) (defun find-labr-bas (bas) - (with-umlisp-query ('labr '(eui abr eui2 bas2) nil 'bas bas) - (destructuring-bind (eui abr eui2 bas2) tuple - (make-instance 'labr :eui (ensure-integer eui) :abr abr :bas2 bas2 - :bas (copy-seq bas) :eui2 (ensure-integer eui2))))) + (with-umlisp-query ('labr (eui abr eui2 bas2) nil 'bas bas) + (make-instance 'labr :eui (ensure-integer eui) :abr abr :bas2 bas2 + :bas (copy-seq bas) :eui2 (ensure-integer eui2)))) (defun find-lagr-eui (eui) - (with-umlisp-query ('lragr '(str sca agr cit bas) nil 'eui eui) - (destructuring-bind (str sca agr cit bas) tuple - (make-instance 'lagr :eui eui :str str :sca sca :agr agr - :cit cit :bas bas)))) + (with-umlisp-query ('lragr (str sca agr cit bas) nil 'eui eui) + (make-instance 'lagr :eui eui :str str :sca sca :agr agr + :cit cit :bas bas))) (defun find-lcmp-eui (eui) - (with-umlisp-query ('lrcmp '(bas sca com) nil 'eui eui) - (destructuring-bind (bas sca com) tuple - (make-instance 'lcmp :eui eui :bas bas :sca sca :com com)))) + (with-umlisp-query ('lrcmp (bas sca com) nil 'eui eui) + (make-instance 'lcmp :eui eui :bas bas :sca sca :com com))) (defun find-lmod-eui (eui) - (with-umlisp-query ('lrmod '(bas sca psn_mod fea) nil 'eui eui) - (destructuring-bind (bas sca psnmod fea) tuple - (make-instance 'lmod :eui eui :bas bas :sca sca :psnmod psnmod :fea fea)))) + (with-umlisp-query ('lrmod (bas sca psn_mod fea) nil 'eui eui) + (make-instance 'lmod :eui eui :bas bas :sca sca :psnmod psnmod :fea fea))) (defun find-lnom-eui (eui) - (with-umlisp-query ('lrnom '(bas sca eui2 bas2 sca2) nil 'eui eui) - (destructuring-bind (bas sca eui2 bas2 sca2) tuple - (make-instance 'lnom :eui eui :bas bas :sca sca :bas2 bas2 :sca2 sca2 - :eui2 (ensure-integer eui2))))) + (with-umlisp-query ('lrnom (bas sca eui2 bas2 sca2) nil 'eui eui) + (make-instance 'lnom :eui eui :bas bas :sca sca :bas2 bas2 :sca2 sca2 + :eui2 (ensure-integer eui2)))) (defun find-lprn-eui (eui) - (with-umlisp-query ('lrprn '(bas num gnd cas pos qnt fea) nil 'eui eui) - (destructuring-bind (bas num gnd cas pos qnt fea) tuple - (make-instance 'lprn :eui eui :bas bas :num num :gnd gnd - :cas cas :pos pos :qnt qnt :fea fea)))) + (with-umlisp-query ('lrprn (bas num gnd cas pos qnt fea) nil 'eui eui) + (make-instance 'lprn :eui eui :bas bas :num num :gnd gnd + :cas cas :pos pos :qnt qnt :fea fea))) (defun find-lprp-eui (eui) - (with-umlisp-query ('lrprp '(bas str sca fea) nil 'eui eui) - (destructuring-bind (bas str sca fea) tuple - (make-instance 'lprp :eui eui :bas bas :str str :sca sca :fea fea)))) + (with-umlisp-query ('lrprp (bas str sca fea) nil 'eui eui) + (make-instance 'lprp :eui eui :bas bas :str str :sca sca :fea fea))) (defun find-lspl-eui (eui) - (with-umlisp-query ('lrspl '(spv bas) nil 'eui eui) - (destructuring-bind (spv bas) tuple - (make-instance 'lspl :eui eui :spv spv :bas bas)))) + (with-umlisp-query ('lrspl (spv bas) nil 'eui eui) + (make-instance 'lspl :eui eui :spv spv :bas bas))) (defun find-ltrm-eui (eui) - (with-umlisp-query ('lrtrm '(bas gen) nil 'eui eui) + (with-umlisp-query ('lrtrm (bas gen) nil 'eui eui) (make-instance 'ltrm :eui eui :bas bas :gen gen))) (defun find-ltyp-eui (eui) - (with-umlisp-query ('lrtyp '(bas sca typ) nil 'eui eui) - (destructuring-bind (bas sca typ) tuple - (make-instance 'ltyp :eui eui :bas bas :sca sca :typ typ)))) + (with-umlisp-query ('lrtyp (bas sca typ) nil 'eui eui) + (make-instance 'ltyp :eui eui :bas bas :sca sca :typ typ))) (defun find-lwd-wrd (wrd) (make-instance 'lwd :wrd - :euilist (with-umlisp-query ('lrwd '(eui) nil 'wrd wrd) - (ensure-integer (car tuple))))) + :euilist (with-umlisp-query ('lrwd (eui) nil 'wrd wrd) + (ensure-integer eui)))) ;;; Semantic Network SQL access functions - + (defun find-sdef-ui (ui) - (with-umlisp-query ('srdef '(rt sty_rl stn_rtn def ex un rh abr rin) + (with-umlisp-query ('srdef (rt sty_rl stn_rtn def ex un rh abr rin) nil 'ui ui :single t) - (destructuring-bind (rt styrl stnrtn def ex un rh abr rin) tuple - (make-instance 'sdef :rt rt :ui ui :styrl styrl :stnrtn stnrtn - :def def :ex ex :un un :rh rh :abr abr :rin rin)))) + (make-instance 'sdef :rt rt :ui ui :styrl styrl :stnrtn stnrtn + :def def :ex ex :un un :rh rh :abr abr :rin rin))) (defun find-sstre1-ui (ui) - (with-umlisp-query ('srstre1 '(ui2 ui3) nil 'ui ui) - (destructuring-bind (ui2 ui3) tuple - (make-instance 'sstre1 :ui ui :ui2 (ensure-integer ui2) - :ui3 (ensure-integer ui3))))) + (with-umlisp-query ('srstre1 (ui2 ui3) nil 'ui ui) + (make-instance 'sstre1 :ui ui :ui2 (ensure-integer ui2) + :ui3 (ensure-integer ui3)))) (defun find-sstre1-ui2 (ui2) - (with-umlisp-query ('srstre1 '(ui ui3) nil 'ui2 ui2) - (destructuring-bind (ui ui3) tuple - (make-instance 'sstre1 :ui (ensure-integer ui) :ui2 ui2 - :ui3 (ensure-integer ui3))))) + (with-umlisp-query ('srstre1 (ui ui3) nil 'ui2 ui2) + (make-instance 'sstre1 :ui (ensure-integer ui) :ui2 ui2 + :ui3 (ensure-integer ui3)))) (defun find-sstr-rl (rl) - (with-umlisp-query ('srstre '(sty_rl sty_rl2 ls) nil 'rl rl) - (destructuring-bind (styrl styrl2 ls) tuple - (make-instance 'sstr :rl rl :styrl styrl :styrl2 styrl2 :ls ls)))) + (with-umlisp-query ('srstre (sty_rl sty_rl2 ls) nil 'rl rl) + (make-instance 'sstr :rl rl :styrl styrl :styrl2 styrl2 :ls ls))) (defun find-sstre2-sty (sty) - (with-umlisp-query ('srstre2 '(rl sty2) nil 'sty sty) - (destructuring-bind (rl sty2) tuple - (make-instance 'sstre2 :sty (copy-seq sty) :rl rl :sty2 sty2)))) + (with-umlisp-query ('srstre2 (rl sty2) nil 'sty sty) + (make-instance 'sstre2 :sty (copy-seq sty) :rl rl :sty2 sty2))) (defun find-sstr-styrl (styrl) - (with-umlisp-query ('srstr '(rl sty_rl2 ls) nil 'styrl styrl) - (destructuring-bind (rl styrl2 ls) tuple - (make-instance 'sstr :styrl styrl :rl rl :styrl2 styrl2 :ls ls)))) + (with-umlisp-query ('srstr (rl sty_rl2 ls) nil 'styrl styrl) + (make-instance 'sstr :styrl styrl :rl rl :styrl2 styrl2 :ls ls))) diff --git a/utils.lisp b/utils.lisp index c25cf1d..6b9e17e 100644 --- a/utils.lisp +++ b/utils.lisp @@ -7,7 +7,7 @@ ;;;; Programmer: Kevin M. Rosenberg ;;;; Date Started: Apr 2000 ;;;; -;;;; $Id: utils.lisp,v 1.2 2002/10/09 23:03:41 kevin Exp $ +;;;; $Id: utils.lisp,v 1.3 2003/05/02 21:49:19 kevin Exp $ ;;;; ;;;; This file, part of UMLisp, is ;;;; Copyright (c) 2000-2002 by Kevin M. Rosenberg, M.D. @@ -93,3 +93,21 @@ (let* ((cui (the fixnum (truncate (/ cuisui +cuisui-scale+)))) (sui (the fixnum (- cuisui (* cui +cuisui-scale+))))) (values cui sui))) + +;;; Lookup functions for uterms,ustr in ucons + +(defun find-uterm-in-ucon (ucon lui) + (find lui (s#term ucon) :key #'lui :test 'equal)) + +(defun find-ustr-in-uterm (uterm sui) + (find sui (s#str uterm) :key #'sui :test 'equal)) + +(defun find-ustr-in-ucon (ucon sui) + (let ((found-ustr nil)) + (dolist (uterm (s#term ucon)) + (unless found-ustr + (dolist (ustr (s#str uterm)) + (unless found-ustr + (when (string-equal sui (sui ustr)) + (setq found-ustr ustr)))))) + found-ustr)) -- 2.34.1