61993c56d93c601ac861f58554818c1aadf2ea49
[scpubgit/DX.git] / lib / DX / Hypothesis.pm
1 package DX::Hypothesis;
2
3 use DX::Class;
4
5 has scope => (is => 'ro', required => 1);
6
7 has resolved_propositions => (is => 'ro', required => 1);
8
9 has outstanding_propositions => (is => 'ro', required => 1);
10
11 has actions => (is => 'ro', required => 1);
12
13 sub head_proposition { shift->outstanding_propositions->[0] }
14
15 sub with_actions {
16   my ($self, @actions) = @_;
17   my $hyp = $self;
18   foreach my $act (@actions) {
19     ($hyp, my @events) = $act->dry_run($hyp);
20     return undef unless $hyp;
21     $hyp = $hyp->but_recheck_for(@events);
22     return undef unless $hyp;
23   }
24   return $hyp;
25 }
26
27 sub but_recheck_for {
28   my ($self, @events) = @_;
29   my ($still_resolved, @recheck) = $self->resolved_propositions
30                                         ->but_expire_for(@events);
31   my $hyp = $self->but(resolved_propositions => $still_resolved);
32   $hyp = $_->but_recheck_against($hyp) or return undef for @recheck;
33   return $hyp;
34 }
35
36 sub resolve_head_dependent_on {
37   my ($self, $depends) = @_;
38   my ($first, @rest) = @{$self->outstanding_propositions};
39   $self->but(
40     resolved_propositions => $self->resolved_propositions
41                                   ->with_resolution_for(
42                                       $first,
43                                       $depends,
44                                     ),
45     outstanding_propositions => \@rest,
46   );
47 }
48
49 1;