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