Change default SQL server host
[umlisp.git] / class-support.lisp
index d1172022645d2e0ddb9fcead2236158a9c89e3d8..47463dd194610fb454fc6a7b5a37ff484bf58cb5 100644 (file)
@@ -7,10 +7,8 @@
 ;;;; Author:   Kevin M. Rosenberg
 ;;;; Created:  Apr 2000
 ;;;;
-;;;; $Id$
-;;;;
 ;;;; This file, part of UMLisp, is
-;;;;    Copyright (c) 2000-2006 by Kevin M. Rosenberg, M.D.
+;;;;    Copyright (c) 2000-2010 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.
 
 (when *has-fixnum-class*
   (defmethod fmt-cui ((c fixnum))
-    (prefixed-fixnum-string c #\C 7)))
+    (if (>= c 10000000)
+        (prefixed-fixnum-string c #\C 8)
+      (prefixed-fixnum-string c #\C 7))))
 
 (defmethod fmt-cui ((c integer))
-    (prefixed-integer-string c #\C 7))
+  (if (>= c 10000000)
+      (prefixed-fixnum-string c #\C 8)
+    (prefixed-fixnum-string c #\C 7)))
 
 (defmethod fmt-cui ((c string))
   (if (eql (aref c 0) #\C)
 
 (when *has-fixnum-class*
   (defmethod fmt-lui ((l fixnum))
-    (prefixed-fixnum-string l #\L 7)))
+    (if (>= l 10000000)
+        (prefixed-fixnum-string l #\L 8)
+      (prefixed-fixnum-string l #\L 7))))
 
 (defmethod fmt-lui ((l integer))
-  (prefixed-integer-string l #\L 7))
+  (if (>= l 10000000)
+      (prefixed-fixnum-string l #\L 8)
+    (prefixed-fixnum-string l #\L 7)))
 
 (defmethod fmt-lui ((l string))
   (if (eql (aref l 0) #\L)
 
 (when *has-fixnum-class*
   (defmethod fmt-sui ((s fixnum))
-    (prefixed-fixnum-string s #\S 7)))
+    (if (>= s 10000000)
+        (prefixed-fixnum-string s #\S 8)
+      (prefixed-fixnum-string s #\S 7))))
 
 (defmethod fmt-sui ((s integer))
-  (prefixed-integer-string s #\S 7))
+  (if (>= s 10000000)
+      (prefixed-fixnum-string s #\S 8)
+    (prefixed-fixnum-string s #\S 7)))
 
 (defmethod fmt-sui ((s string))
   (if (eql (aref s 0) #\S)
 
 (defun check-ui (ui start-char len)
   (when (and (stringp ui)
-            (= (length ui) (1+ len))
-            (char-equal start-char (schar ui 0))
-            (ignore-errors (parse-integer ui :start 1)))
+             (= (length ui) (1+ len))
+             (char-equal start-char (schar ui 0))
+             (ignore-errors (parse-integer ui :start 1)))
     t))
 
 
       (or (not is-term) is-english)))
 
 (defun print-umlsclass (obj &key (stream *standard-output*)
-                       (vid :compact-text)
-                       (file-wrapper nil) (english-only t) (subobjects nil)
-                       (refvars nil) (link-printer nil))
+                        (vid :compact-text)
+                        (file-wrapper nil) (english-only t) (subobjects nil)
+                        (refvars nil) (link-printer nil))
   (view obj :stream stream :vid vid :subobjects subobjects
-       :file-wrapper file-wrapper
-       :filter (if english-only nil #'english-term-filter)
-       :link-printer link-printer
-       :refvars refvars))
+        :file-wrapper file-wrapper
+        :filter (if english-only nil #'english-term-filter)
+        :link-printer link-printer
+        :refvars refvars))
 
 (defmacro define-lookup-display (newfuncname lookup-func)
   "Defines functions for looking up and displaying objects"
   `(defun ,newfuncname  (keyval &key (stream *standard-output*) (vid :compact-text)
-                        (file-wrapper t) (english-only nil) (subobjects nil))
+                         (file-wrapper t) (english-only nil) (subobjects nil))
      (let ((obj (funcall ,lookup-func keyval)))
        (print-umlsclass obj :stream stream :vid vid
-                       :file-wrapper file-wrapper :english-only english-only
-                       :subobjects subobjects)
+                        :file-wrapper file-wrapper :english-only english-only
+                        :subobjects subobjects)
        obj)))
 
 (define-lookup-display display-con #'find-ucon-cui)
   "Returns T if UCON has a semantic type of TUI."
   (some #'(lambda (usty) (= tui (tui usty))) (s#sty ucon)))
 
-(defgeneric suistr (lo))
-(defmethod suistr ((lo ulo))
-  "Return the string for a ulo object"
-  (find-string-sui (sui lo)))
-
 (defgeneric pf-ustr (obj))
 (defmethod pf-ustr ((ucon ucon))
   "Return the preferred ustr for a ucon"
 
 (defmethod mesh-number ((ustr ustr))
   (let ((codes
-        (map-and-remove-nils
-         (lambda (sat)
-           (when (and (string-equal "MSH" (sab sat))
-                      (string-equal "MN" (atn sat)))
-             (atv sat)))
-         (s#sat ustr))))
+         (map-and-remove-nils
+          (lambda (sat)
+            (when (and (string-equal "MSH" (sab sat))
+                       (string-equal "MN" (atn sat)))
+              (atv sat)))
+          (s#sat ustr))))
     (if (= 1 (length codes))
-       (car codes)
+        (car codes)
       codes)))
 
 (defun ucon-ustrs (ucon)
   (let (res)
     (dolist (term (s#term ucon) (nreverse res))
       (dolist (str (s#str term))
-       (push str res)))))
+        (push str res)))))
 
 
 (defmethod pfstr ((uterm uterm))
 (defun rel-abbr-info (rel)
   (nth-value 0 (gethash (string-downcase rel) *rel-info-table*)))
 
-(defun filter-urels-by-rel (urels rel)
-  (remove-if-not (lambda (urel) (string-equal rel (rel urel))) urels))
+(defun filter-urels-by-rel (urels rel &key (remove-duplicate-pfstr2 t) (sort :pfstr2))
+  (let ((f (remove-if-not (lambda (urel) (string-equal rel (rel urel))) urels)))
+    (when remove-duplicate-pfstr2
+      (setq f (remove-duplicates f :test 'equal :key 'u::pfstr2)))
+    (if sort
+        (sort (copy-seq f) 'string-lessp :key 'u::pfstr2)
+        f)))
+
+#+mrcoc
+(defun filter-ucocs (ucocs &key (remove-duplicate-pfstr2 t) (sort :pfstr2))
+  (when remove-duplicate-pfstr2
+    (setq ucocs (remove-duplicates ucocs :test 'equal :key 'u::pfstr2)))
+    (if sort
+        (sort (copy-seq ucocs) 'string-lessp :key 'u::pfstr2)
+        ucocs))
 
 
 (defvar +language-abbreviations+
     (setq stt (subseq stt 1)))
   (loop for c across stt
       collect
-       (cond
-        ((char-equal #\C c)
-         "Upper/lower case")
-        ((char-equal #\W c)
-         "Word order")
-        ((char-equal #\S c)
-         "Singular")
-        ((char-equal #\P c)
-         "Plural")
-        ((char-equal #\O c)
-         "Other"))))
-
-
-(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)
-          (path-min nil))
-      (declare (fixnum min))
-      (dolist (shared shareds)
-        (multiple-value-bind (d1 p1) (ancestor-distance shared anc1)
-          (multiple-value-bind (d2 p2) (ancestor-distance shared anc2)
-            (let ((dtotal (+ d1 d2)))
-              (declare (fixnum d1 d2 dtotal))
-              (when (< dtotal min)
-                (format t "~D ~D ~D ~S ~S~%" d1 d2 dtotal p1 p2)
-                (setq min dtotal
-                      cui-min shared
-                      path-min (append p1 (cdr (reverse p2)))
-                      ))))))
-      (values min cui-min path-min))))
-
-(defun cdist-metric (c1 c2 &key sab include-rb full-ucon (srl *current-srl*))
-  (let* ((anc1 (ucon-ancestors c1 :sab sab :include-rb include-rb :full-ucon full-ucon :srl srl))
-         (anc2 (ucon-ancestors c2 :sab sab :include-rb include-rb :full-ucon full-ucon :srl srl)))
-    (multiple-value-bind (min cui path)
-        (cdist-ancestors (list* c1 anc1) (list* c2 anc2))
-      (if cui
-          (values min cui (append (list c1) path (unless (eql (cui (car (last path))) (cui c2))
-                                                   (list c2))))
-        (values nil nil nil)))))
-               
-
-(defun ancestor-distance (cui ancestors &key (distance 0) path)
-  (cond
-   ((null ancestors)
-    nil)
-   ((atom ancestors)
-    (when (eql cui (cui ancestors))
-      (values distance (nreverse (list* ancestors path)))))
-   ((atom (car ancestors))
-    (if (eql cui (cui (car ancestors)))
-        (values distance (nreverse (list* (car ancestors) path)))
-      (ancestor-distance cui (cdr ancestors) :distance distance :path (list* (car ancestors) path))))
-   (t
-    (let ((%min most-positive-fixnum)
-          %path)
-      (dolist (a ancestors)
-        (multiple-value-bind (d p) (ancestor-distance cui a :distance (1+ distance) :path path)
-          (when (and d (< d %min))
-            (setq %min d
-                  %path p))))
-      (when (< %min most-positive-fixnum)
-        (values %min %path))))))
-
-        
-(defun ucon-ancestors (ucon &key sab include-rb ancestors full-ucon (srl *current-srl*))
-  "Returns a list of ancestor lists for a concept"
-  (unless ucon (return-from ucon-ancestors nil))
-  (let ((parent-rels (find-urel-cui
-                      (cui ucon)
-                      :without-pfstr2 t
-                      :srl srl
-                      :filter (concatenate 'string
-                                (if sab (format nil "SAB='~A' AND " sab) "")
-                                (if include-rb
-                                    "(REL='RB' OR REL='PAR')"
-                                  "REL='PAR'"))))
-        (parents nil))
-    (dolist (rel parent-rels)
-      (let ((parent (if full-ucon
-                        (find-ucon-cui (cui2 rel))
-                      (make-ucon-cui (cui2 rel)))))
-        ;; (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)
-                            :full-ucon full-ucon))
-           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
-              (lambda (cxt) (string-equal "ANC" (cxl cxt)))
-              (s#cxt str)))
-        (num-contexts (if anc
-                          (apply #'max (mapcar (lambda (cxt) (cxn cxt)) anc))
-                        0))
-        (anc-lists '()))
-    (dotimes (i num-contexts (nreverse anc-lists))
-      (let* ((anc-this-cxn (remove-if-not
-                           (lambda (cxt) (= (1+ i) (cxn cxt))) anc)))
-       (push
-        (sort anc-this-cxn (lambda (a b) (< (rank a) (rank b))))
-        anc-lists)))))
+         ((char-equal #\C c)
+          "Upper/lower case")
+         ((char-equal #\W c)
+          "Word order")
+         ((char-equal #\S c)
+          "Singular")
+         ((char-equal #\P c)
+          "Plural")
+         ((char-equal #\O c)
+          "Other"))))
 
 (defun uso-unique-codes (usos)
   (let ((sab-codes (make-hash-table :test 'equal)))
     (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))
+(dolist (c '(urank udef usat uso ucxt ustr uterm usty urel
+             #+mrcoc ucoc
+             uatx uconso 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)))
       (clos:finalize-inheritance cl)))
-
-