alternatives are correct rather than always backtrack
[scpubgit/DX.git] / lib / DX / Step / ResolveProposition.pm
index 231839e..d1ad465 100644 (file)
@@ -1,18 +1,16 @@
 package DX::Step::ResolveProposition;
 
-use Types::Standard qw(ArrayRef);
-use DX::Utils qw(deparse);
-use DX::Class;
-
-with 'DX::Role::Step';
+use DX::Step::EnterRecheck;
+use DX::Step::CompleteResolution;
+use DX::Step::Backtrack;
 
-has actions => (is => 'ro', isa => ArrayRef[Action], required => 1);
+use DX::Utils qw(expand_deps);
 
-has depends_on => (is => 'ro', isa => DependencyGroupList, required => 1);
+use DX::Class;
 
-has resolves => (is => 'ro', isa => Proposition);
+with 'DX::Role::Step';
 
-has alternative_step => (is => 'ro', isa => Step);
+has resolution_space => (is => 'ro', isa => ResolutionSpace);
 
 sub but_first {
   my ($self, @actions) = @_;
@@ -25,45 +23,60 @@ 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 $ns = do {
-    if (my $prop = $ss->next_proposition($new_hyp)) {
-      DX::Step::ConsiderProposition->new(
-        proposition => $prop
-      )
-    } else {
-      $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 ($self, $old_ss) = @_;
+  my $rspace = $self->resolution_space;
+  my $prop = $rspace->proposition;
+  my $res = $rspace->next_resolution;
+  my $vdeps = $res->veracity_depends_on;
+  trace resolve => [ statement => [
+    [ symbol => 'resolve' ],
+    [ block => [
+      [ statement => [
+        [ symbol => 'proposition' ],
+        @{$prop->for_deparse->[1]},
+      ] ],
+      (@{$res->actions}
+        ? [ statement => [
+            [ symbol => 'actions' ],
+            [ block => $res->actions ],
+          ] ]
+        : ()),
+      [ statement => [
+        [ symbol => 'depends_on' ],
+        [ block => [
+          map [ statement => [
+            [ symbol => (split '::', ${$_->[0]})[-1] ],
+            [ value_path => [ @{$_}[1..$#$_] ] ]
+          ] ], @{$vdeps}
+        ] ],
+      ] ],
+    ] ]
+  ] ];
+  my $ss = $old_ss->but(
+    next_step => DX::Step::CompleteResolution->new(
+      original_search_state => $old_ss,
+      resolution_space => $rspace,
+    )
   );
-}
-
-sub _apply_to_hyp {
-  my ($self, $old_hyp) = @_;
+  my $old_hyp = $old_ss->current_hypothesis;
   (my $hyp, my @recheck) = $old_hyp->with_resolution(
-    $self->resolves, $self->depends_on, $self->actions
+    $prop, $vdeps, $res->actions
   );
-  return undef unless $hyp;
-  if (@recheck) {
-    $hyp = $hyp->but_recheck_for(@recheck);
+  unless ($hyp) {
+    return $ss->but(
+      next_step
+        => $rspace->remaining_resolution_space->next_step
+    );
   }
-  return $hyp;
+  return $ss->but(current_hypothesis => $hyp) unless @recheck;
+  return $ss->but(
+    current_hypothesis => $hyp,
+    next_step => DX::Step::EnterRecheck->new(
+      proposition_list => \@recheck,
+      on_completion_step => $ss->next_step,
+      resolution_space => $rspace,
+    ),
+  );
 }
 
 1;