extract recheck setup logic into its own step
[scpubgit/DX.git] / lib / DX / Step / ResolveProposition.pm
1 package DX::Step::ResolveProposition;
2
3 use DX::Step::EnterRecheck;
4
5 use Types::Standard qw(ArrayRef);
6 use DX::Utils qw(deparse);
7
8 use DX::Class;
9
10 with 'DX::Role::Step';
11
12 has actions => (is => 'ro', isa => ArrayRef[Action], required => 1);
13
14 has depends_on => (is => 'ro', isa => DependencyGroupList, required => 1);
15
16 has resolves => (is => 'ro', isa => Proposition);
17
18 has alternative_step => (is => 'ro', isa => Step);
19
20 sub but_first {
21   my ($self, @actions) = @_;
22   $self->but(actions => [ @actions, @{$self->actions} ]);
23 }
24
25 sub but_with_dependencies_on {
26   my ($self, @deps) = @_;
27   $self->but(depends_on => [ @{$self->depends_on}, @deps ]);
28 }
29
30 sub apply_to {
31   my ($self, $ss) = @_;
32   trace 'step.apply.old_hyp '.$self => $ss->current_hypothesis;
33   trace 'step.apply.actions '.$self => $self->actions;
34   my $new_ss = $self->_apply_to_ss($ss);
35   return $ss->but(next_step => DX::Step::Backtrack->new) unless $new_ss;
36   trace 'step.apply.new_hyp '.$self => $new_ss->current_hypothesis;
37   my $ns = do {
38     if (my $prop = $new_ss->next_proposition) {
39       DX::Step::ConsiderProposition->new(
40         proposition => $prop
41       )
42     } else {
43       $new_ss->on_solution_step
44     }
45   };
46   my $alt_step = $self->alternative_step;
47   return (
48     $new_ss->but(
49       next_step => $ns,
50       ($alt_step
51         ? (alternatives => [
52             [ $ss->current_hypothesis, $alt_step ],
53             @{$ss->alternatives}
54           ])
55         : ()
56       ),
57     ),
58   );
59 }
60
61 sub _apply_to_ss {
62   my ($self, $old_ss) = @_;
63   my $old_hyp = $old_ss->current_hypothesis;
64   (my $hyp, my @recheck) = $old_hyp->with_resolution(
65     $self->resolves, $self->depends_on, $self->actions
66   );
67   return undef unless $hyp;
68   return $self->_recheck_for(
69     $old_ss->but(current_hypothesis => $hyp),
70     @recheck
71   );
72 }
73
74 sub _recheck_for {
75   my ($self, $old_ss, @recheck) = @_;
76   return $old_ss unless @recheck;
77   my $ss  = $old_ss;
78   foreach my $prop (@recheck) {
79     return undef unless $ss = $self->_recheck_one($ss, $prop);
80   }
81   return $ss;
82 }
83
84 sub _recheck_one {
85   my ($self, $old_ss, $prop) = @_;
86
87   my $ss = $old_ss->but(
88     next_step => DX::Step::EnterRecheck->new(
89       proposition_list => [ $prop ],
90     ),
91   );
92
93   my $sp = DX::SearchProcess->new(
94     current_search_state => $ss,
95   );
96
97   my $sol_sp = $sp->find_solution;
98
99   unless ($sol_sp) {
100     trace 'step.recheck.fail' => 'argh';
101     return undef;
102   }
103
104   my $sol_rps = $sol_sp->current_hypothesis->resolved_propositions;
105
106   my $old_hyp = $old_ss->current_hypothesis;
107
108   my $rps = $old_hyp->resolved_propositions;
109
110   $rps = $rps->with_updated_dependencies_for(
111     $prop, $sol_rps->dependencies_for($prop)
112   );
113
114   trace 'step.recheck.done' => 'yay';
115
116   return $old_ss->but(
117     current_hypothesis => $old_hyp->but(resolved_propositions => $rps),
118   );
119 }
120
121 1;