Commit | Line | Data |
71217e42 |
1 | package DX::Op::ProposeAction; |
2 | |
3 | use DX::ObservationRequired; |
4 | use Moo; |
5 | |
6 | with 'DX::Role::Op'; |
7 | |
8 | has vars => (is => 'ro', required => 1); |
9 | has builder => (is => 'ro', required => 1); |
10 | |
11 | has _arg_map => (is => 'lazy', builder => sub { |
12 | my ($self) = @_; |
13 | my $name = 'arg0'; |
14 | +{ map +($name++, $_), @{$self->vars} }; |
15 | }); |
16 | |
17 | sub run { |
18 | my ($self, $state) = @_; |
19 | ($state, my %args) = $self->_expand_args($state, %{$self->_arg_map}); |
20 | my @vars = @args{sort keys %args}; |
8c3eab7b |
21 | return $state->backtrack unless @vars == grep $_->is_bound, @vars; |
7ca660cb |
22 | return $state->backtrack unless $state->allow_actions; |
deec7cc4 |
23 | my @deps = $state->action_dependencies(map $_->id, @vars); |
4ce2e778 |
24 | my $action = $self->builder->(map $state->resolve_value($_), @vars) |
deec7cc4 |
25 | ->but(dependencies => \@deps); |
577a2146 |
26 | my ($rec_state, $action_id) = $state->record_action($action); |
e02a5c0a |
27 | my @effect = $action->expected_effect; |
28 | my %facts = %{$state->facts}; |
29 | while (my ($fact_type, $value) = splice(@effect, 0, 2)) { |
30 | my $final_value = $value->but(required_action => $action_id); |
31 | $facts{$fact_type} = $facts{$fact_type}->with_value($final_value); |
32 | } |
33 | $rec_state->but(facts => \%facts) |
577a2146 |
34 | ->then($self->next); |
71217e42 |
35 | } |
36 | |
37 | 1; |