inline _apply_to in ResolveProposition to clarify logic
[scpubgit/DX.git] / lib / DX / Step / ResolveProposition.pm
index 6292ba5..cc38d4b 100644 (file)
@@ -3,38 +3,21 @@ package DX::Step::ResolveProposition;
 use DX::Step::EnterRecheck;
 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 depends_on => (is => 'ro', isa => DependencyGroupList, required => 1);
-
-has depends_on => (is => 'ro', required => 1, coerce => sub {
-  my ($dep_groups) = @_;
-  my @exp;
-  assert_DependencyGroupList $dep_groups;
-  [ map {
-    my ($on, @deps) = @$_;
-    my @exp;
-    DEP: foreach my $dep (@deps) {
-      my ($type, @path) = @$dep;
-      push @exp, [
-        $type,
-        map { ref() ? @{$_->value_path or next DEP} : $_ } @path
-      ];
-    }
-    (@exp ? [ $on, @exp ] : ());
-  } @$dep_groups ];
-});
+has resolution_space => (is => 'ro', isa => ResolutionSpace);
+
+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) = @_;
@@ -48,6 +31,30 @@ sub but_with_dependencies_on {
 
 sub apply_to {
   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 $ns = do {
     if (my $prop = $old_ss->next_proposition) {
       DX::Step::ConsiderProposition->new(
@@ -57,31 +64,20 @@ sub apply_to {
       $old_ss->on_solution_step
     }
   };
-  my $alt_step = $self->alternative_step;
   my $ss = $old_ss->but(
     next_step => $ns,
-    ($alt_step
-      ? (alternatives => [
-          [ $old_ss->current_hypothesis, $alt_step ],
-          @{$old_ss->alternatives}
-        ])
-      : ()
-    ),
+    decisions_taken => [
+      [ $self->resolution_space, $old_ss ],
+      @{$old_ss->decisions_taken}
+    ],
   );
-  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 $ss->but(next_step => DX::Step::Backtrack->new) unless $hyp;
   return $self->_recheck_for(
-    $old_ss->but(current_hypothesis => $hyp),
+    $ss->but(current_hypothesis => $hyp),
     @recheck
   );
 }