add alternatives to search state inside normal step
[scpubgit/DX.git] / lib / DX / Step / Normal.pm
CommitLineData
9d759b64 1package DX::Step::Normal;
2
3e465d5d 3use Types::Standard qw(ArrayRef);
4016201b 4use DX::Utils qw(deparse);
9d759b64 5use DX::Class;
6
3e465d5d 7with 'DX::Role::Step';
9d759b64 8
3e465d5d 9has actions => (is => 'ro', isa => ArrayRef[Action], required => 1);
9d759b64 10
3e465d5d 11has depends_on => (is => 'ro', isa => DependencyGroupList, required => 1);
12
ccf0d4fe 13has resolves => (is => 'ro', isa => Proposition);
14
3e465d5d 15has alternative_step => (is => 'ro', isa => Step);
9d759b64 16
4aeeab1e 17sub but_first {
18 my ($self, @actions) = @_;
19 $self->but(actions => [ @actions, @{$self->actions} ]);
20}
21
0498469a 22sub but_with_dependencies_on {
23 my ($self, @deps) = @_;
24 $self->but(depends_on => [ @{$self->depends_on}, @deps ]);
25}
26
9d759b64 27sub apply_to {
110fd002 28 my ($self, $ss) = @_;
29 my $old_hyp = $ss->current_hypothesis;
bcee3a69 30 trace 'step.apply.old_hyp '.$self => $old_hyp;
173a11ea 31 trace 'step.apply.actions '.$self => $self->actions;
bcee3a69 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;
f696251f 35 my $ns = DX::Step::InvokeNextPredicate->new(
36 proposition => $ss->next_proposition($new_hyp)
37 );
96e5344d 38 my $alt_step = $self->alternative_step;
f696251f 39 return (
40 $ss->but(
41 current_hypothesis => $new_hyp,
42 next_step => $ns,
96e5344d 43 ($alt_step
44 ? (alternatives => [ [ $old_hyp, $alt_step ], @{$ss->alternatives} ])
45 : ()
46 ),
f696251f 47 ),
f696251f 48 );
9d759b64 49}
50
51sub _apply_to_hyp {
52 my ($self, $old_hyp) = @_;
53 return undef unless my $hyp = $old_hyp->with_actions(@{$self->actions});
5787d20d 54 return $hyp->with_resolution($self->resolves, $self->depends_on);
9d759b64 55}
56
571;