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