Commit | Line | Data |
9d759b64 |
1 | package DX::SearchState; |
2 | |
a1aab147 |
3 | use Types::Standard qw(Maybe); |
985b43d1 |
4 | use DX::Step::Backtrack; |
e647e417 |
5 | use DX::Step::InvokeNextPredicate; |
1dcbfaf8 |
6 | use DX::Step::MarkAsSolution; |
9d759b64 |
7 | use DX::Class; |
8 | |
3e465d5d |
9 | has current_hypothesis => (is => 'ro', isa => Hypothesis, required => 1); |
9d759b64 |
10 | |
cb3a1c0c |
11 | has next_step => (is => 'ro', isa => Maybe[Step], required => 1); |
9d759b64 |
12 | |
1350f664 |
13 | has propositions => (is => 'ro', isa => PropositionSequence, required => 1); |
14 | |
3e465d5d |
15 | has alternatives => (is => 'ro', isa => AlternativeList, required => 1); |
16 | |
8cc971ec |
17 | has is_solution_state => (is => 'ro', required => 1); |
18 | |
cb3a1c0c |
19 | has on_exhaustion_step => (is => 'ro', isa => Maybe[Step], required => 1); |
6a0483ff |
20 | |
cb3a1c0c |
21 | has on_solution_step => (is => 'ro', isa => Maybe[Step], required => 1); |
1dcbfaf8 |
22 | |
f25e6894 |
23 | sub next_proposition { |
5787d20d |
24 | my ($self, $hyp) = @_; |
25 | $hyp ||= $self->current_hypothesis; |
f25e6894 |
26 | $self->propositions->members->[ |
5787d20d |
27 | $hyp->resolved_propositions->resolved_count |
f25e6894 |
28 | ]; |
29 | } |
ccf0d4fe |
30 | |
3e465d5d |
31 | sub new_for { |
1350f664 |
32 | my ($class, $hyp, $props) = @_; |
3e465d5d |
33 | $class->new( |
34 | current_hypothesis => $hyp, |
35 | alternatives => [], |
1350f664 |
36 | propositions => $props, |
75389058 |
37 | (@{$props->members} |
38 | ? ( |
39 | next_step => DX::Step::InvokeNextPredicate->new( |
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 | |
51 | sub with_one_step { |
52 | my ($self) = @_; |
e647e417 |
53 | return undef unless my $step = $self->next_step; |
c76de01d |
54 | return $step->apply_to($self); |
9d759b64 |
55 | } |
56 | |
9d759b64 |
57 | sub force_backtrack { |
58 | my ($self) = @_; |
f458fa2c |
59 | my ($first_alt, @rest_alt) = @{$self->alternatives}; |
60 | return undef unless $first_alt; |
72e5c0e0 |
61 | trace 'search.backtrack.forced' => $first_alt->[0]; |
bc7cb635 |
62 | return $self->but( |
63 | next_step => DX::Step::Backtrack->new, |
64 | )->with_one_step; |
9d759b64 |
65 | } |
66 | |
9d759b64 |
67 | 1; |