From: Matt S Trout Date: Sat, 4 Jun 2016 20:39:21 +0000 (+0000) Subject: initial creation of SearchProcess class X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=8c16d3c961b5b2d35fd7894cb9ab935fb28b2983;p=scpubgit%2FDX.git initial creation of SearchProcess class --- diff --git a/lib/DX/Hypothesis.pm b/lib/DX/Hypothesis.pm index 3eaf7cd..db9364f 100644 --- a/lib/DX/Hypothesis.pm +++ b/lib/DX/Hypothesis.pm @@ -64,7 +64,7 @@ sub but_recheck_for { trace 'step.recheck.hyp' => $hyp; - my $ss = DX::SearchState->new_for($hyp, $pseq); + my $ss = DX::SearchProcess->new_for($hyp, $pseq); my $sol_ss = $ss->find_solution; diff --git a/lib/DX/QueryState.pm b/lib/DX/QueryState.pm index a873991..ac4039b 100644 --- a/lib/DX/QueryState.pm +++ b/lib/DX/QueryState.pm @@ -3,7 +3,7 @@ package DX::QueryState; use Types::Standard qw(HashRef); use DX::Scope; use DX::Hypothesis; -use DX::SearchState; +use DX::SearchProcess; use DX::ResolvedPropositionSet; use DX::Value::Unset; use DX::ActionBuilder::UnsetValue; @@ -54,7 +54,7 @@ sub new_search_state_for { action_applications => [], action_policy => DX::ActionPolicy::Allow->new, ); - return DX::SearchState->new_for($hyp, $prop_seq); + return DX::SearchProcess->new_for($hyp, $prop_seq); } sub with_additional_proposition { diff --git a/lib/DX/SearchProcess.pm b/lib/DX/SearchProcess.pm new file mode 100644 index 0000000..10248ab --- /dev/null +++ b/lib/DX/SearchProcess.pm @@ -0,0 +1,58 @@ +package DX::SearchProcess; + +use DX::SearchState; +use DX::Class; + +has current_search_state => ( + is => 'ro', isa => SearchState, required => 1, + handles => [ qw( + current_hypothesis next_step propositions alternatives + ) ], +); + +sub new_for { + my ($class, $hyp, $props) = @_; + $class->new( + current_search_state => DX::SearchState->new( + current_hypothesis => $hyp, + alternatives => [], + next_step => DX::Step::InvokeNextPredicate->new( + proposition => $props->members->[0], + ), + propositions => $props, + ), + ); +} + +sub with_one_step { + my ($self) = @_; + my $new_ss = $self->current_search_state->with_one_step; + return undef unless $new_ss; + return $self->but(current_search_state => $new_ss); +} + +sub find_solution { + my ($self) = @_; + my $state = $self->current_search_state; + while ($state and $state->next_proposition) { + $state = $state->with_one_step; + } + return undef unless $state; + trace 'search.solution.hyp' => $state->current_hypothesis; + return $self->but(current_search_state => $state); +} + +sub force_backtrack { + my ($self) = @_; + my $new_ss = $self->current_search_state->force_backtrack; + return undef unless $new_ss; + return $self->but(current_search_state => $new_ss); +} + +sub find_next_solution { + my ($self) = @_; + return undef unless my $bt = $self->force_backtrack; + return $bt->find_solution; +} + +1; diff --git a/lib/DX/SearchState.pm b/lib/DX/SearchState.pm index 8ed1d87..d29dc09 100644 --- a/lib/DX/SearchState.pm +++ b/lib/DX/SearchState.pm @@ -64,15 +64,6 @@ sub with_one_step { ); } -sub find_solution { - my $state = $_[0]; - while ($state and $state->next_proposition) { - $state = $state->with_one_step; - } - trace 'search.solution.hyp' => $state->current_hypothesis if $state; - return $state; -} - sub force_backtrack { my ($self) = @_; my ($first_alt, @rest_alt) = @{$self->alternatives}; @@ -86,10 +77,4 @@ sub force_backtrack { ); } -sub find_next_solution { - my ($self) = @_; - return undef unless my $bt = $self->force_backtrack; - return $bt->find_solution; -} - 1; diff --git a/lib/DX/Types.pm b/lib/DX/Types.pm index b3c04b2..62fb557 100644 --- a/lib/DX/Types.pm +++ b/lib/DX/Types.pm @@ -6,7 +6,7 @@ use Type::Library -declare => ( (our @CLASSES = qw( Hypothesis Scope ResolvedPropositionSet Proposition DependencyMap - PropositionSequence QueryState ShellState ShellSession + PropositionSequence QueryState ShellState ShellSession SearchState )), (our @ROLES = qw( Step Action ActionPolicy Predicate Value