skip backtracking rspaces with no alternatives left
[scpubgit/DX.git] / lib / DX / Step / Backtrack.pm
index 9c489fa..9a92677 100644 (file)
@@ -4,16 +4,23 @@ use DX::Class;
 
 with 'DX::Role::Step';
 
+#has resolution_space => (is => 'ro', isa => ResolutionSpace, required => 1);
+
 sub apply_to {
   my ($self, $ss) = @_;
-  my ($first_alt, @rest_alt) = @{$ss->alternatives};
-  return $ss->but(next_step => $ss->on_exhaustion_step) unless $first_alt;
-  return $ss->but(
-    current_hypothesis => $first_alt->[1],
-    alternatives => \@rest_alt,
-    next_step => $first_alt->[0]->alternative_step,
-    is_solution_state => 0,
-  );
+  trace backtrack => [ statement => [ [ symbol => 'backtrack' ] ] ];
+  foreach my $adj (@{$ss->decisions_taken}) {
+    my ($rspace_was, $ss_was) = @$adj;
+    next unless @{$rspace_was->remaining_resolution_space->members};
+    trace rspace => [ statement => [
+      [ symbol => 'remaining' ],
+      @{$rspace_was->remaining_resolution_space->for_deparse->[1]}
+    ] ];
+    return $ss_was->but(
+      next_step => $rspace_was->remaining_resolution_space->next_step
+    );
+  }
+  return $ss->but(next_step => $ss->on_exhaustion_step);
 }
 
 1;