initial creation of SearchProcess class
[scpubgit/DX.git] / lib / DX / QueryState.pm
CommitLineData
9eedd677 1package DX::QueryState;
2
fa8f5696 3use Types::Standard qw(HashRef);
384a5e93 4use DX::Scope;
5use DX::Hypothesis;
8c16d3c9 6use DX::SearchProcess;
384a5e93 7use DX::ResolvedPropositionSet;
8use DX::Value::Unset;
9use DX::ActionBuilder::UnsetValue;
10use DX::ActionPolicy::Allow;
11use DX::Utils qw(:builders);
9eedd677 12use DX::Class;
13
fa8f5696 14has predicates => (is => 'ro', isa => HashRef[Predicate], required => 1);
15
16has globals => (is => 'ro', isa => DictValue, required => 1);
17
18has proposition_sequence => (
19 is => 'ro', isa => PropositionSequence, required => 1
20);
9eedd677 21
384a5e93 22has search_state => (
23 is => 'lazy', builder => sub {
24 $_[0]->new_search_state_for($_[0]->proposition_sequence)
25 },
26);
27
28sub new_search_state_for {
29 my ($self, $prop_seq) = @_;
5f12a9d8 30 my @local_names = map { keys %{$_->introduced_names} }
384a5e93 31 @{$prop_seq->members};
32 my $scope = DX::Scope->new(
33 predicates => $self->predicates,
34 globals => $self->globals,
35 locals => [
36 dict(
37 map +($_ => DX::Value::Unset->new(
38 identity_path => [ 0, $_ ],
39 action_builder => DX::ActionBuilder::UnsetValue->new(
40 target_path => [ 0, $_ ],
41 )
42 )
43 ), @local_names
44 )
af69c845 45 ],
46 lex_map => {
47 map +($_ => [ 0, $_ ]), @local_names
48 }
384a5e93 49 );
50 my $hyp = DX::Hypothesis->new(
51 scope => $scope,
52 resolved_propositions => DX::ResolvedPropositionSet->new_empty,
384a5e93 53 actions => [],
e442aff8 54 action_applications => [],
384a5e93 55 action_policy => DX::ActionPolicy::Allow->new,
56 );
8c16d3c9 57 return DX::SearchProcess->new_for($hyp, $prop_seq);
384a5e93 58}
59
60sub with_additional_proposition {
61 my ($self, $prop) = @_;
62 my $prop_seq = $self->proposition_sequence
5f12a9d8 63 ->with_additional_proposition($prop);
31d445d3 64 my $sol_ss = $self->new_search_state_for($prop_seq)
65 ->find_solution;
f458fa2c 66 die "No solution\n" unless $sol_ss;
384a5e93 67 $self->but(
68 proposition_sequence => $prop_seq,
31d445d3 69 search_state => $sol_ss,
384a5e93 70 );
71}
72
f458fa2c 73sub with_forced_backtrack {
74 my ($self) = @_;
75 my $next_ss = $self->search_state->find_next_solution;
76 die "No next solution\n" unless $next_ss;
77 $self->but(search_state => $next_ss);
78}
79
9eedd677 801;