summaryrefslogtreecommitdiff
path: root/src/runtime
diff options
context:
space:
mode:
authorkr.angelov <kr.angelov@gmail.com>2012-12-14 21:25:00 +0000
committerkr.angelov <kr.angelov@gmail.com>2012-12-14 21:25:00 +0000
commitd12c604f9aaa8fcdf4a4e8f9f08ebe7bd8daa0d9 (patch)
tree9e8bfca43117796006a4fef7d7931c96863026e5 /src/runtime
parent16a2c38f38d56cb7a29bf52c0d959aeaacd4e8f3 (diff)
debugging infrastructure in the reasoner
Diffstat (limited to 'src/runtime')
-rw-r--r--src/runtime/c/pgf/reasoner.c66
1 files changed, 66 insertions, 0 deletions
diff --git a/src/runtime/c/pgf/reasoner.c b/src/runtime/c/pgf/reasoner.c
index f1b0f76d1..28714a948 100644
--- a/src/runtime/c/pgf/reasoner.c
+++ b/src/runtime/c/pgf/reasoner.c
@@ -1,8 +1,11 @@
#include <pgf/pgf.h>
#include <pgf/data.h>
+#include <gu/file.h>
#include <math.h>
#include <stdio.h>
+//#define PGF_REASONER_DEBUG
+
typedef struct PgfExprState PgfExprState;
struct PgfExprState {
@@ -46,6 +49,58 @@ cmp_expr_qstate(GuOrder* self, const void* a, const void* b)
static GuOrder
pgf_expr_qstate_order = { cmp_expr_qstate };
+#ifdef PGF_REASONER_DEBUG
+static void
+pgf_print_expr_state(PgfExprState* st,
+ GuWriter* wtr, GuExn* err, GuBuf* stack)
+{
+ gu_buf_push(stack, int, (gu_seq_length(st->hypos) - st->arg_idx - 1));
+
+ if (st->cont != NULL)
+ pgf_print_expr_state(st->cont, wtr, err, stack);
+
+ gu_puts(" (", wtr, err);
+ pgf_print_expr(st->expr, 0, wtr, err);
+}
+
+static void
+pgf_print_expr_qstate(PgfExprQState* q, PgfAbstr* abstract,
+ GuWriter* wtr, GuExn* err, GuPool* tmp_pool)
+{
+ PgfCId fun = q->abscat->functions[q->fun_idx].fun;
+ PgfFunDecl* absfun =
+ gu_map_get(abstract->funs, &fun, PgfFunDecl*);
+
+ prob_t prob = q->cont_prob+absfun->ep.prob;
+ gu_printf(wtr, err, "[%f]", prob);
+
+ size_t n_args = gu_seq_length(absfun->type->hypos);
+
+ GuBuf* stack = gu_new_buf(int, tmp_pool);
+ if (n_args > 0)
+ gu_buf_push(stack, int, n_args);
+
+ if (q->st != NULL)
+ pgf_print_expr_state(q->st, wtr, err, stack);
+
+ if (n_args > 0)
+ gu_puts(" (", wtr, err);
+ else
+ gu_puts(" ", wtr, err);
+ pgf_print_expr(absfun->ep.expr, 0, wtr, err);
+
+ size_t n_counts = gu_buf_length(stack);
+ for (size_t i = 0; i < n_counts; i++) {
+ int count = gu_buf_get(stack, int, i);
+ while (count-- > 0)
+ gu_puts(" ?", wtr, err);
+
+ gu_puts(")", wtr, err);
+ }
+ gu_puts("\n", wtr, err);
+}
+#endif
+
static bool
pgf_reasoner_cat_init(PgfReasoner* rs,
PgfExprState* cont, prob_t cont_prob, PgfCId cat,
@@ -85,6 +140,17 @@ pgf_reasoner_next(PgfReasoner* rs, GuPool* pool)
PgfExprQState q;
gu_buf_heap_pop(rs->pqueue, &pgf_expr_qstate_order, &q);
+#ifdef PGF_REASONER_DEBUG
+ {
+ GuPool* tmp_pool = gu_new_pool();
+ GuOut* out = gu_file_out(stderr, tmp_pool);
+ GuWriter* wtr = gu_new_utf8_writer(out, tmp_pool);
+ GuExn* err = gu_exn(NULL, type, tmp_pool);
+ pgf_print_expr_qstate(&q, rs->abstract, wtr, err, tmp_pool);
+ gu_pool_free(tmp_pool);
+ }
+#endif
+
PgfCId fun = q.abscat->functions[q.fun_idx++].fun;
PgfFunDecl* absfun =
gu_map_get(rs->abstract->funs, &fun, PgfFunDecl*);