use FactRef objects to sanify action handling
[scpubgit/DKit.git] / lib / DX / Op / ProposeAction.pm
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};
21   my @deps = $state->action_dependencies(map $_->id, @vars);
22   my $action = $self->builder->(map $state->resolve_value($_), @vars)
23                     ->but(dependencies => \@deps);
24   my ($fact_type, $value) = $action->expected_effect;
25   my $id = $vars[0]->id;
26   my $var = $state->by_id->{$id}->with_action($action);
27   my $fact_set = $state->facts->{$fact_type}->with_value($value);
28   $state->but(
29             by_id => { %{$state->by_id}, $id => $var },
30             facts => { %{$state->facts}, $fact_type => $fact_set },
31           )
32         ->then($self->next);
33 }
34
35 1;