py_graph.h 6.4 KB

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