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
10 ;;;; This file, part of UMLisp, is
11 ;;;; Copyright (c) 2000-2010 by Kevin M. Rosenberg, M.D.
13 ;;;; UMLisp users are granted the rights to distribute and use this software
14 ;;;; as governed by the terms of the GNU General Public License.
15 ;;;; *************************************************************************
19 ;;; Formatting routines
21 (defgeneric fmt-cui (c))
22 (defmethod fmt-cui ((c ucon))
25 (when *has-fixnum-class*
26 (defmethod fmt-cui ((c fixnum))
28 (prefixed-fixnum-string c #\C 8)
29 (prefixed-fixnum-string c #\C 7))))
31 (defmethod fmt-cui ((c integer))
33 (prefixed-fixnum-string c #\C 8)
34 (prefixed-fixnum-string c #\C 7)))
36 (defmethod fmt-cui ((c string))
37 (if (eql (aref c 0) #\C)
39 (fmt-cui (parse-integer c))))
41 (defmethod fmt-cui ((c null))
44 (defgeneric fmt-lui (c))
45 (defmethod fmt-lui ((l uterm))
48 (when *has-fixnum-class*
49 (defmethod fmt-lui ((l fixnum))
51 (prefixed-fixnum-string l #\L 8)
52 (prefixed-fixnum-string l #\L 7))))
54 (defmethod fmt-lui ((l integer))
56 (prefixed-fixnum-string l #\L 8)
57 (prefixed-fixnum-string l #\L 7)))
59 (defmethod fmt-lui ((l string))
60 (if (eql (aref l 0) #\L)
62 (fmt-lui (parse-integer l))))
64 (defgeneric fmt-sui (s))
65 (defmethod fmt-sui ((s ustr))
68 (when *has-fixnum-class*
69 (defmethod fmt-sui ((s fixnum))
71 (prefixed-fixnum-string s #\S 8)
72 (prefixed-fixnum-string s #\S 7))))
74 (defmethod fmt-sui ((s integer))
76 (prefixed-fixnum-string s #\S 8)
77 (prefixed-fixnum-string s #\S 7)))
79 (defmethod fmt-sui ((s string))
80 (if (eql (aref s 0) #\S)
82 (fmt-sui (parse-integer s))))
84 (defgeneric fmt-tui (tui))
85 (when *has-fixnum-class*
86 (defmethod fmt-tui ((tui fixnum))
87 (prefixed-fixnum-string tui #\T 3)))
89 (defmethod fmt-tui ((tui integer))
90 (prefixed-integer-string tui #\T 3))
92 (defmethod fmt-tui ((tui string))
93 (if (eql (aref tui 0) #\T)
95 (fmt-tui (parse-integer tui))))
97 (defgeneric fmt-aui (aui))
98 (when *has-fixnum-class*
99 (defmethod fmt-aui ((aui fixnum))
100 (if (>= aui 10000000)
101 (prefixed-fixnum-string aui #\A 8)
102 (prefixed-fixnum-string aui #\A 7))))
104 (defmethod fmt-aui ((aui integer))
105 (if (>= aui 10000000)
106 (prefixed-integer-string aui #\A 8)
107 (prefixed-integer-string aui #\A 7)))
109 (defmethod fmt-aui ((aui string))
110 (if (eql (aref aui 0) #\A)
112 (fmt-aui (parse-integer aui))))
114 (defgeneric fmt-rui (rui))
115 (when *has-fixnum-class*
116 (defmethod fmt-rui ((rui fixnum))
117 (prefixed-fixnum-string rui #\A 8)))
119 (defmethod fmt-rui ((rui integer))
120 (prefixed-integer-string rui #\A 8))
122 (defmethod fmt-rui ((rui string))
123 (if (eql (aref rui 0) #\R)
125 (fmt-rui (parse-integer rui))))
127 (defgeneric fmt-eui (e))
128 (when *has-fixnum-class*
129 (defmethod fmt-eui ((e fixnum))
130 (prefixed-fixnum-string e #\E 7)))
132 (defmethod fmt-eui ((e integer))
133 (prefixed-integer-string e #\E 7))
135 (defmethod fmt-eui ((e string))
136 (if (eql (aref e 0) #\E)
138 (fmt-eui (parse-integer e))))
140 (defmethod fmt-eui ((e null))
144 "Check if a string is a CUI"
148 "Check if a string is a LUI"
152 "Check if a string is a SUI"
161 (defun check-ui (ui start-char len)
162 (when (and (stringp ui)
163 (= (length ui) (1+ len))
164 (char-equal start-char (schar ui 0))
165 (ignore-errors (parse-integer ui :start 1)))
169 ;;; Generic display functions
171 (eval-when (:compile-toplevel :load-toplevel :execute)
172 (defun english-term-p (obj)
173 "Returns two values: T/NIL if term is english and T/NIL if obj is a TERM"
174 (if (eq (hyperobject::class-name (hyperobject::class-of obj)) 'uterm)
175 (values (string-equal (lat obj) "ENG") t)
178 (defun english-term-filter (obj)
179 "Retrns NIL if object is a term and not english"
180 (multiple-value-bind (is-english is-term) (english-term-p obj)
181 (or (not is-term) is-english)))
183 (defun print-umlsclass (obj &key (stream *standard-output*)
185 (file-wrapper nil) (english-only t) (subobjects nil)
186 (refvars nil) (link-printer nil))
187 (view obj :stream stream :vid vid :subobjects subobjects
188 :file-wrapper file-wrapper
189 :filter (if english-only nil #'english-term-filter)
190 :link-printer link-printer
193 (defmacro define-lookup-display (newfuncname lookup-func)
194 "Defines functions for looking up and displaying objects"
195 `(defun ,newfuncname (keyval &key (stream *standard-output*) (vid :compact-text)
196 (file-wrapper t) (english-only nil) (subobjects nil))
197 (let ((obj (funcall ,lookup-func keyval)))
198 (print-umlsclass obj :stream stream :vid vid
199 :file-wrapper file-wrapper :english-only english-only
200 :subobjects subobjects)
203 (define-lookup-display display-con #'find-ucon-cui)
204 (define-lookup-display display-term #'find-uterm-lui)
205 (define-lookup-display display-str #'find-ustr-sui)
207 (defun ucon-has-tui (ucon tui)
208 "Returns T if UCON has a semantic type of TUI."
209 (some #'(lambda (usty) (= tui (tui usty))) (s#sty ucon)))
211 (defgeneric pf-ustr (obj))
212 (defmethod pf-ustr ((ucon ucon))
213 "Return the preferred ustr for a ucon"
215 (find-if (lambda (uterm) (string= "P" (ts uterm))) (s#term ucon))))
217 (defmethod pf-ustr ((uterm uterm))
218 "Return the preferred ustr for a uterm"
219 (find-if (lambda (ustr) (string= "PF" (stt ustr))) (s#str uterm)))
221 (defgeneric mesh-number (obj))
222 (defmethod mesh-number ((con ucon))
223 (mesh-number (pf-ustr con)))
225 (defmethod mesh-number ((ustr ustr))
229 (when (and (string-equal "MSH" (sab sat))
230 (string-equal "MN" (atn sat)))
233 (if (= 1 (length codes))
237 (defun ucon-ustrs (ucon)
238 "Return lists of strings for a concept"
240 (dolist (term (s#term ucon) (nreverse res))
241 (dolist (str (s#str term))
245 (defmethod pfstr ((uterm uterm))
246 "Return the preferred string for a uterm"
247 (dolist (ustr (s#str uterm))
248 (when (string= "PF" (stt ustr))
249 (return-from pfstr (str ustr)))))
251 (defmethod pfstr ((ustr ustr))
252 "Return the preferred string for a ustr, which is the string itself"
255 (defun remove-non-english-terms (uterms)
256 (remove-if-not #'english-term-p uterms))
258 (defun remove-english-terms (uterms)
259 (remove-if #'english-term-p uterms))
262 (defvar +relationship-abbreviations+
263 '(("RB" "Broader" "has a broader relationship")
264 ("RN" "Narrower" "has a narrower relationship")
265 ("RO" "Other related" "has relationship other than synonymous, narrower, or broader")
266 ("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")
267 ("RQ" "Unspecified" "unspecified source asserted relatedness, possibly synonymous")
268 ("SY" "Source Synonymy" "source asserted synonymy")
269 ("PAR" "Parent" "has parent relationship in a Metathesaurus source vocabulary")
270 ("CHD" "Child" "has child relationship in a Metathesaurus source vocabulary")
271 ("SIB" "Sibling" "has sibling relationship in a Metathesaurus source vocabulary")
272 ("AQ" "Allowed" "is an allowed qualifier for a concept in a Metathesaurus source vocabulary")
273 ("QB" "Qualified" "can be qualified by a concept in a Metathesaurus source vocabulary")))
275 (defvar *rel-info-table* (make-hash-table :size 30 :test 'equal))
276 (defvar *is-rel-table-init* nil)
277 (unless *is-rel-table-init*
278 (dolist (relinfo +relationship-abbreviations+)
279 (setf (gethash (string-downcase (car relinfo)) *rel-info-table*)
281 (setq *is-rel-table-init* t))
283 (defun rel-abbr-info (rel)
284 (nth-value 0 (gethash (string-downcase rel) *rel-info-table*)))
286 (defun filter-urels-by-rel (urels rel &key (remove-duplicate-pfstr2 t) (sort :pfstr2))
287 (let ((f (remove-if-not (lambda (urel) (string-equal rel (rel urel))) urels)))
288 (when remove-duplicate-pfstr2
289 (setq f (remove-duplicates f :test 'equal :key 'u::pfstr2)))
291 (sort (copy-seq f) 'string-lessp :key 'u::pfstr2)
295 (defun filter-ucocs (ucocs &key (remove-duplicate-pfstr2 t) (sort :pfstr2))
296 (when remove-duplicate-pfstr2
297 (setq ucocs (remove-duplicates ucocs :test 'equal :key 'u::pfstr2)))
299 (sort (copy-seq ucocs) 'string-lessp :key 'u::pfstr2)
303 (defvar +language-abbreviations+
313 ("HUN" . "Hungarian")
316 ("NOR" . "Norwegian")
317 ("POR" . "Portuguese")
320 ("SWE" . "Swedish")))
322 (defvar *lat-info-table* (make-hash-table :size 30 :test 'equal))
323 (defvar *is-lat-table-init* nil)
324 (unless *is-lat-table-init*
325 (dolist (latinfo +language-abbreviations+)
326 (setf (gethash (string-downcase (car latinfo)) *lat-info-table*)
328 (setq *is-lat-table-init* t))
330 (defun lat-abbr-info (lat)
331 (aif (nth-value 0 (gethash (string-downcase lat) *lat-info-table*))
337 (defun stt-abbr-info (stt)
338 (when (string-equal "PF" stt)
339 (return-from stt-abbr-info "Preferred"))
340 (when (char-equal #\V (schar stt 0))
341 (setq stt (subseq stt 1)))
342 (loop for c across stt
356 (defun uso-unique-codes (usos)
357 (let ((sab-codes (make-hash-table :test 'equal)))
359 (setf (gethash (sab uso) sab-codes) (code uso)))
360 (loop for key being the hash-key in sab-codes
361 collect (list key (gethash key sab-codes)))))
363 (defun ucon-has-sab (ucon sab)
364 (and (find-if (lambda (uso) (string-equal sab (sab uso))) (s#so ucon)) t))
368 (dolist (c '(urank udef usat uso ucxt ustr uterm usty urel
370 uatx uconso uxw uxnw uxns lexterm labr lagr lcmp lmod lnom lprn lprp lspl ltrm ltyp lwd sdef sstr sstre1 sstre2 usrl))
371 (let ((cl (find-class c)))
372 (clos:finalize-inheritance cl)))