git.fiddlerwoaroof.com
Browse code

various developments

Ed L authored on 12/07/2016 16:29:50
Showing 5 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,124 @@
1
+(defpackage #:test-clim
2
+  (:use #:clim #:clim-lisp))
3
+(in-package #:test-clim)
4
+(shadow 'inspect)
5
+
6
+(defun tokenize (string)
7
+  (loop with result = '()
8
+        with word = '()
9
+        for char across string
10
+        when (or (char= char #\space) (char= char #\newline) (char= char #\tab))
11
+          do (progn (push (reverse (coerce word 'string)) result)
12
+                    (setf word '()))
13
+
14
+        unless (or (char= char #\space) (char= char #\newline) (char= char #\tab))
15
+          do (push char word)
16
+
17
+        finally (progn (push (reverse (coerce word 'string)) result)
18
+                       (return (reverse result)))))
19
+
20
+(defparameter *records*
21
+  '(
22
+"Networks are in trouble. The volume of data, applications and transactions hitting data centers is increasing at an exponential pace. Add in predictions that by 2020 users will own as many as 25 connected devices and, according to Cisco, the Internet of Things will account for as many as 50 billion new IP-enabled devices and you can see a tsunami of traffic headed our way."
23
+
24
+"Networking systems built around multi-purpose processors are about to slam into a price/performance wall that will either choke traffic or break networking budgets."
25
+
26
+"Recently, Google engineers blogged about a new ASIC they developed, called a Tensor Processing Unit designed to accelerate machine-learning applications.  They argue that this ASIC has fast-forwarded their technology by seven years, allowing them to leapfrog competition and “squeeze more operations per second into the silicon, use more sophisticated and powerful machine learning models, and apply these models more quickly, so users get more intelligent results more rapidly.”"
27
+
28
+"There are a number of challenges for other companies interested in this approach. The development of ASICs is resource-intensive and time consuming."
29
+
30
+"Setting aside issues like the shortage of available talent and the average of four years to develop and bring a new ASIC to market, the material costs alone are high. The manufacturing of a typical two-gram chip requires 1.6 kilograms of fossil fuel, 72 grams of chemicals, and 32 kilograms of water. The materials involved in making a 32Mbit RAM chip can add up to as much as 630 times the mass of the final product."
31
+
32
+"Market analysts and business leaders for years have discounted the value of ASICs. They argued that the time to develop the hardware, design the firmware, and incorporate the technology into current programming and development processes is too expensive. They complain that it can slow down their ability to respond to market changes and say multi-purpose CPUs are fast, cheap and easy to write software for."
33
+
34
+"However, general-purpose microprocessors are about to run up against the limits of Moore's law. Features in some of Intel’s latest chips are said to be just 100 atoms wide. To keep up with current demand, that same chip by 2020 will need to have features ten atoms wide."
35
+
36
+"IBM announced they are spending $3 billion to develop a new generation of microprocessors. It estimates its state-of-the-art chips will use a 7nm process in five years. At that size reliability becomes a serious issue with manufacturing errors and even quantum challenges."
37
+    ))
38
+
39
+(declaim (optimize (debug 3)))
40
+
41
+(define-application-frame
42
+  test-clim ()
43
+
44
+  ()
45
+  (:pointer-documentation t)
46
+  (:panes
47
+    (app :application
48
+         :height 400 :width 600
49
+         :display-time nil
50
+         :scroll-bars :vertical)
51
+    (int :interactor :height 100 :width 600))
52
+  (:layouts
53
+    (default (horizontally () app int))))
54
+
55
+(defun app-main ()
56
+  (let ((frame (make-application-frame 'test-clim)))
57
+    (run-frame-top-level frame)))
58
+
59
+(define-test-clim-command (com-quit :name t) ()
60
+  (frame-exit *application-frame*))
61
+
62
+(test-clim::define-test-clim-command (inspect :name t) ()
63
+ (clouseau:inspector
64
+   (clim:stream-output-history
65
+     (clim:find-pane-named clim:*application-frame* 'test-clim::app))))
66
+
67
+(define-test-clim-command (com-put-records :name t) ()
68
+  (let* ((pane (clim:find-pane-named clim:*application-frame* 'app))
69
+         (history (clim:stream-output-history pane))
70
+         (record-strings
71
+           (mapcar (lambda (text)
72
+                     (with-output-to-string (pane)
73
+                       (pprint-logical-block (pane (tokenize text))
74
+                         (pprint-indent :block 4 pane)
75
+                         (loop
76
+                           (pprint-exit-if-list-exhausted)
77
+                           (princ (pprint-pop) pane)
78
+                           (pprint-exit-if-list-exhausted)
79
+                           (princ #\space pane)
80
+                           (pprint-newline :fill pane)))))
81
+                   *records*))
82
+         (records (mapcar (lambda (text)
83
+                            (clim:with-output-to-output-record (pane)
84
+                              (princ text pane)))
85
+                          (apply #'concatenate 'list
86
+                                 (mapcar (lambda (x) (serapeum:split-sequence #\newline x))
87
+                                         record-strings)))))
88
+    (loop for n from 0
89
+          for record in records
90
+          do (climacs-flexichain-output-history:insert history record n))
91
+    (climacs-flexichain-output-history:change-space-requirements history)
92
+    (clim:replay history pane)))
93
+
94
+(define-test-clim-command (com-put-records1 :name t) ()
95
+  (let* ((pane (clim:find-pane-named clim:*application-frame* 'app))
96
+         (history (clim:stream-output-history pane))
97
+         (records (mapcar (lambda (text)
98
+                            (clim:with-output-to-output-record (pane)
99
+                              (pprint-logical-block (pane (tokenize text))
100
+                                (pprint-indent :block 4 pane)
101
+                                (loop
102
+                                  (pprint-exit-if-list-exhausted)
103
+                                  (princ (pprint-pop) pane)
104
+                                  (pprint-exit-if-list-exhausted)
105
+                                  (princ #\space pane)
106
+                                  (pprint-newline :fill pane)))))
107
+                          *records*)))
108
+    (mapcar (lambda (record)
109
+              (climacs-flexichain-output-history:insert history record 0)
110
+              (climacs-flexichain-output-history:change-space-requirements history) 
111
+              (clim:replay history pane))
112
+            records)))
113
+
114
+(define-test-clim-command (com-initialize :name t) () ()
115
+  (let ((pane (clim:find-pane-named clim:*application-frame* 'app)))
116
+    (setf (clim:stream-recording-p pane) nil)
117
+    ;(setf (clim:stream-end-of-line-action pane) :allow)
118
+    (change-class (clim:stream-output-history pane)
119
+                  'climacs-flexichain-output-history:flexichain-output-history
120
+                  :parent pane)))
121
+
122
+
123
+#+os-macosx (setf mcclim-truetype:*truetype-font-path* "/Library/Fonts/")
124
+(app-main)
0 125
new file mode 100644
... ...
@@ -0,0 +1,125 @@
1
+;;;; it-lookup.lisp
2
+
3
+(defpackage #:it-lookup-clim
4
+  (:use #:clim #:clim-lisp #:it-lookup))
5
+
6
+(in-package #:it-lookup-clim)
7
+(declaim (optimize (debug 3)))
8
+
9
+(define-application-frame
10
+  it-lookup ()
11
+
12
+  ((results :initform (make-instance 'it-lookup::it-results :query "amicus") 
13
+            :accessor results)
14
+   (cursor :initform 0 :accessor cursor))
15
+  (:pointer-documentation t)
16
+  (:panes
17
+    (app :application
18
+         :height 400 :width 600
19
+         :incremental-redisplay t
20
+         :double-buffering t
21
+         ;:display-time nil
22
+         :display-function #'display-app
23
+         )
24
+    (int :interactor :height 100 :width 600))
25
+  (:layouts
26
+    (default (horizontally () app int))))
27
+
28
+(defun display-app (frame pane)
29
+  (let ((history (stream-output-history pane)))
30
+    (loop for element across (it-lookup::cases (results frame))
31
+          do (add-output-record (with-output-to-output-record (pane)
32
+                                  (terpri pane)   
33
+                                  (it-lookup::format-result element pane)
34
+                                  (terpri pane))
35
+                                history))
36
+    (climacs-flexichain-output-history:change-space-requirements history) 
37
+    (clim:replay history pane)))
38
+
39
+(define-presentation-type reference ())
40
+(defmethod it-lookup::format-reference :around ((it-case it-lookup::it-case)
41
+                                               (stream clim:extended-output-stream))
42
+  (clim:with-text-style (stream '(:serif :bold 16))
43
+    (with-output-as-presentation (stream it-case 'reference)
44
+      (call-next-method))))
45
+
46
+(defmethod it-lookup::format-reference :around ((it-case it-lookup::it-case)
47
+                                               (stream sb-pretty:pretty-stream))
48
+  (let ((real-stream (it-lookup::get-real-stream stream)))
49
+    (terpri stream)
50
+    (it-lookup::format-reference it-case real-stream)))
51
+
52
+(defun display-data (&rest args)
53
+  (get-data))
54
+
55
+(defun get-data ()
56
+  (return-from get-data)
57
+  (let* ((pane (find-pane-named *application-frame* 'app))
58
+         (history (stream-output-history pane)))
59
+    (loop for element across (it-lookup::cases (results *application-frame*))
60
+          for n from 0
61
+          do (let ((record (clim:with-output-to-output-record (pane)
62
+                             (it-lookup::format-result element pane)
63
+                             (terpri pane))))
64
+               #|(climb:ou history record n)|#))
65
+    (climacs-flexichain-output-history:change-space-requirements history) 
66
+    (clim:replay history pane)))
67
+
68
+(defclass no-clear-output-history (climacs-flexichain-output-history:flexichain-output-history)
69
+  ())
70
+
71
+(defmethod clear-output-record ((h no-clear-output-history)))
72
+
73
+(defmethod generate-panes :after ((fm frame-manager) (frame it-lookup))
74
+  (let ((*application-frame* frame))
75
+    (let ((pane (clim:find-pane-named frame 'app)))
76
+      (unless pane (break))
77
+      (setf (clim:stream-recording-p pane) nil)
78
+      (setf (clim:stream-end-of-line-action pane) :allow)
79
+      (change-class (clim:stream-output-history pane)
80
+                    ;'climacs-flexichain-output-history:flexichain-output-history
81
+                    'no-clear-output-history
82
+                    :parent pane))))
83
+
84
+(defun app-main ()
85
+  (let* ((frame (make-application-frame 'it-lookup))
86
+         (*application-frame* frame))
87
+    (with-accessors ((results results)) frame
88
+      (parse-results results
89
+                     (run-query results)))
90
+    (run-frame-top-level frame)))
91
+
92
+(define-it-lookup-command (com-inspect :name t) ()
93
+  (clouseau:inspector
94
+    (clim:stream-output-history
95
+      (clim:find-pane-named clim:*application-frame* 'app))))
96
+
97
+(define-it-lookup-command (get-records :name t) ()
98
+  (get-data))
99
+
100
+(define-it-lookup-command (com-quit :name t) ()
101
+  (frame-exit *application-frame*))
102
+
103
+(defparameter *saved-cases* nil)
104
+(defun save-case (case) (push case *saved-cases*))
105
+
106
+(define-it-lookup-command (com-save :name t) ((it-case 'reference))
107
+  (save-case it-case)
108
+  it-case)
109
+
110
+(define-it-lookup-command (com-prev :name t) ()
111
+  (let ((results (results *application-frame*)))
112
+    (with-accessors ((page it-lookup::page)) results
113
+      (when (> page 0)
114
+        (decf page)))
115
+    (parse-results results
116
+                   (run-query results))))
117
+
118
+(define-it-lookup-command (com-next :name t) ()
119
+  (let ((results (results *application-frame*)))
120
+    (incf (it-lookup::page results))
121
+    (parse-results results
122
+                   (run-query results))))
123
+
124
+(setf mcclim-truetype:*truetype-font-path* "/Library/Fonts/")
125
+(app-main)
... ...
@@ -8,8 +8,9 @@
8 8
                 :serapeum
9 9
                 :fwoar.lisputils
10 10
                 :drakma
11
-                :lquery)
11
+                :lquery
12
+                :climacs-flexichain-output-history
13
+                )
12 14
   :serial t
13 15
   :components ((:file "package")
14 16
                (:file "it-lookup")))
15
-
... ...
@@ -1,6 +1,7 @@
1 1
 ;;;; it-lookup.lisp
2 2
 
3 3
 (in-package #:it-lookup)
4
+(declaim (optimize (debug 3)))
4 5
 
5 6
 ;;; "it-lookup" goes here. Hacks and glory await!
6 7
 
... ...
@@ -8,18 +9,34 @@
8 9
 
9 10
 (defparameter *cookie-jar* (make-instance 'drakma:cookie-jar))
10 11
 
11
-(defun run-query (query &optional (page 1))
12
-  (drakma:http-request *post-url*
13
-                       :method :POST
14
-                       :parameters `(("text" . ,query)
15
-                                     ("Form.option.concordances" . "concordances")
16
-                                     ("results.currentPage" . ,(prin1-to-string page)))
17
-                       :cookie-jar *cookie-jar*))
12
+(defun run-query (results)
13
+  (with-accessors ((query query) (page page)) results
14
+    (drakma:http-request *post-url*
15
+                         :method :POST
16
+                         :parameters `(("text" . ,query)
17
+                                       ("Form.option.concordances" . "concordances")
18
+                                       ("results.currentPage" . ,(prin1-to-string page)))
19
+                         :cookie-jar *cookie-jar*)))
18 20
 
19 21
 (defclass it-case ()
20 22
   ((reference :initarg :reference :accessor case-reference)
21 23
    (text :initarg :text :accessor case-text)))
22 24
 
25
+(defun make-adjustable-vector (&optional (base-size 10))
26
+  (make-array base-size :adjustable t :fill-pointer 0))
27
+
28
+(defun to-adjustable-vector (values)
29
+  (let ((els (length values)))
30
+    (make-array els
31
+                :adjustable t
32
+                :fill-pointer els
33
+                :initial-contents values)))
34
+
35
+(defclass it-results ()
36
+  ((%query :initarg :query :accessor query :initform (make-adjustable-vector))
37
+   (%page :initarg :page :accessor page :initform 1)
38
+   (%cases :initarg :cases :accessor cases)))
39
+
23 40
 (defmethod initialize-instance :after ((object it-case) &key)
24 41
   (with-slots (reference text) object
25 42
     (when (arrayp reference)
... ...
@@ -27,8 +44,7 @@
27 44
     (when (plump:element-p reference)
28 45
       (setf reference (plump-dom:text reference)))
29 46
     (setf text (subseq text (+ (search reference text) (length reference))))
30
-    (setf reference (string-right-trim '(#\space) reference))
31
-    ))
47
+    (setf reference (string-right-trim '(#\space) reference))))
32 48
 
33 49
 (defmacro make-constructor (class &rest initargs)
34 50
   (let ((initarg-syms (mapcar #'(lambda (x) x (gensym)) initargs)))
... ...
@@ -38,65 +54,102 @@
38 54
                                      nconc (list initarg sym))))))
39 55
 
40 56
 (make-constructor it-case :reference :text)
57
+(make-constructor it-results :query :cases)
58
+
59
+(defun parse-results (results query-result)
60
+  (setf (cases results)
61
+        (lquery:$ (initialize query-result)
62
+                  "p[title]"
63
+                  (:combine ".ref" (text))
64
+                  (map-apply #'make-it-case))))
41 65
 
42
-(defun parse-results (query-result)
43
-  (lquery:$ (initialize query-result) "p[title]" (:combine ".ref" (text)) (map-apply #'make-it-case)))
66
+(defgeneric format-reference (it-case stream)
67
+  (:method ((it-case it-case) stream)
68
+   (princ (case-reference it-case) stream)
69
+   (fresh-line stream)))
70
+
71
+(defparameter *underlying-stream* nil)
72
+(defun get-real-stream (&optional default)
73
+  (or *underlying-stream* default))
44 74
 
45 75
 (defun format-result (parsed-result &optional (stream t))
46
-  (with-slots (reference text) parsed-result
76
+  (let ((real-stream stream))
47 77
     (pprint-logical-block (stream nil)
48
-      (princ reference stream)
78
+      (let ((*underlying-stream* real-stream))
79
+        (format-reference parsed-result stream))
49 80
       (pprint-indent :block 4 stream)
50 81
       (pprint-newline :mandatory stream)
51
-      (pprint-logical-block (stream (split-sequence #\space text))
52
-        (loop
53
-              (princ (pprint-pop) stream)
54
-              (pprint-exit-if-list-exhausted)
55
-              (princ #\space stream)
56
-              (pprint-newline :fill stream))))))
82
+      (with-slots (text) parsed-result
83
+        (pprint-logical-block (stream (tokens text))
84
+          (loop
85
+            (princ (pprint-pop) stream)
86
+            (pprint-exit-if-list-exhausted)
87
+            (princ #\space stream)
88
+            (pprint-newline :fill stream)))))))
57 89
 
58 90
 (defun format-results (parsed-results &optional (stream t))
59 91
   (fresh-line stream)
60
-  (loop :for parsed-results :across parsed-results
61
-        :do (format-result parsed-results stream)
92
+  (loop :for parsed-result :across parsed-results
93
+        :do (format-result parsed-result stream)
62 94
         :do (terpri stream)))
63 95
 
64 96
 (defmacro compose-funcs ((&rest funcs) &rest args)
65 97
   `(funcall (compose ,@funcs) ,@args))
66 98
 
67 99
 (defun main (args)
100
+  (let ((results (make-instance 'it-results
101
+                                :query nil)))
102
+
103
+    (format *terminal-io* "~&Index Thomisticus Query Utility v0.1~%")
104
+
105
+    (when (cadr args)
106
+      (setf (query results) (cadr args)))
107
+
108
+    (mainloop results)))
109
+
110
+(defun mainloop (results)
68 111
   (handler-case
69
-    (let ((current-page 1)
70
-          current-query)
71
-      (flet ((next-page () (incf current-page))
72
-             (prev-page ()
73
-               (when (> current-page 0)
74
-                 (decf current-page)))
75
-             (get-results (current-query current-page)
76
-               (compose-funcs (#'format-results #'parse-results #'run-query)
77
-                              current-query current-page)))
78
-
79
-        (format *terminal-io* "~&Index Thomisticus Query Utility v0.1~%")
80
-
81
-        (when (cadr args)
82
-          (setf current-query (string-join (cdr args) #\space))
83
-          (format *terminal-io* "~&Query: ~a~%" current-query)
84
-          (get-results current-query current-page))
112
+      (labels ((next-page () (incf (page results)))
113
+               (prev-page ()
114
+                 (with-accessors ((current-page page)) results
115
+                   (when (> current-page 0)
116
+                     (decf current-page))))
117
+               (print-results (results)
118
+                 (format *terminal-io* "~&Query: ~a, Page: ~3d~%"
119
+                         (query results)
120
+                         (page results)))
121
+               (get-results (results)
122
+                 (with-accessors ((page page) (query query)) results
123
+                   (parse-results results
124
+                                  (run-query results))
125
+                   (format-results (cases results))))
126
+               (do-action (action results)
127
+                 (funcall action)
128
+                 (get-results results)))
129
+
85 130
         (loop
86 131
           (format *terminal-io* "~&Query? ")
87 132
           (finish-output *terminal-io*)
88
-          (let ((action (read-line *terminal-io*)))
133
+          (let ((action (or (query results)
134
+                            (read-line *terminal-io*))))
89 135
             (string-case action
90
-              ("quit" (return-from main))
136
+              ("quit" (return-from mainloop))
137
+              ("help"
138
+               (format *terminal-io* "~&Help:~%~:{~4t~a: ~a~%~}~%Anything else searches.~%"
139
+                       `(("next" "Next page of results")
140
+                         ("prev" "Previous page of results")
141
+                         ("quit" "Quits"))))
142
+
91 143
               ("prev"
92
-               (prev-page)
93
-               (format *terminal-io* "~&Query: ~a, Page: ~3d~%" current-query current-page)
94
-               (get-results current-query current-page))      
144
+               (do-action #'prev-page results))
145
+
95 146
               ("next"
96
-               (next-page)
97
-               (format *terminal-io* "~&Query: ~a, Page: ~3d~%" current-query current-page)
98
-               (get-results current-query current-page))      
99
-              (t (setf current-query action)
100
-                 (get-results current-query current-page)))))))
147
+               (do-action #'next-page results))
148
+
149
+              (t (setf (query results)
150
+                       action)
151
+                 (get-results results)))
152
+            (setf (query results) nil))))
153
+
101 154
     (end-of-file (c) (declare (ignore c)))
102 155
     (sb-sys:interactive-interrupt (c) (declare (ignore c)))))
... ...
@@ -1,5 +1,6 @@
1 1
 ;;;; package.lisp
2 2
 
3 3
 (defpackage #:it-lookup
4
-  (:use #:cl #:alexandria #:serapeum #:fwoar.lisputils))
4
+  (:use #:cl #:alexandria #:serapeum #:fwoar.lisputils)
5
+  (:export #:format-results #:parse-results #:run-query))
5 6