r11167: added cdist fn
[umlisp.git] / class-support.lisp
index c14291d7607e9552f1a878a046e5bd95bb21ec19..1d3f96d0902ba0a5f909812b154af04f8d9948c8 100644 (file)
@@ -10,7 +10,7 @@
 ;;;; $Id$
 ;;;;
 ;;;; This file, part of UMLisp, is
-;;;;    Copyright (c) 2000-2004 by Kevin M. Rosenberg, M.D.
+;;;;    Copyright (c) 2000-2006 by Kevin M. Rosenberg, M.D.
 ;;;;
 ;;;; UMLisp users are granted the rights to distribute and use this software
 ;;;; as governed by the terms of the GNU General Public License.
 (defmethod fmt-cui ((c ucon))
   (fmt-cui (cui c)))
 
-(defmethod fmt-cui ((c fixnum))
-  (prefixed-fixnum-string c #\C 7))
+(when *has-fixnum-class*
+  (defmethod fmt-cui ((c fixnum))
+    (prefixed-fixnum-string c #\C 7)))
+
+(defmethod fmt-cui ((c integer))
+    (prefixed-integer-string c #\C 7))
 
 (defmethod fmt-cui ((c string))
   (if (eql (aref c 0) #\C)
 (defmethod fmt-lui ((l uterm))
   (fmt-lui (lui l)))
 
-(defmethod fmt-lui ((l fixnum))
-  (prefixed-fixnum-string l #\L 7))
+(when *has-fixnum-class*
+  (defmethod fmt-lui ((l fixnum))
+    (prefixed-fixnum-string l #\L 7)))
+
+(defmethod fmt-lui ((l integer))
+  (prefixed-integer-string l #\L 7))
 
 (defmethod fmt-lui ((l string))
   (if (eql (aref l 0) #\L)
 (defmethod fmt-sui ((s ustr))
   (fmt-sui (sui s)))
 
-(defmethod fmt-sui ((s fixnum))
-  (prefixed-fixnum-string s #\S 7))
+(when *has-fixnum-class*
+  (defmethod fmt-sui ((s fixnum))
+    (prefixed-fixnum-string s #\S 7)))
+
+(defmethod fmt-sui ((s integer))
+  (prefixed-integer-string s #\S 7))
 
 (defmethod fmt-sui ((s string))
   (if (eql (aref s 0) #\S)
       (fmt-sui (parse-integer s))))
 
 (defgeneric fmt-tui (tui))
-(defmethod fmt-tui ((tui fixnum))
-  (prefixed-fixnum-string tui #\T 3))
+(when *has-fixnum-class*
+  (defmethod fmt-tui ((tui fixnum))
+    (prefixed-fixnum-string tui #\T 3)))
+
+(defmethod fmt-tui ((tui integer))
+  (prefixed-integer-string tui #\T 3))
 
 (defmethod fmt-tui ((tui string))
   (if (eql (aref tui 0) #\T)
     (fmt-tui (parse-integer tui))))
 
 (defgeneric fmt-aui (aui))
-(defmethod fmt-aui ((aui fixnum))
-  (prefixed-fixnum-string aui #\A 7))
+(when *has-fixnum-class*
+  (defmethod fmt-aui ((aui fixnum))
+    (if (>= aui 10000000)
+      (prefixed-fixnum-string aui #\A 8)
+      (prefixed-fixnum-string aui #\A 7))))
+
+(defmethod fmt-aui ((aui integer))
+  (if (>= aui 10000000)
+    (prefixed-integer-string aui #\A 8)
+    (prefixed-integer-string aui #\A 7)))
 
 (defmethod fmt-aui ((aui string))
   (if (eql (aref aui 0) #\A)
       aui
       (fmt-aui (parse-integer aui))))
 
+(defgeneric fmt-rui (rui))
+(when *has-fixnum-class*
+  (defmethod fmt-rui ((rui fixnum))
+    (prefixed-fixnum-string rui #\A 8)))
+
+(defmethod fmt-rui ((rui integer))
+  (prefixed-integer-string rui #\A 8))
+
+(defmethod fmt-rui ((rui string))
+  (if (eql (aref rui 0) #\R)
+      rui
+    (fmt-rui (parse-integer rui))))
+
 (defgeneric fmt-eui (e))
-(defmethod fmt-eui ((e fixnum))
-  (prefixed-fixnum-string e #\E 7))
+(when *has-fixnum-class*
+  (defmethod fmt-eui ((e fixnum))
+    (prefixed-fixnum-string e #\E 7)))
+
+(defmethod fmt-eui ((e integer))
+  (prefixed-integer-string e #\E 7))
 
 (defmethod fmt-eui ((e string))
   (if (eql (aref e 0) #\E)
     (dolist (term (s#term ucon) (nreverse res))
       (dolist (str (s#str term))
        (push str res)))))
-                    
+
 
 (defmethod pfstr ((uterm uterm))
   "Return the preferred string for a uterm"
         ((char-equal #\O c)
          "Other"))))
 
-           
-(defun ucon-parents (con &optional sab)
-  (ucon-ancestors con sab t))
 
-(defun ucon-ancestors (ucon &optional sab single-level)
+(defun ucon-parents (ucon &key sab include-rb)
+  (ucon-ancestors ucon :sab sab :include-rb include-rb
+                  :ancestors nil))
+
+(defun is-ucon-in-ancestors (ucon ancestors)
+  (cond
+   ((null ancestors) nil)
+   ((atom ancestors) (eql (cui ucon) (cui ancestors)))
+   ((listp (car ancestors))
+    (or (is-ucon-in-ancestors ucon (car ancestors))
+        (is-ucon-in-ancestors ucon (cdr ancestors))))
+   (t
+    (or (eql (cui ucon) (cui (car ancestors)))
+        (is-ucon-in-ancestors ucon (cdr ancestors))))))
+
+
+(defun sorted-ancestor-cuis (anc)
+  (sort (remove-duplicates (map 'list 'cui (flatten anc))) #'<))
+
+(defun shared-cuis (a1 a2)
+  (let ((cl1 (sorted-ancestor-cuis a1))
+        (cl2 (sorted-ancestor-cuis a2))
+        (shared nil))
+    (dolist (c1 cl1 (nreverse shared))
+      (dolist (c2 cl2)
+        (cond
+         ((eql c1 c2)
+          (push c1 shared))
+         ((> c2 c1)
+          (return)))))))
+
+(defun cdist-ancestors (anc1 anc2)
+  (let ((shareds (shared-cuis anc1 anc2)))
+    (let ((min most-positive-fixnum)
+          (cui-min nil))
+      (declare (fixnum min))
+      (dolist (shared shareds)
+        (let* ((d1 (ancestor-distance shared anc1))
+               (d2 (ancestor-distance shared anc2))
+               (dtotal (+ d1 d2)))
+          (declare (fixnum d1 d2 dtotal))
+          (when (< dtotal min)
+            (setq min dtotal
+                  cui-min shared))))
+      (values min cui-min))))
+
+(defun cdist (c1 c2 &key sab include-rb)
+  (let* ((anc1 (ucon-ancestors c1 :sab sab :include-rb include-rb))
+         (anc2 (ucon-ancestors c2 :sab sab :include-rb include-rb)))
+    (multiple-value-bind (min cui)
+        (cdist-ancestors anc1 anc2)
+      (if cui
+          (values min cui)
+        (values nil nil)))))
+               
+
+(defun ancestor-distance (cui ancestors &key (distance 0))
+  (cond
+   ((null ancestors)
+    nil)
+   ((atom ancestors)
+    (when (eql cui (cui ancestors))
+      distance))
+   ((atom (car ancestors))
+    (if (eql cui (cui (car ancestors)))
+        distance
+      (ancestor-distance cui (cdr ancestors) :distance distance)))
+   (t
+    (let ((min most-positive-fixnum))
+      (dolist (a ancestors)
+        (let ((d (ancestor-distance cui a :distance (1+ distance))))
+          (when (and d (< d min))
+            (setq min d))))
+      (when (< min most-positive-fixnum)
+        min)))))
+
+    
+        
+(defun ucon-ancestors (ucon &key sab include-rb ancestors)
   "Returns a list of ancestor lists for a concept"
-  (let* ((parent-rels (filter-urels-by-rel (s#rel ucon) "par"))
-        (anc nil))
+  (let* ((parent-rels (append (filter-urels-by-rel (s#rel ucon) "par")
+                              (when include-rb
+                                (filter-urels-by-rel (s#rel ucon) "rb"))))
+         (parents nil))
     (when sab
-      (setq parent-rels (delete-if-not 
+      (setq parent-rels (delete-if-not
                         (lambda (rel) (string-equal sab (sab rel)))
                         parent-rels)))
-    (dolist (rel parent-rels (nreverse anc))
+    (dolist (rel parent-rels)
       (let ((parent (find-ucon-cui (cui2 rel))))
-       (push
-        (if single-level
-            (list parent)
-          (list* parent (car (ucon-ancestors parent (sab rel) nil))))
-        anc)))))
+        ;;(format t "~S ~S ~S ~S~%" rel ucon parent ancestors)
+        (unless (is-ucon-in-ancestors parent ancestors)
+          (push
+           (list*
+            parent
+            (ucon-ancestors parent :sab (sab rel) :ancestors (append (list parent) ancestors)))
+           parents))))
+    (nreverse parents)))
+
 
 (defgeneric cxt-ancestors (obj))
 (defmethod cxt-ancestors ((con ucon))
   (loop for term in (s#term con)
       append (cxt-ancestors term)))
-                   
+
 
 (defmethod cxt-ancestors ((term uterm))
   (loop for str in (s#str term)
       append (cxt-ancestors str)))
-    
+
 (defmethod cxt-ancestors ((str ustr))
   "Return the ancestory contexts of a ustr"
   (let* ((anc (remove-if-not
         (sort anc-this-cxn (lambda (a b) (< (rank a) (rank b))))
         anc-lists)))))
 
-  
+(defun uso-unique-codes (usos)
+  (let ((sab-codes (make-hash-table :test 'equal)))
+    (dolist (uso usos)
+      (setf (gethash (sab uso) sab-codes) (code uso)))
+    (loop for key being the hash-key in sab-codes
+        collect (list key (gethash key sab-codes)))))
+
+
+(defun ucon-has-sab (ucon sab)
+  (and (find-if (lambda (uso) (string-equal sab (sab uso))) (s#so ucon)) t))
+
+
 #+scl
 (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))
     (let ((cl (find-class c)))