utilities.lisp 14.5 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
;;
;; Copyright 2002-2011 Genworks International
;;
;; This source file is part of the General-purpose Declarative
;; Language project (GenDL).
;;
;; This source file contains free software: you can redistribute it
;; and/or modify it under the terms of the GNU Affero General Public
;; License as published by the Free Software Foundation, either
;; version 3 of the License, or (at your option) any later version.
;; 
;; This source file is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;; Affero General Public License for more details.
;; 
;; You should have received a copy of the GNU Affero General Public
;; License along with this source file.  If not, see
;; <http://www.gnu.org/licenses/>.
;; 

(in-package :gdl)

(defun plist2pairs (plist)
  (when plist
    (append (list (list (first plist)
                        (second plist)))
            (plist2pairs (rest (rest plist))))))

(defun plist-keys (plist)
  "List of keyword symbols. Returns the keys from a plist.

:arguments (plist \"Plist\")"
  (when plist
    (cons (first plist)
          (plist-keys (rest (rest plist))))))

(defun plist-values (plist)
  "List of Lisp objects. Returns the values from a plist.

:arguments (plist \"Plist\")"
  (when plist
    (cons (second plist)
          (plist-values (rest (rest plist))))))

(defun remove-plist-entry (plist key &key (test #'eql))
  "Plist. Returns a new plist sans any key/value pairs where the plist key is eql to the given key. 
Optionally a different test than #'eql can be specified with the :test keyword argument.

:arguments (plist \"Plist. The source plist.\"
            key \"matching key, typically a keyword symbol. The key to target for removal.\")
:&key ((test #'eql) \"predicate equality function taking two arguments. The function to use for matching.\")

:examples (remove-plist-entry (list :a \"a\" :b :a) :a)

"
  (mapcan #'(lambda (%key% %val%)
	      (unless (funcall test key %key%)
		(list %key% %val%))) 
	  (plist-keys plist) (plist-values plist)))


(defun remove-plist-key (plist key)
  (let (result on-remark?)
    (dolist (element plist (nreverse result))
      (cond ((eql element key)
             (setq on-remark? t))
            (on-remark?
             (setq on-remark? nil))
            (t (push element result))))))

(defun remove-plist-keys (plist keys)
  (when plist
    (if (null keys) plist
	(remove-plist-keys (remove-plist-key plist (first keys)) (rest keys)))))


(defmacro print-variables (&rest vars)
  "[Macro] Void. Prints the specified variables and current values to standard output.

:arguments (vars \"unquoted symbols (&rest argument)\")"

  `(let (*print-readably*)
     (format *trace-output* 
           ,(with-output-to-string(ss)
              (write-string "~&" ss)
              (mapcar #'(lambda(var)
                          (format ss "~&~a = ~~s~%" var)) vars)
              (write-string "~%" ss))
           ,@vars)))

(defmacro print-messages (&rest messages)
  "[Macro] Void. Prints the specified GDL object messages (i.e. slots) and their current values to standard output.

:arguments (vars \"unquoted symbols  (&rest argument)\")"
  `(let (,@(mapcar #'(lambda(message)
                       `(,message (the ,message))) messages))
     (print-variables ,@messages)))

(defun string-append (&rest args)
  "String. Returns a new string made up of concatenating the arguments.
:arguments 
 (:&rest (args \"strings\"))
"
  
  (apply #'concatenate 'string args))


(defun ensure-keyword (&rest strings)
  (if (and (= (length strings) 1) (keywordp (first strings)))
      (first strings)
      (apply #'make-keyword strings)))

(defun make-keyword (&rest strings)
  "Keyword symbol. Converts given strings to a keyword.
If any of the given arguments is not a string, it will be converted to one with 
 (format nil \"~a\" string).

:arguments (strings \"&rest Strings\")"
  (%make-keyword (apply #'string-append (mapcar #'(lambda(string) (string (%make-keyword string))) strings))))
  

(defun make-keyword-sensitive (string) (glisp:intern string :keyword))


(defun %make-keyword (string)

  (when (not (stringp string)) (setq string (format nil "~a" string)))
  
  (when (and (not (zerop (length string)))
             (eql (aref string 0) #\:))
    (setq string (subseq string 1)))
  
  (multiple-value-bind (symbol flag)
      (glisp:intern (glisp:upcase string) :keyword)
    (declare (ignore flag))
    symbol))

(defun ensure-list (possible-list)
  "List. If argument is not list, returns it in a list. If argument is
a list, returns it unchanged.

:arguments (possible-list \"Lisp object\")"
  (if (listp possible-list) 
      possible-list
    (list possible-list)))


(defun all-superclasses (class)
  (let ((direct-supers (glisp:direct-superclasses class)))
    (remove-duplicates
     (append direct-supers
             (mapcan #'(lambda(class)
                         (all-superclasses class)) direct-supers)))))

(defun compare-items (new-cons old-cons)
  (destructuring-bind (node1 . message1) new-cons
    (destructuring-bind (node2 . message2) old-cons
      (and (eq node1 node2) (eq message1 message2)))))


(defparameter *dependency-hash* (glisp:make-weak-hash-table))


(defparameter *run-with-dependency-recording?* nil)
(defparameter *compile-with-dependency-recording?* t)


(defun find-messages-used-by (instance message-key)
  "List of pairs of instance/keyword. This returns the list of direct dependants of a given 
message in a given instance. Note that this is not recursive; if you want to generate a tree, 
then you have to call this recursively yourself.

If you want an easy way to remember the meaning of dependant and dependency:

You have a dependency on caffeine. Your children are your dependants.

"
  (let ((message-hash (gethash instance *dependency-hash*)))
    (when message-hash (reverse (gethash message-key message-hash)))))

(defun find-dependants (instance message-key)
  (find-messages-which-use instance message-key))

(defun find-messages-which-use (instance message-key)
  "List of pairs of instance/keyword. This returns the list of direct dependants of a given 
message in a given instance. Note that this is not recursive; if you want to generate a tree, 
then you have to call this recursively yourself.

If you want an easy way to remember the meaning of dependant and dependency:

You have a dependency on caffeine. Your children are your dependants.

"
  (let ((notify-cons (second (slot-value instance (glisp:intern (string message-key) :gdl-acc)))))
    ;;
    ;; FLAG -- might have to normalize this into instance/message
    ;; pairs if messages are grouped on single instance
    ;;
    (let (result)
      (dolist (group notify-cons (nreverse result))
        (let ((instance (first group)))
          (dolist (message (rest group)) 
            (push (list instance (make-keyword (string message))) result)))))))

(defun find-dependencies (instance message-key)
  (find-messages-used-by instance message-key))




(let ((value (gensym)) (need? (gensym)))
  (defmacro with-dependency-tracking ((message-symbol &optional (self-sym 'self)) &rest body)
    `(let ((,value (,message-symbol ,self-sym))
	   (,need? (and *run-with-dependency-tracking?* *notify-cons* *root-checking-enabled?*
			;;
			;; FLAG -- check that remotes are effectively from same tree as well
			;;
			(or (and (find-class 'gdl-remote nil) (typep (first *notify-cons*) 'gdl-remote))
			    (same-tree? ,self-sym (first *notify-cons*))))))
       
       
       ,(when *compile-circular-reference-detection?*
	  `(when (and *run-with-circular-reference-detection?*
		  (member (list ,self-sym ',message-symbol) *till-now* :test #'equalp))
	     (error "Circular reference detected")))
       
       (if (eq (first (ensure-list ,value)) 'gdl-rule::%unbound%)
	   (progn (setq ,value (list (let* ,(remove nil
					     (list (when *compile-dependency-tracking?*
						     `(*notify-cons* (when *run-with-dependency-tracking?* 
								       (list ,self-sym ',message-symbol))))
						   (when *compile-circular-reference-detection?*
						     `(*till-now* (when *run-with-circular-reference-detection?*
								    (cons (list ,self-sym ',message-symbol) 
									  *till-now*))))))
				       ,@body)
				     (when ,need? (list (copy-list *notify-cons*)))))
		  (if (not (eql (first ,value) 'gdl-rule:%not-handled%)) 
		      (setf (,message-symbol ,self-sym) ,value)
		      ;;(without-interrupts (setf (,message-symbol ,self-sym) ,value))
		    
		    (progn (setq ,need? nil)
			   (not-handled ,self-sym ',message-symbol))))
	 (when ,need? (add-notify-cons *notify-cons* ,value ,self-sym ',message-symbol)))
       
       (first ,value))))



;;
;; FLAG - this is current
;;
#+nil
(let ((value (gensym)) (need? (gensym)))
  (defmacro with-dependency-tracking ((message-symbol &optional (self-sym 'self)) &rest body)
    `(let ((,value (,message-symbol ,self-sym))
	   (,need? (and *run-with-dependency-tracking?* *notify-cons* *root-checking-enabled?*
			;;
			;; FLAG -- check that remotes are effectively from same tree as well
			;;
			(or (and (find-class 'gdl-remote nil) (typep (first *notify-cons*) 'gdl-remote))
			    (same-tree? ,self-sym (first *notify-cons*))))))
       
       
       ,(when *compile-circular-reference-detection?*
	  `(when (and *run-with-circular-reference-detection?*
		  (member (list ,self-sym ',message-symbol) *till-now* :test #'equalp))
	     (error "Circular reference detected")))
       
       (if (eq (first (ensure-list ,value)) 'gdl-rule::%unbound%)
	   (progn (setq ,value (list (let* ,(remove nil
					     (list (when *compile-dependency-tracking?*
						     `(*notify-cons* (when *run-with-dependency-tracking?* 
								       (list ,self-sym ',message-symbol))))
						   (when *compile-circular-reference-detection?*
						     `(*till-now* (when *run-with-circular-reference-detection?*
								    (cons (list ,self-sym ',message-symbol) 
									  *till-now*))))))
				       ,@body)
				     (when ,need? (list (copy-list *notify-cons*)))))
		  (if (not (eql (first ,value) 'gdl-rule:%not-handled%)) 
		      (setf (,message-symbol ,self-sym) ,value)
		      ;;(without-interrupts (setf (,message-symbol ,self-sym) ,value))
		    
		    (progn (setq ,need? nil)
			   (not-handled ,self-sym ',message-symbol))))
	 (when ,need? (add-notify-cons *notify-cons* ,value)))
       
       (first ,value))))




#+nil
(defun add-notify-cons (notify-cons value)
  (let ((matching-sublist (assoc (first notify-cons) (second value))))
    (if matching-sublist (pushnew (second notify-cons) (rest matching-sublist))
      (push (copy-list notify-cons) (second value)))))


;;
;; FLAG -- merge the following two back in when we have adaptive Abstract Associative Map. 
;;
#+nil
(let ((value (gensym)) (need? (gensym)))
  (defmacro with-dependency-tracking ((message-symbol &optional (self-sym 'self)) &rest body)
    `(let ((,value (,message-symbol ,self-sym))
           (,need? (and *run-with-dependency-tracking?* 
                        *notify-cons* *root-checking-enabled?*)))
       ,(when *compile-circular-reference-detection?*
          `(when (and *run-with-circular-reference-detection?*
                  (member (list ,self-sym ',message-symbol) *till-now* :test #'equalp))
             (error "Circular reference detected")))

       ,(when *compile-with-dependency-recording?*
          `(when (and ,*run-with-dependency-recording?*
                      *notify-cons*)
             (let ((current-hash (gethash (first *notify-cons*) *dependency-hash*)))
               (unless current-hash 
                 (setf (gethash (first *notify-cons*) *dependency-hash*)
                   (make-hash-table)))
               (pushnew (list ,self-sym ,(make-keyword message-symbol))
                        (gethash (make-keyword (second *notify-cons*))
                                 (gethash (first *notify-cons*) *dependency-hash*))
                        :test #'equalp))))
       
       (when (eq (first (ensure-list ,value)) 'gdl-rule::%unbound%)
         (progn (setq ,value (list (let* ,(remove nil
                                           (list (when *compile-dependency-tracking?*
                                                   `(*notify-cons* (when *run-with-dependency-tracking?* 
                                                                     (list ,self-sym ',message-symbol))))
                                                 (when *compile-circular-reference-detection?*
                                                   `(*till-now* (when *run-with-circular-reference-detection?*
                                                                  (cons (list ,self-sym ',message-symbol) 
                                                                        *till-now*))))))
                                       
                                     ,@body)
                                   nil))
                (if (not (eql (first ,value) 'gdl-rule:%not-handled%)) 
                    (setf (,message-symbol ,self-sym) ,value)
                  (progn (setq ,need? nil)
                         (not-handled ,self-sym ',message-symbol)))))
       
       (when ,need? (add-notify-key *notify-cons* ,value))
       
       (first ,value))))

#+nil
(defun add-notify-key (notify-cons value)
  (if (second value)
      (pushnew (second notify-cons) (gethash (first notify-cons) (second value)))
    (setf (second value)
      (let ((ht (make-hash-table)))
        (setf (gethash (first notify-cons) ht) (list (second notify-cons)))
        ht))))



(defun same-tree? (obj1 obj2) (eql (gdl-acc::%root% obj1) (gdl-acc::%root% obj2)))
    
(defun merge-common-keys (plist)
  (let ((ht (make-hash-table)) result)
    (mapc #'(lambda(key value) (let ((current (gethash key ht)))
                                 (setf (gethash key ht) (if current (append current value) value))))
          (plist-keys plist) (plist-values plist))
    (maphash #'(lambda(key value) (push key result) (push value result)) ht) (nreverse result)))


(defclass gdl-basis () () (:metaclass gdl-class))
(defclass gdl-remote () () (:metaclass gdl-class))

(defun to-double-float (num) (coerce num 'double-float))
(defun to-single-float (num) (coerce num 'single-float))


(defvar *patch-fasls* nil)