Commit | Line | Data |
8c16d3c9 |
1 | package DX::SearchProcess; |
2 | |
3 | use DX::SearchState; |
4 | use DX::Class; |
5 | |
6 | has current_search_state => ( |
7 | is => 'ro', isa => SearchState, required => 1, |
8 | handles => [ qw( |
df377b33 |
9 | current_hypothesis next_step propositions adjustments_made |
8c16d3c9 |
10 | ) ], |
11 | ); |
12 | |
13 | sub new_for { |
14 | my ($class, $hyp, $props) = @_; |
15 | $class->new( |
8cc971ec |
16 | current_search_state => DX::SearchState->new_for($hyp, $props), |
8c16d3c9 |
17 | ); |
18 | } |
19 | |
20 | sub with_one_step { |
21 | my ($self) = @_; |
22 | my $new_ss = $self->current_search_state->with_one_step; |
23 | return undef unless $new_ss; |
24 | return $self->but(current_search_state => $new_ss); |
25 | } |
26 | |
27 | sub find_solution { |
28 | my ($self) = @_; |
29 | my $state = $self->current_search_state; |
282b1d76 |
30 | while ($state and (not $state->is_solution_state)) { |
8c16d3c9 |
31 | $state = $state->with_one_step; |
32 | } |
33 | return undef unless $state; |
8c16d3c9 |
34 | return $self->but(current_search_state => $state); |
35 | } |
36 | |
37 | sub force_backtrack { |
38 | my ($self) = @_; |
39 | my $new_ss = $self->current_search_state->force_backtrack; |
76329453 |
40 | # XXX infinite loop without the next line and I'm unsure why we don't |
41 | # get a loop-ending undef from elsewhere if the backtrack failed |
42 | return undef unless $new_ss->next_step; |
8c16d3c9 |
43 | return $self->but(current_search_state => $new_ss); |
44 | } |
45 | |
46 | sub find_next_solution { |
47 | my ($self) = @_; |
48 | return undef unless my $bt = $self->force_backtrack; |
49 | return $bt->find_solution; |
50 | } |
51 | |
52 | 1; |