-
Notifications
You must be signed in to change notification settings - Fork 2
/
workspace-objects.lisp
488 lines (384 loc) · 18.7 KB
/
workspace-objects.lisp
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
;---------------------------------------------
; WORKSPACE-OBJECTS: This file contains flavor definitions and functions
; for workspace objects (either letters or groups).
;---------------------------------------------
(in-package 'user)
;---------------------------------------------
(defflavor workspace-object
(string ; The string the object is in (e.g., initial-string).
string-number ; The object's unique identifying number in its string.
; This number is used for storing and referencing
; objects and structures stored in vectors and arrays.
left-string-position ; If the object is a group, this is the string
; position of the leftmost letter in the group.
; If the object is a letter, this is its string
; position.
right-string-position ; Similar to left-string-position. For letters,
; this is equal to left-string-position.
; Some values for the object. See the file "object-formulas.l"
; for descriptions of these values.
(raw-importance 0)
(relative-importance 0)
(intra-string-happiness 0) (intra-string-unhappiness 0)
(inter-string-happiness 0) (inter-string-unhappiness 0)
(total-happiness 0) (total-unhappiness 0)
(intra-string-salience 0) (inter-string-salience 0) (total-salience 0)
(descriptions nil) ; A list of descriptions of the object.
(extrinsic-descriptions nil) ; A list of descriptions with respect to
; other objects. For now, only the
; letter in the modified string that
; corresponds to the changed letter in
; the initial string can be given an
; extrinsic description (e.g., in
; "abc -> abd, pqrs -> ?", the 'd' would
; probably get the extrinsic description
; "Successor of the 'c' in 'abc'").
; Variables for structures that can be attached to the object.
(outgoing-bonds nil) (incoming-bonds nil)
(left-bond nil) (right-bond nil)
(group nil) (replacement nil) (correspondence nil)
(changed? nil) ; T if the letter is the initial-string letter that
; changed.
(new-answer-letter? nil) ; T if this is a new letter for the answer,
; not corresponding to any of the target-string
; letters (this is used when the
; translated-rule calls for length changes).
(clamp-salience? nil) ; T if the salience of this object is to be
; clamped (used when a snag has been hit, to
; clamp the salience of the object causing the
; snag).
pname ; The print name of the object.
graphics-obj) ; The object used in the graphics representation of the
; object.
()
:gettable-instance-variables
:settable-instance-variables
:initable-instance-variables)
;---------------------------------------------
(defmethod (workspace-object :print) ()
(format t "~a ~a:~a in ~a~&"
(send (send self :get-descriptor plato-object-category) :pname)
pname
left-string-position
(send string :pname)))
;---------------------------------------------
(defmethod (workspace-object :letter-span) ()
; Returns the number of letters spanned by the object.
(if* (typep self 'letter)
then 1
else (list-sum (send-method-to-list
(send self :object-list) :letter-span))))
;---------------------------------------------
(defmethod (workspace-object :letter-list) ()
; Returns a list of the letters at the lowest level of the object.
(if* (typep self 'letter)
then (list self)
else (flatten (loop for obj in (send self :object-list)
collect (send obj :letter-list)))))
;---------------------------------------------
(defmethod (workspace-object :leftmost-in-string?) ()
(= left-string-position 0))
;---------------------------------------------
(defmethod (workspace-object :middle-in-string?)
(&aux left-neighbor right-neighbor)
(setq left-neighbor (send self :ungrouped-left-neighbor))
(setq right-neighbor (send self :ungrouped-right-neighbor))
(and left-neighbor right-neighbor
(send left-neighbor :leftmost-in-string?)
(send right-neighbor :rightmost-in-string?)))
;---------------------------------------------
(defmethod (workspace-object :rightmost-in-string?) ()
(= right-string-position (1- (length (send string :letter-list)))))
;---------------------------------------------
(defmethod (workspace-object :spans-whole-string?) ()
; Returns t if the object is the single letter in its string or a group
; that spans the string.
(= (send self :letter-span) (send string :length)))
;----------------------------------------------
(defmethod (workspace-object :string-spanning-group?) ()
; Returns t if the object is a group that spans the string.
(and (typep self 'group) (send self :spans-whole-string?)))
;----------------------------------------------
(defmethod (workspace-object :ungrouped-left-neighbor) ()
; Returns the left-neighbor of the group that either isn't in a group itself,
; or is in the same group as the given object. E.g., suppose in the
; string "aabbcc", the pairs of letters have been grouped as 'A', 'B', and
; 'C', and the whole string has been grouped as 'ABC'. The leftmost 'b' has
; two left-neighbors: the letter 'a' and the group 'A', which is in
; the same higher-level group the 'b' is in, namely, 'ABC'. The ungrouped
; left-neighbor of the 'b' is the group 'A'.
(if* (send self :leftmost-in-string?)
then nil
else (loop for left-neighbor in (send self :all-left-neighbors)
when (or (null (send left-neighbor :group))
(recursive-group-member?
self (send left-neighbor :group)))
return left-neighbor)))
;---------------------------------------------
(defmethod (workspace-object :ungrouped-right-neighbor) ()
; Similar to the "ungrouped-left-neighbor" method.
(if* (send self :rightmost-in-string?)
then nil
else (loop for right-neighbor in (send self :all-right-neighbors)
when (or (null (send right-neighbor :group))
(recursive-group-member?
self (send right-neighbor :group)))
return right-neighbor)))
;---------------------------------------------
(defmethod (workspace-object :all-left-neighbors) ()
; Returns a list of all of the object's immediate left-neighbors, both
; letters and groups. E.g., E.g., suppose in the string "aabbcc", the pairs
; of letters have been grouped as 'A', 'B', and 'C'. The leftmost 'b' has
; two left-neighbors: the letter 'a' and the group 'A'.
(if* (send self :leftmost-in-string?)
then nil
else (loop for obj in (vref (send string :object-position-vector)
(1- left-string-position))
if (not (and (typep obj 'group)
(recursive-group-member? self obj)))
collect obj))) ; Only collect if obj is not the group this
; object is in.
;---------------------------------------------
(defmethod (workspace-object :all-right-neighbors) ()
; Similar to the "all-left-neighbors" method.
; Returns a list of all of the object's left-neighbors.
(if* (send self :rightmost-in-string?)
then nil
else (loop for obj in (vref (send string :object-position-vector)
(1+ right-string-position))
if (not (and (typep obj 'group)
(recursive-group-member? self obj)))
collect obj))) ; Only collect if obj is not the group
; this obj is in.
;---------------------------------------------
(defmethod (workspace-object :all-neighbors) ()
; Returns a list of all the immediate neighbors of the object.
(append (send self :all-left-neighbors) (send self :all-right-neighbors)))
;---------------------------------------------
(defmethod (workspace-object :random-left-neighbor) ()
; Returns a randomly selected left-neighbor.
(random-list-item (send self :all-left-neighbors)))
;---------------------------------------------
(defmethod (workspace-object :random-right-neighbor) ()
; Returns a randomly selected left-neighbor.
(random-list-item (send self :all-right-neighbors)))
;---------------------------------------------
(defmethod (workspace-object :random-neighbor) ()
; Returns a randomly selected neighbor.
(random-list-item (send self :all-neighbors)))
;---------------------------------------------
(defmethod (workspace-object :choose-left-neighbor) ()
; Chooses a left-neighbor probabilistically, based on intra-string-salience.
(select-list-item-by-method (send self :all-left-neighbors)
:intra-string-salience))
;---------------------------------------------
(defmethod (workspace-object :choose-right-neighbor) ()
; Chooses a right-neighbor probabilistically, based on intra-string-salience.
(select-list-item-by-method (send self :all-right-neighbors)
:intra-string-salience))
;---------------------------------------------
(defmethod (workspace-object :choose-neighbor) ()
; Chooses a neighbor (left or right) probabilistically, based on
; intra-string-salience.
(select-list-item-by-method (send self :all-neighbors)
:intra-string-salience))
;---------------------------------------------
(defmethod (workspace-object :all-bonds) ()
; Returns all the bonds connected to this object, either incoming or
; outgoing.
(append incoming-bonds outgoing-bonds))
;---------------------------------------------
(defmethod (workspace-object :add-description) (d)
; Adds the given description to the object's decription list.
(send d :set-description-number (length (send self :descriptions)))
(push d descriptions))
;---------------------------------------------
(defmethod (workspace-object :add-extrinsic-description) (d)
(push d extrinsic-descriptions))
;---------------------------------------------
(defmethod (workspace-object :get-descriptor) (description-type)
; Returns the descriptor of the object corresponding to the given
; description-type. E.g., if the description-type is "object-category", the
; descriptor might be "letter" or "group". If the description-type
; is "letter-category", the descriptor might be 'A' if the object is an
; 'A'. If the object has no such description, this returns nil.
; Note: this assumes that there is at most one description with a given
; object facet, which is a limitation of the current version of Copycat.
(loop for description in descriptions
when (eq (send description :description-type) description-type)
return (send description :descriptor)))
;----------------------------------------------
(defmethod (workspace-object :relevant-descriptions) ()
; Returns a list of the object's relevant descriptions (those whose
; description-type is fully active).
(loop for description in descriptions
when (send (send description :description-type) :active?)
collect description))
;----------------------------------------------
(defmethod (workspace-object :distinguishing-descriptions) ()
; Returns a list of the object's distinguishing descriptions (those which
; distinguish it in the string).
(loop for d in descriptions
when (send self :distinguishing-descriptor? (send d :descriptor))
collect d))
;----------------------------------------------
(defmethod (workspace-object :non-distinguishing-descriptions) ()
(loop for d in descriptions
when (null (send self :distinguishing-descriptor?
(send d :descriptor)))
collect d))
;----------------------------------------------
(defmethod (workspace-object :distinguishing-descriptor?)
(descriptor &aux other-objects other-descriptors)
; Returns t if no other object of the same type has the same descriptor.
; For now, object-category and length descriptions are not
; distinguishing. Maybe this should be changed.
(if* (or (eq descriptor plato-letter) (eq descriptor plato-group)
(member descriptor *slipnet-numbers*))
then nil
else (if* (typep self 'letter)
then (setq other-objects (remove self (send string :letter-list)))
else (setq other-objects (remove self (send string :group-list)))
(if* (send self :group) ; Don't count the group this object is
; inside of, if there is one.
then (setq other-objects
(remove (send self :group) other-objects)))
; Don't count other groups inside of this group.
(loop for obj in (send self :object-list)
when (typep obj 'group) do
(setq other-objects (remove obj other-objects))))
(setq other-descriptors
(send-method-to-list
(flatten (send-method-to-list other-objects
:descriptions)) :descriptor))
(null (memq descriptor other-descriptors))))
;----------------------------------------------
(defmethod (workspace-object :relevant-distinguishing-descriptions) ()
(loop for description in (send self :distinguishing-descriptions)
when (send (send description :description-type) :active?)
collect description))
;----------------------------------------------
(defmethod (workspace-object :relevant-non-distinguishing-descriptions) ()
(loop for description in (send self :non-distinguishing-descriptions)
when (send (send description :description-type) :active?)
collect description))
;----------------------------------------------
(defmethod (workspace-object :choose-relevant-description-by-activation)
(&aux relevant-descriptions)
; Chooses a relevant description probabilistically, based on the descriptor's
; activation.
(setq relevant-descriptions (send self :relevant-descriptions))
(if* (null relevant-descriptions)
then nil
else (nth (select-list-position
(send-method-to-list
(send-method-to-list relevant-descriptions :descriptor)
:activation))
relevant-descriptions)))
;----------------------------------------------
(defmethod (workspace-object
:choose-relevant-distinguishing-description-by-conceptual-depth)
(&aux relevant-distinguishing-descriptions)
; Chooses a relevant, distinguishing description probabilistically, based on
; the descriptor's conceptual-depth.
(setq relevant-distinguishing-descriptions
(send self :relevant-distinguishing-descriptions))
(if* (null relevant-distinguishing-descriptions)
then nil
else (select-list-item-by-method relevant-distinguishing-descriptions
:conceptual-depth)))
;----------------------------------------------
(defmethod (workspace-object :description-present?) (new-description)
; Returns t if this object already has this description.
(loop for d in descriptions
when (and (eq (send d :description-type)
(send new-description :description-type))
(eq (send d :descriptor)
(send new-description :descriptor)))
return t
finally (return nil)))
;----------------------------------------------
(defmethod (workspace-object :rule-initial-string-descriptions) ()
; Returns all the descriptions that can be used in making the initial-string
; part of the rule, with this object as the changed object in the
; initial-string.
(loop for d in descriptions
when (and (send (send d :description-type) :active?)
(send self :distinguishing-descriptor?
(send d :descriptor))
(not (eq (send d :description-type) plato-object-category)))
collect d))
;----------------------------------------------
(defmethod (workspace-object :rule-modified-string-descriptions) ()
; Returns all the non-extrinsic descriptions that can be used in making the
; modified-string part of the rule, with this object as the object in the
; modified string corresponding to the initial-string changed object.
(loop for d in descriptions
when (and (send (send d :description-type) :active?)
(send self :distinguishing-descriptor?
(send d :descriptor))
(not (eq (send d :description-type)
plato-string-position-category))
(not (eq (send d :description-type) plato-object-category)))
collect d))
;----------------------------------------------
(defmethod (workspace-object :add-incoming-bond) (b)
; Adds a new incoming bond to the object.
(push b incoming-bonds))
;----------------------------------------------
(defmethod (workspace-object :add-outgoing-bond) (b)
; Adds a new outgoing bond to the object.
(push b outgoing-bonds))
;----------------------------------------------
(defmethod (workspace-object :structure-list) ()
; Returns a list of the structures attached to the object.
(append descriptions
(list left-bond right-bond group correspondence)))
;----------------------------------------------
(defmethod (workspace-object :descriptor-present?) (d)
; Returns t if this object has a description with this descriptor.
(loop for description in descriptions
when (eq (send description :descriptor) d) return t
finally (return nil)))
;----------------------------------------------
(defmethod (workspace-object :description-type-present?) (given-description-type)
; Returns t if this object has a description with this description-type.
(loop for description in descriptions
when (eq (send description :description-type) given-description-type) return t
finally (return nil)))
;----------------------------------------------
(defun letter-distance (obj1 obj2 &aux left-obj right-obj)
; Returns the distance in letters between the two objects.
(if* (< (send obj1 :left-string-position)
(send obj2 :left-string-position))
then (setq left-obj obj1 right-obj obj2)
else (setq left-obj obj2 right-obj obj1))
(- (send right-obj :left-string-position)
(send left-obj :right-string-position)))
;----------------------------------------------
(defun get-common-groups (obj1 obj2)
; Returns any groups that contain both objects (at some level).
(loop for group in (send (send obj1 :string) :group-list)
when (and (recursive-group-member? obj1 group)
(recursive-group-member? obj2 group))
collect group))
;---------------------------------------------
; LETTERS
;---------------------------------------------
(defflavor letter
()
(workspace-object)
:gettable-instance-variables
:settable-instance-variables
:initable-instance-variables)
;---------------------------------------------
(defun make-letter (string letter-category string-position)
; Returns a new letter.
(make-instance 'letter
:string string
:left-string-position string-position
:right-string-position string-position
:pname (string-downcase (send letter-category :pname))))
;----------------------------------------
; The flavor definition and methods for groups are in the file groups.l.
;----------------------------------------