Commit | Line | Data |
b40e416a |
1 | package DX::Solver; |
2 | |
3 | use DX::RuleSet; |
4 | use DX::State; |
5 | use DX::ResultStream; |
6 | use List::Util qw(reduce); |
7 | use Moo; |
8 | |
37e9670d |
9 | has rule_set => ( |
10 | is => 'lazy', |
11 | handles => [ qw(add_predicate add_rule) ], |
12 | builder => sub { |
13 | DX::RuleSet->new |
14 | }, |
15 | ); |
b40e416a |
16 | |
0676b282 |
17 | has facts => (is => 'ro', default => sub { {} }); |
b40e416a |
18 | |
5ef4d923 |
19 | has observation_policy => (is => 'ro'); |
20 | |
b40e416a |
21 | sub query { |
7d384eca |
22 | my ($self, @terms) = @_; |
b40e416a |
23 | my $rule_set = $self->rule_set; |
7d384eca |
24 | my $head = $rule_set->expand_and_link(undef, @terms, [ 'materialize' ]); |
b40e416a |
25 | my $state = DX::State->new( |
26 | next_op => $head, |
27 | return_stack => [], |
28 | by_id => {}, |
29 | scope => {}, |
30 | last_choice => [], |
31 | facts => $self->facts, |
32 | rule_set => $rule_set, |
7d384eca |
33 | ); |
b40e416a |
34 | return DX::ResultStream->new( |
5ef4d923 |
35 | for_state => $state, |
36 | ($self->observation_policy |
37 | ? (observation_policy => $self->observation_policy) |
38 | : ()), |
b40e416a |
39 | ); |
40 | } |
41 | |
e7117efc |
42 | sub run_action { |
43 | my ($self, $action) = @_; |
44 | my @invalidate = $action->run; |
45 | while (my ($type, $value) = splice @invalidate, 0, 2) { |
46 | $self->facts->{$type}->remove_value($value); |
47 | } |
48 | } |
49 | |
b40e416a |
50 | 1; |