remove unused variable
[scpubgit/DX.git] / lib / DX / Step / ResolveProposition.pm
1 package DX::Step::ResolveProposition;
2
3 use DX::Step::EnterRecheck;
4 use DX::Step::Backtrack;
5
6 use Types::Standard qw(ArrayRef);
7 use DX::Utils qw(deparse);
8
9 use DX::Class;
10
11 with 'DX::Role::Step';
12
13 has actions => (is => 'ro', isa => ArrayRef[Action], required => 1);
14
15 has depends_on => (is => 'ro', isa => ArrayRef[DependencySpec], required => 1);
16
17 has resolves => (is => 'ro', isa => Proposition);
18
19 has alternative_step => (is => 'ro', isa => Step);
20
21 sub but_first {
22   my ($self, @actions) = @_;
23   $self->but(actions => [ @actions, @{$self->actions} ]);
24 }
25
26 sub but_with_dependencies_on {
27   my ($self, @deps) = @_;
28   $self->but(depends_on => [ @{$self->depends_on}, @deps ]);
29 }
30
31 sub apply_to {
32   my ($self, $old_ss) = @_;
33   my $ns = do {
34     if (my $prop = $old_ss->next_proposition) {
35       DX::Step::ConsiderProposition->new(
36         proposition => $prop
37       )
38     } else {
39       $old_ss->on_solution_step
40     }
41   };
42   my $ss = $old_ss->but(
43     next_step => $ns,
44     (@{$self->actions}
45       ? (adjustments_made => [
46           [ $self, $old_ss ],
47           @{$old_ss->adjustments_made}
48         ])
49       : ()
50     ),
51   );
52   my $new_ss = $self->_apply_to_ss($ss);
53   return $ss->but(next_step => DX::Step::Backtrack->new) unless $new_ss;
54   return $new_ss;
55 }
56
57 sub _apply_to_ss {
58   my ($self, $old_ss) = @_;
59   my $old_hyp = $old_ss->current_hypothesis;
60   (my $hyp, my @recheck) = $old_hyp->with_resolution(
61     $self->resolves, $self->depends_on, $self->actions
62   );
63   return undef unless $hyp;
64   return $self->_recheck_for(
65     $old_ss->but(current_hypothesis => $hyp),
66     @recheck
67   );
68 }
69
70 sub _recheck_for {
71   my ($self, $old_ss, @recheck) = @_;
72
73   return $old_ss unless @recheck;
74
75   my $ss = $old_ss->but(
76     next_step => DX::Step::EnterRecheck->new(
77       proposition_list => \@recheck,
78       on_completion_step => $old_ss->next_step,
79       on_failure_step => DX::Step::Backtrack->new,
80     ),
81   );
82
83   return $ss;
84 }
85
86 1;