;;; $Id: obj-sql.lisp,v 1.1 2002/10/05 20:17:14 kevin Exp $ (in-package :umlisp) (declaim (optimize (speed 3) (safety 1))) (defvar *current-srl* nil) (defun current-srl () *current-srl*) (defun current-srl! (srl) (setq *current-srl* srl)) ;;; Initializers (defun post-import-sql () (make-ustats) (make-usrl) (make-user-table) #+pubmed (create-pmsearch-table)) ;;; Accessors (read on demand) ;; defines a slot-unbound method for class and slot-name, fills ;; the slot by calling reader function with the slot values of ;; the instance's reader-keys (defmacro def-lazy-reader (class slot-name reader &rest reader-keys) (let* ((the-slot-name (gensym)) (the-class (gensym)) (the-instance (gensym)) (keys '())) (dolist (key reader-keys) (push (list 'slot-value the-instance (list 'quote key)) keys)) (setq keys (nreverse keys)) `(defmethod slot-unbound (,the-class (,the-instance ,class) (,the-slot-name (eql ',slot-name))) (declare (ignore ,the-class)) (setf (slot-value ,the-instance ,the-slot-name) (,reader ,@keys))))) (def-lazy-reader ucon s#term find-uterm-cui cui) (def-lazy-reader ucon s#def find-udef-cui cui) (def-lazy-reader ucon s#sty find-usty-cui cui) (def-lazy-reader ucon s#rel find-urel-cui cui) (def-lazy-reader ucon s#coc find-ucoc-cui cui) (def-lazy-reader ucon s#lo find-ulo-cui cui) (def-lazy-reader ucon s#atx find-uatx-cui cui) (def-lazy-reader ucon s#sat find-usat-ui cui) ;; For uterms (def-lazy-reader uterm s#str find-ustr-cuilui cui lui) (def-lazy-reader uterm s#sat find-usat-ui cui lui) ;; For ustrs (def-lazy-reader ustr s#sat find-usat-ui cui lui sui) (def-lazy-reader ustr s#cxt find-ucxt-cuisui cui sui) (def-lazy-reader ustr s#so find-uso-cuisui cui sui) ;;; Object lookups ;;; Lookup functions for uterms,ustr in ucons (defun find-uterm-in-ucon (ucon lui) (find lui (s#term ucon) :key #'uterm-lui :test 'equal)) (defun find-ustr-in-uterm (uterm sui) (find sui (s#str uterm) :key #'ustr-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)) (defun find-ucon-cui (cui &key (srl *current-srl*)) "Find ucon for a cui" (if (stringp cui) (setq cui (parse-cui cui))) (if cui (let ((ls (format nil "select KPFSTR,KCUILRL from MRCON where CUI=~d" cui))) (if srl (string-append ls (format nil " and KCUILRL <= ~d limit 1" srl)) (string-append ls " limit 1")) (gu:awhen (car (mutex-sql-query ls)) (make-instance 'ucon :cui cui :pfstr (car gu::it) :lrl (ensure-integer (cadr gu::it))))) nil)) (defun find-ucon-lui (lui &key (srl *current-srl*)) "Find list of ucon for lui" (if (stringp lui) (setq lui (parse-lui lui))) (if lui (let ((ucons '()) (ls (format nil "select distinct CUI,KPFSTR,KCUILRL from MRCON where LUI=~d" lui))) (if srl (string-append ls (format nil " and KCUILRL <= ~d" srl))) (dolist (tuple (mutex-sql-query ls)) (push (make-instance 'ucon :cui (ensure-integer (nth 0 tuple)) :pfstr (nth 1 tuple) :lrl (ensure-integer (nth 2 tuple))) ucons)) (nreverse ucons)) nil)) (defun find-ucon-sui (sui &key (srl *current-srl*)) "Find list of ucon for sui" (if (stringp sui) (setq sui (parse-sui sui))) (if sui (let ((ucons '()) (ls (format nil "select distinct CUI,KPFSTR,KCUILRL from MRCON where SUI=~d" sui))) (when srl (string-append ls (format nil " and KCUILRL <= ~d" srl))) (let ((tuples (mutex-sql-query ls))) (dolist (tuple tuples) (push (make-instance 'ucon :cui (ensure-integer (nth 0 tuple)) :pfstr (nth 1 tuple) :lrl (ensure-integer (nth 2 tuple))) ucons))) (nreverse ucons)) nil)) (defun find-ucon-cuisui (cui sui &key (srl *current-srl*)) "Find ucon for cui/sui" (if (stringp cui) (setq cui (parse-cui cui))) (if (stringp sui) (setq sui (parse-sui sui))) (if (and cui sui) (let ((ls (format nil "select distinct CUI,KPFSTR,KCUILRL from MRCON where KCUISUI=~d" (make-cuisui cui sui)))) (when srl (string-append ls (format nil " and KCUILRL <= ~d" srl))) (gu:aif (car (mutex-sql-query ls)) (make-instance 'ucon :cui (ensure-integer (nth 0 gu::it)) :pfstr (nth 1 gu::it) :lrl (ensure-integer (nth 2 gu::it))) nil)) nil)) (defun find-ucon-str (str &key (srl *current-srl*)) "Find ucon that are exact match for str" (if str (let ((ucons '()) (ls (format nil "select distinct CUI,KPFSTR,KCUILRL from MRCON where STR='~a'" str))) (when srl (string-append ls " and KCUILRL <= ~d" srl)) (dolist (tuple (mutex-sql-query ls)) (push (make-instance 'ucon :cui (ensure-integer (nth 0 tuple)) :pfstr (nth 1 tuple) :lrl (ensure-integer (nth 2 tuple))) ucons)) (nreverse ucons)) nil)) (defun find-ucon-all (&key (srl *current-srl*)) "Return list of all ucon's" (let ((ls "select distinct CUI,KPFSTR,KCUILRL from MRCON")) (when srl (string-append ls (format nil " where KCUILRL <= ~d" srl))) (string-append ls " order by CUI asc") (with-sql-connection (db) (clsql:map-query 'list #'(lambda (cui pfstr cuilrl) (make-instance 'ucon :cui (ensure-integer cui) :pfstr pfstr :lrl (ensure-integer cuilrl))) ls :database db)))) (defun find-udef-cui (cui &key (srl *current-srl*)) "Return a list of udefs for cui" (let ((udefs '()) (ls (format nil "select SAB,DEF from MRDEF where CUI=~d" cui))) (when srl (string-append ls (format nil " and KSRL <= ~d" srl))) (dolist (tuple (mutex-sql-query ls)) (push (make-instance 'udef :sab (car tuple) :def (cadr tuple)) udefs)) (nreverse udefs))) (defun find-usty-cui (cui &key (srl *current-srl*)) "Return a list of usty for cui" (let ((ustys '()) (ls (format nil "select TUI,STY from MRSTY where CUI=~d" cui))) (when srl (string-append ls (format nil " and KLRL <= ~d" srl))) (dolist (tuple (mutex-sql-query ls)) (push (make-instance 'usty :tui (ensure-integer (car tuple)) :sty (cadr tuple)) ustys)) ustys)) (defun find-usty-word (word &key (srl *current-srl*)) "Return a list of usty that match word" (let ((ustys '()) (ls (format nil "select distinct TUI,STY from MRSTY where STY like '%~a%'" word))) (when srl (string-append ls (format nil " and KLRL <= ~d" srl))) (dolist (tuple (mutex-sql-query ls)) (push (make-instance 'usty :tui (ensure-integer (car tuple)) :sty (cadr tuple)) ustys)) ustys)) (defun find-urel-cui (cui &key (srl *current-srl*)) "Return a list of urel for cui" (let ((urels '()) (ls (format nil "select REL,CUI2,RELA,SAB,SL,MG,KPFSTR2 from MRREL where CUI1=~d" cui))) (when srl (string-append ls (format nil " and KSRL <= ~d" srl))) (dolist (tuple (mutex-sql-query ls)) (push (make-instance 'urel :cui1 cui :rel (nth 0 tuple) :cui2 (ensure-integer (nth 1 tuple)) :rela (nth 2 tuple) :sab (nth 3 tuple) :sl (nth 4 tuple) :mg (nth 5 tuple) :pfstr2 (nth 6 tuple)) urels)) (nreverse urels))) (defun find-urel-cui2 (cui2 &key (srl *current-srl*)) "Return a list of urel for cui2" (let ((urels '()) (ls (format nil "select REL,CUI1,RELA,SAB,SL,MG,KPFSTR2 from MRREL where CUI2=~d" cui2))) (when srl (string-append ls (format nil " and SRL <= ~d" srl))) (dolist (tuple (mutex-sql-query ls)) (push (make-instance 'urel :cui2 cui2 :rel (nth 0 tuple) :cui1 (ensure-integer (nth 1 tuple)) :rela (nth 2 tuple) :sab (nth 3 tuple) :sl (nth 4 tuple) :mg (nth 5 tuple) :pfstr2 (nth 6 tuple)) urels)) (nreverse urels))) (defun find-ucon-rel-cui2 (cui2 &key (srl *current-srl*)) (mapcar #'(lambda (cui) (find-ucon-cui cui :key srl)) (remove-duplicates (mapcar #'cui1 (find-urel-cui2 cui2 :srl srl))))) (defun find-ucoc-cui (cui &key (srl *current-srl*)) "Return a list of ucoc for cui" (let ((ucocs '()) (ls (format nil "select CUI2,SOC,COT,COF,COA,KPFSTR2 from MRCOC where CUI1=~d" cui))) (when srl (string-append ls (format nil " and KLRL <= ~d" srl))) (string-append ls " order by COF asc") (dolist (tuple (mutex-sql-query ls)) (let ((cui2 (ensure-integer (nth 0 tuple)))) (when (zerop cui2) (setq cui2 nil)) (push (make-instance 'ucoc :cui1 cui :cui2 cui2 :soc (nth 1 tuple) :cot (nth 2 tuple) :cof (ensure-integer (nth 3 tuple)) :coa (nth 4 tuple) :pfstr2 (nth 5 tuple)) ucocs))) ucocs)) ;; akready ordered by SQL select (defun find-ucoc-cui2 (cui2 &key (srl *current-srl*)) "Return a list of ucoc for cui2" (let ((ucocs '()) (ls (format nil "select CUI1,SOC,COT,COF,COA,KPFSTR2 from MRCOC where CUI2=~d" cui2))) (when srl (string-append ls (format nil " and KSRL <= ~d" srl))) (string-append ls " order by COF asc") (dolist (tuple (mutex-sql-query ls)) (push (make-instance 'ucoc :cui1 (ensure-integer (nth 0 tuple)) :cui2 cui2 :soc (nth 1 tuple) :cot (nth 2 tuple) :cof (ensure-integer (nth 3 tuple)) :coa (nth 4 tuple) :pfstr2 (nth 5 tuple)) ucocs)) ucocs)) ;; already ordered by SQL select (defun find-ucon-coc-cui2 (cui2 &key (srl *current-srl*)) "List of ucon with co-occurance cui2" (mapcar #'(lambda (cui) (find-ucon-cui cui :key srl)) (remove-duplicates (mapcar #'cui1 (find-ucoc-cui2 cui2 :srl srl))))) (defun find-ulo-cui (cui &key (srl *current-srl*)) "Return a list of ulo for cui" (let ((ulos '()) (ls (format nil "select ISN,FR,UN,SUI,SNA,SOUI from MRLO where CUI=~d" cui))) (when srl (string-append ls (format nil " and KLRL <= ~d" srl))) (dolist (tuple (mutex-sql-query ls)) (push (make-instance 'ulo :isn (nth 0 tuple) :fr (ensure-integer (nth 1 tuple)) :un (nth 2 tuple) :sui (ensure-integer (nth 3 tuple)) :sna (nth 4 tuple) :soui (nth 5 tuple)) ulos)) (nreverse ulos))) (defmethod suistr ((lo ulo)) "Return the string for a ulo object" (find-string-sui (sui lo))) (defun find-uatx-cui (cui &key (srl *current-srl*)) "Return a list of uatx for cui" (let ((uatxs '()) (ls (format nil "select SAB,REL,ATX from MRATX where CUI=~d" cui))) (when srl (string-append ls (format nil " and KSRL <= ~d" srl))) (dolist (tuple (mutex-sql-query ls)) (push (make-instance 'uatx :sab (nth 0 tuple) :rel (nth 1 tuple) :atx (nth 2 tuple)) uatxs)) (nreverse uatxs))) (defun find-uterm-cui (cui &key (srl *current-srl*)) "Return a list of uterm for cui" (let ((uterms '()) (ls (format nil "select distinct LUI,LAT,TS,KLUILRL from MRCON where CUI=~d" cui))) (when srl (string-append ls (format nil " and KLUILRL <= ~d" srl))) (dolist (tuple (mutex-sql-query ls)) (push (make-instance 'uterm :lui (ensure-integer (nth 0 tuple)) :cui cui :lat (nth 1 tuple) :ts (nth 2 tuple) :lrl (ensure-integer (nth 3 tuple))) uterms)) (nreverse uterms))) (defun find-uterm-lui (lui &key (srl *current-srl*)) "Return a list of uterm for lui" (if (stringp lui) (setq lui (parse-lui lui))) (let ((uterms '()) (ls (format nil "select distinct CUI,LAT,TS,KLUILRL from MRCON where LUI=~d" lui))) (when srl (string-append ls (format nil " and KLUILRL <= ~d" srl))) (dolist (tuple (mutex-sql-query ls)) (push (make-instance 'uterm :cui (ensure-integer (nth 0 tuple)) :lui lui :lat (nth 1 tuple) :ts (nth 2 tuple) :lrl (ensure-integer (nth 3 tuple))) uterms)) (nreverse uterms))) (defun find-uterm-cuilui (cui lui &key (srl *current-srl*)) "Return single uterm for cui/lui" (let ((ls (format nil "select LAT,TS,KLUILRL from MRCON where KCUILUI=~d limit 1" (make-cuilui cui lui)))) (when srl (string-append ls (format nil " and KLUILRL <= ~d" srl))) (gu:aif (car (mutex-sql-query ls)) (make-instance 'uterm :cui cui :lui lui :lat (nth 0 gu::it) :ts (nth 1 gu::it) :lrl (ensure-integer (nth 2 gu::it))) nil))) (defun find-ustr-cuilui (cui lui &key (srl *current-srl*)) "Return a list of ustr for cui/lui" (declare (fixnum cui lui)) (let ((ustrs '()) (ls (format nil "select SUI,STT,STR,LRL from MRCON where KCUILUI=~d" (make-cuilui cui lui)))) (when srl (string-append ls (format nil " and LRL <= ~d" srl))) (dolist (tuple (mutex-sql-query ls)) (let* ((sui (ensure-integer (car tuple))) (ustr (make-instance 'ustr :sui sui :cui cui :cuisui (make-cuisui cui sui) :lui lui :stt (nth 1 tuple) :str (nth 2 tuple) :lrl (ensure-integer (nth 3 tuple))))) (push ustr ustrs))) (nreverse ustrs))) (defun find-ustr-cuisui (cui sui &key (srl *current-srl*)) "Return the single ustr for cuisui" (let ((ls (format nil "select LUI,STT,STR,LRL from MRCON where KCUISUI=~d" (make-cuisui cui sui)))) (when srl (string-append ls (format nil " and LRL <= ~d" srl))) (gu:aif (car (mutex-sql-query ls)) (make-instance 'ustr :sui sui :cui cui :cuisui (make-cuisui cui sui) :lui (ensure-integer (nth 0 gu::it)) :stt (nth 1 gu::it) :str (nth 2 gu::it) :lrl (ensure-integer (nth 3 gu::it))) nil))) (defun find-ustr-sui (sui &key (srl *current-srl*)) "Return the list of ustr for sui" (if (stringp sui) (setq sui (parse-sui sui))) (let ((ustrs '()) (ls (format nil "select CUI,LUI,STT,STR,LRL from MRCON where SUI=~d" sui))) (when srl (string-append ls (format nil " and LRL <= ~d" srl))) (dolist (tuple (mutex-sql-query ls)) (let ((cui (ensure-integer (car tuple)))) (push (make-instance 'ustr :sui sui :cui cui :cuisui (make-cuisui cui sui) :lui (ensure-integer (nth 1 tuple)) :stt (nth 2 tuple) :str (nth 3 tuple) :lrl (ensure-integer (nth 4 tuple))) ustrs))) (nreverse ustrs))) (defun find-ustr-sab (sab &key (srl *current-srl*)) "Return the list of ustr for sab" (let ((ustrs '()) (ls (format nil "select KCUISUI from MRSO where SAB='~a'" sab))) (when srl (string-append ls (format nil " and SRL <= ~d" srl))) (dolist (tuple (mutex-sql-query ls)) (let ((cuisui (ensure-integer (car tuple)))) (push (apply #'find-ustr-cuisui (append (multiple-value-list (decompose-cuisui cuisui)) (list :srl srl))) ustrs))) (nreverse ustrs))) (defun find-ustr-all (&key (srl *current-srl*)) "Return list of all ustr's" (let ((ls "select distinct CUI,LUI,SUI,STT,LRL,KPFSTR from MRCON")) (when srl (string-append ls (format nil " where LRL <= ~d" srl))) (string-append ls " order by SUI asc") (with-sql-connection (db) (clsql:map-query 'list #'(lambda (cui lui sui stt lrl pfstr) (setq cui (ensure-integer cui)) (setq lui (ensure-integer lui)) (setq sui (ensure-integer sui)) (setq lrl (ensure-integer lrl)) (make-instance 'ustr :cui cui :lui lui :sui sui :cuisui (make-cuisui cui sui) :stt stt :lrl lrl :str pfstr)) ls :database db)))) (defun find-string-sui (sui &key (srl *current-srl*)) "Return the string associated with sui" (let ((ls (format nil "select STR from MRCON where SUI=~d" sui))) (when srl (string-append ls (format nil " and LRL <= ~d" srl))) (string-append ls " limit 1") (caar (mutex-sql-query ls)))) (defun find-uso-cuisui (cui sui &key (srl *current-srl*)) (declare (fixnum cui sui)) (let ((usos '()) (ls (format nil "select SAB,CODE,SRL,TTY from MRSO where KCUISUI=~d" (make-cuisui cui sui)))) (when srl (string-append ls (format nil " and SRL <= ~d" srl))) (dolist (tuple (mutex-sql-query ls)) (push (make-instance 'uso :sab (nth 0 tuple) :code (nth 1 tuple) :srl (nth 2 tuple) :tty (nth 3 tuple)) usos)) (nreverse usos))) (defun find-ucxt-cuisui (cui sui &key (srl *current-srl*)) (declare (fixnum cui sui)) (let ((ucxts '()) (ls (format nil "select SAB,CODE,CXN,CXL,RNK,CXS,CUI2,HCD,RELA,XC from MRCXT where KCUISUI=~d" (make-cuisui cui sui)))) (when srl (string-append ls (format nil " and KSRL <= ~d" srl))) (dolist (tuple (mutex-sql-query ls)) (push (make-instance 'ucxt :sab (nth 0 tuple) :code (nth 1 tuple) :cxn (ensure-integer (nth 2 tuple)) :cxl (nth 3 tuple) :rnk (ensure-integer (nth 4 tuple)) :cxs (nth 5 tuple) :cui2 (ensure-integer (nth 6 tuple)) :hcd (nth 7 tuple) :rela (nth 8 tuple) :xc (nth 9 tuple)) ucxts)) (nreverse ucxts))) (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)))) (when srl (string-append ls (format nil " and KSRL <= ~d" srl))) (let ((usats '())) (dolist (tuple (mutex-sql-query ls)) (push (make-instance 'usat :code (nth 0 tuple) :atn (nth 1 tuple) :sab (nth 2 tuple) :atv (nth 3 tuple)) usats)) (nreverse usats)))) (defun find-bsab-sab (sab) (gu:aif (car (mutex-sql-query (format nil "select NAME,COUNT from BONUS_SAB where SAB='~a'" sab))) (make-instance 'bsab :sab sab :name (nth 0 gu::it) :hits (ensure-integer (nth 1 gu::it))) nil)) (defun find-bsab-all () (let ((usabs '())) (dolist (tuple (mutex-sql-query "select SAB,NAME,COUNT from BONUS_SAB")) (push (make-instance 'bsab :sab (nth 0 tuple) :name (nth 1 tuple) :hits (ensure-integer (nth 2 tuple))) usabs)) (nreverse usabs))) (defun find-btty-tty (tty) (gu:aif (car (mutex-sql-query (format nil "select NAME from BONUS_TTY where TTY='~a'" tty))) (make-instance 'btty :tty tty :name (nth 0 gu::it)) nil)) (defun find-btty-all () (let ((uttys '())) (dolist (tuple (mutex-sql-query "select TTY,NAME from BONUS_TTY")) (push (make-instance 'btty :tty (nth 0 tuple) :name (nth 1 tuple)) uttys)) (nreverse uttys))) (defun find-brel-rel (rel) (let ((brels '())) (dolist (tuple (mutex-sql-query (format nil "select SAB,SL,REL,RELA,COUNT from BONUS_REL where REL='~a'" rel))) (push (make-instance 'brel :sab (nth 0 tuple) :sl (nth 1 tuple) :rel (nth 2 tuple) :rela (nth 3 tuple) :hits (ensure-integer (nth 4 tuple))) brels)) (nreverse brels))) (defun find-pfstr-cui (cui) (caar (mutex-sql-query (format nil "select KPFSTR from MRCON where CUI=~d limit 1" cui)))) (defun find-usty-tui (tui) "Find usty for tui" (setq tui (parse-tui tui)) (gu:aif (car (mutex-sql-query (format nil "select STY from MRSTY where TUI=~d limit 1" tui))) (make-instance 'usty :tui tui :sty (nth 0 gu::it)) nil)) (defun find-usty-sty (sty) "Find usty for a sty" (gu:aif (car (mutex-sql-query (format nil "select TUI from MRSTY where STY='~a' limit 1" sty))) (make-instance 'usty :tui (ensure-integer (nth 0 gu::it)) :sty sty) nil)) (defun find-usty-all () "Return list of usty's for all semantic types" (let ((ustys '())) (dolist (tuple (mutex-sql-query "select distinct TUI from MRSTY")) (push (find-usty-tui (nth 0 tuple)) ustys)) (nreverse ustys))) (defun find-usty_freq-all () (let ((usty_freqs '())) (dolist (tuple (mutex-sql-query "select distinct TUI from MRSTY")) (let* ((tui (car tuple)) (freq (ensure-integer (caar (mutex-sql-query (format nil "select count(*) from MRSTY where TUI=~a" tui)))))) (push (make-instance 'usty_freq :usty (find-usty-tui tui) :freq freq) usty_freqs))) (sort usty_freqs #'> :key #'usty_freq-freq))) (defun make-user-table () (mutex-sql-execute "create table UMLISP_USERS (ID integer UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,FIRST_NAME varchar(20),LAST_NAME varchar(20),ORGANIZATION varchar(80),ADDRESS1 varchar(60),ADDRESS2 varchar(60),CITY varchar(30),STATE char(2),ZIP char(10),COUNTRY varchar(40),OCCUPATION varchar(120),EMAIL varchar(80),PASSWD varchar(20),MAILLIST char(1),LICENSED char(1),SRL integer,TIMEOUT integer,DATETIME_CREATED datetime,DATETIME_MODIFIED datetime)")) (defun find-umlisp-user-email (email) (let ((tuple (car (mutex-sql-query (format nil "select ID,FIRST_NAME,LAST_NAME,ORGANIZATION,ADDRESS1,ADDRESS2,CITY,STATE,ZIP,COUNTRY,OCCUPATION,LICENSED,MAILLIST,PASSWD,SRL,TIMEOUT,DATETIME_CREATED,DATETIME_MODIFIED from UMLISP_USERS where EMAIL='~a'" email))))) (when tuple (make-instance 'umlisp-user :email email :id (ensure-integer (nth 0 tuple)) :first-name (nth 1 tuple) :last-name (nth 2 tuple) :organization (nth 3 tuple) :address1 (nth 4 tuple) :address2 (nth 5 tuple) :city (nth 6 tuple) :state (nth 7 tuple) :zip (nth 8 tuple) :country (nth 9 tuple) :occupation (nth 10 tuple) :licensed (if (string-equal "Y" (nth 11 tuple)) t nil) :maillist (if (string-equal "Y" (nth 12 tuple)) t nil) :passwd (nth 13 tuple) :srl (ensure-integer (nth 14 tuple)) :timeout (ensure-integer (nth 15 tuple)) :datetime-created (nth 16 tuple) :datetime-modified (nth 17 tuple))))) (defun find-umlisp-user-all () (let ((users '())) (dolist (email (find-umlisp-user-all-email)) (push (find-umlisp-user-email email) users)) (nreverse users))) (defun find-umlisp-user-all-email () (let ((emails '())) (dolist (tuple (mutex-sql-query "select EMAIL from UMLISP_USERS")) (push (car tuple) emails)) (nreverse emails))) (defun find-umlisp-user-announce-email () (let ((emails '())) (dolist (tuple (mutex-sql-query "select EMAIL from UMLISP_USERS where MAILLIST='Y'")) (push (car tuple) emails)) (nreverse emails))) (defun add-umlisp-user (user) (if (typep user 'umlisp-user) (progn (mutex-sql-execute (format nil "insert into UMLISP_USERS (FIRST_NAME,LAST_NAME,ORGANIZATION,ADDRESS1,ADDRESS2,CITY,STATE,ZIP,COUNTRY,OCCUPATION,LICENSED,MAILLIST,EMAIL,PASSWD,SRL,TIMEOUT,DATETIME_CREATED,DATETIME_MODIFIED) values ('~a','~a','~a','~a','~a','~a','~a','~a','~a','~a','~a','~a','~a','~a',~d,~d,NOW(),NOW())" (first-name user) (last-name user) (organization user) (address1 user) (address2 user) (city user) (state user) (zip user) (country user) (occupation user) (if (licensed user) #\Y #\N) (if (maillist user) #\Y #\N) (email user) (passwd user) (srl user) (timeout user))) (let ((read-user (find-umlisp-user-email (email user)))) (setf (slot-value user 'id) (id read-user) (slot-value user 'datetime-created) (datetime-created read-user) (slot-value user 'datetime-modified) (datetime-modified read-user))) t) nil)) (defun umlisp-user-verify-passwd (user passwd) (when user (string-equal passwd (passwd user)))) (defun umlisp-user-set-srl (email srl) (when (and (integerp srl) (find-umlisp-user-email email)) (mutex-sql-execute (format nil "update UMLISP_USERS set SRL=~d,DATETIME_MODIFIED=NOW() where EMAIL='~a'" srl email)) srl)) (defun make-ustats () (with-sql-connection (conn) (sql-execute "drop table if exists USTATS" conn) (sql-execute "create table USTATS (NAME varchar(160), COUNT bigint, SRL integer)" conn) (dotimes (srl 4) (insert-ustats-count conn "Concept Count" "MRCON" "distinct CUI" "KCUILRL" srl) (insert-ustats-count conn "Term Count" "MRCON" "distinct KCUILUI" "KCUILRL" srl) (insert-ustats-count conn "Distinct Term Count" "MRCON" "distinct LUI" "KLUILRL" srl) (insert-ustats-count conn "String Count" "MRCON" "*" "LRL" srl) (insert-ustats-count conn "Distinct String Count" "MRCON" "distinct SUI" "LRL" srl) (insert-ustats-count conn "Associated Expression Count" "MRATX" "*" "KSRL" srl) (insert-ustats-count conn "Context Count" "MRCXT" "*" "KSRL" srl) (insert-ustats-count conn "Co-occuring Concept Count" "MRCOC" "*" "KLRL" srl) (insert-ustats-count conn "Definition Count" "MRDEF" "*" "KSRL" srl) (insert-ustats-count conn "Locator Count" "MRLO" "*" "KLRL" srl) (insert-ustats-count conn "Rank Count" "MRRANK" "*" "KSRL" srl) (insert-ustats-count conn "Relationship Count" "MRREL" "*" "KSRL" srl) (insert-ustats-count conn "Semantic Type Count" "MRSTY" "*" "KLRL" srl) (insert-ustats-count conn "Simple Attribute Count" "MRSAT" "*" "KSRL" srl) (insert-ustats-count conn "Source Count" "MRSO" "*" "SRL" srl) (insert-ustats-count conn "Word Index Count" "MRXW_ENG" "*" "KLRL" srl) (insert-ustats-count conn "Normalized Word Index Count" "MRXNW_ENG" "*" "KLRL" srl) (insert-ustats-count conn "Normalized String Index Count" "MRXNS_ENG" "*" "KLRL" srl) (insert-ustats-count conn "Bonus Attribute Name Count" "BONUS_ATN" "*" nil srl) (insert-ustats-count conn "Bonus Relationship Count" "BONUS_REL" "*" nil srl) (insert-ustats-count conn "Bonus Source Abbreviation Count" "BONUS_SAB" "*" nil srl) (insert-ustats-count conn "Bonus Term Type Count" "BONUS_TTY" "*" nil srl)) (sql-execute "create index USTATS_SRL on USTATS (SRL)" conn)) (find-ustats-all)) (defun insert-ustats-count (conn name table count-variable srl-control srl) (insert-ustats conn name (find-count-table conn table srl count-variable srl-control) srl)) (defun find-count-table (conn table srl count-variable srl-control) (cond ((stringp srl-control) (ensure-integer (caar (sql-query (format nil "select count(~a) from ~a where ~a <= ~d" count-variable table srl-control srl) conn)))) ((null srl-control) (ensure-integer (caar (sql-query (format nil "select count(~a) from ~a" count-variable table ) conn)))) (t (error "Unknown srl-control") 0))) (defun insert-ustats (conn name count srl) (sql-execute (format nil "insert into USTATS (name,count,srl) values ('~a',~d,~d)" name count (if srl srl 3)) conn)) (defun find-ustats-all (&key (srl *current-srl*)) (let ((ustats '()) (ls "select NAME,COUNT,SRL from USTATS")) (when srl (string-append ls (format nil " where SRL=~d" srl))) (string-append ls " order by NAME asc") (dolist (tuple (mutex-sql-query ls)) (push (make-instance 'ustats :name (nth 0 tuple) :hits (ensure-integer (nth 1 tuple)) :srl (ensure-integer (nth 2 tuple))) ustats)) (nreverse ustats))) (defun find-ustats-srl (srl) (let ((ustats '())) (dolist (tuple (mutex-sql-query (format nil "select NAME,COUNT from USTATS where SRL=~d order by NAME asc" srl))) (push (make-instance 'ustats :name (nth 0 tuple) :hits (ensure-integer (nth 1 tuple)) :srl srl) ustats)) (nreverse ustats))) (defun make-usrl () (with-sql-connection (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))) (find-usrl-all)) (defun find-usrl-all () (let ((usrls '()) (tuples (mutex-sql-query "select SAB,SRL from USRL order by SAB desc"))) (dolist (tuple tuples) (push (make-instance 'usrl :sab (nth 0 tuple) :srl (ensure-integer (nth 1 tuple))) usrls)) usrls)) ;; already reversed by sql (defun find-usrl_freq-all () (let ((freqs '())) (dolist (usrl (find-usrl-all)) (let ((freq (ensure-integer (caar (mutex-sql-query (format nil "select count(*) from MRSO where SAB='~a'" (sab usrl))))))) (push (make-instance 'usrl_freq :usrl usrl :freq freq) freqs))) (sort freqs #'> :key #'usrl_freq-freq))) (defun find-cui-max () (let ((cui (caar (mutex-sql-query "select max(CUI) from MRCON")))) (ensure-integer cui))) ;;;; Cross table find functions (defun find-ucon-tui (tui &key (srl *current-srl*)) "Find list of ucon for tui" (when (stringp tui) (setq tui (parse-tui tui))) (let ((ucons '()) (ls (format nil "select CUI from MRSTY where TUI=~d" tui))) (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 (ensure-integer (car tuple)) :srl srl) ucons)) ucons)) (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)) (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)) (defun find-ustr-word (word &key (srl *current-srl*)) "Return list of ustrs that match word" (let ((ustrs '()) (ls (format nil "select cui,sui from MRXW_ENG where wd='~a'" word))) (when srl (string-append ls (format nil " and KLRL <= ~d" srl))) (string-append ls " order by cui desc,sui desc") (dolist (tuple (mutex-sql-query ls)) (push (find-ustr-cuisui (ensure-integer (car tuple)) (ensure-integer (cadr tuple)) :srl srl) ustrs)) ustrs)) (defun find-ustr-normalized-word (word &key (srl *current-srl*)) "Return list of ustrs that match word" (let ((ustrs '()) (ls (format nil "select cui,sui from MRXNW_ENG where nwd='~a'" word))) (when srl (string-append ls (format nil " and KLRL <= ~d" srl))) (string-append ls " order by cui desc,sui desc") (dolist (tuple (mutex-sql-query ls)) (push (find-ustr-cuisui (ensure-integer (car tuple)) (ensure-integer (cadr tuple)) :srl srl) ustrs)) ustrs)) ;;; Multiword lookup and score functions (defun find-ucon-multiword (str &key (srl *current-srl*)) "Return sorted list of ucon's that match a multiword string" (let* ((words (delimited-string-to-list str #\space)) (ucons '())) (dolist (word words) (setq ucons (append ucons (find-ucon-word word :srl srl)))) (sort-score-ucon-str str (delete-duplicates ucons :test #'eql :key #'cui)))) (defun find-ucon-normalized-multiword (str &key (srl *current-srl*)) "Return sorted list of ucon's that match a multiword string" (let* ((words (delimited-string-to-list str #\space)) (ucons '()) (nwords '())) (dolist (word words) (let ((nws (lvg:process-terms word))) (dolist (nword nws) (push nword nwords)))) (dolist (word nwords) (setq ucons (append ucons (find-ucon-word word :srl srl)))) (sort-score-ucon-str str (delete-duplicates ucons :test #'eql :key #'cui)))) (defun find-ustr-multiword (str &key (srl *current-srl*)) "Return sorted list of ustr's that match a multiword string" (let* ((words (delimited-string-to-list str #\space)) (ustrs '())) (dolist (word words) (setq ustrs (append ustrs (find-ustr-word word :srl srl)))) (sort-score-ustr-str str (delete-duplicates ustrs :test #'eql :key #'cui)))) (defun find-ustr-normalized-multiword (str &key (srl *current-srl*)) "Return sorted list of ustr's that match a multiword string" (let* ((words (delimited-string-to-list str #\space)) (ustrs '()) (nwords '())) (dolist (word words) (let ((nws (lvg:process-terms word))) (dolist (nword nws) (push nword nwords)))) (dolist (word nwords) (setq ustrs (append ustrs (find-ustr-word word :srl srl)))) (sort-score-ustr-str str (delete-duplicates ustrs :test #'eql :key #'ustr-cui)))) (defun a (str) (find-normalized-matches-for-str str #'find-ustr-normalized-word #'ustr-sui)) (defun find-normalized-matches-for-str (str lookup-func key-func) "Return list of objects that normalize match for words in string, eliminate duplicates." (let ((objs '()) (nwords '())) (dolist (word (delimited-string-to-list str #\space)) (dolist (nword (lvg:process-terms word)) (unless (member nword nwords :test #'string-equal) (push nword nwords)))) (dolist (nw nwords) (setq objs (append objs (funcall lookup-func nw)))) (delete-duplicates objs :key key-func :test #'eql))) (defun sort-score-ucon-str (str ucons) "Return list of sorted and scored ucons. Score by match of str to ucon-pfstr" (sort-score-umlsclass-str ucons str #'pfstr)) (defun sort-score-ustr-str (str ustrs) "Return list of sorted and scored ucons. Score by match of str to ucon-pfstr" (sort-score-umlsclass-str ustrs str #'str)) (defun sort-score-umlsclass-str (objs str lookup-func) "Sort a list of objects based on scoring to a string" (let ((scored '())) (dolist (obj objs) (push (list obj (score-multiword-match str (funcall lookup-func obj))) scored)) (mapcar #'car (sort scored #'> :key #'cadr)))) (defun score-multiword-match (s1 s2) "Score a match between two strings with s1 being reference string" (let* ((word-list-1 (delimited-string-to-list s1 #\space)) (word-list-2 (delimited-string-to-list s2 #\space)) (n1 (length word-list-1)) (n2 (length word-list-2)) (unmatched n1) (score 0) (nlong 0) (nshort 0) short-list long-list) (declare (fixnum n1 n2 nshort nlong score unmatched)) (if (> n1 n2) (progn (setq nlong n1) (setq nshort n2) (setq long-list word-list-1) (setq short-list word-list-2)) (progn (setq nlong n2) (setq nshort n1) (setq long-list word-list-2) (setq short-list word-list-1))) (decf score (- nlong nshort)) ;; reduce score for extra words (dotimes (iword nshort) (declare (fixnum iword)) (gu:aif (position (nth iword short-list) long-list :test #'string-equal) (progn (incf score (- 10 (abs (- gu::it iword)))) (decf unmatched)))) (decf score (* 2 unmatched)) score)) ;;; LEX SQL functions (defun find-lexterm-eui (eui) (gu:awhen (car (mutex-sql-query (format nil "select WRD from LRWD where EUI=~d" eui))) (make-instance 'lexterm :eui eui :wrd (nth 0 gu:it)))) (defun find-lexterm-word (wrd) (gu:awhen (mutex-sql-query (format nil "select EUI from LRWD where WRD='~a'" wrd)) (let ((terms '())) (dolist (tuple gu:it) (let ((eui (ensure-integer (nth 0 tuple)))) (push (make-instance 'lexterm :eui eui :wrd (copy-seq wrd)) terms))) (nreverse terms)))) ;; LEXTERM accessors, read on demand (def-lazy-reader lexterm s#abr find-labr-eui eui) (def-lazy-reader lexterm s#agr find-lagr-eui eui) (def-lazy-reader lexterm s#cmp find-lcmp-eui eui) (def-lazy-reader lexterm s#mod find-lmod-eui eui) (def-lazy-reader lexterm s#nom find-lnom-eui eui) (def-lazy-reader lexterm s#prn find-lprn-eui eui) (def-lazy-reader lexterm s#prp find-lprp-eui eui) (def-lazy-reader lexterm s#spl find-lspl-eui eui) (def-lazy-reader lexterm s#trm find-ltrm-eui eui) (def-lazy-reader lexterm s#typ find-ltyp-eui eui) ;; LEX SQL Read functions (defun find-labr-eui (eui) (gu:awhen (mutex-sql-query (format nil "select BAS,ABR,EUI2,BAS2 from LRABR where EUI=~d" eui)) (let ((results '())) (dolist (tuple gu::it) (push (make-instance 'labr :eui eui :bas (nth 0 tuple) :abr (nth 1 tuple) :eui2 (ensure-integer (nth 2 tuple)) :bas2 (nth 3 tuple)) results)) (nreverse results)))) (defun find-labr-bas (bas) (gu:awhen (mutex-sql-query (format nil "select EUI,ABR,EUI2,BAS2 from LRABR where BAS='~a'" bas)) (let ((results '())) (dolist (tuple gu::it) (push (make-instance 'labr :eui (ensure-integer (nth 0 tuple)) :bas (copy-seq bas) :abr (nth 1 tuple) :eui2 (ensure-integer (nth 2 tuple)) :bas2 (nth 3 tuple)) results)) (nreverse results)))) (defun find-lagr-eui (eui) (gu:awhen (mutex-sql-query (format nil "select STR,SCA,AGR,CIT,BAS from LRAGR where EUI=~d" eui)) (let ((results '())) (dolist (tuple gu::it) (push (make-instance 'lagr :eui eui :str (nth 0 tuple) :sca (nth 1 tuple) :agr (nth 2 tuple) :cit (nth 3 tuple) :bas (nth 4 tuple)) results)) (nreverse results)))) (defun find-lcmp-eui (eui) (gu:awhen (mutex-sql-query (format nil "select BAS,SCA,COM from LRCMP where EUI=~d" eui)) (let ((results '())) (dolist (tuple gu::it) (push (make-instance 'lcmp :eui eui :bas (nth 0 tuple) :sca (nth 1 tuple) :com (nth 2 tuple)) results)) (nreverse results)))) (defun find-lmod-eui (eui) (gu:awhen (mutex-sql-query (format nil "select BAS,SCA,PSN_MOD,FEA from LRMOD where EUI=~d" eui)) (let ((results '())) (dolist (tuple gu::it) (push (make-instance 'lmod :eui eui :bas (nth 0 tuple) :sca (nth 1 tuple) :psnmod (nth 2 tuple) :fea (nth 3 tuple)) results)) (nreverse results)))) (defun find-lnom-eui (eui) (gu:awhen (mutex-sql-query (format nil "select BAS,SCA,EUI2,BAS2,SCA2 from LRNOM where EUI=~d" eui)) (let ((results '())) (dolist (tuple gu::it) (push (make-instance 'lnom :eui eui :bas (nth 0 tuple) :sca (nth 1 tuple) :eui2 (ensure-integer (nth 2 tuple)) :bas2 (nth 3 tuple) :sca2 (nth 4 tuple)) results)) (nreverse results)))) (defun find-lprn-eui (eui) (gu:awhen (mutex-sql-query (format nil "select BAS,NUM,GND,CAS,POS,QNT,FEA from LRPRN where EUI=~d" eui)) (let ((results '())) (dolist (tuple gu::it) (push (make-instance 'lprn :eui eui :bas (nth 0 tuple) :num (nth 1 tuple) :gnd (nth 2 tuple) :cas (nth 3 tuple) :pos (nth 4 tuple) :qnt (nth 5 tuple) :fea (nth 6 tuple)) results)) (nreverse results)))) (defun find-lprp-eui (eui) (gu:awhen (mutex-sql-query (format nil "select BAS,STR,SCA,FEA from LRPRP where EUI=~d" eui)) (let ((results '())) (dolist (tuple gu::it) (push (make-instance 'lprp :eui eui :bas (nth 0 tuple) :str (nth 1 tuple) :sca (nth 2 tuple) :fea (nth 3 tuple)) results)) (nreverse results)))) (defun find-lspl-eui (eui) (gu:awhen (mutex-sql-query (format nil "select SPV,BAS from LRSPL where EUI=~d" eui)) (let ((results '())) (dolist (tuple gu::it) (push (make-instance 'lspl :eui eui :spv (nth 0 tuple) :bas (nth 1 tuple)) results)) (nreverse results)))) (defun find-ltrm-eui (eui) (gu:awhen (mutex-sql-query (format nil "select BAS,GEN from LRTRM where EUI=~d" eui)) (let ((results '())) (dolist (tuple gu::it) (push (make-instance 'ltrm :eui eui :bas (nth 0 tuple) :gen (nth 1 tuple)) results)) (nreverse results)))) (defun find-ltyp-eui (eui) (gu:awhen (mutex-sql-query (format nil "select BAS,SCA,TYP from LRTYP where EUI=~d" eui)) (let ((results '())) (dolist (tuple gu::it) (push (make-instance 'ltyp :eui eui :bas (nth 0 tuple) :sca (nth 1 tuple) :typ (nth 2 tuple)) results)) (nreverse results)))) (defun find-lwd-wrd (wrd) (gu:awhen (mutex-sql-query (format nil "select EUI from LRWD where WRD='~a'" wrd)) (let ((results '())) (dolist (tuple gu::it) (push (ensure-integer (nth 0 tuple)) results)) (make-instance 'lwd :wrd wrd :euilist (nreverse results))))) ;;; Semantic Network SQL access functions (defun find-sdef-ui (ui) (gu:awhen (car (mutex-sql-query (format nil "select RT,STY_RL,STN_RTN,DEF,EX,UN,RH,ABR,RIN from SRDEF where UI=~d" ui))) (make-instance 'sdef :rt (nth 0 gu::it) :ui ui :styrl (nth 1 gu::it) :stnrtn (nth 2 gu::it) :def (nth 3 gu::it) :ex (nth 4 gu::it) :un (nth 5 gu::it) :rh (nth 6 gu::it) :abr (nth 7 gu::it) :rin (nth 8 gu::it)))) (defun find-sstre1-ui (ui) (gu:awhen (mutex-sql-query (format nil "select UI2,UI3 from SRSTRE1 where UI=~d" ui)) (let ((results '())) (dolist (tuple gu::it) (push (make-instance 'sstre1 :ui ui :ui2 (ensure-integer (nth 0 tuple)) :ui3 (ensure-integer (nth 1 tuple))) results)) (nreverse results)))) (defun find-sstre1-ui2 (ui2) (gu:awhen (mutex-sql-query (format nil "select UI,UI3 from SRSTRE1 where UI2=~d" ui2)) (let ((results '())) (dolist (tuple gu::it) (push (make-instance 'sstre1 :ui (ensure-integer (nth 0 tuple)) :ui2 ui2 :ui3 (ensure-integer (nth 1 tuple))) results)) (nreverse results)))) (defun find-sstr-rl (rl) (gu:awhen (mutex-sql-query (format nil "select STY_RL,STY_RL2,LS from SRSTRE where RL='~a'" rl)) (let ((results '())) (dolist (tuple gu::it) (push (make-instance 'sstr :rl rl :styrl (nth 0 tuple) :styrl2 (nth 1 tuple) :ls (nth 2 tuple)) results)) (nreverse results)))) (defun find-sstre2-sty (sty) (gu:awhen (mutex-sql-query (format nil "select RL,STY2 from SRSTRE2 where STY='~a'" sty)) (let ((results '())) (dolist (tuple gu::it) (push (make-instance 'sstre2 :sty (copy-seq sty) :rl (nth 0 tuple) :sty2 (nth 1 tuple)) results)) (nreverse results)))) (defun find-sstr-styrl (styrl) (gu:awhen (mutex-sql-query (format nil "select RL,STY_RL2,LS from SRSTR where RL='~a'" styrl)) (let ((results '())) (dolist (tuple gu::it) (push (make-instance 'sstr :styrl styrl :rl (nth 0 tuple) :styrl2 (nth 1 tuple) :ls (nth 2 tuple)) results)) (nreverse results))))