-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBenchmarks.st
executable file
·1931 lines (1830 loc) · 56 KB
/
Benchmarks.st
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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
Smalltalk
package 'Benchmarks' = (
'Benchmarks-Microbenchmarks'
class Benchmark = Object (
""
| "instance variables" |
| "class pool variables" |
| "shared pool variables" |
'as yet unclassified'
measureForAtLeast: milliseconds = (
| runs start elapsed |
runs := 0. Smalltalk garbageCollect.
start := Time millisecondClockValue.
[self run.
runs := runs + 1.
elapsed :=Time millisecondClockValue - start.
elapsed < milliseconds] whileTrue.
^(runs * 1000.0 / elapsed) asFloat
)
report = (
| score |
self setup.
self measureForAtLeast: 300. "Warm up"
Smalltalk garbageCollect.
score := self measureForAtLeast: 2000.
Transcript cr; show: self class name; space; show: (score printShowingDecimalPlaces: 1).
^score
)
setup = (
)) : (
| "class instance variables" |
'as yet unclassified'
report = (
^self new report
)
reportAll = (
^(self subclasses sortBy: [:a :b | a name < b name]) collect:
[:benchmarkClass | benchmarkClass name -> benchmarkClass report]
))
'Benchmarks-DeltaBlue'
class DBBinaryConstraint = DBConstraint (
"I am an abstract superclass for constraints having two possible output variables."
| "instance variables" v1 v2 direction |
| "class pool variables" |
| "shared pool variables" |
'as yet unclassified'
addToGraph = (
"Add myself to the constraint graph."
v1 addConstraint: self.
v2 addConstraint: self.
direction := nil.
)
chooseMethod: mark = (
"Decide if I can be satisfied and which way I should flow based on the relative strength of the variables I relate, and record that decision."
v1 mark == mark ifTrue:
[(v2 mark ~= mark and: [strength strongerThan: v2 walkStrength])
ifTrue: [direction:= DBForward]
ifFalse: [direction:= nil].
^self].
v2 mark == mark ifTrue:
[(v1 mark ~= mark and: [strength strongerThan: v1 walkStrength])
ifTrue: [direction:= DBBackward]
ifFalse: [direction:= nil].
^self].
"If we get here, neither variable is marked, so we have a choice."
(v1 walkStrength weakerThan: v2 walkStrength)
ifTrue:
[direction:= (strength strongerThan: v1 walkStrength)
ifTrue: [DBBackward]
ifFalse: [nil]]
ifFalse:
[direction:= (strength strongerThan: v2 walkStrength)
ifTrue: [DBForward]
ifFalse: ["nil" DBBackward]].
)
input = (
"Answer my current input variable"
^direction == DBForward ifTrue: [v1] ifFalse: [v2]
)
inputsKnown: mark = (
| in |
in := self input.
^in mark == mark or: [in stay or: [in determinedBy isNil]]
)
isSatisfied = (
"Answer true if this constraint is satisfied in the current solution."
^direction notNil
)
markInputs: mark = (
"Mark the input variable with the given mark."
self input mark: mark
)
markUnsatisfied = (
"Record the fact that I am unsatisfied."
direction := nil.
)
output = (
"Answer my current output variable."
^direction == DBForward ifTrue: [v2] ifFalse: [v1]
)
recalculate = (
"Calculate the walkabout strength, the stay flag, and, if it is 'stay', the value for the current output of this constraint. Assume this constraint is satisfied."
| in out |
in := self input. out := self output.
out walkStrength: (strength weakest: in walkStrength).
out stay: in stay.
out stay ifTrue: [self execute].
)
removeFromGraph = (
"Remove myself from the constraint graph."
v1 isNil ifFalse: [v1 removeConstraint: self].
v2 isNil ifFalse: [v2 removeConstraint: self].
direction := nil.
)
var: v1p var: v2p strength: s = (
super strength: s.
v1 := v1p.
v2 := v2p.
)) : (
| "class instance variables" |
'as yet unclassified'
var: v1 var: v2 strength: s = (
^self new var: v1 var: v2 strength: s
))
'Benchmarks-DeltaBlue'
class DBConstraint = Object (
"I am an abstract class representing a system-maintainable relationship (or ''constraint'') between a set of variables. I supply a strength instance variable; concrete subclasses provide a means of storing the constrained variables and other information required to represent a constraint."
| "instance variables" strength |
| "class pool variables" |
| "shared pool variables" |
'as yet unclassified'
addConstraint = (
"Activate this constraint and attempt to satisfy it."
self addToGraph.
DBThePlanner incrementalAdd: self.
)
addToGraph = (
"Add myself to the constraint graph."
self subclassResponsibility
)
chooseMethod: mark = (
"Decide if I can be satisfied and record that decision. The output of the chosen method must not have the given mark and must have a walkabout strength less than that of this constraint."
self subclassResponsibility
)
execute = (
"Enforce this constraint. Assume that it is satisfied."
self subclassResponsibility
)
inputsKnown: mark = (
"Assume that I am satisfied. Answer true if all my current inputs are known. A variable is known if either a) it is 'stay' (i.e. it is a constant at plan execution time), b) it has the given mark (indicating that it has been computed by a constraint appearing earlier in the plan), or c) it is not determined by any constraint."
self subclassResponsibility
)
isInput = (
"Normal constraints are not input constraints. An input constraint is one that depends on external state, such as the mouse, the keyboard, a clock, or some arbitrary piece of imperative code."
^false
)
isSatisfied = (
"Answer true if this constraint is satisfied in the current solution."
self subclassResponsibility
)
markInputs: mark = (
"Set the mark of all input from the given mark."
self subclassResponsibility
)
markUnsatisfied = (
"Record the fact that I am unsatisfied."
self subclassResponsibility
)
output = (
"Answer my current output variable. Raise an error if I am not currently satisfied."
self subclassResponsibility
)
recalculate = (
"Calculate the walkabout strength, the stay flag, and, if it is 'stay', the value for the current output of this constraint. Assume this constraint is satisfied."
self subclassResponsibility
)
removeFromGraph = (
"Remove myself from the constraint graph."
self subclassResponsibility
)
satisfy: mark = (
"Attempt to find a way to enforce this constraint. If successful, record the solution, perhaps modifying the current dataflow graph. Answer the constraint that this constraint overrides, if there is one, or nil, if there isn't. Assume: I am not already satisfied."
| out overridden |
self chooseMethod: mark.
self isSatisfied ifFalse:
[strength == DBRequired ifTrue:
[Error signal: 'Could not satisfy a required constraint'].
^nil].
"constraint can be satisfied"
"mark inputs to allow cycle detection in addPropagate"
self markInputs: mark.
out := self output.
overridden := out determinedBy.
overridden isNil ifFalse: [overridden markUnsatisfied].
out determinedBy: self.
(DBThePlanner addPropagate: self mark: mark) ifFalse: [Error signal: 'Cycle encountered'].
out mark: mark.
^overridden
)
strength = (
^strength
)
strength: s = (
strength := s.
)'as-yet-unclassified'
destroyConstraint = (
"Deactivate this constraint, remove it from the constraint graph, possibly causing other constraints to be satisfied, and destroy it."
self isSatisfied ifTrue: [DBThePlanner incrementalRemove: self].
self removeFromGraph.
)) : (
| "class instance variables" |
)
'Benchmarks-DeltaBlue'
class DBDirection = Object (
""
| "instance variables" name |
| "class pool variables" Backward Forward |
| "shared pool variables" |
'as yet unclassified'
name: n = (
name := n.
)) : (
| "class instance variables" |
'as yet unclassified'
initialize = (
"DBDirection initialize"
DBForward := self name: 'forward'.
DBBackward := self name: 'backward'.
)
name: n = (
^self basicNew name: n
))
'Benchmarks-DeltaBlue'
class DBEditConstraint = DBUnaryConstraint (
"I am a unary input constraint used to mark a variable that the client wishes to change."
| "instance variables" |
| "class pool variables" |
| "shared pool variables" |
'as yet unclassified'
execute = (
"Edit constraints do nothing."
)
isInput = (
"I indicate that a variable is to be changed by imperative code."
^true
)
var: v strength: s = (
super var: v strength: s.
self addConstraint.
)) : (
| "class instance variables" |
)
'Benchmarks-DeltaBlue'
class DBEqualityConstraint = DBBinaryConstraint (
"I constrain two variables to have the same value: ''v1 = v2''."
| "instance variables" |
| "class pool variables" |
| "shared pool variables" |
'as yet unclassified'
execute = (
"Enforce this constraint. Assume that it is satisfied."
self output value: self input value.
)
var: v1 var: v2 strength: s = (
super var: v1 var: v2 strength: s.
self addConstraint.
)) : (
| "class instance variables" |
)
'Benchmarks-DeltaBlue'
class DBPlan = Object (
"A Plan is an ordered list of constraints to be executed in sequence to resatisfy all currently satisfiable constraints in the face of one or more changing inputs."
| "instance variables" constraints |
| "class pool variables" |
| "shared pool variables" |
'as yet unclassified'
addConstraint: c = (
constraints addLast: c
)
execute = (
"Execute my constraints in order."
constraints do: [:c | c execute]
)
initialize = (
constraints := OrderedCollection new.
)) : (
| "class instance variables" |
)
'Benchmarks-DeltaBlue'
class DBPlanner = Object (
""
| "instance variables" currentMark |
| "class pool variables" |
| "shared pool variables" |
'as yet unclassified'
addConstraintsConsuming: v to: list = (
| determining |
determining := v determinedBy.
v constraints do: [:c | (c ~= determining and: [c isSatisfied]) ifTrue: [list addLast: c]].
)
addPropagate: c mark: mark = (
"Recompute the walkabout strengths and stay flags of all variables downstream of the given constraint and recompute the actual values of all variables whose stay flag is true. If a cycle is detected, remove the given constraint and answer false. Otherwise, answer true."
"Details: Cycles are detected when a marked variable is encountered downstream of the given constraint. The sender is assumed to have marked the inputs of the given constraint with the given mark. Thus, encountering a marked node downstream of the output constraint means that there is a path from the constraint's output to one of its inputs."
| todo |
todo := OrderedCollection new.
todo addLast: c.
[todo isEmpty] whileFalse: [
| d |
d := todo removeLast.
d output mark == mark ifTrue: [self incrementalRemove: c. ^false].
d recalculate.
self addConstraintsConsuming: d output to: todo.
].
^true
)
extractPlanFromConstraints: constraints = (
"Extract a plan for resatisfaction starting from the outputs of the given constraints, usually a set of input constraints."
| sources |
sources := OrderedCollection new.
constraints do: [:c | (c isInput and: [c isSatisfied]) ifTrue: [sources addLast: c]].
^self makePlan: sources
)
incrementalAdd: c = (
"Attempt to satisfy the given constraint and, if successful, incrementally update the dataflow graph. Details: If satisfying the constraint is successful, it may override a weaker constraint on its output. The algorithm attempts to resatisfy that constraint using some other method. This process is repeated until either a) it reaches a variable that was not previously determined by any constraint or b) it reaches a constraint that is too weak to be satisfied using any of its methods. The variables of constraints that have been processed are marked with a unique mark value so that we know where we've been. This allows the algorithm to avoid getting into an infinite loop even if the constraint graph has an inadvertent cycle."
| mark overridden |
mark := self newMark.
overridden := c satisfy: mark.
[overridden isNil] whileFalse: [overridden := overridden satisfy: mark].
)
incrementalRemove: c = (
"Entry point for retracting a constraint. Remove the given constraint and incrementally update the dataflow graph."
"Details: Retracting the given constraint may allow some currently unsatisfiable downstream constraint to be satisfied. We therefore collect a list of unsatisfied downstream constraints and attempt to satisfy each one in turn. This list is traversed by constraint strength, strongest first, as a heuristic for avoiding unnecessarily adding and then overriding weak constraints. Assume: c is satisfied."
| out unsatisfied |
out := c output.
c markUnsatisfied.
c removeFromGraph.
unsatisfied := self removePropagateFrom: out.
DBDescendingStrengths do: [:strength |
unsatisfied do: [:u |
u strength = strength
ifTrue: [self incrementalAdd: u]]].
)
initialize = (
currentMark := 0.
)
makePlan: sources = (
"Extract a plan for resatisfaction starting from the given source constraints, usually a set of input constraints. This method assumes that stay optimization is desired; the plan will contain only constraints whose output variables are not stay. Constraints that do no computation, such as stay and edit constraints, are not included in the plan."
"Details: The outputs of a constraint are marked when it is added to the plan under construction. A constraint may be appended to the plan when all its input variables are known. A variable is known if either a) the variable is marked (indicating that has been computed by a constraint appearing earlier in the plan), b) the variable is 'stay' (i.e. it is a constant at plan execution time), or c) the variable is not determined by any constraint. The last provision is for past states of history variables, which are not stay but which are also not computed by any constraint. Assume: sources are all satisfied."
| mark plan todo |
mark := self newMark.
plan := DBPlan new.
todo := sources.
[todo isEmpty] whileFalse: [
| c |
c := todo removeLast.
(c output mark ~= mark and: [c inputsKnown: mark]) ifTrue: [
"not in plan already and eligible for inclusion"
plan addConstraint: c.
c output mark: mark.
self addConstraintsConsuming: c output to: todo.
]
].
^plan
)
newMark = (
"Select a previously unused mark value."
currentMark := currentMark + 1.
^currentMark
)
projectionTest: n = (
"This test constructs a two sets of variables related to each other by a simple linear transformation (scale and offset). The time is measured to change a variable on either side of the mapping and to change the scale and offset factors."
| scale offset src dst dests |
scale := DBVariable name: 'scale' initialValue: 10.
offset := DBVariable name: 'offset' initialValue: 1000.
dests := OrderedCollection new.
0 to: n-1 do: [:i |
src := DBVariable name: 'src', i printString initialValue: i.
dst := DBVariable name: 'dst', i printString initialValue: i.
dests addLast: dst.
DBStayConstraint var: src strength: DBNormal.
DBScaleConstraint source: src scale: scale offset: offset destination: dst strength: DBRequired.
].
self setValueOf: src to: 17.
dst value = 1170 ifFalse: [Error signal: 'Projection test 1 failed!'].
self setValueOf: dst to: 1050.
src value = 5 ifFalse: [Error signal: 'Projection test 2 failed!'].
self setValueOf: scale to: 5.
0 to: n - 2 do:
[:i | (dests at: i + 1) value = (i * 5 + 1000)
ifFalse: [Error signal: 'Projection test 3 failed!'] ].
self setValueOf: offset to: 2000.
0 to: n - 2 do:
[:i | (dests at: i + 1) value = (i * 5 + 2000)
ifFalse: [Error signal: 'Projection test 4 failed!']].
)
propagateFrom: v = (
"The given variable has changed. Propagate new values downstream."
| todo |
todo := OrderedCollection new.
self addConstraintsConsuming: v to: todo.
[todo isEmpty] whileFalse: [
| c |
c := todo removeLast.
c execute.
self addConstraintsConsuming: c output to: todo.
].
)
removePropagateFrom: out = (
"Update the walkabout strengths and stay flags of all variables downstream of the given constraint. Answer a collection of unsatisfied constraints sorted in order of decreasing strength."
| unsatisfied todo |
out determinedBy: nil.
out walkStrength: DBWeakest.
out stay: true.
unsatisfied := OrderedCollection new.
todo := OrderedCollection new.
todo addLast: out.
[todo isEmpty] whileFalse: [
| v determining |
v := todo removeLast.
v constraints do: [:c | c isSatisfied ifFalse: [unsatisfied addLast: c]].
determining := v determinedBy.
v constraints do: [:nextC |
(nextC ~= determining and: [nextC isSatisfied])
ifTrue: [nextC recalculate. todo addLast: nextC output]].
].
^unsatisfied
)'as-yet-unclassified'
chainTest: n = (
"This is the standard DeltaBlue benchmark. A long chain of equality constraints is constructed with a stay constraint on one end. An edit constraint is then added to the opposite end and the time is measured for adding and removing this constraint, and extracting and executing a constraint satisfaction plan. There are two cases. In case 1, the added constraint is stronger than the stay constraint and values must propagate down the entire length of the chain. In case 2, the added constraint is weaker than the stay constraint so it cannot be accommodated. The cost in this case is, of course, very low. Typical situations lie somewhere between these two extremes."
| prev first last editC editV plan |
1 to: n do: [:i |
| name v |
name := 'v', i printString.
v := DBVariable name: name initialValue: 0.
prev isNil ifFalse: [DBEqualityConstraint var: prev var: v strength: DBRequired].
i == 1 ifTrue: [first := v].
i == n ifTrue: [last := v].
prev := v.
].
DBStayConstraint var: last strength: DBStrongDefault.
editC := DBEditConstraint var: first strength: DBPreferred.
editV := OrderedCollection new.
editV addLast: editC.
plan := self extractPlanFromConstraints: editV.
1 to: n do: [:i |
first value: i.
plan execute.
last value == i ifFalse: [Error signal: 'Chain test failed!'].
].
editC destroyConstraint.
)
setValueOf: var to: newValue = (
| editC editV plan |
editC := DBEditConstraint var: var strength: DBPreferred.
editV := OrderedCollection new.
editV addLast: editC.
plan := self extractPlanFromConstraints: editV.
10 timesRepeat: [var value: newValue. plan execute].
editC destroyConstraint.
)) : (
| "class instance variables" |
'as yet unclassified'
initialize = (
"DBPlanner initialize"
DBThePlanner := DBPlanner new.
))
'Benchmarks-DeltaBlue'
class DBScaleConstraint = DBBinaryConstraint (
"I relate two variables by the linear scaling relationship: ''v2 = (v1 * scale) + offset''. Either v1 or v2 may be changed to maintain this relationship but the scale factor and offset are considered read-only."
| "instance variables" scale offset |
| "class pool variables" |
| "shared pool variables" |
'as yet unclassified'
addToGraph = (
"Add myself to the constraint graph."
super addToGraph.
scale addConstraint: self.
offset addConstraint: self.
)
execute = (
"Enforce this constraint. Assume that it is satisfied."
direction == DBForward
ifTrue: [v2 value: v1 value * scale value + offset value]
ifFalse: [v1 value: (v2 value - offset value) // scale value].
)
markInputs: mark = (
"Mark the inputs from the given mark."
super markInputs: mark.
scale mark: mark.
offset mark: mark.
)
removeFromGraph = (
"Remove myself from the constraint graph."
super removeFromGraph.
scale isNil ifFalse: [scale removeConstraint: self].
offset isNil ifFalse: [offset removeConstraint: self].
)
source: src scale: scalep offset: offsetp destination: dst strength: s = (
super var: src var: dst strength: s.
scale := scalep.
offset := offsetp.
self addConstraint.
)'as-yet-unclassified'
recalculate = (
"Calculate the walkabout strength, the stay flag, and, if it is 'stay', the value for the current output of this constraint. Assume this constraint is satisfied."
| in out |
in := self input. out := self output.
out walkStrength: (strength weakest: in walkStrength).
out stay: (in stay and: [scale stay and: [offset stay]]).
out stay ifTrue: [self execute]. "stay optimization"
)) : (
| "class instance variables" |
'as yet unclassified'
source: src scale: scale offset: offset destination: dst strength: s = (
^self new source: src scale: scale offset: offset destination: dst strength: s
))
'Benchmarks-DeltaBlue'
class DBStayConstraint = DBUnaryConstraint (
"I mark variables that should, with some level of preference, stay the same. I have one method with zero inputs and one output, which does nothing. Planners may exploit the fact that, if I am satisfied, my output will not change during plan execution. This is called ''stay optimization''."
| "instance variables" |
| "class pool variables" |
| "shared pool variables" |
'as yet unclassified'
execute = (
"Stay constraints do nothing."
)
var: v strength: s = (
super var: v strength: s.
self addConstraint.
)) : (
| "class instance variables" |
)
'Benchmarks-DeltaBlue'
class DBStrength = Object (
"Strengths are used to measure the relative importance of constraints. New strengths may be inserted in the strength hierarchy without disrupting current constraints. Strengths cannot be created outside this class, so pointer comparison can be used for value comparison."
| "instance variables" name value |
| "class pool variables" |
| "shared pool variables" |
'as yet unclassified'
name: n value: v = (
name := n.
value := v.
)
strongerThan: other = (
^value < other value
)
strongest: other = (
^(self strongerThan: other) ifTrue: [self] ifFalse: [other]
)
value = (
^value
)
weakerThan: other = (
^value > other value
)
weakest: other = (
^(self weakerThan: other) ifTrue: [self] ifFalse: [other]
)) : (
| "class instance variables" |
'as yet unclassified'
initialize = (
"DBStrength initialize"
DBRequired := self name: 'required' value: 0.
DBStrongPreferred := self name: 'strongPreferred' value: 1.
DBPreferred := self name: 'preferred' value: 2.
DBStrongDefault := self name: 'strongDefault' value: 3.
DBNormal := self name: 'normal' value: 4.
DBWeakDefault := self name: 'weakDefault' value: 5.
DBWeakest := self name: 'weakest' value: 6.
DBDescendingStrengths :=
{DBRequired.
DBStrongPreferred.
DBPreferred.
DBStrongDefault.
DBNormal.
DBWeakDefault.
DBWeakest}.
)
name: n value: v = (
^self basicNew name: n value: v
))
'Benchmarks-DeltaBlue'
class DBUnaryConstraint = DBConstraint (
"I am an abstract superclass for constraints having a single possible output variable."
| "instance variables" myOutput satisfied |
| "class pool variables" |
| "shared pool variables" |
'as yet unclassified'
addToGraph = (
"Add myself to the constraint graph."
myOutput addConstraint: self.
satisfied := false.
)
chooseMethod: mark = (
"Decide if I can be satisfied and record that decision."
satisfied := (myOutput mark ~= mark and: [strength strongerThan: myOutput walkStrength])
)
inputsKnown: mark = (
^true
)
isSatisfied = (
"Answer true if this constraint is satisfied in the current solution."
^satisfied
)
markInputs: mark = (
"I have no inputs."
)
markUnsatisfied = (
"Record the fact that I am unsatisfied."
satisfied := false
)
output = (
^myOutput
)
recalculate = (
"Calculate the walkabout strength, the stay flag, and, if it is 'stay', the value for the current output of this constraint. Assume this constraint is satisfied."
myOutput walkStrength: strength.
myOutput stay: self isInput not.
myOutput stay ifTrue: [self execute]. "Stay optimization"
)
removeFromGraph = (
"Remove myself from the constraint graph."
myOutput isNil ifFalse: [myOutput removeConstraint: self].
satisfied := false.
)
var: v strength: s = (
super strength: s.
myOutput := v.
satisfied := false.
)) : (
| "class instance variables" |
'as yet unclassified'
var: v strength: s = (
^self new var: v strength: s
))
'Benchmarks-DeltaBlue'
class DBVariable = Object (
"I represent a constrained variable. In addition to my value, I maintain the structure of the constraint graph, the current dataflow graph, and various parameters of interest to the DeltaBlue incremental constraint solver."
| "instance variables" value constraints determinedBy mark walkStrength stay name |
| "class pool variables" |
| "shared pool variables" |
'as yet unclassified'
addConstraint: c = (
"Add the given constraint to the set of all constraints that refer to me."
constraints addLast: c.
)
constraints = (
^constraints
)
determinedBy = (
^determinedBy
)
determinedBy: c = (
determinedBy := c
)
mark = (
^mark
)
mark: m = (
mark := m
)
name: n initialValue: v = (
value := v.
constraints := OrderedCollection new: 2.
determinedBy := nil.
mark := 0.
walkStrength := DBWeakest.
stay := true.
name := n.
)
removeConstraint: c = (
"Remove all traces of c from this variable."
constraints remove: c ifAbsent: [].
determinedBy == c ifTrue: [determinedBy := nil].
)
stay = (
^stay
)
stay: b = (
stay := b
)
value = (
^value
)
value: v = (
value := v.
)
walkStrength = (
^walkStrength
)
walkStrength: s = (
walkStrength := s
)) : (
| "class instance variables" |
'as yet unclassified'
name: n initialValue: v = (
^self basicNew name: n initialValue: v
))
'Benchmarks-ParserCombinators'
class PCAlternatingParser = PCCombinatorialParser (
""
| "instance variables" p q |
| "class pool variables" |
| "shared pool variables" |
'as yet unclassified'
compress = (
compressed ifTrue: [^self].
compressed := true.
p := p compress.
q := q compress.
^self
)
either: pArg or: qArg = (
compressed := false.
p := pArg.
q := qArg.
)
parseWithContext: ctxt ifError: onError = (
| pos |
pos := ctxt position.
^p parseWithContext: ctxt ifError: [ctxt position: pos. ^q parseWithContext: ctxt ifError: onError]
)) : (
| "class instance variables" |
)
'Benchmarks-ParserCombinators'
class PCCharacterRangeParser = PCCombinatorialParser (
""
| "instance variables" lowerBound upperBound |
| "class pool variables" |
| "shared pool variables" |
'as yet unclassified'
between: c1 and: c2 = (
lowerBound := c1.
upperBound := c2.
)
compress = (
^self
)
parseWithContext: ctxt ifError: onError = (
| c |
ctxt atEnd ifTrue: [onError value].
c := ctxt next.
(lowerBound <= c) & (c <= upperBound) ifTrue: [^c].
onError value.
)) : (
| "class instance variables" |
)
'Benchmarks-ParserCombinators'
class PCCombinatorialParser = Object (
""
| "instance variables" compressed |
| "class pool variables" |
| "shared pool variables" |
'as yet unclassified'
, p = (
^PCSequencingParser new subparsers: {self. p}
)
char: c = (
^PCCharacterRangeParser new between: c and: c.
)
charBetween: c1 and: c2 = (
^PCCharacterRangeParser new between: c1 and: c2.
)
eoi = (
^PCEOIParser new
)
parseWithContext: ctxt ifError: onError = (
self subclassResponsibility
)
star = (
^PCStarParser new repeat: self
)
wrapper: block = (
^PCWrappingParser new wrap: self with: block
)
| q = (
^PCAlternatingParser new either: self or: q
)) : (
| "class instance variables" |
)
'Benchmarks-ParserCombinators'
class PCEOIParser = PCCombinatorialParser (
""
| "instance variables" |
| "class pool variables" |
| "shared pool variables" |
'as yet unclassified'
compress = (
^self
)
parseWithContext: ctxt ifError: onError = (
ctxt atEnd ifTrue: [^nil].
onError value.
)) : (
| "class instance variables" |
)
'Benchmarks-ParserCombinators'
class PCForwardReferenceParser = PCCombinatorialParser (
""
| "instance variables" forwardee |
| "class pool variables" |
| "shared pool variables" |
'as yet unclassified'
bind: p = (
self assert: [forwardee isNil] message: ''.
forwardee := p.
)
compress = (
^forwardee compress
)) : (
| "class instance variables" |
)
'Benchmarks-ParserCombinators'
class PCParserContext = Object (
""
| "instance variables" content position |
| "class pool variables" |
| "shared pool variables" |
'as yet unclassified'
atEnd = (
^position >= content size
)
next = (
position := position + 1.
^content at: position
)
over: s = (
content := s.
position := 0.
)
position = (
^position
)
position: v = (
position := v
)) : (
| "class instance variables" |
)
'Benchmarks-ParserCombinators'
class PCSequencingParser = PCCombinatorialParser (
""
| "instance variables" subparsers |
| "class pool variables" |
| "shared pool variables" |
'as yet unclassified'
, p = (
^PCSequencingParser new subparsers: (subparsers copyWith: p)
)
compress = (
compressed ifTrue: [^self].
compressed := true.
subparsers := subparsers collect: [:e | e compress].
^self
)
parseWithContext: ctxt ifError: onError = (
^subparsers collect: [:p | p parseWithContext: ctxt ifError: onError]
)
subparsers: ps = (
compressed := false.
subparsers := ps.
)
wrapper: block = (
^PCWrappingParser new wrap: self with: [:results | block valueWithArguments: results]
)) : (
| "class instance variables" |
)
'Benchmarks-ParserCombinators'
class PCSimpleExpressionGrammar = PCCombinatorialParser (
""
| "instance variables" start exp e1 e2 parenExp number plus times digit lparen rparen |
| "class pool variables" |
| "shared pool variables" |
'as yet unclassified'
initialize = (
start := PCForwardReferenceParser new.
exp := PCForwardReferenceParser new.
e1 := PCForwardReferenceParser new.
e2 := PCForwardReferenceParser new.
parenExp := PCForwardReferenceParser new.
number := PCForwardReferenceParser new.
plus := PCForwardReferenceParser new.
times := PCForwardReferenceParser new.
digit := PCForwardReferenceParser new.
lparen := PCForwardReferenceParser new.
rparen := PCForwardReferenceParser new.
start bind: (exp, self eoi wrapper: [:v :dollar | v]).
exp bind: (e1, (plus, e1) star wrapper: [:lhs :rhss |
| z | z := lhs. rhss do: [:rhs | z := z + (rhs at: 2) rem: 16rFFFF]. z]).
e1 bind: (e2, (times, e2) star wrapper: [:lhs :rhss |
| z | z := lhs. rhss do: [:rhs | z := z * (rhs at: 2) rem: 16rFFFF]. z]).
e2 bind: (number | parenExp).
parenExp bind: (lparen, exp, rparen wrapper: [:lhs :e :rhs | e]).
number bind: (digit wrapper: [:d | d asString asNumber]).
plus bind: (self char: $+).
times bind: (self char: $*).
digit bind: (self charBetween: $0 and: $9).
lparen bind: (self char: $().
rparen bind: (self char: $))
)
start = (
^start
)) : (
| "class instance variables" |
)
'Benchmarks-ParserCombinators'
class PCStarParser = PCCombinatorialParser (
""
| "instance variables" p |
| "class pool variables" |
| "shared pool variables" |
'as yet unclassified'
compress = (
compressed ifTrue: [^self].
compressed := true.
p := p compress.
^self
)
parseWithContext: ctxt ifError: onError = (
| results |
results := OrderedCollection new.
[
| pos |
pos := ctxt position.
results add: (p parseWithContext: ctxt ifError: [ctxt position: pos. ^results]).
] repeat.
)
repeat: pArg = (
compressed := false.
p := pArg.
)
wrapper: block = (
^PCWrappingParser new wrap: self with: [:results | block valueWithArguments: results]
)) : (
| "class instance variables" |
)
'Benchmarks-ParserCombinators'
class PCWrappingParser = PCCombinatorialParser (
""
| "instance variables" p b |
| "class pool variables" |
| "shared pool variables" |
'as yet unclassified'
compress = (
compressed ifTrue: [^self].
compressed := true.
p := p compress.
^self
)
parseWithContext: ctxt ifError: onError = (
^b value: (p parseWithContext: ctxt ifError: onError)
)
wrap: pArg with: bArg = (
compressed := false.
p := pArg.
b := bArg.
)) : (
| "class instance variables" |
)
'Benchmarks-Richards'
class RDeviceTask = RTask (
"A task that suspends itself after each time it has been run to simulate waiting for data from an external device."
| "instance variables" pending |
| "class pool variables" |
| "shared pool variables" |