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