Commit | Line | Data |
9d759b64 |
1 | package DX::SearchState; |
2 | |
a1aab147 |
3 | use Types::Standard qw(Maybe); |
985b43d1 |
4 | use DX::Step::Backtrack; |
e647e417 |
5 | use DX::Step::InvokeNextPredicate; |
9d759b64 |
6 | use DX::Class; |
7 | |
3e465d5d |
8 | has current_hypothesis => (is => 'ro', isa => Hypothesis, required => 1); |
9d759b64 |
9 | |
e647e417 |
10 | has next_step => (is => 'ro', isa => Maybe[Step]); |
9d759b64 |
11 | |
1350f664 |
12 | has propositions => (is => 'ro', isa => PropositionSequence, required => 1); |
13 | |
3e465d5d |
14 | has alternatives => (is => 'ro', isa => AlternativeList, required => 1); |
15 | |
8cc971ec |
16 | has is_solution_state => (is => 'ro', required => 1); |
17 | |
f25e6894 |
18 | sub next_proposition { |
5787d20d |
19 | my ($self, $hyp) = @_; |
20 | $hyp ||= $self->current_hypothesis; |
f25e6894 |
21 | $self->propositions->members->[ |
5787d20d |
22 | $hyp->resolved_propositions->resolved_count |
f25e6894 |
23 | ]; |
24 | } |
ccf0d4fe |
25 | |
3e465d5d |
26 | sub new_for { |
1350f664 |
27 | my ($class, $hyp, $props) = @_; |
3e465d5d |
28 | $class->new( |
29 | current_hypothesis => $hyp, |
30 | alternatives => [], |
1350f664 |
31 | propositions => $props, |
75389058 |
32 | (@{$props->members} |
33 | ? ( |
34 | next_step => DX::Step::InvokeNextPredicate->new( |
35 | proposition => $props->members->[0], |
36 | ), |
37 | is_solution_state => 0, |
38 | ) |
39 | : ( is_solution_state => 1 ) |
40 | ), |
3e465d5d |
41 | ); |
42 | } |
9d759b64 |
43 | |
44 | sub with_one_step { |
45 | my ($self) = @_; |
e647e417 |
46 | return undef unless my $step = $self->next_step; |
c76de01d |
47 | return $step->apply_to($self); |
9d759b64 |
48 | } |
49 | |
9d759b64 |
50 | sub force_backtrack { |
51 | my ($self) = @_; |
f458fa2c |
52 | my ($first_alt, @rest_alt) = @{$self->alternatives}; |
53 | return undef unless $first_alt; |
72e5c0e0 |
54 | trace 'search.backtrack.forced' => $first_alt->[0]; |
bc7cb635 |
55 | return $self->but( |
56 | next_step => DX::Step::Backtrack->new, |
57 | )->with_one_step; |
9d759b64 |
58 | } |
59 | |
9d759b64 |
60 | 1; |