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