10c4eb94636b31e15e4f53d545e28ea03a67a9a5
[scpubgit/DX.git] / lib / DX / Step / Normal.pm
1 package DX::Step::Normal;
2
3 use Types::Standard qw(ArrayRef);
4 use DX::Utils qw(deparse);
5 use DX::Class;
6
7 with 'DX::Role::Step';
8
9 has actions => (is => 'ro', isa => ArrayRef[Action], required => 1);
10
11 has depends_on => (is => 'ro', isa => DependencyGroupList, required => 1);
12
13 has resolves => (is => 'ro', isa => Proposition);
14
15 has alternative_step => (is => 'ro', isa => Step);
16
17 sub but_first {
18   my ($self, @actions) = @_;
19   $self->but(actions => [ @actions, @{$self->actions} ]);
20 }
21
22 sub but_with_dependencies_on {
23   my ($self, @deps) = @_;
24   $self->but(depends_on => [ @{$self->depends_on}, @deps ]);
25 }
26
27 sub apply_to {
28   my ($self, $ss) = @_;
29   my $old_hyp = $ss->current_hypothesis;
30   trace 'step.apply.old_hyp '.$self => $old_hyp;
31   trace 'step.apply.actions '.$self => $self->actions;
32   my $new_hyp = $self->_apply_to_hyp($old_hyp);
33   return (undef, $self->alternative_step) unless $new_hyp;
34   trace 'step.apply.new_hyp '.$self => $new_hyp;
35   my $ns = DX::Step::InvokeNextPredicate->new(
36     proposition => $ss->next_proposition($new_hyp)
37   );
38   return (
39     $ss->but(
40       current_hypothesis => $new_hyp,
41       next_step => $ns,
42     ),
43     $self->alternative_step
44   );
45 }
46
47 sub _apply_to_hyp {
48   my ($self, $old_hyp) = @_;
49   return undef unless my $hyp = $old_hyp->with_actions(@{$self->actions});
50   return $hyp->with_resolution($self->resolves, $self->depends_on);
51 }
52
53 1;