59e0480a576dc7e42763d61a88d2113d6c06942f
[scpubgit/DX.git] / lib / DX / Hypothesis.pm
1 package DX::Hypothesis;
2
3 use DX::ActionPolicy::LockScope;
4 use Types::Standard qw(ArrayRef);
5 use DX::Utils qw(deparse);
6 use DX::Class;
7
8 has scope => (is => 'ro', isa => Scope, required => 1);
9
10 has resolved_propositions => (
11   is => 'ro', isa => ResolvedPropositionSet, required => 1
12 );
13
14 has outstanding_propositions => (
15   is => 'ro', isa => ArrayRef[Proposition], required => 1
16 );
17
18 has actions => (
19   is => 'ro', isa => ArrayRef[Action], required => 1
20 );
21
22 has action_applications => (
23   is => 'ro', isa => ArrayRef[Action], required => 1
24 );
25
26 has action_policy => (is => 'ro', isa => ActionPolicy, required => 1);
27
28 sub head_proposition { shift->outstanding_propositions->[0] }
29
30 sub with_actions {
31   my ($self, @actions) = @_;
32   my $hyp = $self;
33   foreach my $act (@actions) {
34     return undef unless $self->action_policy->allows($act);
35     ($hyp, my @events) = $act->dry_run($hyp);
36     return undef unless $hyp;
37     $hyp = $hyp->but_recheck_for(@events);
38     return undef unless $hyp;
39   }
40   return $hyp;
41 }
42
43 sub but_recheck_for {
44   my ($self, @events) = @_;
45   my ($still_resolved, @recheck) = $self->resolved_propositions
46                                         ->but_expire_for(@events);
47   return $self unless @recheck;
48
49   my $ap = DX::ActionPolicy::LockScope->new(
50     lock_to_depth => $self->scope->depth,
51     next_policy => $self->action_policy,
52   );
53
54   # we should probably be doing something about pruning the scope
55   # but that's completely pointless until we have rules
56
57   my $hyp = ref($self)->new(
58     scope => $self->scope,
59     resolved_propositions => DX::ResolvedPropositionSet->new_empty,
60     outstanding_propositions => \@recheck,
61     actions => [],
62     action_applications => [],
63     action_policy => $ap,
64   );
65
66   trace 'step.recheck.hyp' => $hyp;
67
68   my $ss = DX::SearchState->new_for($hyp);
69
70   my $sol_ss = $ss->find_solution;
71
72   unless ($sol_ss) {
73     trace 'step.recheck.fail' => 'argh';
74     return undef;
75   }
76
77   my $sol_rps = $sol_ss->current_hypothesis->resolved_propositions;
78
79   my $rps = $still_resolved;
80
81   $rps = $rps->with_updated_dependencies_for(
82     $_, $sol_rps->dependencies_for($_)
83   ) for @recheck;
84
85   trace 'step.recheck.done' => 'yay';
86
87   return $self->but(resolved_propositions => $rps);
88 }
89
90 sub resolve_head_dependent_on {
91   my ($self, $depends) = @_;
92   my ($first, @rest) = @{$self->outstanding_propositions};
93   $self->but(
94     resolved_propositions => $self->resolved_propositions
95                                   ->with_resolution_for(
96                                       $first,
97                                       $depends,
98                                     ),
99     outstanding_propositions => \@rest,
100   );
101 }
102
103 1;