follow alternative rspace entries without backtracking, explicitly resolve
[scpubgit/DX.git] / lib / DX / Step / ResolveProposition.pm
index ee5ff14..15ecf86 100644 (file)
@@ -1,21 +1,24 @@
 package DX::Step::ResolveProposition;
 
 use DX::Step::EnterRecheck;
+use DX::Step::CompleteResolution;
+use DX::Step::Backtrack;
 
-use Types::Standard qw(ArrayRef);
-use DX::Utils qw(deparse);
+use DX::Utils qw(expand_deps);
 
 use DX::Class;
 
 with 'DX::Role::Step';
 
-has actions => (is => 'ro', isa => ArrayRef[Action], required => 1);
+has resolution_space => (is => 'ro', isa => ResolutionSpace);
 
-has depends_on => (is => 'ro', isa => DependencyGroupList, required => 1);
+sub resolves { shift->resolution_space->proposition }
 
-has resolves => (is => 'ro', isa => Proposition);
+sub current_resolution { shift->resolution_space->next_resolution }
 
-has alternative_step => (is => 'ro', isa => Step);
+sub actions { shift->current_resolution->actions }
+
+sub depends_on { shift->current_resolution->veracity_depends_on }
 
 sub but_first {
   my ($self, @actions) = @_;
@@ -28,81 +31,52 @@ 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 $ns = do {
-    if (my $prop = $new_ss->next_proposition) {
-      DX::Step::ConsiderProposition->new(
-        proposition => $prop
-      )
-    } else {
-      $new_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}
-          ])
-        : ()
-      ),
-    ),
-  );
-}
-
-sub _apply_to_ss {
   my ($self, $old_ss) = @_;
+  trace resolve => [ statement => [
+    [ symbol => 'resolve' ],
+    [ block => [
+      [ statement => [
+        [ symbol => 'proposition' ],
+        @{$self->resolves->for_deparse->[1]},
+      ] ],
+      (@{$self->actions}
+        ? [ statement => [
+            [ symbol => 'actions' ],
+            [ block => $self->actions ],
+          ] ]
+        : ()),
+      [ statement => [
+        [ symbol => 'depends_on' ],
+        [ block => [
+          map [ statement => [
+            [ symbol => (split '::', ${$_->[0]})[-1] ],
+            [ value_path => [ @{$_}[1..$#$_] ] ]
+          ] ], @{$self->depends_on}
+        ] ],
+      ] ],
+    ] ]
+  ] ];
+  my $rspace = $self->resolution_space;
+  my $ss = $old_ss->but(
+    next_step => DX::Step::CompleteResolution->new(
+      original_search_state => $old_ss,
+      resolution_space => $rspace,
+    )
+  );
   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 _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 $ss = $old_ss->but(
+  return $ss->but(next_step => DX::Step::Backtrack->new) unless $hyp;
+  return $ss->but(current_hypothesis => $hyp) unless @recheck;
+  return $ss->but(
+    current_hypothesis => $hyp,
     next_step => DX::Step::EnterRecheck->new(
-      proposition_list => [ $prop ],
-      on_completion_step => DX::Step::MarkAsSolution->new,
+      proposition_list => \@recheck,
+      on_completion_step => $ss->next_step,
+      on_failure_step => $rspace->remaining_resolution_space->next_step,
     ),
   );
-
-  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;
-  }
-
-  return $sol_sp->current_search_state->but(is_solution_state => 0);
 }
 
 1;