cleanup resolveprop apply_to logic a bit
[scpubgit/DX.git] / lib / DX / Step / ResolveProposition.pm
index 09064c7..66a980d 100644 (file)
@@ -26,46 +26,62 @@ sub but_with_dependencies_on {
 
 sub apply_to {
   my ($self, $ss) = @_;
-  my $old_hyp = $ss->current_hypothesis;
-  trace 'step.apply.old_hyp '.$self => $old_hyp;
+  trace 'step.apply.old_hyp '.$self => $ss->current_hypothesis;
   trace 'step.apply.actions '.$self => $self->actions;
-  my $new_hyp = $self->_apply_to_hyp($old_hyp);
-  return $ss->but(next_step => DX::Step::Backtrack->new) unless $new_hyp;
-  trace 'step.apply.new_hyp '.$self => $new_hyp;
+  my $new_ss = $self->_apply_to_ss($ss);
+  return $ss->but(next_step => DX::Step::Backtrack->new) unless $new_ss;
+  trace 'step.apply.new_hyp '.$self => $new_ss->current_hypothesis;
   my $ns = do {
-    if (my $prop = $ss->next_proposition($new_hyp)) {
+    if (my $prop = $new_ss->next_proposition) {
       DX::Step::ConsiderProposition->new(
         proposition => $prop
       )
     } else {
-      $ss->on_solution_step
+      $new_ss->on_solution_step
     }
   };
   my $alt_step = $self->alternative_step;
   return (
-    $ss->but(
-      current_hypothesis => $new_hyp,
+    $new_ss->but(
       next_step => $ns,
       ($alt_step
-        ? (alternatives => [ [ $old_hyp, $alt_step ], @{$ss->alternatives} ])
+        ? (alternatives => [
+            [ $ss->current_hypothesis, $alt_step ],
+            @{$ss->alternatives}
+          ])
         : ()
       ),
     ),
   );
 }
 
-sub _apply_to_hyp {
-  my ($self, $old_hyp) = @_;
+sub _apply_to_ss {
+  my ($self, $old_ss) = @_;
+  my $old_hyp = $old_ss->current_hypothesis;
   (my $hyp, my @recheck) = $old_hyp->with_resolution(
     $self->resolves, $self->depends_on, $self->actions
   );
-  return $self->_recheck_hyp_for($hyp, @recheck);
+  return undef unless $hyp;
+  return $self->_recheck_for(
+    $old_ss->but(current_hypothesis => $hyp),
+    @recheck
+  );
 }
 
-sub _recheck_hyp_for {
-  my ($self, $old_hyp, @recheck) = @_;
-  return undef unless $old_hyp;
-  return $old_hyp unless @recheck;
+sub _recheck_for {
+  my ($self, $old_ss, @recheck) = @_;
+  return $old_ss unless @recheck;
+  my $ss  = $old_ss;
+  foreach my $prop (@recheck) {
+    return undef unless $ss = $self->_recheck_one($ss, $prop);
+  }
+  return $ss;
+}
+
+sub _recheck_one {
+  my ($self, $old_ss, $prop) = @_;
+
+  my $old_hyp = $old_ss->current_hypothesis;
 
   my $ap = DX::ActionPolicy::LockScope->new(
     lock_to_depth => $old_hyp->scope->depth,
@@ -84,33 +100,49 @@ sub _recheck_hyp_for {
   );
 
   my $pseq = DX::PropositionSequence->new(
-    members => \@recheck,
+    members => [ $prop ],
     external_names => {},
     internal_names => {},
   );
 
   trace 'step.recheck.hyp' => $hyp;
 
-  my $ss = DX::SearchProcess->new_for($hyp, $pseq);
+  my $ss = DX::SearchState->new(
+    current_hypothesis => $hyp,
+    alternatives => [],
+    propositions => $pseq,
+    next_step => DX::Step::ConsiderProposition->new(
+                   proposition => $prop,
+                 ),
+    is_solution_state => 0,
+    on_exhaustion_step => undef,
+    on_solution_step => DX::Step::MarkAsSolution->new,
+  );
+
+  my $sp = DX::SearchProcess->new(
+    current_search_state => $ss,
+  );
 
-  my $sol_ss = $ss->find_solution;
+  my $sol_sp = $sp->find_solution;
 
-  unless ($sol_ss) {
+  unless ($sol_sp) {
     trace 'step.recheck.fail' => 'argh';
     return undef;
   }
 
-  my $sol_rps = $sol_ss->current_hypothesis->resolved_propositions;
+  my $sol_rps = $sol_sp->current_hypothesis->resolved_propositions;
 
   my $rps = $old_hyp->resolved_propositions;
 
   $rps = $rps->with_updated_dependencies_for(
-    $_, $sol_rps->dependencies_for($_)
-  ) for @recheck;
+    $prop, $sol_rps->dependencies_for($prop)
+  );
 
   trace 'step.recheck.done' => 'yay';
 
-  return $old_hyp->but(resolved_propositions => $rps);
+  return $old_ss->but(
+    current_hypothesis => $old_hyp->but(resolved_propositions => $rps),
+  );
 }
 
 1;