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