move actions to being held by fact objects
[scpubgit/DKit.git] / lib / DX / Var.pm
1 package DX::Var;
2
3 use Moo;
4
5 has id => (is => 'ro', required => 1);
6
7 has root_set => (is => 'ro', predicate => 1);
8
9 has bound_stream => (
10   is => 'lazy', predicate => 1, clearer => 1,
11   builder => sub {
12     my ($self) = @_;
13     $self->root_set->to_stream;
14   }
15 );
16
17 has bound_value => (is => 'lazy', predicate => 1, clearer => 1, builder => sub {
18   if (defined(my $next = $_[0]->bound_stream->next)) {
19     return $next;
20   }
21   DX::State->return_from_op('backtrack');
22   return;
23 });
24
25 sub is_bound {
26   my ($self) = @_;
27   $self->has_bound_value || $self->has_bound_stream || $self->has_root_set;
28 }
29
30 sub with_stream {
31   my ($self, $stream) = @_;
32   $self->new(%$self, bound_stream => $stream);
33 }
34
35 sub with_value {
36   my ($self, $stream) = @_;
37   $self->new(%$self, bound_value => $stream);
38 }
39
40 sub with_root_set {
41   my ($self, $set) = @_;
42   $self->new(%$self, root_set => $set);
43 }
44
45 sub copy {
46   my ($self) = @_;
47   ref($self)->new(%$self);
48 }
49
50 1;