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( |
9 | current_hypothesis next_step propositions alternatives |
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; |
30 | while ($state and $state->next_proposition) { |
31 | $state = $state->with_one_step; |
32 | } |
33 | return undef unless $state; |
34 | trace 'search.solution.hyp' => $state->current_hypothesis; |
35 | return $self->but(current_search_state => $state); |
36 | } |
37 | |
38 | sub force_backtrack { |
39 | my ($self) = @_; |
40 | my $new_ss = $self->current_search_state->force_backtrack; |
41 | return undef unless $new_ss; |
42 | return $self->but(current_search_state => $new_ss); |
43 | } |
44 | |
45 | sub find_next_solution { |
46 | my ($self) = @_; |
47 | return undef unless my $bt = $self->force_backtrack; |
48 | return $bt->find_solution; |
49 | } |
50 | |
51 | 1; |