const and basic tracing
[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 outstanding_propositions => (
15 is => 'ro', isa => ArrayRef[Proposition], required => 1
16);
9d759b64 17
3e465d5d 18has actions => (
19 is => 'ro', isa => ArrayRef[Action], required => 1
20);
21
e442aff8 22has action_applications => (
23 is => 'ro', isa => ArrayRef[Action], required => 1
24);
25
3e465d5d 26has action_policy => (is => 'ro', isa => ActionPolicy, required => 1);
9d759b64 27
efad53c4 28sub head_proposition { shift->outstanding_propositions->[0] }
29
9d759b64 30sub with_actions {
31 my ($self, @actions) = @_;
32 my $hyp = $self;
33 foreach my $act (@actions) {
3e465d5d 34 return undef unless $self->action_policy->allows($act);
efad53c4 35 ($hyp, my @events) = $act->dry_run($hyp);
9d759b64 36 return undef unless $hyp;
37 $hyp = $hyp->but_recheck_for(@events);
38 return undef unless $hyp;
39 }
40 return $hyp;
41}
42
43sub but_recheck_for {
44 my ($self, @events) = @_;
45 my ($still_resolved, @recheck) = $self->resolved_propositions
46 ->but_expire_for(@events);
3e465d5d 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 => [],
e442aff8 62 action_applications => [],
3e465d5d 63 action_policy => $ap,
64 );
65
bcee3a69 66 trace 'step.recheck.hyp' => $hyp;
67
3e465d5d 68 my $ss = DX::SearchState->new_for($hyp);
69
70 return undef unless my $sol_ss = $ss->find_solution;
71
72 my $sol_rps = $sol_ss->current_hypothesis->resolved_propositions;
73
74 my $rps = $still_resolved;
75
76 $rps = $rps->with_updated_dependencies_for(
77 $_, $sol_rps->dependencies_for($_)
78 ) for @recheck;
79
bcee3a69 80 trace 'step.recheck.done' => 'yay';
81
3e465d5d 82 return $self->but(resolved_propositions => $rps);
9d759b64 83}
84
85sub resolve_head_dependent_on {
86 my ($self, $depends) = @_;
87 my ($first, @rest) = @{$self->outstanding_propositions};
88 $self->but(
89 resolved_propositions => $self->resolved_propositions
efad53c4 90 ->with_resolution_for(
91 $first,
92 $depends,
9d759b64 93 ),
efad53c4 94 outstanding_propositions => \@rest,
9d759b64 95 );
96}
97
981;