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