cb50a2f78d4bfe59565e516dff33ebd463fb827d
[scpubgit/DX.git] / lib / DX / SearchProcess.pm
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(
16     current_search_state => DX::SearchState->new_for($hyp, $props),
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 (not $state->is_solution_state)) {
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;