move ResolveProposition step over to rspace system
[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 step);
8
9 use DX::Class;
10
11 with 'DX::Role::Step';
12
13 has resolves => (is => 'ro', isa => Proposition);
14
15 has resolution_space => (is => 'ro', isa => ResolutionSpace);
16
17 has current_resolution => (is => 'lazy', init_arg => undef, builder => sub {
18   my ($self) = @_;
19   $self->resolution_space->next_resolution;
20 });
21
22 has actions => (is => 'lazy', init_arg => undef, builder => sub {
23   my ($self) = @_;
24   $self->current_resolution->actions;
25 });
26
27 has depends_on => (is => 'lazy', init_arg => undef, builder => sub {
28   my ($self) = @_;
29   my $_expand_dep = sub {
30     my ($type, @path) = @{$_[0]};
31     my @expanded = map {
32       ref() ? @{$_->value_path or return ()} : $_
33     } @path;
34     return [ $type, @expanded ];
35   };
36   [ map $_expand_dep->($_),
37       @{$self->current_resolution->veracity_depends_on} ];
38 });
39
40 has alternative_step => (is => 'lazy', init_arg => undef, builder => sub {
41   my ($self) = @_;
42   my $rspace = $self->resolution_space->remaining_resolution_space;
43   return undef unless @{$rspace->members};
44   return step(
45     resolves => $self->resolves,
46     resolution_space => $rspace
47   );
48 });
49
50 sub but_first {
51   my ($self, @actions) = @_;
52   $self->but(actions => [ @actions, @{$self->actions} ]);
53 }
54
55 sub but_with_dependencies_on {
56   my ($self, @deps) = @_;
57   $self->but(depends_on => [ @{$self->depends_on}, @deps ]);
58 }
59
60 sub apply_to {
61   my ($self, $old_ss) = @_;
62   my $ns = do {
63     if (my $prop = $old_ss->next_proposition) {
64       DX::Step::ConsiderProposition->new(
65         proposition => $prop
66       )
67     } else {
68       $old_ss->on_solution_step
69     }
70   };
71   my $ss = $old_ss->but(
72     next_step => $ns,
73     (@{$self->actions}
74       ? (adjustments_made => [
75           [ $self, $old_ss ],
76           @{$old_ss->adjustments_made}
77         ])
78       : ()
79     ),
80   );
81   my $new_ss = $self->_apply_to_ss($ss);
82   return $ss->but(next_step => DX::Step::Backtrack->new) unless $new_ss;
83   return $new_ss;
84 }
85
86 sub _apply_to_ss {
87   my ($self, $old_ss) = @_;
88   my $old_hyp = $old_ss->current_hypothesis;
89   (my $hyp, my @recheck) = $old_hyp->with_resolution(
90     $self->resolves, $self->depends_on, $self->actions
91   );
92   return undef unless $hyp;
93   return $self->_recheck_for(
94     $old_ss->but(current_hypothesis => $hyp),
95     @recheck
96   );
97 }
98
99 sub _recheck_for {
100   my ($self, $old_ss, @recheck) = @_;
101
102   return $old_ss unless @recheck;
103
104   my $ss = $old_ss->but(
105     next_step => DX::Step::EnterRecheck->new(
106       proposition_list => \@recheck,
107       on_completion_step => $old_ss->next_step,
108       on_failure_step => DX::Step::Backtrack->new,
109     ),
110   );
111
112   return $ss;
113 }
114
115 1;