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