rename InvokeNextPredicate to ConsiderPropositon
[scpubgit/DX.git] / lib / DX / Step / Normal.pm
index 11890ca..cff9543 100644 (file)
@@ -1,6 +1,7 @@
 package DX::Step::Normal;
 
 use Types::Standard qw(ArrayRef);
+use DX::Utils qw(deparse);
 use DX::Class;
 
 with 'DX::Role::Step';
@@ -9,28 +10,54 @@ has actions => (is => 'ro', isa => ArrayRef[Action], required => 1);
 
 has depends_on => (is => 'ro', isa => DependencyGroupList, required => 1);
 
+has resolves => (is => 'ro', isa => Proposition);
+
 has alternative_step => (is => 'ro', isa => Step);
 
+sub but_first {
+  my ($self, @actions) = @_;
+  $self->but(actions => [ @actions, @{$self->actions} ]);
+}
+
 sub but_with_dependencies_on {
   my ($self, @deps) = @_;
   $self->but(depends_on => [ @{$self->depends_on}, @deps ]);
 }
 
-sub but_with_alternative_step {
-  my ($self, $step) = @_;
-  bless { %$self, alternative_step => $step }, ref($self);
-}
-
 sub apply_to {
-  my ($self, $old_hyp) = @_;
-#::Dwarn($self->depends_on);
-  return ($self->_apply_to_hyp($old_hyp), $self->alternative_step);
+  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} ])
+        : ()
+      ),
+    ),
+  );
 }
 
 sub _apply_to_hyp {
   my ($self, $old_hyp) = @_;
   return undef unless my $hyp = $old_hyp->with_actions(@{$self->actions});
-  return $hyp->resolve_head_dependent_on($self->depends_on);
+  return $hyp->with_resolution($self->resolves, $self->depends_on);
 }
 
 1;