1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10; Package: umlisp -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
5 ;;;; Name: classes-support.lisp
6 ;;;; Purpose: Support for UMLisp classes
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 ;;; Formatting routines
23 (defgeneric fmt-cui (c))
24 (defmethod fmt-cui ((c ucon))
27 (when *has-fixnum-class*
28 (defmethod fmt-cui ((c fixnum))
29 (prefixed-fixnum-string c #\C 7)))
31 (defmethod fmt-cui ((c integer))
32 (prefixed-integer-string c #\C 7))
34 (defmethod fmt-cui ((c string))
35 (if (eql (aref c 0) #\C)
37 (fmt-cui (parse-integer c))))
39 (defmethod fmt-cui ((c null))
42 (defgeneric fmt-lui (c))
43 (defmethod fmt-lui ((l uterm))
46 (when *has-fixnum-class*
47 (defmethod fmt-lui ((l fixnum))
48 (prefixed-fixnum-string l #\L 7)))
50 (defmethod fmt-lui ((l integer))
51 (prefixed-integer-string l #\L 7))
53 (defmethod fmt-lui ((l string))
54 (if (eql (aref l 0) #\L)
56 (fmt-lui (parse-integer l))))
58 (defgeneric fmt-sui (s))
59 (defmethod fmt-sui ((s ustr))
62 (when *has-fixnum-class*
63 (defmethod fmt-sui ((s fixnum))
64 (prefixed-fixnum-string s #\S 7)))
66 (defmethod fmt-sui ((s integer))
67 (prefixed-integer-string s #\S 7))
69 (defmethod fmt-sui ((s string))
70 (if (eql (aref s 0) #\S)
72 (fmt-sui (parse-integer s))))
74 (defgeneric fmt-tui (tui))
75 (when *has-fixnum-class*
76 (defmethod fmt-tui ((tui fixnum))
77 (prefixed-fixnum-string tui #\T 3)))
79 (defmethod fmt-tui ((tui integer))
80 (prefixed-integer-string tui #\T 3))
82 (defmethod fmt-tui ((tui string))
83 (if (eql (aref tui 0) #\T)
85 (fmt-tui (parse-integer tui))))
87 (defgeneric fmt-aui (aui))
88 (when *has-fixnum-class*
89 (defmethod fmt-aui ((aui fixnum))
91 (prefixed-fixnum-string aui #\A 8)
92 (prefixed-fixnum-string aui #\A 7))))
94 (defmethod fmt-aui ((aui integer))
96 (prefixed-integer-string aui #\A 8)
97 (prefixed-integer-string aui #\A 7)))
99 (defmethod fmt-aui ((aui string))
100 (if (eql (aref aui 0) #\A)
102 (fmt-aui (parse-integer aui))))
104 (defgeneric fmt-rui (rui))
105 (when *has-fixnum-class*
106 (defmethod fmt-rui ((rui fixnum))
107 (prefixed-fixnum-string rui #\A 8)))
109 (defmethod fmt-rui ((rui integer))
110 (prefixed-integer-string rui #\A 8))
112 (defmethod fmt-rui ((rui string))
113 (if (eql (aref rui 0) #\R)
115 (fmt-rui (parse-integer rui))))
117 (defgeneric fmt-eui (e))
118 (when *has-fixnum-class*
119 (defmethod fmt-eui ((e fixnum))
120 (prefixed-fixnum-string e #\E 7)))
122 (defmethod fmt-eui ((e integer))
123 (prefixed-integer-string e #\E 7))
125 (defmethod fmt-eui ((e string))
126 (if (eql (aref e 0) #\E)
128 (fmt-eui (parse-integer e))))
130 (defmethod fmt-eui ((e null))
134 "Check if a string is a CUI"
138 "Check if a string is a LUI"
142 "Check if a string is a SUI"
151 (defun check-ui (ui start-char len)
152 (when (and (stringp ui)
153 (= (length ui) (1+ len))
154 (char-equal start-char (schar ui 0))
155 (ignore-errors (parse-integer ui :start 1)))
159 ;;; Generic display functions
161 (eval-when (:compile-toplevel :load-toplevel :execute)
162 (defun english-term-p (obj)
163 "Returns two values: T/NIL if term is english and T/NIL if obj is a TERM"
164 (if (eq (hyperobject::class-name (hyperobject::class-of obj)) 'uterm)
165 (values (string-equal (lat obj) "ENG") t)
168 (defun english-term-filter (obj)
169 "Retrns NIL if object is a term and not english"
170 (multiple-value-bind (is-english is-term) (english-term-p obj)
171 (or (not is-term) is-english)))
173 (defun print-umlsclass (obj &key (stream *standard-output*)
175 (file-wrapper nil) (english-only t) (subobjects nil)
176 (refvars nil) (link-printer nil))
177 (view obj :stream stream :vid vid :subobjects subobjects
178 :file-wrapper file-wrapper
179 :filter (if english-only nil #'english-term-filter)
180 :link-printer link-printer
183 (defmacro define-lookup-display (newfuncname lookup-func)
184 "Defines functions for looking up and displaying objects"
185 `(defun ,newfuncname (keyval &key (stream *standard-output*) (vid :compact-text)
186 (file-wrapper t) (english-only nil) (subobjects nil))
187 (let ((obj (funcall ,lookup-func keyval)))
188 (print-umlsclass obj :stream stream :vid vid
189 :file-wrapper file-wrapper :english-only english-only
190 :subobjects subobjects)
193 (define-lookup-display display-con #'find-ucon-cui)
194 (define-lookup-display display-term #'find-uterm-lui)
195 (define-lookup-display display-str #'find-ustr-sui)
197 (defun ucon-has-tui (ucon tui)
198 "Returns T if UCON has a semantic type of TUI."
199 (some #'(lambda (usty) (= tui (tui usty))) (s#sty ucon)))
201 (defgeneric suistr (lo))
202 (defmethod suistr ((lo ulo))
203 "Return the string for a ulo object"
204 (find-string-sui (sui lo)))
206 (defgeneric pf-ustr (obj))
207 (defmethod pf-ustr ((ucon ucon))
208 "Return the preferred ustr for a ucon"
210 (find-if (lambda (uterm) (string= "P" (ts uterm))) (s#term ucon))))
212 (defmethod pf-ustr ((uterm uterm))
213 "Return the preferred ustr for a uterm"
214 (find-if (lambda (ustr) (string= "PF" (stt ustr))) (s#str uterm)))
216 (defgeneric mesh-number (obj))
217 (defmethod mesh-number ((con ucon))
218 (mesh-number (pf-ustr con)))
220 (defmethod mesh-number ((ustr ustr))
224 (when (and (string-equal "MSH" (sab sat))
225 (string-equal "MN" (atn sat)))
228 (if (= 1 (length codes))
232 (defun ucon-ustrs (ucon)
233 "Return lists of strings for a concept"
235 (dolist (term (s#term ucon) (nreverse res))
236 (dolist (str (s#str term))
240 (defmethod pfstr ((uterm uterm))
241 "Return the preferred string for a uterm"
242 (dolist (ustr (s#str uterm))
243 (when (string= "PF" (stt ustr))
244 (return-from pfstr (str ustr)))))
246 (defmethod pfstr ((ustr ustr))
247 "Return the preferred string for a ustr, which is the string itself"
250 (defun remove-non-english-terms (uterms)
251 (remove-if-not #'english-term-p uterms))
253 (defun remove-english-terms (uterms)
254 (remove-if #'english-term-p uterms))
257 (defvar +relationship-abbreviations+
258 '(("RB" "Broader" "has a broader relationship")
259 ("RN" "Narrower" "has a narrower relationship")
260 ("RO" "Other related" "has relationship other than synonymous, narrower, or broader")
261 ("RL" "Like" "the two concepts are similar or 'alike'. In the current edition of the Metathesaurus, most relationships with this attribute are mappings provided by a source")
262 ("RQ" "Unspecified" "unspecified source asserted relatedness, possibly synonymous")
263 ("SY" "Source Synonymy" "source asserted synonymy")
264 ("PAR" "Parent" "has parent relationship in a Metathesaurus source vocabulary")
265 ("CHD" "Child" "has child relationship in a Metathesaurus source vocabulary")
266 ("SIB" "Sibling" "has sibling relationship in a Metathesaurus source vocabulary")
267 ("AQ" "Allowed" "is an allowed qualifier for a concept in a Metathesaurus source vocabulary")
268 ("QB" "Qualified" "can be qualified by a concept in a Metathesaurus source vocabulary")))
270 (defvar *rel-info-table* (make-hash-table :size 30 :test 'equal))
271 (defvar *is-rel-table-init* nil)
272 (unless *is-rel-table-init*
273 (dolist (relinfo +relationship-abbreviations+)
274 (setf (gethash (string-downcase (car relinfo)) *rel-info-table*)
276 (setq *is-rel-table-init* t))
278 (defun rel-abbr-info (rel)
279 (nth-value 0 (gethash (string-downcase rel) *rel-info-table*)))
281 (defun filter-urels-by-rel (urels rel)
282 (remove-if-not (lambda (urel) (string-equal rel (rel urel))) urels))
285 (defvar +language-abbreviations+
295 ("HUN" . "Hungarian")
298 ("NOR" . "Norwegian")
299 ("POR" . "Portuguese")
302 ("SWE" . "Swedish")))
304 (defvar *lat-info-table* (make-hash-table :size 30 :test 'equal))
305 (defvar *is-lat-table-init* nil)
306 (unless *is-lat-table-init*
307 (dolist (latinfo +language-abbreviations+)
308 (setf (gethash (string-downcase (car latinfo)) *lat-info-table*)
310 (setq *is-lat-table-init* t))
312 (defun lat-abbr-info (lat)
313 (aif (nth-value 0 (gethash (string-downcase lat) *lat-info-table*))
319 (defun stt-abbr-info (stt)
320 (when (string-equal "PF" stt)
321 (return-from stt-abbr-info "Preferred"))
322 (when (char-equal #\V (schar stt 0))
323 (setq stt (subseq stt 1)))
324 (loop for c across stt
339 (defun ucon-parents (ucon &key sab include-rb)
340 (ucon-ancestors ucon :sab sab :include-rb include-rb
343 (defun is-ucon-in-ancestors (ucon ancestors)
345 ((null ancestors) nil)
346 ((atom ancestors) (eql (cui ucon) (cui ancestors)))
347 ((listp (car ancestors))
348 (or (is-ucon-in-ancestors ucon (car ancestors))
349 (is-ucon-in-ancestors ucon (cdr ancestors))))
351 (or (eql (cui ucon) (cui (car ancestors)))
352 (is-ucon-in-ancestors ucon (cdr ancestors))))))
355 (defun ucon-ancestors (ucon &key sab include-rb ancestors)
356 "Returns a list of ancestor lists for a concept"
357 (let* ((parent-rels (append (filter-urels-by-rel (s#rel ucon) "par")
359 (filter-urels-by-rel (s#rel ucon) "rb"))))
362 (setq parent-rels (delete-if-not
363 (lambda (rel) (string-equal sab (sab rel)))
365 (dolist (rel parent-rels)
366 (let ((parent (find-ucon-cui (cui2 rel))))
367 ;; (format t "~S ~S ~S ~S~%" rel ucon parent ancestors)
368 (unless (is-ucon-in-ancestors parent ancestors)
372 (ucon-ancestors parent :sab (sab rel) :ancestors (append (list parent) ancestors)))
377 (defun find-minimum-distance (anc1 anc2 &key (distance 0) (minimum 0) path)
379 ((or (null anc1) (null anc2) nil))
380 ((and (atom (car anc1)) (atom (car anc2)))
381 (when (eql (cui ucon1) (cui ucon2))
382 (return-from find-minimum-distance (values distance path)))
384 (when (> distance minimum)
385 (return-from find-minimum-distance nil))
386 (multiple-value-bind (dist1 path1)
387 (find-minimum-distance anc1 (cdr anc2)
388 :distance distance :minimum minimum
389 :path (list* anc1 path))
390 (when (and dist1 (< dist1 minimum))
393 (multiple-value-bind (dist2 path2)
394 (find-minimum-distance (cdr anc1) anc2
395 :distance distance :minimum minimum
396 :path (list* anc2 path))
397 (when (and dist2 (< dist2 minimum))
400 (return-from find-minimum-distance (values distance path))
403 (min2 (find-minimum-distance
405 :distance distance :minimum minimum
406 :path (list* anc2 path)))
407 (when (and min2 (< min2 minimum))
409 path (list* anc2 path))))))
413 (defun ucon-cdist (ucon1 ucon2 &key include-rb sab)
414 "Compute James Cimino's CDist metric"
415 (let ((anc1 (ucon-ancestors ucon1 :include-rb include-rb :sab sab))
416 (anc2 (ucon-ancestors ucon2 :include-rb include-rb :sab sab)))
417 (find-minimum-distance (list ucon1 anc1) (list ucon2 anc2))))
420 (defgeneric cxt-ancestors (obj))
421 (defmethod cxt-ancestors ((con ucon))
422 (loop for term in (s#term con)
423 append (cxt-ancestors term)))
426 (defmethod cxt-ancestors ((term uterm))
427 (loop for str in (s#str term)
428 append (cxt-ancestors str)))
430 (defmethod cxt-ancestors ((str ustr))
431 "Return the ancestory contexts of a ustr"
432 (let* ((anc (remove-if-not
433 (lambda (cxt) (string-equal "ANC" (cxl cxt)))
435 (num-contexts (if anc
436 (apply #'max (mapcar (lambda (cxt) (cxn cxt)) anc))
439 (dotimes (i num-contexts (nreverse anc-lists))
440 (let* ((anc-this-cxn (remove-if-not
441 (lambda (cxt) (= (1+ i) (cxn cxt))) anc)))
443 (sort anc-this-cxn (lambda (a b) (< (rank a) (rank b))))
446 (defun uso-unique-codes (usos)
447 (let ((sab-codes (make-hash-table :test 'equal)))
449 (setf (gethash (sab uso) sab-codes) (code uso)))
450 (loop for key being the hash-key in sab-codes
451 collect (list key (gethash key sab-codes)))))
454 (defun ucon-has-sab (ucon sab)
455 (and (find-if (lambda (uso) (string-equal sab (sab uso))) (s#so ucon)) t))
459 (dolist (c '(urank udef usat uso ucxt ustr ulo uterm usty urel ucoc uatx ucon uxw uxnw uxns lexterm labr lagr lcmp lmod lnom lprn lprp lspl ltrm ltyp lwd sdef sstr sstre1 sstre2 usrl))
460 (let ((cl (find-class c)))
461 (clos:finalize-inheritance cl)))