From d08f9885326784e68b78994695ca3f9002b1b9f5 Mon Sep 17 00:00:00 2001 From: Krisztian Litkey Date: Wed, 7 Nov 2012 22:42:27 +0200 Subject: [PATCH] lua-decision: added basic resolver target generation. --- src/core/lua-decision/element.c | 60 ++++++++++++++++++++++++++++++--- src/core/lua-decision/mdb.c | 50 +++++++++++++++++++++++++-- src/core/lua-decision/tests/Makefile.am | 3 +- 3 files changed, 105 insertions(+), 8 deletions(-) diff --git a/src/core/lua-decision/element.c b/src/core/lua-decision/element.c index d8424bd..447eb97 100644 --- a/src/core/lua-decision/element.c +++ b/src/core/lua-decision/element.c @@ -30,18 +30,21 @@ #include #include #include - +#include #include #include #include +#include +#include #include #include #include #include #include +#include #define ELEMENT_CLASS MRP_LUA_CLASS(element, lua) @@ -267,27 +270,74 @@ static mrp_lua_element_t *element_check(lua_State *L, int idx) return (mrp_lua_element_t *)mrp_lua_check_object(L, ELEMENT_CLASS, idx); } +static int element_update_cb(mrp_scriptlet_t *script, mrp_context_tbl_t *ctbl) +{ + mrp_lua_element_t *el = (mrp_lua_element_t *)script->data; + + MRP_UNUSED(ctbl); + + printf("*** should update element '%s'\n", el->name); + + return TRUE; +} + +static mrp_interpreter_t element_updater = { + { NULL, NULL }, + "element_updater", + NULL, + NULL, + NULL, + element_update_cb, + NULL +}; + + static void element_install(lua_State *L, mrp_lua_element_t *el) { mrp_lua_element_input_t *inp; + mrp_context_t *ctx; size_t i; - char buf[1024]; + char buf[1024], target[1024]; + const char **depends, *d; + int ndepend; char *p, *e; MRP_UNUSED(L); + ctx = mrp_lua_get_murphy_context(); + + if (ctx == NULL || ctx->r == NULL) { + printf("Invalid or incomplete murphy context.\n"); + return; + } + + depends = alloca(el->ninput * sizeof(depends[0])); + ndepend = el->ninput; + for (i = 0, e = (p = buf) + sizeof(buf); i < el->ninput && p < e; i++) { inp = el->inputs + i; if (inp->type == SELECT) { - p += snprintf(p, e-p, " select_%s", - mrp_lua_select_name(inp->select)); + d = mrp_lua_select_name(inp->select); + p += snprintf(p, e-p, " select_%s", d); + + depends[i] = alloca(strlen(d) + 1); + strcpy((char *)depends[i], d); } } for (i = 0; i < el->noutput; i++) { printf("\ntable_%s:%s\n\tupdate(%s)\n\n", mrp_lua_table_name(el->outputs[i]), buf, el->name); + + snprintf(target, sizeof(target), "table_%s", + mrp_lua_table_name(el->outputs[i])); + + if (!mrp_resolver_add_prepared_target(ctx->r, target, depends, ndepend, + &element_updater, NULL, el)) { + printf("Failed to install resolver target for element '%s'.\n", + el->name); + } } } @@ -457,7 +507,7 @@ static mrp_lua_element_input_t *element_input_create_userdata(lua_State *L, i->type = SELECT; i->select = mrp_lua_select_check(L, -1); break; - + default: luaL_error(L, "invalid input type %s", lua_typename(L, lua_type(L, -1))); diff --git a/src/core/lua-decision/mdb.c b/src/core/lua-decision/mdb.c index 1251b00..37aaedd 100644 --- a/src/core/lua-decision/mdb.c +++ b/src/core/lua-decision/mdb.c @@ -38,10 +38,12 @@ #include #include +#include +#include #include #include #include - +#include #define TABLE_CLASS MRP_LUA_CLASS(mdb, table) #define SELECT_CLASS MRP_LUA_CLASS(mdb, select) @@ -352,7 +354,7 @@ static int table_setfield(lua_State *L) luaL_checktype(L, -1, LUA_TTABLE); } - printf("*** setting row %d in table '%s'\n", rowidx+1, tbl->name); + printf("*** setting row %zd in table '%s'\n", rowidx+1, tbl->name); } @@ -571,12 +573,56 @@ static int select_update_from_lua(lua_State *L) return 1; } +static int select_update_cb(mrp_scriptlet_t *script, mrp_context_tbl_t *ctbl) +{ + mrp_lua_mdb_select_t *sel = (mrp_lua_mdb_select_t *)script->data; + + MRP_UNUSED(ctbl); + + printf("*** should update element '%s'\n", sel->name); + + return TRUE; +} + +static mrp_interpreter_t select_updater = { + { NULL, NULL }, + "select_updater", + NULL, + NULL, + NULL, + select_update_cb, + NULL +}; + + static void select_install(lua_State *L, mrp_lua_mdb_select_t *sel) { + mrp_context_t *ctx; + char target[1024], table[1024]; + const char *depends; + MRP_UNUSED(L); + ctx = mrp_lua_get_murphy_context(); + + if (ctx == NULL || ctx->r == NULL) { + printf("Invalid or incomplete murphy context.\n"); + return; + } + printf("\nselect_%s: table_%s\n\tupdate(%s)\n", sel->name, sel->table.name, sel->name); + + snprintf(target, sizeof(target), "select_%s", sel->name); + snprintf(table , sizeof(table) , "$%s", sel->table.name); + + depends = table; + + if (!mrp_resolver_add_prepared_target(ctx->r, target, &depends, 1, + &select_updater, NULL, sel)) { + printf("Failed to install resolver target for element '%s'.\n", + sel->name); + } } static void select_row_class_create(lua_State *L) diff --git a/src/core/lua-decision/tests/Makefile.am b/src/core/lua-decision/tests/Makefile.am index 1b8d5e4..5821193 100644 --- a/src/core/lua-decision/tests/Makefile.am +++ b/src/core/lua-decision/tests/Makefile.am @@ -7,7 +7,8 @@ noinst_PROGRAMS = decision-test # lua decision network test decision_test_SOURCES = decision-test.c decision_test_CFLAGS = $(AM_CFLAGS) $(LUA_CFLAGS) -decision_test_LDADD = ../../../libmurphy-common.la \ +decision_test_LDADD = ../../../libmurphy-resolver.la \ + ../../../libmurphy-common.la \ ../../../libmurphy-core.la \ ../../../libmurphy-lua-utils.la \ ../../../libmurphy-lua-decision.la \ -- 2.7.4