|
@@ -1,6 +1,28 @@
|
|
|
#include "lua_lsup.h"
|
|
|
|
|
|
|
|
|
+LSUP_rc
|
|
|
+term_set_to_table (lua_State *L, LSUP_TermSet *ts)
|
|
|
+{
|
|
|
+ size_t i = 0;
|
|
|
+ LSUP_Term *tmp = NULL;
|
|
|
+ LSUP_rc rc;
|
|
|
+ lua_newtable (L);
|
|
|
+ while ((rc = LSUP_term_set_next (ts, &i, &tmp)) == LSUP_OK) {
|
|
|
+ LSUP_Term **t2_p = (LSUP_Term **)lua_newuserdata (L, sizeof *t2_p);
|
|
|
+ luaL_getmetatable (L, "LSUP.Term");
|
|
|
+ lua_setmetatable (L, -2);
|
|
|
+ *t2_p = LSUP_term_copy (tmp);
|
|
|
+
|
|
|
+ lua_pushboolean (L, true);
|
|
|
+ lua_rawset (L, -3);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (rc < LSUP_OK) return rc;
|
|
|
+ return LSUP_OK;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
static LSUP_Graph **allocate_graph (lua_State *L)
|
|
|
{
|
|
|
LSUP_Graph **gp = lua_newuserdatauv (L, sizeof (*gp), 1);
|
|
@@ -34,12 +56,9 @@ static int l_graph_list (lua_State *L)
|
|
|
const LSUP_Store *store = *(LSUP_Store **)luaL_checkudata (
|
|
|
L, 1, "LSUP.Store");
|
|
|
|
|
|
- LSUP_TermSet **ts_p = lua_newuserdata (L, sizeof *ts_p);
|
|
|
- luaL_getmetatable (L, "LSUP.TermSet");
|
|
|
- lua_setmetatable (L, -2);
|
|
|
-
|
|
|
- *ts_p = LSUP_graph_list (store);
|
|
|
- LUA_NLCHECK (ts_p, "Error retrieving context list.");
|
|
|
+ LSUP_TermSet *ts = LSUP_graph_list (store);
|
|
|
+ LUA_NLCHECK (ts, "Error retrieving context list.");
|
|
|
+ LUA_PCHECK (term_set_to_table (L, ts), "Error generating term set table");
|
|
|
|
|
|
return 1;
|
|
|
}
|
|
@@ -153,6 +172,46 @@ static int l_graph_contains (lua_State *L)
|
|
|
}
|
|
|
|
|
|
|
|
|
+/// Initialize iterative addition.
|
|
|
+static int l_graph_add_init (lua_State *L)
|
|
|
+{
|
|
|
+ LSUP_Graph *gr = check_graph (L);
|
|
|
+
|
|
|
+ LSUP_GraphIterator **it_p = lua_newuserdata (L, sizeof *it_p);
|
|
|
+ luaL_getmetatable (L, "LSUP.GraphIterator");
|
|
|
+ lua_setmetatable (L, -2);
|
|
|
+
|
|
|
+ *it_p = LSUP_graph_add_init (gr);
|
|
|
+ LUA_NLCHECK (*it_p, "Error creating graph iterator.");
|
|
|
+
|
|
|
+ return 1;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+/// Add one triple.
|
|
|
+static int l_graph_add_iter (lua_State *L)
|
|
|
+{
|
|
|
+ LSUP_GraphIterator **it_p = luaL_checkudata (L, 1, "LSUP.GraphIterator");
|
|
|
+ const LSUP_Triple **spo = luaL_checkudata (L, 2, "LSUP.Triple");
|
|
|
+
|
|
|
+ LUA_PCHECK (LSUP_graph_add_iter (*it_p, *spo), "Error adding triple");
|
|
|
+
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+/// Finalize iterative addition.
|
|
|
+static int l_graph_add_done (lua_State *L)
|
|
|
+{
|
|
|
+ LSUP_GraphIterator **it_p = lua_touserdata (L, 1);
|
|
|
+ LSUP_graph_add_done (*it_p);
|
|
|
+
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+/** @brief Add triples from an indexed table.
|
|
|
+ */
|
|
|
static int l_graph_add (lua_State *L)
|
|
|
{
|
|
|
LSUP_Graph *gr = check_graph (L);
|
|
@@ -243,8 +302,7 @@ static int l_graph_lookup (lua_State *L)
|
|
|
if lua_isnoneornil (L, 4) o = NULL;
|
|
|
else o = *(LSUP_Term **)luaL_checkudata (L, 4, "LSUP.Term");
|
|
|
|
|
|
- LSUP_GraphIterator **it_p =
|
|
|
- (LSUP_GraphIterator **)lua_newuserdata (L, sizeof *it_p);
|
|
|
+ LSUP_GraphIterator **it_p = lua_newuserdata (L, sizeof *it_p);
|
|
|
*it_p = NULL;
|
|
|
luaL_getmetatable (L, "LSUP.GraphIterator");
|
|
|
lua_setmetatable (L, -2);
|
|
@@ -325,12 +383,9 @@ static int l_graph_term_set (lua_State *L)
|
|
|
const LSUP_Term *t2 = *(LSUP_Term **)luaL_checkudata (L, 4, "LSUP.Term");
|
|
|
const LSUP_TriplePos t2_pos = luaL_checkinteger (L, 5);
|
|
|
|
|
|
- LSUP_TermSet **ts_p = lua_newuserdata (L, sizeof *ts_p);
|
|
|
- luaL_getmetatable (L, "LSUP.TermSet");
|
|
|
- lua_setmetatable (L, -2);
|
|
|
-
|
|
|
- *ts_p = LSUP_graph_term_set (gr, t1, t1_pos, t2, t2_pos);
|
|
|
- LUA_NLCHECK (*ts_p, "Error creating term set.");
|
|
|
+ LSUP_TermSet *ts = LSUP_graph_term_set (gr, t1, t1_pos, t2, t2_pos);
|
|
|
+ LUA_NLCHECK (ts, "Error creating term set from graph.");
|
|
|
+ LUA_PCHECK (term_set_to_table (L, ts), "Error generating term set table");
|
|
|
|
|
|
return 1;
|
|
|
}
|
|
@@ -341,12 +396,9 @@ static int l_graph_unique_terms (lua_State *L)
|
|
|
const LSUP_Graph *gr = check_graph (L);
|
|
|
const LSUP_TriplePos pos = luaL_checkinteger (L, 2);
|
|
|
|
|
|
- LSUP_TermSet **ts_p = lua_newuserdata (L, sizeof *ts_p);
|
|
|
- luaL_getmetatable (L, "LSUP.TermSet");
|
|
|
- lua_setmetatable (L, -2);
|
|
|
-
|
|
|
- *ts_p = LSUP_graph_unique_terms (gr, pos);
|
|
|
- LUA_NLCHECK (*ts_p, "Error creating term set.");
|
|
|
+ LSUP_TermSet *ts = LSUP_graph_unique_terms (gr, pos);
|
|
|
+ LUA_NLCHECK (ts, "Error creating term set from unique terms.");
|
|
|
+ LUA_PCHECK (term_set_to_table (L, ts), "Error generating term set table");
|
|
|
|
|
|
return 1;
|
|
|
}
|