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