3728a639e5a8cb7aa7a9cc2f8b103bb95f581f51
[scpubgit/DX.git] / lib / DX / Step / ResolveProposition.pm
1 package DX::Step::ResolveProposition;
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 $ss->but(next_step => DX::Step::Backtrack->new) unless $new_hyp;
34   trace 'step.apply.new_hyp '.$self => $new_hyp;
35   my $ns = do {
36     if (my $prop = $ss->next_proposition($new_hyp)) {
37       DX::Step::ConsiderProposition->new(
38         proposition => $prop
39       )
40     } else {
41       $ss->on_solution_step
42     }
43   };
44   my $alt_step = $self->alternative_step;
45   return (
46     $ss->but(
47       current_hypothesis => $new_hyp,
48       next_step => $ns,
49       ($alt_step
50         ? (alternatives => [ [ $old_hyp, $alt_step ], @{$ss->alternatives} ])
51         : ()
52       ),
53     ),
54   );
55 }
56
57 sub _apply_to_hyp {
58   my ($self, $old_hyp) = @_;
59   (my $hyp, my @recheck) = $old_hyp->with_resolution(
60     $self->resolves, $self->depends_on, $self->actions
61   );
62   return $self->_recheck_hyp_for($hyp, @recheck);
63 }
64
65 sub _recheck_hyp_for {
66   my ($self, $old_hyp, @recheck) = @_;
67   return undef unless $old_hyp;
68   return $old_hyp unless @recheck;
69   my $hyp = $old_hyp;
70   foreach my $prop (@recheck) {
71     return undef unless $hyp = $self->_recheck_one($hyp, $prop);
72   }
73   return $hyp;
74 }
75
76 sub _recheck_one {
77   my ($self, $old_hyp, $prop) = @_;
78
79   my $ap = DX::ActionPolicy::LockScope->new(
80     lock_to_depth => $old_hyp->scope->depth,
81     next_policy => $old_hyp->action_policy,
82   );
83
84   # we should probably be doing something about pruning the scope
85   # but that's completely pointless until we have rules
86
87   my $hyp = ref($old_hyp)->new(
88     scope => $old_hyp->scope,
89     resolved_propositions => DX::ResolvedPropositionSet->new_empty,
90     actions => [],
91     action_applications => [],
92     action_policy => $ap,
93   );
94
95   my $pseq = DX::PropositionSequence->new(
96     members => [ $prop ],
97     external_names => {},
98     internal_names => {},
99   );
100
101   trace 'step.recheck.hyp' => $hyp;
102
103   my $ss = DX::SearchState->new(
104     current_hypothesis => $hyp,
105     alternatives => [],
106     propositions => $pseq,
107     next_step => DX::Step::ConsiderProposition->new(
108                    proposition => $prop,
109                  ),
110     is_solution_state => 0,
111     on_exhaustion_step => undef,
112     on_solution_step => DX::Step::MarkAsSolution->new,
113   );
114
115   my $sp = DX::SearchProcess->new(
116     current_search_state => $ss,
117   );
118
119   my $sol_sp = $sp->find_solution;
120
121   unless ($sol_sp) {
122     trace 'step.recheck.fail' => 'argh';
123     return undef;
124   }
125
126   my $sol_rps = $sol_sp->current_hypothesis->resolved_propositions;
127
128   my $rps = $old_hyp->resolved_propositions;
129
130   $rps = $rps->with_updated_dependencies_for(
131     $prop, $sol_rps->dependencies_for($prop)
132   );
133
134   trace 'step.recheck.done' => 'yay';
135
136   return $old_hyp->but(resolved_propositions => $rps);
137 }
138
139 1;