r11099: add error checking
[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     (prefixed-fixnum-string aui #\A 7)))
91
92 (defmethod fmt-aui ((aui integer))
93   (prefixed-integer-string aui #\A 7))
94
95 (defmethod fmt-aui ((aui string))
96   (if (eql (aref aui 0) #\A)
97       aui
98       (fmt-aui (parse-integer aui))))
99
100 (defgeneric fmt-eui (e))
101 (when *has-fixnum-class*
102   (defmethod fmt-eui ((e fixnum))
103     (prefixed-fixnum-string e #\E 7)))
104
105 (defmethod fmt-eui ((e integer))
106   (prefixed-integer-string e #\E 7))
107
108 (defmethod fmt-eui ((e string))
109   (if (eql (aref e 0) #\E)
110       e
111       (fmt-eui (parse-integer e))))
112
113 (defmethod fmt-eui ((e null))
114   (format nil "nil"))
115
116 (defun cui-p (ui)
117   "Check if a string is a CUI"
118   (check-ui ui #\C 7))
119
120 (defun lui-p (ui)
121   "Check if a string is a LUI"
122   (check-ui ui #\L 7))
123
124 (defun sui-p (ui)
125   "Check if a string is a SUI"
126   (check-ui ui #\S 7))
127
128 (defun tui-p (ui)
129   (check-ui ui #\T 3))
130
131 (defun eui-p (ui)
132   (check-ui ui #\E 7))
133
134 (defun check-ui (ui start-char len)
135   (when (and (stringp ui)
136              (= (length ui) (1+ len))
137              (char-equal start-char (schar ui 0))
138              (ignore-errors (parse-integer ui :start 1)))
139     t))
140
141
142 ;;; Generic display functions
143
144 (eval-when (:compile-toplevel :load-toplevel :execute)
145 (defun english-term-p (obj)
146   "Returns two values: T/NIL if term is english and T/NIL if obj is a TERM"
147   (if (eq (hyperobject::class-name (hyperobject::class-of obj)) 'uterm)
148       (values (string-equal (lat obj) "ENG") t)
149     (values nil nil))))
150
151 (defun english-term-filter (obj)
152   "Retrns NIL if object is a term and not english"
153   (multiple-value-bind (is-english is-term) (english-term-p obj)
154       (or (not is-term) is-english)))
155
156 (defun print-umlsclass (obj &key (stream *standard-output*)
157                         (vid :compact-text)
158                         (file-wrapper nil) (english-only t) (subobjects nil)
159                         (refvars nil) (link-printer nil))
160   (view obj :stream stream :vid vid :subobjects subobjects
161         :file-wrapper file-wrapper
162         :filter (if english-only nil #'english-term-filter)
163         :link-printer link-printer
164         :refvars refvars))
165
166 (defmacro define-lookup-display (newfuncname lookup-func)
167   "Defines functions for looking up and displaying objects"
168   `(defun ,newfuncname  (keyval &key (stream *standard-output*) (vid :compact-text)
169                          (file-wrapper t) (english-only nil) (subobjects nil))
170      (let ((obj (funcall ,lookup-func keyval)))
171        (print-umlsclass obj :stream stream :vid vid
172                         :file-wrapper file-wrapper :english-only english-only
173                         :subobjects subobjects)
174        obj)))
175
176 (define-lookup-display display-con #'find-ucon-cui)
177 (define-lookup-display display-term #'find-uterm-lui)
178 (define-lookup-display display-str #'find-ustr-sui)
179
180 (defun ucon-has-tui (ucon tui)
181   "Returns T if UCON has a semantic type of TUI."
182   (some #'(lambda (usty) (= tui (tui usty))) (s#sty ucon)))
183
184 (defgeneric suistr (lo))
185 (defmethod suistr ((lo ulo))
186   "Return the string for a ulo object"
187   (find-string-sui (sui lo)))
188
189 (defgeneric pf-ustr (obj))
190 (defmethod pf-ustr ((ucon ucon))
191   "Return the preferred ustr for a ucon"
192   (pf-ustr
193    (find-if (lambda (uterm) (string= "P" (ts uterm))) (s#term ucon))))
194
195 (defmethod pf-ustr ((uterm uterm))
196   "Return the preferred ustr for a uterm"
197   (find-if (lambda (ustr) (string= "PF" (stt ustr))) (s#str uterm)))
198
199 (defgeneric mesh-number (obj))
200 (defmethod mesh-number ((con ucon))
201   (mesh-number (pf-ustr con)))
202
203 (defmethod mesh-number ((ustr ustr))
204   (let ((codes
205          (map-and-remove-nils
206           (lambda (sat)
207             (when (and (string-equal "MSH" (sab sat))
208                        (string-equal "MN" (atn sat)))
209               (atv sat)))
210           (s#sat ustr))))
211     (if (= 1 (length codes))
212         (car codes)
213       codes)))
214
215 (defun ucon-ustrs (ucon)
216   "Return lists of strings for a concept"
217   (let (res)
218     (dolist (term (s#term ucon) (nreverse res))
219       (dolist (str (s#str term))
220         (push str res)))))
221                      
222
223 (defmethod pfstr ((uterm uterm))
224   "Return the preferred string for a uterm"
225   (dolist (ustr (s#str uterm))
226     (when (string= "PF" (stt ustr))
227       (return-from pfstr (str ustr)))))
228
229 (defmethod pfstr ((ustr ustr))
230   "Return the preferred string for a ustr, which is the string itself"
231   (str ustr))
232
233 (defun remove-non-english-terms (uterms)
234   (remove-if-not #'english-term-p uterms))
235
236 (defun remove-english-terms (uterms)
237   (remove-if #'english-term-p uterms))
238
239
240 (defvar +relationship-abbreviations+
241   '(("RB" "Broader" "has a broader relationship")
242     ("RN" "Narrower" "has a narrower relationship")
243     ("RO" "Other related" "has relationship other than synonymous, narrower, or broader")
244     ("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")
245     ("RQ" "Unspecified" "unspecified source asserted relatedness, possibly synonymous")
246     ("SY" "Source Synonymy" "source asserted synonymy")
247     ("PAR" "Parent" "has parent relationship in a Metathesaurus source vocabulary")
248     ("CHD" "Child" "has child relationship in a Metathesaurus source vocabulary")
249     ("SIB" "Sibling" "has sibling relationship in a Metathesaurus source vocabulary")
250     ("AQ" "Allowed" "is an allowed qualifier for a concept in a Metathesaurus source vocabulary")
251     ("QB" "Qualified" "can be qualified by a concept in a Metathesaurus source vocabulary")))
252
253 (defvar *rel-info-table* (make-hash-table :size 30 :test 'equal))
254 (defvar *is-rel-table-init* nil)
255 (unless *is-rel-table-init*
256   (dolist (relinfo +relationship-abbreviations+)
257     (setf (gethash (string-downcase (car relinfo)) *rel-info-table*)
258       (cdr relinfo)))
259   (setq *is-rel-table-init* t))
260
261 (defun rel-abbr-info (rel)
262   (nth-value 0 (gethash (string-downcase rel) *rel-info-table*)))
263
264 (defun filter-urels-by-rel (urels rel)
265   (remove-if-not (lambda (urel) (string-equal rel (rel urel))) urels))
266
267
268 (defvar +language-abbreviations+
269     '(("BAQ" . "Basque")
270       ("CZE" . "Chech")
271       ("DAN" . "Danish")
272       ("DUT" . "Dutch")
273       ("ENG" . "English")
274       ("FIN" . "Finnish")
275       ("FRE" . "French")
276       ("GER" . "German")
277       ("HEB" . "Hebrew")
278       ("HUN" . "Hungarian")
279       ("ITA" . "Italian")
280       ("JPN" . "Japanese")
281       ("NOR" . "Norwegian")
282       ("POR" . "Portuguese")
283       ("RUS" . "Russian")
284       ("SPA" . "Spanish")
285       ("SWE" . "Swedish")))
286
287 (defvar *lat-info-table* (make-hash-table :size 30 :test 'equal))
288 (defvar *is-lat-table-init* nil)
289 (unless *is-lat-table-init*
290   (dolist (latinfo +language-abbreviations+)
291     (setf (gethash (string-downcase (car latinfo)) *lat-info-table*)
292       (cdr latinfo)))
293   (setq *is-lat-table-init* t))
294
295 (defun lat-abbr-info (lat)
296   (aif (nth-value 0 (gethash (string-downcase lat) *lat-info-table*))
297        it
298        lat))
299
300
301
302 (defun stt-abbr-info (stt)
303   (when (string-equal "PF" stt)
304     (return-from stt-abbr-info "Preferred"))
305   (when (char-equal #\V (schar stt 0))
306     (setq stt (subseq stt 1)))
307   (loop for c across stt
308       collect
309         (cond
310          ((char-equal #\C c)
311           "Upper/lower case")
312          ((char-equal #\W c)
313           "Word order")
314          ((char-equal #\S c)
315           "Singular")
316          ((char-equal #\P c)
317           "Plural")
318          ((char-equal #\O c)
319           "Other"))))
320
321             
322 (defun ucon-parents (con &optional sab)
323   (ucon-ancestors con sab t))
324
325 (defun ucon-ancestors (ucon &optional sab single-level)
326   "Returns a list of ancestor lists for a concept"
327   (let* ((parent-rels (filter-urels-by-rel (s#rel ucon) "par"))
328          (anc nil))
329     (when sab
330       (setq parent-rels (delete-if-not 
331                          (lambda (rel) (string-equal sab (sab rel)))
332                          parent-rels)))
333     (dolist (rel parent-rels (nreverse anc))
334       (let ((parent (find-ucon-cui (cui2 rel))))
335         (push
336          (if single-level
337              (list parent)
338            (list* parent (car (ucon-ancestors parent (sab rel) nil))))
339          anc)))))
340
341 (defgeneric cxt-ancestors (obj))
342 (defmethod cxt-ancestors ((con ucon))
343   (loop for term in (s#term con)
344       append (cxt-ancestors term)))
345                     
346
347 (defmethod cxt-ancestors ((term uterm))
348   (loop for str in (s#str term)
349       append (cxt-ancestors str)))
350     
351 (defmethod cxt-ancestors ((str ustr))
352   "Return the ancestory contexts of a ustr"
353   (let* ((anc (remove-if-not
354                (lambda (cxt) (string-equal "ANC" (cxl cxt)))
355                (s#cxt str)))
356          (num-contexts (if anc
357                            (apply #'max (mapcar (lambda (cxt) (cxn cxt)) anc))
358                          0))
359          (anc-lists '()))
360     (dotimes (i num-contexts (nreverse anc-lists))
361       (let* ((anc-this-cxn (remove-if-not
362                             (lambda (cxt) (= (1+ i) (cxn cxt))) anc)))
363         (push
364          (sort anc-this-cxn (lambda (a b) (< (rank a) (rank b))))
365          anc-lists)))))
366
367   
368 #+scl
369 (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))
370     (let ((cl (find-class c)))
371       (clos:finalize-inheritance cl)))
372
373