switch to preserving original step in alt list
[scpubgit/DX.git] / lib / DX / Step / ResolveProposition.pm
index 1a00d22..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,39 +42,59 @@ 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.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 ($self, $old_ss) = @_;
   my $ns = do {
-    if (my $prop = $ss->next_proposition($new_hyp)) {
+    if (my $prop = $old_ss->next_proposition) {
       DX::Step::ConsiderProposition->new(
         proposition => $prop
       )
     } else {
-      $ss->on_solution_step
+      $old_ss->on_solution_step
     }
   };
   my $alt_step = $self->alternative_step;
-  return (
-    $ss->but(
-      current_hypothesis => $new_hyp,
-      next_step => $ns,
-      ($alt_step
-        ? (alternatives => [ [ $old_hyp, $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 {
+  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 undef unless $hyp;
+  return $self->_recheck_for(
+    $old_ss->but(current_hypothesis => $hyp),
+    @recheck
+  );
 }
 
-sub _apply_to_hyp {
-  my ($self, $old_hyp) = @_;
-  return undef unless my $hyp = $old_hyp->with_actions(@{$self->actions});
-  return $hyp->with_resolution($self->resolves, $self->depends_on);
+sub _recheck_for {
+  my ($self, $old_ss, @recheck) = @_;
+
+  return $old_ss unless @recheck;
+
+  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,
+    ),
+  );
+
+  return $ss;
 }
 
 1;