add exhaustionstep and resumesearch concepts
[scpubgit/DX.git] / lib / DX / SearchState.pm
CommitLineData
9d759b64 1package DX::SearchState;
2
985b43d1 3use DX::Step::Backtrack;
86dbedb6 4use DX::Step::ConsiderProposition;
1dcbfaf8 5use DX::Step::MarkAsSolution;
9d759b64 6use DX::Class;
7
3e465d5d 8has current_hypothesis => (is => 'ro', isa => Hypothesis, required => 1);
9d759b64 9
cb3a1c0c 10has next_step => (is => 'ro', isa => Maybe[Step], required => 1);
9d759b64 11
1350f664 12has propositions => (is => 'ro', isa => PropositionSequence, required => 1);
13
1c02730b 14has decisions_taken => (is => 'ro', isa => DecisionList, required => 1);
3e465d5d 15
80d78e1b 16has is_solution_state => (is => 'ro', isa => Bool, required => 1);
8cc971ec 17
ae6f4d03 18has is_exhaustion_state => (is => 'ro', isa => Bool, required => 1);
19
cb3a1c0c 20has on_exhaustion_step => (is => 'ro', isa => Maybe[Step], required => 1);
6a0483ff 21
cb3a1c0c 22has on_solution_step => (is => 'ro', isa => Maybe[Step], required => 1);
1dcbfaf8 23
f25e6894 24sub next_proposition {
7af7ed1e 25 my ($self) = @_;
26 my $hyp = $self->current_hypothesis;
f25e6894 27 $self->propositions->members->[
7af7ed1e 28 $hyp->resolved_propositions->resolved_count + 1
f25e6894 29 ];
30}
ccf0d4fe 31
3e465d5d 32sub new_for {
1350f664 33 my ($class, $hyp, $props) = @_;
3e465d5d 34 $class->new(
35 current_hypothesis => $hyp,
1c02730b 36 decisions_taken => [],
1350f664 37 propositions => $props,
75389058 38 (@{$props->members}
39 ? (
86dbedb6 40 next_step => DX::Step::ConsiderProposition->new(
75389058 41 proposition => $props->members->[0],
42 ),
43 is_solution_state => 0,
44 )
ae6f4d03 45 : ( next_step => DX::Step::MarkAsExhaustion->new, is_solution_state => 1 )
75389058 46 ),
ae6f4d03 47 is_exhaustion_state => 0,
48 on_exhaustion_step => DX::Step::MarkAsExhaustion->new,
1dcbfaf8 49 on_solution_step => DX::Step::MarkAsSolution->new,
3e465d5d 50 );
51}
9d759b64 52
53sub with_one_step {
54 my ($self) = @_;
e647e417 55 return undef unless my $step = $self->next_step;
5b6cab1b 56 #trace step => $step;
c76de01d 57 return $step->apply_to($self);
9d759b64 58}
59
9d759b64 60sub force_backtrack {
61 my ($self) = @_;
bc7cb635 62 return $self->but(
63 next_step => DX::Step::Backtrack->new,
64 )->with_one_step;
9d759b64 65}
66
9d759b64 671;