remove alternative addition handling from search state
[scpubgit/DX.git] / lib / DX / SearchState.pm
1 package DX::SearchState;
2
3 use Types::Standard qw(Maybe);
4 use DX::Step::InvokeNextPredicate;
5 use DX::Class;
6
7 has current_hypothesis => (is => 'ro', isa => Hypothesis, required => 1);
8
9 has next_step => (is => 'ro', isa => Maybe[Step]);
10
11 has propositions => (is => 'ro', isa => PropositionSequence, required => 1);
12
13 has alternatives => (is => 'ro', isa => AlternativeList, required => 1);
14
15 sub next_proposition {
16   my ($self, $hyp) = @_;
17   $hyp ||= $self->current_hypothesis;
18   $self->propositions->members->[
19     $hyp->resolved_propositions->resolved_count
20   ];
21 }
22
23 sub new_for {
24   my ($class, $hyp, $props) = @_;
25   $class->new(
26     current_hypothesis => $hyp,
27     alternatives => [],
28     next_step => DX::Step::InvokeNextPredicate->new(
29       proposition => $props->members->[0],
30     ),
31     propositions => $props,
32   );
33 }
34
35 sub with_one_step {
36   my ($self) = @_;
37   my $hyp = $self->current_hypothesis;
38   return undef unless my $step = $self->next_step;
39   my ($new_ss) = $step->apply_to($self);
40   return $new_ss if $new_ss;
41   my ($first_alt, @rest_alt) = @{$self->alternatives};
42   return undef unless $first_alt;
43   trace 'search.backtrack.rewind_to' => $first_alt->[1];
44   return $self->but(
45     current_hypothesis => $first_alt->[0],
46     alternatives => \@rest_alt,
47     next_step => $first_alt->[1],
48   );
49 }
50
51 sub force_backtrack {
52   my ($self) = @_;
53   my ($first_alt, @rest_alt) = @{$self->alternatives};
54   return undef unless $first_alt;
55   trace 'search.backtrack.forced' => $first_alt->[0];
56   return ref($self)->new(
57     current_hypothesis => $first_alt->[0],
58     next_step => $first_alt->[1],
59     alternatives => \@rest_alt,
60     propositions => $self->propositions,
61   );
62 }
63
64 1;