has facts => (is => 'ro', default => sub { {} });
-has observation_policy => (is => 'ro');
+has observation_policy => (is => 'rw');
+
+has action_callback => (is => 'rw');
sub query {
my ($self, @terms) = @_;
sub run_action {
my ($self, $action) = @_;
- warn +(split('::', ref($action)))[-1]."\n";
+ if (my $cb = $self->action_callback) {
+ $cb->($action);
+ }
my @invalidate = $action->run;
while (my ($type, $value) = splice @invalidate, 0, 2) {
$self->facts->{$type}->remove_value($value);
return $result ? $self->_implode_result($result) : 'false';
}
+sub _tcl_set_action_cb {
+ my ($self, @cb) = @_;
+ my $get_tcl = $self->curry::weak::tcl;
+ my $implode = $self->curry::weak::_implode_action;
+ $self->solver->action_callback(sub {
+ $get_tcl->()->call(@cb, $implode->($_[0]));
+ });
+ return;
+}
+
sub _implode_result {
my ($self, $result) = @_;
my $imploded = $self->_implode_hashref($result->all_values);
if (my @act = $result->actions) {
- my @flat_act = map +($_->[0], $self->_implode_hashref($_->[1])),
- map [ %{$_->as_structure} ], @act;
+ my @flat_act = map $self->_implode_action, @act;
unshift @$imploded, +(_actions => \@flat_act);
}
return $imploded;
}
+sub _implode_action {
+ my ($self, $action) = @_;
+ map +($_->[0], $self->_implode_hashref($_->[1])), [ %{$action->as_structure} ];
+}
+
sub _implode_hashref {
my ($self, $values) = @_;
my @flat;
return [format_response $res $out]
}
+ proc format_action {type data} {
+ set flat "$type ->\n";
+ append flat [format_response $data "" " "]
+ return $flat
+ }
+
proc format_response {res out {i ""}} {
if {[llength $res] < 2} {
return "${i}$res\n";
set flat ""
if [dict exists $res _actions] {
foreach {n v} [dict get $res _actions] {
- append flat "$n -> \n";
- append flat [format_response $v "" " "]
+ append flat [format_action $type $data]
}
append flat "->\n";
}
return [lmap x $raw { mangle_arg $x }]
}
+ proc action_cb args {
+ _call set_action_cb {*}$args
+ }
+
+ action_cb apply {{n v} {puts [DX::format_action $n $v]}}
+
namespace eval ::DX::Eval {
proc findall args { DX::body_add findall $args }
proc foreach args { DX::body_add foreach $args }