py_graph.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. #ifndef _PY_GRAPH_MOD_H
  2. #define _PY_GRAPH_MOD_H
  3. #define PY_SSIZE_T_CLEAN
  4. #include <Python.h>
  5. #include <structmember.h>
  6. #include "graph.h"
  7. #include "py_triple.h"
  8. typedef struct {
  9. PyObject_HEAD
  10. LSUP_Graph *ob_struct;
  11. } GraphObject;
  12. static int
  13. Graph_init (GraphObject *self, PyObject *args, PyObject *kwargs)
  14. {
  15. unsigned char store_type;
  16. if (!PyArg_ParseTuple (args, "b", &store_type))
  17. return -1;
  18. self->ob_struct = LSUP_graph_new ((LSUP_store_type)store_type);
  19. if (!self->ob_struct) {
  20. PyErr_SetString (PyExc_ValueError, "Could not create graph.");
  21. return -1;
  22. }
  23. return 0;
  24. }
  25. static void
  26. Graph_dealloc (GraphObject *self)
  27. {
  28. LSUP_graph_free (self->ob_struct);
  29. Py_TYPE (self)->tp_free ((PyObject *) self);
  30. }
  31. static PyObject *
  32. Graph_get_uri (GraphObject *self, void *closure)
  33. {
  34. PyObject *uri = PyUnicode_FromString (
  35. LSUP_graph_uri (self->ob_struct)->data);
  36. if ( UNLIKELY (!uri)) return NULL;
  37. Py_INCREF(uri);
  38. return uri;
  39. }
  40. static int
  41. Graph_set_uri (GraphObject *self, PyObject *value, void *closure)
  42. {
  43. if (!PyObject_TypeCheck (value, &TermType)) return -1;
  44. LSUP_rc rc = LSUP_graph_set_uri (
  45. self->ob_struct, ((TermObject*)value)->ob_struct->data);
  46. return rc == LSUP_OK ? 0 : -1;
  47. }
  48. static PyGetSetDef Graph_getsetters[] = {
  49. {
  50. "uri", (getter) Graph_get_uri, (setter) Graph_set_uri,
  51. "Graph URI.", NULL
  52. },
  53. {NULL}
  54. };
  55. static PyObject *
  56. Graph_copy (PyTypeObject *cls, PyObject *src)
  57. {
  58. if (! PyObject_TypeCheck (src, cls)) return NULL;
  59. GraphObject *res = (GraphObject *) cls->tp_alloc(cls, 0);
  60. if (!res) return NULL;
  61. res->ob_struct = LSUP_graph_copy (((GraphObject *) src)->ob_struct);
  62. Py_INCREF(res);
  63. return (PyObject *) res;
  64. }
  65. static PyObject *
  66. Graph_richcmp (PyObject *self, PyObject *other, int op)
  67. {
  68. if (op != Py_EQ && op != Py_NE) Py_RETURN_NOTIMPLEMENTED;
  69. Py_RETURN_TRUE; // TODO use graph xor == 0
  70. /*
  71. PyObject *res = NULL;
  72. LSUP_Graph *t1 = ((GraphObject *) self)->ob_struct;
  73. LSUP_Graph *t2 = ((GraphObject *) other)->ob_struct;
  74. if (LSUP_graph_equals (t1, t2) && op == Py_EQ) Py_RETURN_TRUE;
  75. Py_RETURN_FALSE;
  76. */
  77. }
  78. static inline PyObject *
  79. Graph_bool_op (
  80. PyTypeObject *cls, LSUP_bool_op op, PyObject *gr1, PyObject *gr2)
  81. {
  82. Py_RETURN_NONE; //TODO Implement mdbstore bool ops
  83. /*
  84. if (! PyObject_TypeCheck (gr1, cls) || ! PyObject_TypeCheck (gr2, cls))
  85. return NULL;
  86. GraphObject *res = (GraphObject *) cls->tp_alloc (cls, 0);
  87. if (!res) return NULL;
  88. res->ob_struct = LSUP_graph_bool_op (
  89. op, ((GraphObject *) gr1)->ob_struct,
  90. ((GraphObject *) gr2)->ob_struct);
  91. Py_INCREF(res);
  92. return (PyObject *) res;
  93. */
  94. }
  95. static PyObject *
  96. Graph_add (PyObject *self, PyObject *triples)
  97. {
  98. // Triple may be any iterable.
  99. PyObject *iter = PyObject_GetIter (triples);
  100. if (! iter) {
  101. PyErr_SetString (
  102. PyExc_ValueError, "Triples object cannot be iterated.");
  103. return NULL;
  104. }
  105. PyObject *trp_obj = NULL;
  106. int rc = 0;
  107. size_t i;
  108. LSUP_SerTriple *sspo = STRP_DUMMY;
  109. LSUP_GraphIterator *it = LSUP_graph_add_stream_init (
  110. ((GraphObject *)self)->ob_struct);
  111. for (i = 0; (trp_obj = PyIter_Next (iter)); i++) {
  112. if (!PyObject_TypeCheck (trp_obj, &TripleType)) {
  113. PyErr_SetString (
  114. PyExc_ValueError, "Object is not a triple.");
  115. rc = -1;
  116. goto finalize;
  117. }
  118. TRACE ("Inserting triple #%lu\n", i);
  119. LSUP_triple_serialize (((TripleObject *)trp_obj)->ob_struct, sspo);
  120. LSUP_rc db_rc = LSUP_graph_add_stream_iter (it, sspo);
  121. if (db_rc == LSUP_OK) rc = LSUP_OK;
  122. if (UNLIKELY (db_rc < 0)) {
  123. rc = -1;
  124. goto finalize;
  125. }
  126. }
  127. finalize:
  128. LSUP_graph_add_stream_done (it);
  129. LSUP_striple_free (sspo);
  130. PyObject *ret = PyLong_FromSize_t (LSUP_graph_iter_cur (it));
  131. if (rc == 0) {
  132. Py_INCREF (ret);
  133. return ret;
  134. }
  135. return NULL;
  136. }
  137. static int Graph_remove (PyObject *self, PyObject *s, PyObject *p, PyObject *o)
  138. {
  139. return 0;
  140. }
  141. static PyObject *Graph_lookup (
  142. PyObject *self, PyObject *s, PyObject *p, PyObject *o)
  143. {
  144. return NULL;
  145. }
  146. static PyMethodDef Graph_methods[] = {
  147. {"copy", (PyCFunction) Graph_copy, METH_CLASS, "Copy a graph."},
  148. {"add", (PyCFunction) Graph_add, METH_O, "Add triples to a graph."},
  149. {
  150. "remove", (PyCFunction) Graph_remove, METH_VARARGS,
  151. "Remove triples from a graph by matching a pattern."
  152. },
  153. {
  154. "lookup", (PyCFunction) Graph_lookup, METH_VARARGS,
  155. "Look triples in a graph by matching a pattern."
  156. },
  157. {NULL},
  158. };
  159. static inline PyObject *Graph_bool_and (
  160. PyTypeObject *cls, PyObject *gr1, PyObject *gr2)
  161. { return Graph_bool_op (cls, LSUP_BOOL_INTERSECTION, gr1, gr2); }
  162. static inline PyObject *Graph_bool_or (
  163. PyTypeObject *cls, PyObject *gr1, PyObject *gr2)
  164. { return Graph_bool_op (cls, LSUP_BOOL_UNION, gr1, gr2); }
  165. static inline PyObject *Graph_bool_subtract (
  166. PyTypeObject *cls, PyObject *gr1, PyObject *gr2)
  167. { return Graph_bool_op (cls, LSUP_BOOL_SUBTRACTION, gr1, gr2); }
  168. static inline PyObject *Graph_bool_xor (
  169. PyTypeObject *cls, PyObject *gr1, PyObject *gr2)
  170. { return Graph_bool_op (cls, LSUP_BOOL_XOR, gr1, gr2); }
  171. static PyNumberMethods Graph_number_methods = {
  172. .nb_and = (binaryfunc) Graph_bool_and,
  173. .nb_or = (binaryfunc) Graph_bool_or,
  174. .nb_subtract = (binaryfunc) Graph_bool_subtract,
  175. .nb_xor = (binaryfunc) Graph_bool_xor,
  176. };
  177. static Py_ssize_t
  178. Graph_get_size (PyObject *self)
  179. { return LSUP_graph_size (((GraphObject *) self)->ob_struct); }
  180. static PySequenceMethods Graph_seq_methods = {
  181. .sq_length = (lenfunc) Graph_get_size,
  182. //.sq_contains = Graph_contains, // TODO
  183. };
  184. PyTypeObject GraphType = {
  185. PyVarObject_HEAD_INIT(NULL, 0)
  186. .tp_name = "graph.Graph",
  187. .tp_doc = "RDF graph",
  188. .tp_basicsize = sizeof (GraphObject),
  189. .tp_itemsize = 0,
  190. .tp_flags = Py_TPFLAGS_DEFAULT,
  191. .tp_new = PyType_GenericNew,
  192. .tp_init = (initproc) Graph_init,
  193. .tp_dealloc = (destructor) Graph_dealloc,
  194. .tp_getset = Graph_getsetters,
  195. .tp_methods = Graph_methods,
  196. .tp_richcompare = (richcmpfunc) Graph_richcmp,
  197. .tp_as_number = &Graph_number_methods,
  198. .tp_as_sequence = &Graph_seq_methods,
  199. };
  200. #endif