-
Notifications
You must be signed in to change notification settings - Fork 2
/
thingseeker.pas
2338 lines (2122 loc) · 94.6 KB
/
thingseeker.pas
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
{$MODE OBJFPC} { -*- delphi -*- }
{$INCLUDE settings.inc}
unit thingseeker;
//{$DEFINE DEBUG_SEEKER}
interface
uses
physics, grammarian;
type
{ Ending clauses allow the collector to distingish "put [foo on bar]" from "put [foo] on bar". }
TEndingClauseKind = (eckNormal, eckIn, eckOn);
TEndingClauseKinds = set of TEndingClauseKind;
TAllImpliedScope = set of (aisSurroundings, aisSelf);
TThingCollectionOptions = set of (optAllowMultiples);
TThingCollector = class
protected
FTokenCount: Cardinal;
FDisambiguate: Boolean;
FThingList: TThingList;
{$IFOPT C+} FCurrentlyCollecting, FCollected: Boolean; {$ENDIF}
FCurrentPreferredGrammaticalNumber: TGrammaticalNumber;
FCurrentBestLength: Cardinal;
FCurrentBestGrammaticalNumber: TGrammaticalNumber;
FCurrentBestThingList: TThingList;
procedure Add(Thing: TThing; Count: Cardinal; GrammaticalNumber: TGrammaticalNumber);
public
constructor Create();
destructor Destroy(); override;
function Collect(Perspective: TAvatar; Tokens, OriginalTokens: TTokens; Start: Cardinal; Options: TThingCollectionOptions; Scope: TAllImpliedScope; Ends: TEndingClauseKinds; Verb: UTF8String): Boolean;
function GetTokenCount(): Cardinal;
function GetDisambiguate(): Boolean;
function GetThingList(): TThingList; { must be called exactly once after Collect() returns true }
end;
implementation
uses
{$IFDEF DEBUG} debug, {$ENDIF}
sysutils;
type
TThingSelectionMechanism = (tsmPickAll, tsmPickNumber, tsmPickSome, tsmPickOnlyNumber, tsmPickWithoutArticle);
TClauseFlags = set of (cfAllowExceptions, cfSingular, cfPlural, cfDisambiguateAnyLoneResult, cfDisambiguateSingularLoneResult,
cfHaveThatIsClause, cfHaveThatAreClause, cfRemoveHiddenThings, cfHadArticle);
TIsMatchFunction = function (Candidate, Condition: TThing): Boolean of object;
TAbstractJoiningClause = class;
TAbstractClause = class
protected
FFlags: TClauseFlags;
FNumber: Cardinal;
FSelectionMechanism: TThingSelectionMechanism;
FThings: TThingList;
FInputFragment, FFilterFragments: UTF8String;
FNext: TAbstractClause;
FPrevious: TAbstractClause;
procedure Preselect(Target: TAbstractClause; var Count: Cardinal; var SelectionMechanism: TThingSelectionMechanism); virtual;
function IsPlainClause(): Boolean; virtual;
function GetPreviousOpenClause(): TAbstractClause; virtual;
function GetArticle(NounPhrase: UTF8String = ''): UTF8String; virtual;
procedure AddFilterFragment(Fragment: UTF8String);
public
constructor Create(Number: Cardinal; SelectionMechanism: TThingSelectionMechanism; Flags: TClauseFlags; Things: TThingList; InputFragment: UTF8String); virtual;
destructor Destroy(); override;
procedure CheckContext(); virtual;
function AcceptsJoins(Peer: TAbstractClause): Boolean; virtual; abstract;
procedure RegisterJoin(Peer: TAbstractJoiningClause); virtual; abstract;
function AcceptsFilter(Peer: TAbstractClause; out CanContinue: Boolean): Boolean; virtual;
procedure SelfCensor(Allowlist: TThingList); virtual;
function Select(): Boolean; virtual;
procedure Process(); virtual; abstract;
procedure Add(Next: TAbstractClause);
class function GetPreferredGrammaticalNumber(DefaultGrammaticalNumber: TGrammaticalNumber): TGrammaticalNumber; virtual;
class function CanFail(): Boolean; virtual; { return True if the clause could also be a preposition or action join (e.g. "from", "and") }
class procedure ReportFailedMatch(Tokens: TTokens; ClauseStart, CurrentToken: Cardinal; Verb: UTF8String); virtual;
function GetFragment(): UTF8String;
end;
TAbstractClauseClass = class of TAbstractClause;
TAbstractJoiningClause = class(TAbstractClause)
protected
FJoinedTo: TAbstractClause;
procedure Preselect(Target: TAbstractClause; var Count: Cardinal; var SelectionMechanism: TThingSelectionMechanism); override;
function IsPlainClause(): Boolean; override;
function GetPreviousOpenClause(): TAbstractClause; override;
function GetFragmentAnnotation(): UTF8String; virtual;
function GetClausePrefix(): UTF8String; virtual; abstract;
public
function AcceptsJoins(Peer: TAbstractClause): Boolean; override;
procedure RegisterJoin(Peer: TAbstractJoiningClause); override;
procedure Process(); override;
end;
TAbstractPlainJoiningClause = class(TAbstractJoiningClause)
public
procedure CheckContext(); override;
end;
TAndClause = class(TAbstractPlainJoiningClause)
protected
function GetClausePrefix(): UTF8String; override;
public
class function CanFail(): Boolean; override;
end;
TPlusClause = class(TAbstractPlainJoiningClause)
protected
function GetClausePrefix(): UTF8String; override;
end;
TAbstractJoiningPreconditionFilterClause = class(TAbstractJoiningClause)
protected
class function IsMatch(Candidate, Condition: TThing): Boolean; virtual; abstract;
public
procedure CheckContext(); override;
procedure ReportNoJoinee(); virtual;
end;
TAndFromClause = class(TAbstractJoiningPreconditionFilterClause)
protected
class function IsMatch(Candidate, Condition: TThing): Boolean; override;
function GetClausePrefix(): UTF8String; override;
public
class procedure ReportFailedMatch(Tokens: TTokens; ClauseStart, CurrentToken: Cardinal; Verb: UTF8String); override;
end;
TAndOfClause = class(TAbstractJoiningPreconditionFilterClause)
protected
class function IsMatch(Candidate, Condition: TThing): Boolean; override;
function GetClausePrefix(): UTF8String; override;
public
class procedure ReportFailedMatch(Tokens: TTokens; ClauseStart, CurrentToken: Cardinal; Verb: UTF8String); override;
end;
TAndInClause = class(TAbstractJoiningPreconditionFilterClause)
protected
class function IsMatch(Candidate, Condition: TThing): Boolean; override;
function GetClausePrefix(): UTF8String; override;
public
class procedure ReportFailedMatch(Tokens: TTokens; ClauseStart, CurrentToken: Cardinal; Verb: UTF8String); override;
end;
TAndOnClause = class(TAbstractJoiningPreconditionFilterClause)
protected
class function IsMatch(Candidate, Condition: TThing): Boolean; override;
function GetClausePrefix(): UTF8String; override;
public
class procedure ReportFailedMatch(Tokens: TTokens; ClauseStart, CurrentToken: Cardinal; Verb: UTF8String); override;
end;
TJoinableClause = class(TAbstractClause)
protected
FRegisteredJoins: array of TAbstractJoiningClause;
public
function AcceptsJoins(Peer: TAbstractClause): Boolean; override;
procedure RegisterJoin(Peer: TAbstractJoiningClause); override;
end;
TAbstractFilteringClause = class(TJoinableClause)
protected
FVictims: array of TAbstractClause;
function GetPreviousOpenClause(): TAbstractClause; override;
procedure Filter(Victim: TAbstractClause); virtual;
class function IsMatch(Candidate, Condition: TThing): Boolean; virtual; abstract;
function KeepMatches(): Boolean; virtual; abstract;
procedure Victimise(Clause: TAbstractClause); virtual;
function GetIsMatch(Target: TAbstractClause): TIsMatchFunction; virtual;
function GetFragmentAnnotation(): UTF8String; virtual;
function GetClausePrefix(): UTF8String; virtual; abstract;
public
procedure CheckContext(); override;
function AcceptsFilter(Peer: TAbstractClause; out CanContinue: Boolean): Boolean; override;
procedure ReportNoVictims(); virtual;
procedure ReportNothingLeft(); virtual;
procedure Process(); override;
class function GetPreferredGrammaticalNumber(DefaultGrammaticalNumber: TGrammaticalNumber): TGrammaticalNumber; override;
end;
TInclusionFilterClause = class(TAbstractFilteringClause)
protected
procedure Filter(Victim: TAbstractClause); override;
function KeepMatches(): Boolean; override;
end;
TAbstractThatIsAreClause = class(TInclusionFilterClause)
protected
procedure Preselect(Target: TAbstractClause; var Count: Cardinal; var SelectionMechanism: TThingSelectionMechanism); override;
class function IsMatch(Candidate, Condition: TThing): Boolean; override;
end;
TAbstractThatIsClause = class(TAbstractThatIsAreClause)
protected
procedure Victimise(Clause: TAbstractClause); override;
public
procedure CheckContext(); override;
procedure ReportNotRightGrammaticalNumber(); virtual;
procedure ReportThatIsAll(); virtual;
procedure ReportExplicitNumber(); virtual;
end;
TThatIsClause = class(TAbstractThatIsClause)
protected
function GetClausePrefix(): UTF8String; override;
end;
TAndThatIsClause = class(TAbstractThatIsClause)
protected
function GetClausePrefix(): UTF8String; override;
public
function AcceptsFilter(Peer: TAbstractClause; out CanContinue: Boolean): Boolean; override;
end;
TAbstractThatAreClause = class(TAbstractThatIsAreClause)
protected
procedure Victimise(Clause: TAbstractClause); override;
public
procedure CheckContext(); override;
procedure ReportInappropriateArticle(); virtual;
procedure ReportExplicitNumber(); virtual;
end;
TThatAreClause = class(TAbstractThatAreClause)
protected
function GetClausePrefix(): UTF8String; override;
end;
TAndThatAreClause = class(TAbstractThatAreClause)
protected
function GetClausePrefix(): UTF8String; override;
public
function AcceptsFilter(Peer: TAbstractClause; out CanContinue: Boolean): Boolean; override;
end;
TAbstractPreconditionFilterClause = class(TInclusionFilterClause)
protected
procedure Preselect(Target: TAbstractClause; var Count: Cardinal; var SelectionMechanism: TThingSelectionMechanism); override;
function GetIsMatch(Target: TAbstractClause): TIsMatchFunction; override;
public
function AcceptsJoins(Peer: TAbstractClause): Boolean; override;
function AcceptsFilter(Peer: TAbstractClause; out CanContinue: Boolean): Boolean; override;
end;
TFromClause = class(TAbstractPreconditionFilterClause)
protected
class function IsMatch(Candidate, Condition: TThing): Boolean; override;
function GetClausePrefix(): UTF8String; override;
public
end;
TOfClause = class(TAbstractPreconditionFilterClause)
protected
class function IsMatch(Candidate, Condition: TThing): Boolean; override;
function GetClausePrefix(): UTF8String; override;
public
end;
TInClause = class(TAbstractPreconditionFilterClause)
protected
class function IsMatch(Candidate, Condition: TThing): Boolean; override;
function GetClausePrefix(): UTF8String; override;
public
end;
TOnClause = class(TAbstractPreconditionFilterClause)
protected
class function IsMatch(Candidate, Condition: TThing): Boolean; override;
public
function GetClausePrefix(): UTF8String; override;
end;
TExclusionFilteringClause = class(TAbstractFilteringClause)
protected
procedure Preselect(Target: TAbstractClause; var Count: Cardinal; var SelectionMechanism: TThingSelectionMechanism); override;
function KeepMatches(): Boolean; override;
end;
TAbstractThatIsAreNotClause = class(TExclusionFilteringClause)
protected
procedure Preselect(Target: TAbstractClause; var Count: Cardinal; var SelectionMechanism: TThingSelectionMechanism); override;
class function IsMatch(Candidate, Condition: TThing): Boolean; override;
public
procedure ReportNotRightGrammaticalNumber(); virtual;
end;
TAbstractThatIsNotClause = class(TAbstractThatIsAreNotClause)
protected
procedure Victimise(Clause: TAbstractClause); override;
public
procedure CheckContext(); override;
procedure ReportThatIsAll(); virtual;
end;
TThatIsNotClause = class(TAbstractThatIsNotClause)
protected
function GetClausePrefix(): UTF8String; override;
end;
TAndThatIsNotClause = class(TAbstractThatIsNotClause)
protected
function GetClausePrefix(): UTF8String; override;
public
function AcceptsFilter(Peer: TAbstractClause; out CanContinue: Boolean): Boolean; override;
end;
TAbstractThatAreNotClause = class(TAbstractThatIsAreNotClause)
protected
procedure Victimise(Clause: TAbstractClause); override;
public
procedure CheckContext(); override;
procedure ReportInappropriateArticle(); virtual;
end;
TThatAreNotClause = class(TAbstractThatAreNotClause)
protected
function GetClausePrefix(): UTF8String; override;
end;
TAndThatAreNotClause = class(TAbstractThatAreNotClause)
protected
function GetClausePrefix(): UTF8String; override;
public
function AcceptsFilter(Peer: TAbstractClause; out CanContinue: Boolean): Boolean; override;
end;
TAbstractButClause = class(TExclusionFilteringClause) { filters that filter the nearest sequence of "all"s }
protected
class function IsMatch(Candidate, Condition: TThing): Boolean; override;
public
function AcceptsJoins(Peer: TAbstractClause): Boolean; override;
end;
TButClause = class(TAbstractButClause)
protected
function GetClausePrefix(): UTF8String; override;
end;
TStartClause = class(TJoinableClause)
protected
function IsPlainClause(): Boolean; override;
public
procedure CheckContext(); override;
procedure PreCheckNoMultiples(Perspective: TAvatar; const Verb: UTF8String);
procedure PostCheckNoMultiples(Perspective: TAvatar; const Verb: UTF8String);
function AcceptsJoins(Peer: TAbstractClause): Boolean; override;
function AcceptsFilter(Peer: TAbstractClause; out CanContinue: Boolean): Boolean; override;
procedure Process(); override;
procedure Bank(Perspective: TAvatar; var Things: TThingList; var Disambiguate: Boolean);
{$IFDEF DEBUG_SEEKER} function GetCompleteFragment(): UTF8String; {$ENDIF}
class function CanFail(): Boolean; override;
class procedure ReportFailedMatch(Tokens: TTokens; ClauseStart, CurrentToken: Cardinal; Verb: UTF8String); override;
end;
EMatcherException = class
FClause: TAbstractClause;
constructor Create(Clause: TAbstractClause);
function Message(Perspective: TAvatar; const Input, Verb: UTF8String): UTF8String; virtual; abstract;
end;
ECountWrong = class(EMatcherException)
FWanted, FGot: Cardinal;
constructor Create(Clause: TAbstractClause; Wanted, Got: Cardinal);
end;
ESelectNotEnough = class(ECountWrong)
function Message(Perspective: TAvatar; const Input, Verb: UTF8String): UTF8String; override;
end;
ESelectTooMany = class(ECountWrong)
function Message(Perspective: TAvatar; const Input, Verb: UTF8String): UTF8String; override;
end;
constructor EMatcherException.Create(Clause: TAbstractClause);
begin
inherited Create();
FClause := Clause;
end;
constructor ECountWrong.Create(Clause: TAbstractClause; Wanted, Got: Cardinal);
begin
inherited Create(Clause);
FWanted := Wanted;
FGot := Got;
end;
function ESelectNotEnough.Message(Perspective: TAvatar; const Input, Verb: UTF8String): UTF8String;
begin
Assert(Length(Input) > 0);
Assert(FGot > 0);
Assert(FWanted > FGot);
Result := 'About the ' + NumberToEnglish(FWanted) + ' ' + Input + '... I can only find ' + NumberToEnglish(FGot) + ': ' + FClause.FThings.GetLongDefiniteString(Perspective, 'and') + '.';
end;
function ESelectTooMany.Message(Perspective: TAvatar; const Input, Verb: UTF8String): UTF8String;
begin
Assert(Length(Input) > 0);
Assert(FGot > FWanted);
if (FWanted = 1) then
Result := 'Which ' + Input + ' do you mean, ' + FClause.FThings.GetLongDefiniteString(Perspective, 'or') + '?'
else
if (FWanted = 1) then
Result := 'About the ' + Input + '... I count ' + NumberToEnglish(FGot) + ', not ' + NumberToEnglish(FWanted) + '.'
else
Result := 'About the ' + NumberToEnglish(FWanted) + ' ' + Input + '... I count ' + NumberToEnglish(FGot) + ', not ' + NumberToEnglish(FWanted) + '.';
end;
constructor TAbstractClause.Create(Number: Cardinal; SelectionMechanism: TThingSelectionMechanism; Flags: TClauseFlags; Things: TThingList; InputFragment: UTF8String);
begin
{$IFDEF DEBUG_SEEKER} Writeln('TAbstractClause.Create() constructing a ', ClassName, ' with Number=', Number, '; SelectionMechanism=', SelectionMechanism, '; Things=', Things.GetDefiniteString(nil, 'and'), '; InputFragment="', InputFragment, '"'); {$ENDIF}
Assert((cfSingular in Flags) or (cfPlural in Flags));
Assert(Assigned(Things));
inherited Create();
FNumber := Number;
FSelectionMechanism := SelectionMechanism;
FFlags := Flags;
FThings := Things;
FInputFragment := InputFragment;
end;
destructor TAbstractClause.Destroy();
begin
if (Assigned(FThings)) then
FThings.Free(); { TStartClause.Bank() hands it off to someone else }
FNext.Free();
inherited;
end;
procedure TAbstractClause.CheckContext();
begin
Assert(Assigned(FPrevious) xor (Self is TStartClause));
{$IFDEF DEBUG_SEEKER} Writeln('TAbstractClause.CheckContext() called on a ', ClassName, ' with cfAllowExceptions=', cfAllowExceptions in FFlags, ', cfSingular=', cfSingular in FFlags, ', cfPlural=', cfPlural in FFlags, ', cfDisambiguateAnyLoneResult=', cfDisambiguateAnyLoneResult in FFlags, ', cfDisambiguateSingularLoneResult=', cfDisambiguateSingularLoneResult in FFlags, ', cfHaveThatIsClause=', cfHaveThatIsClause in FFlags, ', cfHaveThatAreClause=', cfHaveThatAreClause in FFlags, ', cfRemoveHiddenThings=', cfRemoveHiddenThings in FFlags, ', cfHadArticle=', cfHadArticle in FFlags, '; FSelectionMechanism=', FSelectionMechanism, '; FNumber=', FNumber, '; FThings=', FThings.GetDefiniteString(nil, 'and')); {$ENDIF}
end;
function TAbstractClause.AcceptsFilter(Peer: TAbstractClause; out CanContinue: Boolean): Boolean;
var
PreviousOpen: TAbstractClause;
begin
{$IFDEF DEBUG_SEEKER} Writeln('TAbstractClause.AcceptsFilter() called on a ', ClassName, ' for a peer that is a ', Peer.ClassName); {$ENDIF}
if (Peer is TAbstractButClause) then
begin
Result := cfAllowExceptions in FFlags;
PreviousOpen := GetPreviousOpenClause();
CanContinue := (not Result) or (Assigned(PreviousOpen) and ((Self is TAbstractFilteringClause) = (PreviousOpen is TAbstractFilteringClause)));
end
else
if ((Peer is TThatIsClause) or (Peer is TThatIsNotClause)) then
begin
Result := (cfSingular in FFlags);
CanContinue := False;
end
else
if ((Peer is TAndThatIsClause) or (Peer is TAndThatIsNotClause)) then
begin
Result := cfHaveThatIsClause in FFlags;
CanContinue := (not Result) and (not IsPlainClause());
end
else
if ((Peer is TThatAreClause) or (Peer is TThatAreNotClause)) then
begin
Result := cfPlural in FFlags;
CanContinue := False;
end
else
if ((Peer is TAndThatAreClause) or (Peer is TAndThatAreNotClause)) then
begin
Result := cfHaveThatAreClause in FFlags;
CanContinue := (not Result) and (not IsPlainClause());
end
else
if (Peer is TAbstractPreconditionFilterClause) then
begin
Result := IsPlainClause();
PreviousOpen := GetPreviousOpenClause();
CanContinue := (not Result) or (Assigned(PreviousOpen) and ((cfAllowExceptions in FFlags) = (cfAllowExceptions in GetPreviousOpenClause().FFlags)));
end
else
begin
Result := False;
CanContinue := False;
end;
{$IFDEF DEBUG_SEEKER} Writeln('Result=', Result, ' CanContinue=', CanContinue); {$ENDIF}
end;
function TAbstractClause.GetArticle(NounPhrase: UTF8String = ''): UTF8String;
begin
if (cfHadArticle in FFlags) then
begin
if (NounPhrase = '') then
NounPhrase := GetFragment();
case FSelectionMechanism of
tsmPickAll:
if (cfPlural in FFlags) then
begin
if (cfAllowExceptions in FFlags) then
Result := 'all of the'
else
Result := 'the';
end
else
begin
Assert(cfSingular in FFlags);
Assert(cfAllowExceptions in FFlags);
Result := 'every'
end;
tsmPickNumber:
if (FNumber = 1) then
begin
if (cfPlural in FFlags) then
begin
if (cfAllowExceptions in FFlags) then
Result := 'any of the'
else
Result := 'one of the';
end
else
begin
Assert(cfSingular in FFlags);
if (cfAllowExceptions in FFlags) then
Result := 'any'
else
Result := IndefiniteArticle(NounPhrase);
end;
end
else
begin
if (cfAllowExceptions in FFlags) then
Result := 'any ' + NumberToEnglish(FNumber)
else
Result := NumberToEnglish(FNumber);
end;
tsmPickSome:
begin
Assert(not (cfSingular in FFlags));
Assert(cfPlural in FFlags);
Assert(not (cfAllowExceptions in FFlags));
Result := 'some';
end;
tsmPickOnlyNumber:
if ((FNumber = 1) and (cfSingular in FFlags)) then
begin
if (cfAllowExceptions in FFlags) then
Result := 'all of the'
else
Result := 'the';
end
else
begin
if (cfAllowExceptions in FFlags) then
Result := 'all ' + NumberToEnglish(FNumber)
else
Result := 'the ' + NumberToEnglish(FNumber);
end;
tsmPickWithoutArticle:
begin
Assert(cfSingular in FFlags);
Assert(not (cfPlural in FFlags));
Assert(not (cfAllowExceptions in FFlags));
Result := '';
end;
end;
end
else
Result := '';
end;
function TAbstractClause.Select(): Boolean;
var
Count: Cardinal;
procedure SelectN(N: Cardinal);
var
E: TThingList.TEnumerator;
begin
Assert(N <= Count);
E := FThings.GetEnumerator();
try
repeat
{$IFOPT C+} Assert( {$ENDIF} E.MoveNext() {$IFOPT C+} ) {$ENDIF} ;
Dec(N);
until N = 0;
if (E.MoveNext()) then
E.RemoveRemainder();
finally
E.Free();
end;
end;
var
SelectionMechanism: TThingSelectionMechanism;
begin
{$IFDEF DEBUG_SEEKER} Writeln('TAbstractClause.Select() for a ', ClassName, ' with FThings=', FThings.GetDefiniteString(nil, 'and'), '; SelectionMechanism=', FSelectionMechanism); {$ENDIF}
{ TThingSelectionMechanism = (tsmPickAll, tsmPickNumber, tsmPickSome, tsmPickOnlyNumber, tsmPickWithoutArticle); }
Assert(((FSelectionMechanism in [tsmPickAll, tsmPickSome, tsmPickWithoutArticle]) and (FNumber = 1)) or
((FSelectionMechanism in [tsmPickNumber, tsmPickOnlyNumber]) and (FNumber >= 1)),
'FSelectionMechanism=' + IntToStr(Cardinal(FSelectionMechanism)) + '; FNumber=' + IntToStr(FNumber));
Count := FThings.Length;
Assert((Count > 0) or (FSelectionMechanism = tsmPickAll), 'Count=' + IntToStr(Count) + '; FSelectionMechanism=' + IntToStr(Cardinal(FSelectionMechanism)));
SelectionMechanism := FSelectionMechanism;
Preselect(Self, Count, SelectionMechanism);
if ((Count < FNumber) and (SelectionMechanism in [tsmPickNumber, tsmPickOnlyNumber])) then
begin
Assert(Count > 0);
raise ESelectNotEnough.Create(Self, FNumber, Count);
end;
case SelectionMechanism of
tsmPickAll: Result := False;
tsmPickNumber:
begin
SelectN(FNumber);
Result := Count > FNumber;
end;
tsmPickSome:
begin
if (Count <= 2) then
begin
SelectN(Count);
Result := False;
end
else
begin
if (Count > 8) then
SelectN(7)
else
SelectN((Count div 2) + 1); // $R-
Result := True;
end;
end;
tsmPickOnlyNumber, tsmPickWithoutArticle:
begin
Assert((SelectionMechanism <> tsmPickWithoutArticle) or (FNumber = 1));
if (Count <> FNumber) then
begin
Assert(Count > FNumber);
raise ESelectTooMany.Create(Self, FNumber, Count);
end;
Result := False;
end;
end;
{$IFDEF DEBUG_SEEKER} Writeln('TAbstractClause.Select() for a ', ClassName, ' ended with FThings=', FThings.GetDefiniteString(nil, 'and'), '; Result=', Result); {$ENDIF}
end;
procedure TAbstractClause.SelfCensor(Allowlist: TThingList);
var
E: TThingList.TEnumerator;
begin
{$IFDEF DEBUG_SEEKER} Writeln('TAbstractClause.SelfCensor() for a ', ClassName, ' with FThings=', FThings.GetDefiniteString(nil, 'and'), '; Allowlist=', Allowlist.GetDefiniteString(nil, 'and')); {$ENDIF}
E := FThings.GetEnumerator();
try
while (E.MoveNext()) do
begin
if (not Allowlist.Contains(E.Current)) then
E.Remove();
end;
finally
E.Free();
end;
{$IFDEF DEBUG_SEEKER} Writeln('TAbstractClause.SelfCensor() for a ', ClassName, ' ending with FThings=', FThings.GetDefiniteString(nil, 'and')); {$ENDIF}
end;
procedure TAbstractClause.Preselect(Target: TAbstractClause; var Count: Cardinal; var SelectionMechanism: TThingSelectionMechanism);
begin
{$IFDEF DEBUG_SEEKER} Writeln('TAbstractClause.Preselect() for a ', ClassName, ' with FThings=', FThings.GetDefiniteString(nil, 'and'), '; Target=', Target.ClassName, ', Count=', Count, ', SelectionMechanism=', SelectionMechanism); {$ENDIF}
end;
procedure TAbstractClause.Add(Next: TAbstractClause);
begin
Assert(Assigned(Next));
Assert(not Assigned(FNext));
Assert(not Assigned(Next.FPrevious));
FNext := Next;
Next.FPrevious := Self;
end;
function TAbstractClause.IsPlainClause(): Boolean;
begin
Result := False;
end;
function TAbstractClause.GetPreviousOpenClause(): TAbstractClause;
begin
Result := FPrevious;
end;
class function TAbstractClause.GetPreferredGrammaticalNumber(DefaultGrammaticalNumber: TGrammaticalNumber): TGrammaticalNumber;
begin
Result := DefaultGrammaticalNumber;
end;
class function TAbstractClause.CanFail(): Boolean;
begin
Result := False;
end;
class procedure TAbstractClause.ReportFailedMatch(Tokens: TTokens; ClauseStart, CurrentToken: Cardinal; Verb: UTF8String);
begin
Assert(ClauseStart <> CurrentToken);
Fail('I was with you until you said "' + Serialise(Tokens, ClauseStart, CurrentToken - ClauseStart + 1) + '".'); // $R-
end;
procedure TAbstractClause.AddFilterFragment(Fragment: UTF8String);
begin
if (FFilterFragments <> '') then
FFilterFragments := Fragment + ' ' + FFilterFragments
else
FFilterFragments := Fragment;
end;
function TAbstractClause.GetFragment(): UTF8String;
begin
Result := FInputFragment;
if (FFilterFragments <> '') then
Result := Result + ' ' + FFilterFragments;
end;
procedure TAbstractJoiningClause.Process();
begin
Assert(Assigned(FJoinedTo));
FJoinedTo.RegisterJoin(Self);
end;
procedure TAbstractJoiningClause.Preselect(Target: TAbstractClause; var Count: Cardinal; var SelectionMechanism: TThingSelectionMechanism);
begin
{$IFDEF DEBUG_SEEKER} Writeln('TAbstractJoiningClause.Preselect() for a ', ClassName, ' with FThings=', FThings.GetDefiniteString(nil, 'and'), '; Target=', Target.ClassName, ', Count=', Count, ', SelectionMechanism=', SelectionMechanism, ' -- deferring to ', FJoinedTo.ClassName); {$ENDIF}
Assert(Assigned(FJoinedTo));
Assert(Target is TAbstractJoiningClause);
FJoinedTo.Preselect(Target, Count, SelectionMechanism);
end;
function TAbstractJoiningClause.AcceptsJoins(Peer: TAbstractClause): Boolean;
begin
Assert(Assigned(FJoinedTo));
Result := FJoinedTo.AcceptsJoins(Peer);
end;
procedure TAbstractJoiningClause.RegisterJoin(Peer: TAbstractJoiningClause);
begin
Assert(Assigned(FJoinedTo));
FJoinedTo.RegisterJoin(Peer);
end;
function TAbstractJoiningClause.IsPlainClause(): Boolean;
begin
Assert(Assigned(FJoinedTo));
Result := FJoinedTo.IsPlainClause();
end;
function TAbstractJoiningClause.GetPreviousOpenClause(): TAbstractClause;
begin
Assert(Assigned(FJoinedTo));
Result := FJoinedTo;
end;
function TAbstractJoiningClause.GetFragmentAnnotation(): UTF8String;
var
Article: UTF8String;
begin
Article := GetArticle();
if (Article <> '') then
Article := Article + ' ';
Result := GetClausePrefix() + ' ' + Article + GetFragment();
end;
procedure TAbstractPlainJoiningClause.CheckContext();
var
EarlierClause: TAbstractClause;
begin
inherited;
{$IFDEF DEBUG_SEEKER} Writeln('TAbstractPlainJoiningClause.CheckContext() on a ', ClassName); {$ENDIF}
EarlierClause := FPrevious;
while (not EarlierClause.AcceptsJoins(Self)) do
begin
EarlierClause := EarlierClause.GetPreviousOpenClause();
Assert(Assigned(EarlierClause));
end;
FJoinedTo := EarlierClause;
end;
function TAndClause.GetClausePrefix(): UTF8String;
begin
Result := 'and';
end;
class function TAndClause.CanFail(): Boolean;
begin
Result := True;
end;
function TPlusClause.GetClausePrefix(): UTF8String;
begin
Result := 'plus';
end;
procedure TAbstractJoiningPreconditionFilterClause.CheckContext();
var
EarlierClause: TAbstractClause;
begin
inherited;
{$IFDEF DEBUG_SEEKER} Writeln('TAbstractJoiningPreconditionFilterClause.CheckContext() begin'); {$ENDIF}
EarlierClause := FPrevious;
{$IFDEF DEBUG_SEEKER} Writeln('asking a ' + EarlierClause.ClassName); {$ENDIF}
while ((Assigned(EarlierClause)) and (not EarlierClause.AcceptsJoins(Self))) do
begin
{$IFDEF DEBUG_SEEKER} Writeln('got rejected by a ' + EarlierClause.ClassName); {$ENDIF}
EarlierClause := EarlierClause.GetPreviousOpenClause();
{$IFDEF DEBUG_SEEKER} if (Assigned(EarlierClause)) then Writeln('now asking a ' + EarlierClause.ClassName); {$ENDIF}
end;
if (not Assigned(EarlierClause)) then
ReportNoJoinee();
{$IFDEF DEBUG_SEEKER} Writeln('accepted by a ' + EarlierClause.ClassName); {$ENDIF}
FJoinedTo := EarlierClause;
{$IFDEF DEBUG_SEEKER} Writeln('TAbstractJoiningPreconditionFilterClause.CheckContext() end'); {$ENDIF}
end;
procedure TAbstractJoiningPreconditionFilterClause.ReportNoJoinee();
begin
{$IFDEF DEBUG_SEEKER} Writeln('TAbstractJoiningPreconditionFilterClause.ReportNoJoinee() for a ' + ClassName); {$ENDIF}
Fail('You used the term "' + GetClausePrefix() + '" in a way I don''t understand.');
end;
class function TAndFromClause.IsMatch(Candidate, Condition: TThing): Boolean;
begin
Result := TFromClause.IsMatch(Candidate, Condition);
end;
function TAndFromClause.GetClausePrefix(): UTF8String;
begin
Result := 'and from';
end;
class procedure TAndFromClause.ReportFailedMatch(Tokens: TTokens; ClauseStart, CurrentToken: Cardinal; Verb: UTF8String);
begin
Fail('I don''t see any "' + Tokens[CurrentToken] + '".');
end;
class function TAndOfClause.IsMatch(Candidate, Condition: TThing): Boolean;
begin
Result := TOfClause.IsMatch(Candidate, Condition);
end;
function TAndOfClause.GetClausePrefix(): UTF8String;
begin
Result := 'and of';
end;
class procedure TAndOfClause.ReportFailedMatch(Tokens: TTokens; ClauseStart, CurrentToken: Cardinal; Verb: UTF8String);
begin
Fail('I don''t see anything of "' + Tokens[CurrentToken] + '".');
end;
class function TAndInClause.IsMatch(Candidate, Condition: TThing): Boolean;
begin
Result := TInClause.IsMatch(Candidate, Condition);
end;
function TAndInClause.GetClausePrefix(): UTF8String;
begin
Result := 'and in';
end;
class procedure TAndInClause.ReportFailedMatch(Tokens: TTokens; ClauseStart, CurrentToken: Cardinal; Verb: UTF8String);
begin
Fail('I don''t see any "' + Tokens[CurrentToken] + '" for anything to be in.');
end;
class function TAndOnClause.IsMatch(Candidate, Condition: TThing): Boolean;
begin
Result := TOnClause.IsMatch(Candidate, Condition);
end;
function TAndOnClause.GetClausePrefix(): UTF8String;
begin
Result := 'and on';
end;
class procedure TAndOnClause.ReportFailedMatch(Tokens: TTokens; ClauseStart, CurrentToken: Cardinal; Verb: UTF8String);
begin
Fail('I don''t see any "' + Tokens[CurrentToken] + '" for anything to be on.');
end;
function TJoinableClause.AcceptsJoins(Peer: TAbstractClause): Boolean;
begin
Result := False;
end;
procedure TJoinableClause.RegisterJoin(Peer: TAbstractJoiningClause);
begin
SetLength(FRegisteredJoins, Length(FRegisteredJoins)+1);
FRegisteredJoins[High(FRegisteredJoins)] := Peer;
end;
procedure TAbstractFilteringClause.CheckContext();
var
CurrentClause, SkipUntilClause: TAbstractClause;
Continue: Boolean;
begin
inherited;
{$IFDEF DEBUG_SEEKER} Writeln('TAbstractFilteringClause.CheckContext() for a ', ClassName); {$ENDIF}
CurrentClause := FPrevious;
SkipUntilClause := CurrentClause;
Continue := True;
while (Continue) do
begin
{$IFDEF DEBUG_SEEKER} Writeln('TAbstractFilteringClause.CheckContext() looking for a victim; target=', CurrentClause.ClassName); {$ENDIF}
if (CurrentClause.AcceptsFilter(Self, Continue)) then
begin
{$IFDEF DEBUG_SEEKER} Writeln('TAbstractFilteringClause.CheckContext() looking for a victim; target=', CurrentClause.ClassName, ' accepted! Continue=', Continue); {$ENDIF}
if (CurrentClause = SkipUntilClause) then
begin
{$IFDEF DEBUG_SEEKER} Writeln('TAbstractFilteringClause.CheckContext() looking for a victim; target=', CurrentClause.ClassName, ' accepted and SELECTED!'); {$ENDIF}
Victimise(CurrentClause);
end;
end;
if (SkipUntilClause = CurrentClause) then
SkipUntilClause := CurrentClause.GetPreviousOpenClause();
{$IFDEF DEBUG_SEEKER} if (Assigned(SkipUntilClause)) then Writeln('TAbstractFilteringClause.CheckContext() now skipping until ', SkipUntilClause.ClassName); {$ENDIF}
CurrentClause := CurrentClause.FPrevious;
Assert((not Continue) or (Assigned(CurrentClause)));
end;
{$IFDEF DEBUG_SEEKER} Writeln('TAbstractFilteringClause.CheckContext() loop end'); {$ENDIF}
if (not (Length(FVictims) > 0)) then
ReportNoVictims();
{$IFDEF DEBUG_SEEKER} Writeln('TAbstractFilteringClause.CheckContext() for a ', ClassName, ' checked out a-ok.'); {$ENDIF}
end;
procedure TAbstractFilteringClause.Filter(Victim: TAbstractClause);
var
VictimEnumerator: TThingList.TEnumerator;
FilterEnumerator, OldFilterEnumerator: TThingList.TEnumerator;
Done, Found: Boolean;
NextRegisteredJoin: Cardinal;
CurrentIsMatch: TIsMatchFunction;
NewFilterFragment: UTF8String;
begin
{$IFDEF DEBUG_SEEKER} Writeln('TAbstractFilteringClause.Filter() for a ', ClassName, ' with FThings=', FThings.GetDefiniteString(nil, 'and'), '; Victim=', Victim.ClassName, '; Victim.FThings=', Victim.FThings.GetDefiniteString(nil, 'and')); {$ENDIF}
VictimEnumerator := Victim.FThings.GetEnumerator();
try
while (VictimEnumerator.MoveNext()) do
begin
{$IFDEF DEBUG_SEEKER} Writeln('TInclusionFilterClause.Filter() for a ', ClassName, ': examining ', VictimEnumerator.Current.GetDefiniteName(nil)); {$ENDIF}
FilterEnumerator := FThings.GetEnumerator();
try
NextRegisteredJoin := Low(FRegisteredJoins);
CurrentIsMatch := @IsMatch;
Done := False;
Found := False;
while (not Done) do
begin
Done := not FilterEnumerator.MoveNext();
while ((Done) and (NextRegisteredJoin <= High(FRegisteredJoins))) do
begin
{ perform a little dance here to make sure the 'finally' block below can never try to free freed memory }
OldFilterEnumerator := FilterEnumerator;
FilterEnumerator := nil;
OldFilterEnumerator.Free();
FilterEnumerator := FRegisteredJoins[NextRegisteredJoin].FThings.GetEnumerator();
CurrentIsMatch := GetIsMatch(FRegisteredJoins[NextRegisteredJoin]);
Inc(NextRegisteredJoin);
Done := not FilterEnumerator.MoveNext();
end;
if ((not Done) and (CurrentIsMatch(VictimEnumerator.Current, FilterEnumerator.Current))) then
begin
Found := True;
Done := True;
end;
end;
if (Found <> KeepMatches()) then { i.e. if found and not keep matches, or if not found and keep matches }
begin
{$IFDEF DEBUG_SEEKER} Writeln('TInclusionFilterClause.Filter() for a ', ClassName, ': decided to remove ', VictimEnumerator.Current.GetDefiniteName(nil)); {$ENDIF}
VictimEnumerator.Remove();
end
else
begin
{$IFDEF DEBUG_SEEKER} Writeln('TInclusionFilterClause.Filter() for a ', ClassName, ': decided to keep ', VictimEnumerator.Current.GetDefiniteName(nil)); {$ENDIF}
end;
finally
FilterEnumerator.Free();
end;
end;
finally
VictimEnumerator.Free();
end;
if (Victim.FThings.Length = 0) then
ReportNothingLeft();
NewFilterFragment := GetFragmentAnnotation();
if (Length(FRegisteredJoins) > 0) then
for NextRegisteredJoin := High(FRegisteredJoins) downto Low(FRegisteredJoins) do
NewFilterFragment := NewFilterFragment + ' ' + FRegisteredJoins[NextRegisteredJoin].GetFragmentAnnotation();
Victim.AddFilterFragment(NewFilterFragment);
end;
function TAbstractFilteringClause.GetIsMatch(Target: TAbstractClause): TIsMatchFunction;
begin
Result := @IsMatch;
end;
procedure TAbstractFilteringClause.Victimise(Clause: TAbstractClause);
begin
{$IFDEF DEBUG_SEEKER} Writeln('TAbstractFilteringClause.Victimise() called on a ', ClassName, ' for a peer that is a ', Clause.ClassName); {$ENDIF}
SetLength(FVictims, Length(FVictims)+1);
FVictims[High(FVictims)] := Clause;
end;
function TAbstractFilteringClause.AcceptsFilter(Peer: TAbstractClause; out CanContinue: Boolean): Boolean;
begin
{$IFDEF DEBUG_SEEKER} Writeln('TAbstractFilteringClause.AcceptsFilter() called on a ', ClassName, ' for a peer that is a ', Peer.ClassName); {$ENDIF}
if (Peer is ClassType) then
begin
Result := False;
CanContinue := False;
{$IFDEF DEBUG_SEEKER} Writeln('TAbstractFilteringClause.AcceptsFilter() on a ', ClassName, ' is saying don''t continue.') {$ENDIF};
end