move recheck logic into resolution step
[scpubgit/DX.git] / lib / DX / Step / ResolveProposition.pm
CommitLineData
a3104d41 1package DX::Step::ResolveProposition;
9d759b64 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);
c76de01d 33 return $ss->but(next_step => DX::Step::Backtrack->new) unless $new_hyp;
bcee3a69 34 trace 'step.apply.new_hyp '.$self => $new_hyp;
1dcbfaf8 35 my $ns = do {
36 if (my $prop = $ss->next_proposition($new_hyp)) {
86dbedb6 37 DX::Step::ConsiderProposition->new(
1dcbfaf8 38 proposition => $prop
39 )
40 } else {
41 $ss->on_solution_step
42 }
43 };
96e5344d 44 my $alt_step = $self->alternative_step;
f696251f 45 return (
46 $ss->but(
47 current_hypothesis => $new_hyp,
48 next_step => $ns,
96e5344d 49 ($alt_step
50 ? (alternatives => [ [ $old_hyp, $alt_step ], @{$ss->alternatives} ])
51 : ()
52 ),
f696251f 53 ),
f696251f 54 );
9d759b64 55}
56
57sub _apply_to_hyp {
58 my ($self, $old_hyp) = @_;
77065529 59 (my $hyp, my @recheck) = $old_hyp->with_resolution(
cdca8723 60 $self->resolves, $self->depends_on, $self->actions
61 );
04844099 62 return $self->_recheck_hyp_for($hyp, @recheck);
63}
64
65sub _recheck_hyp_for {
66 my ($self, $old_hyp, @recheck) = @_;
67 return undef unless $old_hyp;
68 return $old_hyp unless @recheck;
69
70 my $ap = DX::ActionPolicy::LockScope->new(
71 lock_to_depth => $old_hyp->scope->depth,
72 next_policy => $old_hyp->action_policy,
73 );
74
75 # we should probably be doing something about pruning the scope
76 # but that's completely pointless until we have rules
77
78 my $hyp = ref($old_hyp)->new(
79 scope => $old_hyp->scope,
80 resolved_propositions => DX::ResolvedPropositionSet->new_empty,
81 actions => [],
82 action_applications => [],
83 action_policy => $ap,
84 );
85
86 my $pseq = DX::PropositionSequence->new(
87 members => \@recheck,
88 external_names => {},
89 internal_names => {},
90 );
91
92 trace 'step.recheck.hyp' => $hyp;
93
94 my $ss = DX::SearchProcess->new_for($hyp, $pseq);
95
96 my $sol_ss = $ss->find_solution;
97
98 unless ($sol_ss) {
99 trace 'step.recheck.fail' => 'argh';
100 return undef;
77065529 101 }
04844099 102
103 my $sol_rps = $sol_ss->current_hypothesis->resolved_propositions;
104
105 my $rps = $old_hyp->resolved_propositions;
106
107 $rps = $rps->with_updated_dependencies_for(
108 $_, $sol_rps->dependencies_for($_)
109 ) for @recheck;
110
111 trace 'step.recheck.done' => 'yay';
112
113 return $old_hyp->but(resolved_propositions => $rps);
9d759b64 114}
115
1161;