r11624: improve find-uconso-code
[umlisp.git] / class-support.lisp
1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10; Package: umlisp -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
4 ;;;;
5 ;;;; Name:     classes-support.lisp
6 ;;;; Purpose:  Support for UMLisp classes
7 ;;;; Author:   Kevin M. Rosenberg
8 ;;;; Created:  Apr 2000
9 ;;;;
10 ;;;; $Id$
11 ;;;;
12 ;;;; This file, part of UMLisp, is
13 ;;;;    Copyright (c) 2000-2006 by Kevin M. Rosenberg, M.D.
14 ;;;;
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 ;;;; *************************************************************************
18
19 (in-package #:umlisp)
20
21 ;;; Formatting routines
22
23 (defgeneric fmt-cui (c))
24 (defmethod fmt-cui ((c ucon))
25   (fmt-cui (cui c)))
26
27 (when *has-fixnum-class*
28   (defmethod fmt-cui ((c fixnum))
29     (prefixed-fixnum-string c #\C 7)))
30
31 (defmethod fmt-cui ((c integer))
32     (prefixed-integer-string c #\C 7))
33
34 (defmethod fmt-cui ((c string))
35   (if (eql (aref c 0) #\C)
36       c
37       (fmt-cui (parse-integer c))))
38
39 (defmethod fmt-cui ((c null))
40   (format nil "nil"))
41
42 (defgeneric fmt-lui (c))
43 (defmethod fmt-lui ((l uterm))
44   (fmt-lui (lui l)))
45
46 (when *has-fixnum-class*
47   (defmethod fmt-lui ((l fixnum))
48     (prefixed-fixnum-string l #\L 7)))
49
50 (defmethod fmt-lui ((l integer))
51   (prefixed-integer-string l #\L 7))
52
53 (defmethod fmt-lui ((l string))
54   (if (eql (aref l 0) #\L)
55       l
56       (fmt-lui (parse-integer l))))
57
58 (defgeneric fmt-sui (s))
59 (defmethod fmt-sui ((s ustr))
60   (fmt-sui (sui s)))
61
62 (when *has-fixnum-class*
63   (defmethod fmt-sui ((s fixnum))
64     (prefixed-fixnum-string s #\S 7)))
65
66 (defmethod fmt-sui ((s integer))
67   (prefixed-integer-string s #\S 7))
68
69 (defmethod fmt-sui ((s string))
70   (if (eql (aref s 0) #\S)
71       s
72       (fmt-sui (parse-integer s))))
73
74 (defgeneric fmt-tui (tui))
75 (when *has-fixnum-class*
76   (defmethod fmt-tui ((tui fixnum))
77     (prefixed-fixnum-string tui #\T 3)))
78
79 (defmethod fmt-tui ((tui integer))
80   (prefixed-integer-string tui #\T 3))
81
82 (defmethod fmt-tui ((tui string))
83   (if (eql (aref tui 0) #\T)
84       tui
85     (fmt-tui (parse-integer tui))))
86
87 (defgeneric fmt-aui (aui))
88 (when *has-fixnum-class*
89   (defmethod fmt-aui ((aui fixnum))
90     (if (>= aui 10000000)
91       (prefixed-fixnum-string aui #\A 8)
92       (prefixed-fixnum-string aui #\A 7))))
93
94 (defmethod fmt-aui ((aui integer))
95   (if (>= aui 10000000)
96     (prefixed-integer-string aui #\A 8)
97     (prefixed-integer-string aui #\A 7)))
98
99 (defmethod fmt-aui ((aui string))
100   (if (eql (aref aui 0) #\A)
101       aui
102       (fmt-aui (parse-integer aui))))
103
104 (defgeneric fmt-rui (rui))
105 (when *has-fixnum-class*
106   (defmethod fmt-rui ((rui fixnum))
107     (prefixed-fixnum-string rui #\A 8)))
108
109 (defmethod fmt-rui ((rui integer))
110   (prefixed-integer-string rui #\A 8))
111
112 (defmethod fmt-rui ((rui string))
113   (if (eql (aref rui 0) #\R)
114       rui
115     (fmt-rui (parse-integer rui))))
116
117 (defgeneric fmt-eui (e))
118 (when *has-fixnum-class*
119   (defmethod fmt-eui ((e fixnum))
120     (prefixed-fixnum-string e #\E 7)))
121
122 (defmethod fmt-eui ((e integer))
123   (prefixed-integer-string e #\E 7))
124
125 (defmethod fmt-eui ((e string))
126   (if (eql (aref e 0) #\E)
127       e
128       (fmt-eui (parse-integer e))))
129
130 (defmethod fmt-eui ((e null))
131   (format nil "nil"))
132
133 (defun cui-p (ui)
134   "Check if a string is a CUI"
135   (check-ui ui #\C 7))
136
137 (defun lui-p (ui)
138   "Check if a string is a LUI"
139   (check-ui ui #\L 7))
140
141 (defun sui-p (ui)
142   "Check if a string is a SUI"
143   (check-ui ui #\S 7))
144
145 (defun tui-p (ui)
146   (check-ui ui #\T 3))
147
148 (defun eui-p (ui)
149   (check-ui ui #\E 7))
150
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)))
156     t))
157
158
159 ;;; Generic display functions
160
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)
166     (values nil nil))))
167
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)))
172
173 (defun print-umlsclass (obj &key (stream *standard-output*)
174                         (vid :compact-text)
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
181         :refvars refvars))
182
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)
191        obj)))
192
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)
196
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)))
200
201 (defgeneric pf-ustr (obj))
202 (defmethod pf-ustr ((ucon ucon))
203   "Return the preferred ustr for a ucon"
204   (pf-ustr
205    (find-if (lambda (uterm) (string= "P" (ts uterm))) (s#term ucon))))
206
207 (defmethod pf-ustr ((uterm uterm))
208   "Return the preferred ustr for a uterm"
209   (find-if (lambda (ustr) (string= "PF" (stt ustr))) (s#str uterm)))
210
211 (defgeneric mesh-number (obj))
212 (defmethod mesh-number ((con ucon))
213   (mesh-number (pf-ustr con)))
214
215 (defmethod mesh-number ((ustr ustr))
216   (let ((codes
217          (map-and-remove-nils
218           (lambda (sat)
219             (when (and (string-equal "MSH" (sab sat))
220                        (string-equal "MN" (atn sat)))
221               (atv sat)))
222           (s#sat ustr))))
223     (if (= 1 (length codes))
224         (car codes)
225       codes)))
226
227 (defun ucon-ustrs (ucon)
228   "Return lists of strings for a concept"
229   (let (res)
230     (dolist (term (s#term ucon) (nreverse res))
231       (dolist (str (s#str term))
232         (push str res)))))
233
234
235 (defmethod pfstr ((uterm uterm))
236   "Return the preferred string for a uterm"
237   (dolist (ustr (s#str uterm))
238     (when (string= "PF" (stt ustr))
239       (return-from pfstr (str ustr)))))
240
241 (defmethod pfstr ((ustr ustr))
242   "Return the preferred string for a ustr, which is the string itself"
243   (str ustr))
244
245 (defun remove-non-english-terms (uterms)
246   (remove-if-not #'english-term-p uterms))
247
248 (defun remove-english-terms (uterms)
249   (remove-if #'english-term-p uterms))
250
251
252 (defvar +relationship-abbreviations+
253   '(("RB" "Broader" "has a broader relationship")
254     ("RN" "Narrower" "has a narrower relationship")
255     ("RO" "Other related" "has relationship other than synonymous, narrower, or broader")
256     ("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")
257     ("RQ" "Unspecified" "unspecified source asserted relatedness, possibly synonymous")
258     ("SY" "Source Synonymy" "source asserted synonymy")
259     ("PAR" "Parent" "has parent relationship in a Metathesaurus source vocabulary")
260     ("CHD" "Child" "has child relationship in a Metathesaurus source vocabulary")
261     ("SIB" "Sibling" "has sibling relationship in a Metathesaurus source vocabulary")
262     ("AQ" "Allowed" "is an allowed qualifier for a concept in a Metathesaurus source vocabulary")
263     ("QB" "Qualified" "can be qualified by a concept in a Metathesaurus source vocabulary")))
264
265 (defvar *rel-info-table* (make-hash-table :size 30 :test 'equal))
266 (defvar *is-rel-table-init* nil)
267 (unless *is-rel-table-init*
268   (dolist (relinfo +relationship-abbreviations+)
269     (setf (gethash (string-downcase (car relinfo)) *rel-info-table*)
270       (cdr relinfo)))
271   (setq *is-rel-table-init* t))
272
273 (defun rel-abbr-info (rel)
274   (nth-value 0 (gethash (string-downcase rel) *rel-info-table*)))
275
276 (defun filter-urels-by-rel (urels rel)
277   (remove-if-not (lambda (urel) (string-equal rel (rel urel))) urels))
278
279
280 (defvar +language-abbreviations+
281     '(("BAQ" . "Basque")
282       ("CZE" . "Chech")
283       ("DAN" . "Danish")
284       ("DUT" . "Dutch")
285       ("ENG" . "English")
286       ("FIN" . "Finnish")
287       ("FRE" . "French")
288       ("GER" . "German")
289       ("HEB" . "Hebrew")
290       ("HUN" . "Hungarian")
291       ("ITA" . "Italian")
292       ("JPN" . "Japanese")
293       ("NOR" . "Norwegian")
294       ("POR" . "Portuguese")
295       ("RUS" . "Russian")
296       ("SPA" . "Spanish")
297       ("SWE" . "Swedish")))
298
299 (defvar *lat-info-table* (make-hash-table :size 30 :test 'equal))
300 (defvar *is-lat-table-init* nil)
301 (unless *is-lat-table-init*
302   (dolist (latinfo +language-abbreviations+)
303     (setf (gethash (string-downcase (car latinfo)) *lat-info-table*)
304       (cdr latinfo)))
305   (setq *is-lat-table-init* t))
306
307 (defun lat-abbr-info (lat)
308   (aif (nth-value 0 (gethash (string-downcase lat) *lat-info-table*))
309        it
310        lat))
311
312
313
314 (defun stt-abbr-info (stt)
315   (when (string-equal "PF" stt)
316     (return-from stt-abbr-info "Preferred"))
317   (when (char-equal #\V (schar stt 0))
318     (setq stt (subseq stt 1)))
319   (loop for c across stt
320       collect
321         (cond
322          ((char-equal #\C c)
323           "Upper/lower case")
324          ((char-equal #\W c)
325           "Word order")
326          ((char-equal #\S c)
327           "Singular")
328          ((char-equal #\P c)
329           "Plural")
330          ((char-equal #\O c)
331           "Other"))))
332
333 (defun uso-unique-codes (usos)
334   (let ((sab-codes (make-hash-table :test 'equal)))
335     (dolist (uso usos)
336       (setf (gethash (sab uso) sab-codes) (code uso)))
337     (loop for key being the hash-key in sab-codes
338         collect (list key (gethash key sab-codes)))))
339
340 (defun ucon-has-sab (ucon sab)
341   (and (find-if (lambda (uso) (string-equal sab (sab uso))) (s#so ucon)) t))
342
343
344 #+scl
345 (dolist (c '(urank udef usat uso ucxt ustr uterm usty urel ucoc uatx uconso uxw uxnw uxns lexterm labr lagr lcmp lmod lnom lprn lprp lspl ltrm ltyp lwd sdef sstr sstre1 sstre2 usrl))
346     (let ((cl (find-class c)))
347       (clos:finalize-inheritance cl)))
348
349