3 use Return::MultiLevel qw(with_return);
5 use Scalar::Util qw(blessed);
8 has next_op => (is => 'ro', required => 1);
10 has return_stack => (is => 'ro', required => 1);
12 has by_id => (is => 'ro', required => 1);
14 has scope => (is => 'ro', required => 1);
16 has last_choice => (is => 'ro', required => 1);
18 has id_gen => (is => 'ro', default => sub { {} });
20 has rule_set => (is => 'ro');
22 has facts => (is => 'ro');
24 has dependencies => (is => 'ro', default => sub { {} });
27 my ($self, $name) = @_;
28 $self->by_id->{$self->scope->{$name}};
32 my ($self, $name, $var, $id_gen) = @_;
33 my $id = join('_', $name, ++($id_gen->{$name}||='000'));
34 DX::Var->new(id => $id, %$var);
38 my ($self, %vars) = @_;
39 my %by_id = %{$self->by_id};
40 my %id_gen = %{$self->id_gen};
41 foreach my $name (keys %vars) {
42 unless (blessed($vars{$name})) {
43 my $var = $vars{$name} = $self->allocate_var(
44 $name, $vars{$name}, \%id_gen
46 $by_id{$var->id} = $var;
50 by_id => \%by_id, id_gen => \%id_gen,
55 my ($self, %vars) = @_;
56 my ($state, %expanded) = $self->expand_vars(%vars);
58 scope => { %{$self->scope}, map +($_ => $expanded{$_}->id), keys %expanded }
63 my ($self, $var_id, $value) = @_;
64 my $bound = $self->by_id->{$var_id}->with_value($value);
66 by_id => { %{$self->by_id}, $var_id => $bound },
71 my ($self, $var, $stream) = @_;
72 my $bound = $var->with_stream($stream);
74 by_id => { %{$self->by_id}, $var->id => $bound },
75 )->mark_choice($bound);
79 my ($self, $var_id, $set) = @_;
80 my $bound = $self->by_id->{$var_id}->with_root_set($set);
82 by_id => { %{$self->by_id}, $var_id => $bound },
83 )->mark_choice($bound);
87 my ($self, $var) = @_;
88 $self->but(last_choice => [ $self, $var ]);
93 my ($state, $var) = ($self);
94 while (($state, $var) = @{$state->last_choice}) {
95 $var->bound_value; $var->clear_bound_value;
96 return $state->mark_choice($var) unless $var->bound_stream->is_exhausted;
98 $self->return_from_run(undef);
102 my ($self, $then) = @_;
103 $self->but(next_op => $then);
106 sub return_from_run {
107 my (undef, $return) = @_;
108 (our $Run_Return)->($return);
112 my (undef, $return) = @_;
113 if (our $Op_Return) {
114 $Op_Return->($return);
122 local our $Run_Return = $return;
123 while (my $op = $state->next_op) {
124 my $backtrack = with_return {
126 local our $Op_Return = $return;
127 $state = $op->run($state);
130 if ($backtrack) { $state = $state->backtrack }
137 $_[0]->then(DX::Op::Backtrack->new);
141 my ($self, @but) = @_;
142 $self->new(%$self, @but);
145 sub pop_return_stack {
147 my @stack = @{$self->return_stack};
148 my $top = pop @stack;
149 $self->but(return_stack => \@stack, next_op => $top->[0]);
152 sub push_return_then {
153 my ($self, $return, $then) = @_;
155 return_stack => [ @{$self->return_stack}, [ $return, $self ] ],
160 sub add_dependencies {
161 my ($self, @pairs) = @_;
162 my %deps = %{$self->dependencies};
163 while (my ($from, $to) = splice(@pairs, 0, 2)) {
164 unless ($deps{$from}{$to}) {
165 $deps{$from} = { %{$deps{$from}||{}}, $to => 1 };
168 $self->but(dependencies => \%deps);
171 sub action_dependencies {
172 my ($self, @ids) = @_;
174 my $deps = $self->dependencies;
175 my $by_id = $self->by_id;
178 while (my $id = shift @queue) {
180 push @found, $id if $by_id->{$id}->has_action;
181 push @queue, grep !$seen{$_}, keys %{$deps->{$id}};
188 my $by_id = $self->by_id;
189 $self->but(by_id => {
190 map +($_ => $by_id->{$_}->copy), keys %$by_id