move resolution step construction later
[scpubgit/DX.git] / lib / DX / Step / ResolveProposition.pm
index f2d2f27..6cf99b9 100644 (file)
@@ -1,52 +1,17 @@
 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 step);
+use DX::Utils qw(expand_deps);
 
 use DX::Class;
 
 with 'DX::Role::Step';
 
-has resolves => (is => 'ro', isa => Proposition);
-
 has resolution_space => (is => 'ro', isa => ResolutionSpace);
 
-has current_resolution => (is => 'lazy', init_arg => undef, builder => sub {
-  my ($self) = @_;
-  $self->resolution_space->next_resolution;
-});
-
-has actions => (is => 'lazy', init_arg => undef, builder => sub {
-  my ($self) = @_;
-  $self->current_resolution->actions;
-});
-
-has depends_on => (is => 'lazy', init_arg => undef, builder => sub {
-  my ($self) = @_;
-  my $_expand_dep = sub {
-    my ($type, @path) = @{$_[0]};
-    my @expanded = map {
-      ref() ? @{$_->value_path or return ()} : $_
-    } @path;
-    return [ $type, @expanded ];
-  };
-  [ map $_expand_dep->($_),
-      @{$self->current_resolution->veracity_depends_on} ];
-});
-
-has alternative_step => (is => 'lazy', init_arg => undef, builder => sub {
-  my ($self) = @_;
-  my $rspace = $self->resolution_space->remaining_resolution_space;
-  return undef unless @{$rspace->members};
-  return step(
-    resolves => $self->resolves,
-    resolution_space => $rspace
-  );
-});
-
 sub but_first {
   my ($self, @actions) = @_;
   $self->but(actions => [ @actions, @{$self->actions} ]);
@@ -59,72 +24,59 @@ sub but_with_dependencies_on {
 
 sub apply_to {
   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' ],
-        @{$self->resolves->for_deparse->[1]},
+        @{$prop->for_deparse->[1]},
       ] ],
-      (@{$self->actions}
+      (@{$res->actions}
         ? [ statement => [
             [ symbol => 'actions' ],
-            [ block => [ @{$self->actions} ] ],
+            [ block => $res->actions ],
           ] ]
         : ()),
+      [ statement => [
+        [ symbol => 'depends_on' ],
+        [ block => [
+          map [ statement => [
+            [ symbol => (split '::', ${$_->[0]})[-1] ],
+            [ value_path => [ @{$_}[1..$#$_] ] ]
+          ] ], @{$vdeps}
+        ] ],
+      ] ],
     ] ]
   ] ];
-  my $ns = do {
-    if (my $prop = $old_ss->next_proposition) {
-      DX::Step::ConsiderProposition->new(
-        proposition => $prop
-      )
-    } else {
-      $old_ss->on_solution_step
-    }
-  };
-  my $ss = $old_ss->but(
-    next_step => $ns,
-    (@{$self->actions}
-      ? (adjustments_made => [
-          [ $self, $old_ss ],
-          @{$old_ss->adjustments_made}
-        ])
-      : ()
-    ),
-  );
-  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
+    $prop, $vdeps, $res->actions
   );
-  return undef unless $hyp;
-  return $self->_recheck_for(
-    $old_ss->but(current_hypothesis => $hyp),
-    @recheck
+  unless ($hyp) {
+    return $old_ss->but(
+      next_step
+        => $rspace->remaining_resolution_space->next_step
+    );
+  }
+  my $next_step = DX::Step::CompleteResolution->new(
+    original_search_state => $old_ss,
+    resolution_space => $rspace,
   );
-}
-
-sub _recheck_for {
-  my ($self, $old_ss, @recheck) = @_;
-
-  return $old_ss unless @recheck;
-
-  my $ss = $old_ss->but(
+  unless (@recheck) {
+    return $old_ss->but(next_step => $next_step, current_hypothesis => $hyp);
+  }
+  return $old_ss->but(
+    current_hypothesis => $hyp,
     next_step => DX::Step::EnterRecheck->new(
       proposition_list => \@recheck,
-      on_completion_step => $old_ss->next_step,
-      on_failure_step => DX::Step::Backtrack->new,
+      on_completion_step => $next_step,
+      resolution_space => $rspace,
     ),
   );
-
-  return $ss;
 }
 
 1;