switch to preserving original step in alt list
[scpubgit/DX.git] / lib / DX / Step / ResolveProposition.pm
index 66a980d..e56b19c 100644 (file)
@@ -1,14 +1,31 @@
 package DX::Step::ResolveProposition;
 
+use DX::Step::EnterRecheck;
+use DX::Step::Backtrack;
+
 use Types::Standard qw(ArrayRef);
 use DX::Utils qw(deparse);
+
 use DX::Class;
 
 with 'DX::Role::Step';
 
 has actions => (is => 'ro', isa => ArrayRef[Action], required => 1);
 
-has depends_on => (is => 'ro', isa => DependencyGroupList, required => 1);
+#has depends_on => (is => 'ro', isa => DependencyGroupList, required => 1);
+
+has depends_on => (is => 'ro', required => 1, coerce => sub {
+  my ($deps) = @_;
+  my @exp;
+  DEP: foreach my $dep (@$deps) {
+    my ($type, @path) = @$dep;
+    push @exp, [
+      $type,
+      map { ref() ? @{$_->value_path or next DEP} : $_ } @path
+    ];
+  }
+  \@exp
+});
 
 has resolves => (is => 'ro', isa => Proposition);
 
@@ -25,34 +42,30 @@ sub but_with_dependencies_on {
 }
 
 sub apply_to {
-  my ($self, $ss) = @_;
-  trace 'step.apply.old_hyp '.$self => $ss->current_hypothesis;
-  trace 'step.apply.actions '.$self => $self->actions;
-  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 ($self, $old_ss) = @_;
   my $ns = do {
-    if (my $prop = $new_ss->next_proposition) {
+    if (my $prop = $old_ss->next_proposition) {
       DX::Step::ConsiderProposition->new(
         proposition => $prop
       )
     } else {
-      $new_ss->on_solution_step
+      $old_ss->on_solution_step
     }
   };
   my $alt_step = $self->alternative_step;
-  return (
-    $new_ss->but(
-      next_step => $ns,
-      ($alt_step
-        ? (alternatives => [
-            [ $ss->current_hypothesis, $alt_step ],
-            @{$ss->alternatives}
-          ])
-        : ()
-      ),
+  my $ss = $old_ss->but(
+    next_step => $ns,
+    ($alt_step
+      ? (alternatives => [
+          [ $self, $old_ss->current_hypothesis ],
+          @{$old_ss->alternatives}
+        ])
+      : ()
     ),
   );
+  my $new_ss = $self->_apply_to_ss($ss);
+  return $ss->but(next_step => DX::Step::Backtrack->new) unless $new_ss;
+  return $new_ss;
 }
 
 sub _apply_to_ss {
@@ -70,79 +83,18 @@ sub _apply_to_ss {
 
 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,
-    next_policy => $old_hyp->action_policy,
-  );
-
-  # we should probably be doing something about pruning the scope
-  # but that's completely pointless until we have rules
 
-  my $hyp = ref($old_hyp)->new(
-    scope => $old_hyp->scope,
-    resolved_propositions => DX::ResolvedPropositionSet->new_empty,
-    actions => [],
-    action_applications => [],
-    action_policy => $ap,
-  );
-
-  my $pseq = DX::PropositionSequence->new(
-    members => [ $prop ],
-    external_names => {},
-    internal_names => {},
-  );
-
-  trace 'step.recheck.hyp' => $hyp;
-
-  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_sp = $sp->find_solution;
-
-  unless ($sol_sp) {
-    trace 'step.recheck.fail' => 'argh';
-    return undef;
-  }
-
-  my $sol_rps = $sol_sp->current_hypothesis->resolved_propositions;
-
-  my $rps = $old_hyp->resolved_propositions;
+  return $old_ss unless @recheck;
 
-  $rps = $rps->with_updated_dependencies_for(
-    $prop, $sol_rps->dependencies_for($prop)
+  my $ss = $old_ss->but(
+    next_step => DX::Step::EnterRecheck->new(
+      proposition_list => \@recheck,
+      on_completion_step => $old_ss->next_step,
+      on_failure_step => DX::Step::Backtrack->new,
+    ),
   );
 
-  trace 'step.recheck.done' => 'yay';
-
-  return $old_ss->but(
-    current_hypothesis => $old_hyp->but(resolved_propositions => $rps),
-  );
+  return $ss;
 }
 
 1;