From: Matt S Trout Date: Wed, 26 Feb 2014 05:41:06 +0000 (+0000) Subject: better action debug info X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=e28f7460084f26d463047e4791cd0671c803ace1;p=scpubgit%2FDKit.git better action debug info --- diff --git a/lib/DX/Solver.pm b/lib/DX/Solver.pm index 753abe5..f1579a2 100644 --- a/lib/DX/Solver.pm +++ b/lib/DX/Solver.pm @@ -16,7 +16,9 @@ has rule_set => ( 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) = @_; @@ -64,7 +66,9 @@ sub _solve { 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); diff --git a/lib/DX/Tcl.pm b/lib/DX/Tcl.pm index 2a9955a..239a77c 100644 --- a/lib/DX/Tcl.pm +++ b/lib/DX/Tcl.pm @@ -70,17 +70,31 @@ sub _tcl_ensure_result { 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; @@ -182,6 +196,12 @@ namespace eval DX { 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"; @@ -192,8 +212,7 @@ namespace eval DX { 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"; } @@ -251,6 +270,12 @@ namespace eval DX { 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 }