durable_rules 0.34.57 → 2.00.001

Sign up to get free protection for your applications and to get access to all the features.
data/src/rules/state.h CHANGED
@@ -1,34 +1,77 @@
1
1
 
2
+ #include <time.h>
3
+
2
4
  #define HASH_ID 926444256
3
5
  #define HASH_SID 3593476751
4
6
  #define UNDEFINED_INDEX 0xFFFFFFFF
5
- #define MAX_NAME_LENGTH 256
6
7
  #define SID_BUFFER_LENGTH 2
7
8
  #define ID_BUFFER_LENGTH 22
8
- #define UNDEFINED_HASH_OFFSET 0xFFFFFFFF
9
9
 
10
- #define JSON_OBJECT_SEQUENCED 1
11
- #define JSON_OBJECT_HASHED 2
10
+ #define MESSAGE_TYPE_EVENT 0
11
+ #define MESSAGE_TYPE_FACT 1
12
+
13
+ #define UNDEFINED_HASH_OFFSET 0
14
+ #define MAX_OBJECT_PROPERTIES 32
15
+ #define MAX_MESSAGE_FRAMES 16
16
+ #define MAX_MESSAGE_INDEX_LENGTH 512
17
+ #define MAX_LEFT_FRAME_INDEX_LENGTH 512
18
+ #define MAX_RIGHT_FRAME_INDEX_LENGTH 512
19
+ #define MAX_LOCATION_INDEX_LENGTH 16
20
+
21
+ #define LEFT_FRAME 0
22
+ #define RIGHT_FRAME 1
23
+ #define A_FRAME 2
24
+ #define B_FRAME 3
25
+ #define ACTION_FRAME 4
26
+
27
+ #define STATE_LEASE_TIME 30
28
+
29
+ #define MESSAGE_NODE(state, offset) &((messageNode *)state->messagePool.content)[offset]
30
+
31
+ #define RIGHT_FRAME_NODE(state, index, offset) &((rightFrameNode *)state->betaState[index].rightFramePool.content)[offset]
32
+
33
+ #define LEFT_FRAME_NODE(state, index, offset) &((leftFrameNode *)state->betaState[index].leftFramePool.content)[offset]
34
+
35
+ #define A_FRAME_NODE(state, index, offset) &((leftFrameNode *)state->connectorState[index].aFramePool.content)[offset]
36
+
37
+ #define B_FRAME_NODE(state, index, offset) &((leftFrameNode *)state->connectorState[index].bFramePool.content)[offset]
12
38
 
13
- #define MAX_OBJECT_PROPERTIES 128
39
+ #define ACTION_FRAME_NODE(state, index, offset) &((leftFrameNode *)state->actionState[index].resultPool.content)[offset]
40
+
41
+ #define RESULT_FRAME(actionNode, offset) &((leftFrameNode *)actionNode->resultPool.content)[offset];
42
+
43
+ #define STATE_NODE(tree, offset) &((stateNode *)((ruleset *)tree)->statePool.content)[offset]
44
+
45
+ #define LOCATION_NODE(message, offset) &((locationNode *)message->locationPool.content)[offset]
46
+
47
+
48
+ // defined in rete.h
49
+ struct node;
50
+
51
+ typedef struct pool {
52
+ void *content;
53
+ unsigned int freeOffset;
54
+ unsigned int contentLength;
55
+ unsigned int count;
56
+ } pool;
14
57
 
15
58
  typedef struct jsonProperty {
16
59
  unsigned int hash;
17
60
  unsigned char type;
18
- unsigned char isMaterial;
19
- char *valueString;
61
+ unsigned short valueOffset;
20
62
  unsigned short valueLength;
21
- char name[MAX_NAME_LENGTH];
22
- unsigned short nameLength;
23
63
  union {
24
64
  long i;
25
65
  double d;
26
66
  unsigned char b;
67
+ char *s;
27
68
  } value;
28
69
  } jsonProperty;
29
70
 
30
71
  typedef struct jsonObject {
72
+ char *content;
31
73
  jsonProperty properties[MAX_OBJECT_PROPERTIES];
74
+ unsigned short propertyIndex[MAX_OBJECT_PROPERTIES];
32
75
  unsigned char propertiesLength;
33
76
  unsigned int idIndex;
34
77
  unsigned int sidIndex;
@@ -36,28 +79,120 @@ typedef struct jsonObject {
36
79
  char idBuffer[ID_BUFFER_LENGTH];
37
80
  } jsonObject;
38
81
 
39
- typedef struct stateEntry {
40
- unsigned int nextHashOffset;
41
- unsigned int nextLruOffset;
42
- unsigned int prevLruOffset;
43
- unsigned int sidHash;
44
- unsigned int bindingIndex;
45
- unsigned int lastRefresh;
46
- char *state;
47
- char *sid;
82
+ typedef struct frameLocation {
83
+ unsigned char frameType;
84
+ unsigned int nodeIndex;
85
+ unsigned int frameOffset;
86
+ } frameLocation;
87
+
88
+ typedef struct locationNode {
89
+ unsigned int prevOffset;
90
+ unsigned int nextOffset;
91
+ unsigned int hash;
92
+ frameLocation location;
93
+ unsigned char isActive;
94
+ } locationNode;
95
+
96
+ typedef struct messageNode {
97
+ unsigned int prevOffset;
98
+ unsigned int nextOffset;
99
+ unsigned int hash;
100
+ unsigned char isActive;
101
+ unsigned char messageType;
102
+ pool locationPool;
103
+ unsigned int locationIndex[MAX_LOCATION_INDEX_LENGTH * 2];
48
104
  jsonObject jo;
49
- } stateEntry;
105
+ } messageNode;
106
+
107
+ typedef struct messageFrame {
108
+ unsigned int hash;
109
+ unsigned int nameOffset;
110
+ unsigned int messageNodeOffset;
111
+ } messageFrame;
112
+
113
+ typedef struct leftFrameNode {
114
+ unsigned int prevOffset;
115
+ unsigned int nextOffset;
116
+ unsigned int nameOffset;
117
+ unsigned int hash;
118
+ unsigned char isActive;
119
+ unsigned char isDispatching;
120
+ unsigned short messageCount;
121
+ unsigned short reverseIndex[MAX_MESSAGE_FRAMES];
122
+ messageFrame messages[MAX_MESSAGE_FRAMES];
123
+ } leftFrameNode;
124
+
125
+ typedef struct rightFrameNode {
126
+ unsigned int prevOffset;
127
+ unsigned int nextOffset;
128
+ unsigned int hash;
129
+ unsigned char isActive;
130
+ unsigned int messageOffset;
131
+ } rightFrameNode;
132
+
133
+ typedef struct actionStateNode {
134
+ struct node *reteNode;
135
+ pool resultPool;
136
+ unsigned int resultIndex[2];
137
+ } actionStateNode;
138
+
139
+ typedef struct connectorStateNode {
140
+ struct node *reteNode;
141
+ pool aFramePool;
142
+ unsigned int aFrameIndex[2];
143
+ pool bFramePool;
144
+ unsigned int bFrameIndex[2];
145
+ } connectorStateNode;
146
+
147
+ typedef struct betaStateNode {
148
+ struct node *reteNode;
149
+ pool leftFramePool;
150
+ unsigned int leftFrameIndex[MAX_LEFT_FRAME_INDEX_LENGTH * 2];
151
+ pool rightFramePool;
152
+ unsigned int rightFrameIndex[MAX_RIGHT_FRAME_INDEX_LENGTH * 2];
153
+ } betaStateNode;
154
+
155
+ typedef struct actionContext {
156
+ unsigned int actionStateIndex;
157
+ unsigned int resultCount;
158
+ unsigned int resultFrameOffset;
159
+ char *messages;
160
+ char *stateFact;
161
+ } actionContext;
162
+
163
+ typedef struct stateNode {
164
+ char *sid;
165
+ time_t lockExpireTime;
166
+ unsigned int offset;
167
+ unsigned int prevOffset;
168
+ unsigned int nextOffset;
169
+ unsigned int factOffset;
170
+ unsigned int hash;
171
+ unsigned char isActive;
172
+ pool messagePool;
173
+ unsigned int messageIndex[MAX_MESSAGE_INDEX_LENGTH * 2];
174
+ betaStateNode *betaState;
175
+ actionStateNode *actionState;
176
+ connectorStateNode *connectorState;
177
+ actionContext context;
178
+ } stateNode;
179
+
50
180
 
51
181
  unsigned int fnv1Hash32(char *str, unsigned int len);
52
182
 
53
- void rehydrateProperty(jsonProperty *property, char *state);
183
+ unsigned int getObjectProperty(jsonObject *jo,
184
+ unsigned int hash,
185
+ jsonProperty **property);
54
186
 
55
- unsigned int refreshState(void *tree, char *sid);
187
+ unsigned int setObjectProperty(jsonObject *jo,
188
+ unsigned int hash,
189
+ unsigned char type,
190
+ unsigned short valueOffset,
191
+ unsigned short valueLength);
56
192
 
57
193
  unsigned int constructObject(char *root,
58
194
  char *parentName,
59
195
  char *object,
60
- char layout,
61
196
  char generateId,
62
197
  jsonObject *jo,
63
198
  char **next);
@@ -66,14 +201,153 @@ unsigned int resolveBinding(void *tree,
66
201
  char *sid,
67
202
  void **rulesBinding);
68
203
 
69
- unsigned int fetchStateProperty(void *tree,
70
- char *sid,
71
- unsigned int propertyHash,
72
- unsigned int maxTime,
73
- unsigned char ignoreStaleState,
74
- char **state,
75
- jsonProperty **property);
76
204
 
77
- unsigned int getStateVersion(void *tree,
205
+ unsigned int getHash(char *sid, char *key);
206
+
207
+ unsigned int initStatePool(void *tree);
208
+
209
+ unsigned int addFrameLocation(stateNode *state,
210
+ frameLocation location,
211
+ unsigned int messageNodeOffset);
212
+
213
+ unsigned int deleteFrameLocation(stateNode *state,
214
+ unsigned int messageNodeOffset,
215
+ frameLocation location);
216
+
217
+
218
+ unsigned int deleteMessageFromFrame(unsigned int messageNodeOffset,
219
+ leftFrameNode *frame);
220
+
221
+ unsigned int getMessageFromFrame(stateNode *state,
222
+ messageFrame *messages,
223
+ unsigned int hash,
224
+ jsonObject **message);
225
+
226
+ unsigned int setMessageInFrame(leftFrameNode *node,
227
+ unsigned int nameOffset,
228
+ unsigned int hash,
229
+ unsigned int messageNodeOffset);
230
+
231
+ unsigned int getLastLeftFrame(stateNode *state,
232
+ unsigned int index,
233
+ unsigned int hash,
234
+ frameLocation *location,
235
+ leftFrameNode **node);
236
+
237
+ unsigned int setLeftFrame(stateNode *state,
238
+ unsigned int hash,
239
+ frameLocation location);
240
+
241
+
242
+ unsigned int deleteLeftFrame(stateNode *state,
243
+ frameLocation location);
244
+
245
+ unsigned int createLeftFrame(stateNode *state,
246
+ struct node *reteNode,
247
+ leftFrameNode *oldNode,
248
+ leftFrameNode **newNode,
249
+ frameLocation *newLocation);
250
+
251
+ unsigned int getLastConnectorFrame(stateNode *state,
252
+ unsigned int frameType,
253
+ unsigned int index,
254
+ unsigned int *valueOffset,
255
+ leftFrameNode **node);
256
+
257
+ unsigned int setConnectorFrame(stateNode *state,
258
+ unsigned int frameType,
259
+ frameLocation location);
260
+
261
+
262
+ unsigned int deleteConnectorFrame(stateNode *state,
263
+ unsigned int frameType,
264
+ frameLocation location);
265
+
266
+ unsigned int createConnectorFrame(stateNode *state,
267
+ unsigned int frameType,
268
+ struct node *reteNode,
269
+ leftFrameNode *oldNode,
270
+ leftFrameNode **newNode,
271
+ frameLocation *newLocation);
272
+
273
+ unsigned int getLastRightFrame(stateNode *state,
274
+ unsigned int index,
275
+ unsigned int hash,
276
+ rightFrameNode **node);
277
+
278
+ unsigned int setRightFrame(stateNode *state,
279
+ unsigned int hash,
280
+ frameLocation location);
281
+
282
+ unsigned int deleteRightFrame(stateNode *state,
283
+ frameLocation location);
284
+
285
+ unsigned int createRightFrame(stateNode *state,
286
+ struct node *reteNode,
287
+ rightFrameNode **node,
288
+ frameLocation *location);
289
+
290
+ unsigned int getActionFrame(stateNode *state,
291
+ frameLocation resultLocation,
292
+ leftFrameNode **resultNode);
293
+
294
+ unsigned int setActionFrame(stateNode *state,
295
+ frameLocation location);
296
+
297
+
298
+ unsigned int deleteActionFrame(stateNode *state,
299
+ frameLocation location);
300
+
301
+ unsigned int deleteDispatchingActionFrame(stateNode *state,
302
+ frameLocation location);
303
+
304
+ unsigned int createActionFrame(stateNode *state,
305
+ struct node *reteNode,
306
+ leftFrameNode *oldNode,
307
+ leftFrameNode **newNode,
308
+ frameLocation *newLocation);
309
+
310
+ unsigned int deleteMessage(stateNode *state,
311
+ unsigned int messageNodeOffset);
312
+
313
+ unsigned int getMessage(stateNode *state,
314
+ char *mid,
315
+ unsigned int *valueOffset);
316
+
317
+ unsigned int storeMessage(stateNode *state,
318
+ char *mid,
319
+ jsonObject *message,
320
+ unsigned char messageType,
321
+ unsigned int *valueOffset);
322
+
323
+ unsigned int ensureStateNode(void *tree,
78
324
  char *sid,
79
- unsigned long *stateVersion);
325
+ unsigned char *isNew,
326
+ stateNode **state);
327
+
328
+ unsigned int serializeResult(void *tree,
329
+ stateNode *state,
330
+ actionStateNode *actionNode,
331
+ unsigned int count,
332
+ char **result);
333
+
334
+ unsigned int serializeState(stateNode *state,
335
+ char **stateFact);
336
+
337
+ unsigned int getNextResultInState(void *tree,
338
+ stateNode *state,
339
+ unsigned int *actionStateIndex,
340
+ unsigned int *resultCount,
341
+ unsigned int *resultFrameOffset,
342
+ actionStateNode **resultAction);
343
+
344
+ unsigned int getNextResult(void *tree,
345
+ time_t currentTime,
346
+ stateNode **resultState,
347
+ unsigned int *actionStateIndex,
348
+ unsigned int *resultCount,
349
+ unsigned int *resultFrameOffset,
350
+ actionStateNode **resultAction);
351
+
352
+
353
+
@@ -4,7 +4,6 @@ RbConfig::MAKEFILE_CONFIG['CC'] = ENV['CC'] if ENV['CC']
4
4
 
5
5
  rules_include = File.join(File.dirname(__FILE__), %w{.. .. src rules})
6
6
  rules_lib = File.join(File.dirname(__FILE__), %w{.. .. src rules})
7
- hiredis_lib = File.join(File.dirname(__FILE__), %w{.. .. deps hiredis})
8
7
 
9
8
  RbConfig::CONFIG['configure_args'] =~ /with-make-prog\=(\w+)/
10
9
  make_program = $1 || ENV['make']
@@ -17,12 +16,6 @@ else
17
16
  'make'
18
17
  end
19
18
 
20
- # Make sure hiredis is built...
21
- Dir.chdir(hiredis_lib) do
22
- success = system("#{make_program} static")
23
- raise "Building hiredis failed" if !success
24
- end
25
-
26
19
  # Make sure rules is built...
27
20
  Dir.chdir(rules_lib) do
28
21
  success = system("#{make_program} static")
@@ -31,7 +24,7 @@ end
31
24
 
32
25
  # Statically link to hiredis (mkmf can't do this for us)
33
26
  $CFLAGS << " -I#{rules_lib} "
34
- $LDFLAGS << " -L#{rules_lib} -L#{hiredis_lib} -lrules -lhiredis"
27
+ $LDFLAGS << " -L#{rules_lib} -lrules"
35
28
 
36
29
  have_func("rb_thread_fd_select")
37
30
  create_makefile('src/rulesrb/rules')
data/src/rulesrb/rules.c CHANGED
@@ -3,12 +3,12 @@
3
3
 
4
4
  VALUE rulesModule = Qnil;
5
5
 
6
- static VALUE rbCreateRuleset(VALUE self, VALUE name, VALUE rules, VALUE stateCacheSize) {
6
+ static VALUE rbCreateRuleset(VALUE self, VALUE name, VALUE rules) {
7
7
  Check_Type(name, T_STRING);
8
8
  Check_Type(rules, T_STRING);
9
9
 
10
- void *output = NULL;
11
- unsigned int result = createRuleset(&output, RSTRING_PTR(name), RSTRING_PTR(rules), FIX2INT(stateCacheSize));
10
+ unsigned int output = 0;
11
+ unsigned int result = createRuleset(&output, RSTRING_PTR(name), RSTRING_PTR(rules));
12
12
  if (result != RULES_OK) {
13
13
  if (result == ERR_OUT_OF_MEMORY) {
14
14
  rb_raise(rb_eNoMemError, "Out of memory");
@@ -35,110 +35,17 @@ static VALUE rbDeleteRuleset(VALUE self, VALUE handle) {
35
35
  return Qnil;
36
36
  }
37
37
 
38
- static VALUE rbCreateClient(VALUE self, VALUE name, VALUE stateCacheSize) {
39
- Check_Type(name, T_STRING);
40
- Check_Type(stateCacheSize, T_FIXNUM);
41
-
42
- void *output = NULL;
43
- unsigned int result = createClient(&output, RSTRING_PTR(name), FIX2INT(stateCacheSize));
44
- if (result != RULES_OK) {
45
- if (result == ERR_OUT_OF_MEMORY) {
46
- rb_raise(rb_eNoMemError, "Out of memory");
47
- } else {
48
- rb_raise(rb_eException, "Could not create client, error code: %d", result);
49
- }
50
- }
51
-
52
- return INT2FIX(output);
53
- }
54
-
55
- static VALUE rbDeleteClient(VALUE self, VALUE handle) {
56
- Check_Type(handle, T_FIXNUM);
57
-
58
- unsigned int result = deleteClient((void *)FIX2LONG(handle));
59
- if (result != RULES_OK) {
60
- if (result == ERR_OUT_OF_MEMORY) {
61
- rb_raise(rb_eNoMemError, "Out of memory");
62
- } else {
63
- rb_raise(rb_eException, "Could not delete client, error code: %d", result);
64
- }
65
- }
66
-
67
- return Qnil;
68
- }
69
-
70
- static VALUE rbBindRuleset(VALUE self, VALUE handle, VALUE host, VALUE port, VALUE password, VALUE db) {
71
- Check_Type(handle, T_FIXNUM);
72
- Check_Type(host, T_STRING);
73
- Check_Type(port, T_FIXNUM);
74
- Check_Type(db, T_FIXNUM);
75
-
76
- unsigned int result;
77
- if (TYPE(password) == T_STRING) {
78
- result = bindRuleset(FIX2INT(handle), RSTRING_PTR(host), FIX2INT(port), RSTRING_PTR(password), FIX2INT(db));
79
- } else if (TYPE(password) == T_NIL) {
80
- result = bindRuleset(FIX2INT(handle), RSTRING_PTR(host), FIX2INT(port), NULL, FIX2INT(db));
81
- } else {
82
- rb_raise(rb_eTypeError, "Wrong argument type for password");
83
- }
84
-
85
- if (result != RULES_OK) {
86
- if (result == ERR_OUT_OF_MEMORY) {
87
- rb_raise(rb_eNoMemError, "Out of memory");
88
- } else {
89
- rb_raise(rb_eException, "Could not create connection, error code: %d", result);
90
- }
91
- }
92
-
93
- return Qnil;
94
- }
95
-
96
- static VALUE rbComplete(VALUE self, VALUE rulesBinding, VALUE replyCount) {
97
- Check_Type(rulesBinding, T_FIXNUM);
98
- Check_Type(replyCount, T_FIXNUM);
99
-
100
- unsigned int result = complete((void *)FIX2LONG(rulesBinding), FIX2LONG(replyCount));
101
- if (result != RULES_OK) {
102
- if (result == ERR_OUT_OF_MEMORY) {
103
- rb_raise(rb_eNoMemError, "Out of memory");
104
- } else {
105
- rb_raise(rb_eException, "Could not complete action, error code: %d", result);
106
- }
107
- }
108
-
109
- return Qnil;
110
- }
111
-
112
- static VALUE rbStartAssertEvent(VALUE self, VALUE handle, VALUE event) {
113
- Check_Type(handle, T_FIXNUM);
114
- Check_Type(event, T_STRING);
115
-
116
- unsigned int replyCount;
117
- void *rulesBinding = NULL;
118
- unsigned int result = startAssertEvent(FIX2INT(handle), RSTRING_PTR(event), &rulesBinding, &replyCount);
119
- if (result == RULES_OK || result == ERR_EVENT_NOT_HANDLED) {
120
- VALUE output = rb_ary_new();
121
- rb_ary_push(output, INT2FIX(rulesBinding));
122
- rb_ary_push(output, INT2FIX(replyCount));
123
- return output;
124
- } else {
125
- if (result == ERR_OUT_OF_MEMORY) {
126
- rb_raise(rb_eNoMemError, "Out of memory");
127
- } else {
128
- rb_raise(rb_eException, "Could not assert event, error code: %d", result);
129
- }
130
- }
131
-
132
- return Qnil;
133
- }
134
-
135
38
  static VALUE rbAssertEvent(VALUE self, VALUE handle, VALUE event) {
136
39
  Check_Type(handle, T_FIXNUM);
137
40
  Check_Type(event, T_STRING);
138
41
 
139
- unsigned int result = assertEvent(FIX2INT(handle), RSTRING_PTR(event));
42
+ unsigned int stateOffset;
43
+ unsigned int result = assertEvent(FIX2INT(handle), RSTRING_PTR(event), &stateOffset);
140
44
  if (result == RULES_OK || result == ERR_EVENT_NOT_HANDLED || result == ERR_EVENT_OBSERVED) {
141
- return INT2FIX(result);
45
+ VALUE output = rb_ary_new();
46
+ rb_ary_push(output, INT2FIX(result));
47
+ rb_ary_push(output, INT2FIX(stateOffset));
48
+ return output;
142
49
  } else {
143
50
  if (result == ERR_OUT_OF_MEMORY) {
144
51
  rb_raise(rb_eNoMemError, "Out of memory");
@@ -150,100 +57,23 @@ static VALUE rbAssertEvent(VALUE self, VALUE handle, VALUE event) {
150
57
  return Qnil;
151
58
  }
152
59
 
153
- static VALUE rbQueueAssertEvent(VALUE self, VALUE handle, VALUE sid, VALUE destination, VALUE event) {
154
- Check_Type(handle, T_FIXNUM);
155
- Check_Type(sid, T_STRING);
156
- Check_Type(destination, T_STRING);
157
- Check_Type(event, T_STRING);
158
-
159
- unsigned int result = queueMessage(FIX2INT(handle), QUEUE_ASSERT_EVENT, RSTRING_PTR(sid), RSTRING_PTR(destination), RSTRING_PTR(event));
160
- if (result != RULES_OK) {
161
- if (result == ERR_OUT_OF_MEMORY) {
162
- rb_raise(rb_eNoMemError, "Out of memory");
163
- } else {
164
- rb_raise(rb_eException, "Could not queue assert event, error code: %d", result);
165
- }
166
- }
167
-
168
- return Qnil;
169
- }
170
-
171
- static VALUE rbStartAssertEvents(VALUE self, VALUE handle, VALUE events) {
172
- Check_Type(handle, T_FIXNUM);
173
- Check_Type(events, T_STRING);
174
-
175
- unsigned int replyCount;
176
- void *rulesBinding = NULL;
177
- unsigned int result = startAssertEvents(FIX2INT(handle), RSTRING_PTR(events), &rulesBinding, &replyCount);
178
- if (result == RULES_OK || result == ERR_EVENT_NOT_HANDLED) {
179
- VALUE output = rb_ary_new();
180
- rb_ary_push(output, INT2FIX(rulesBinding));
181
- rb_ary_push(output, INT2FIX(replyCount));
182
- return output;
183
- } else {
184
- if (result == ERR_OUT_OF_MEMORY) {
185
- rb_raise(rb_eNoMemError, "Out of memory");
186
- } else {
187
- rb_raise(rb_eException, "Could not assert events, error code: %d", result);
188
- }
189
- }
190
-
191
- return Qnil;
192
- }
193
60
 
194
61
  static VALUE rbAssertEvents(VALUE self, VALUE handle, VALUE events) {
195
62
  Check_Type(handle, T_FIXNUM);
196
63
  Check_Type(events, T_STRING);
197
64
 
198
- unsigned int result = assertEvents(FIX2INT(handle), RSTRING_PTR(events));
65
+ unsigned int stateOffset;
66
+ unsigned int result = assertEvents(FIX2INT(handle), RSTRING_PTR(events), &stateOffset);
199
67
  if (result == RULES_OK || result == ERR_EVENT_NOT_HANDLED || result == ERR_EVENT_OBSERVED) {
200
- return INT2FIX(result);
201
- } else {
202
- if (result == ERR_OUT_OF_MEMORY) {
203
- rb_raise(rb_eNoMemError, "Out of memory");
204
- } else {
205
- rb_raise(rb_eException, "Could not assert events, error code: %d", result);
206
- }
207
- }
208
-
209
- return Qnil;
210
- }
211
-
212
- static VALUE rbRetractEvent(VALUE self, VALUE handle, VALUE event) {
213
- Check_Type(handle, T_FIXNUM);
214
- Check_Type(event, T_STRING);
215
-
216
- unsigned int result = retractEvent(FIX2INT(handle), RSTRING_PTR(event));
217
- if (result == RULES_OK || result == ERR_EVENT_NOT_HANDLED || result == ERR_EVENT_OBSERVED) {
218
- return INT2FIX(result);
219
- } else {
220
- if (result == ERR_OUT_OF_MEMORY) {
221
- rb_raise(rb_eNoMemError, "Out of memory");
222
- } else {
223
- rb_raise(rb_eException, "Could not retract event, error code: %d", result);
224
- }
225
- }
226
-
227
- return Qnil;
228
- }
229
-
230
- static VALUE rbStartAssertFact(VALUE self, VALUE handle, VALUE fact) {
231
- Check_Type(handle, T_FIXNUM);
232
- Check_Type(fact, T_STRING);
233
-
234
- unsigned int replyCount;
235
- void *rulesBinding = NULL;
236
- unsigned int result = startAssertFact(FIX2INT(handle), RSTRING_PTR(fact), &rulesBinding, &replyCount);
237
- if (result == RULES_OK || result == ERR_EVENT_NOT_HANDLED) {
238
68
  VALUE output = rb_ary_new();
239
- rb_ary_push(output, INT2FIX(rulesBinding));
240
- rb_ary_push(output, INT2FIX(replyCount));
241
- return output;
69
+ rb_ary_push(output, INT2FIX(result));
70
+ rb_ary_push(output, INT2FIX(stateOffset));
71
+ return output;
242
72
  } else {
243
73
  if (result == ERR_OUT_OF_MEMORY) {
244
74
  rb_raise(rb_eNoMemError, "Out of memory");
245
75
  } else {
246
- rb_raise(rb_eException, "Could not assert fact, error code: %d", result);
76
+ rb_raise(rb_eException, "Could not assert events, error code: %d", result);
247
77
  }
248
78
  }
249
79
 
@@ -254,55 +84,18 @@ static VALUE rbAssertFact(VALUE self, VALUE handle, VALUE fact) {
254
84
  Check_Type(handle, T_FIXNUM);
255
85
  Check_Type(fact, T_STRING);
256
86
 
257
- unsigned int result = assertFact(FIX2INT(handle), RSTRING_PTR(fact));
87
+ unsigned int stateOffset;
88
+ unsigned int result = assertFact(FIX2INT(handle), RSTRING_PTR(fact), &stateOffset);
258
89
  if (result == RULES_OK || result == ERR_EVENT_NOT_HANDLED || result == ERR_EVENT_OBSERVED) {
259
- return INT2FIX(result);
260
- } else {
261
- if (result == ERR_OUT_OF_MEMORY) {
262
- rb_raise(rb_eNoMemError, "Out of memory");
263
- } else {
264
- rb_raise(rb_eException, "Could not assert fact, error code: %d", result);
265
- }
266
- }
267
-
268
- return Qnil;
269
- }
270
-
271
- static VALUE rbQueueAssertFact(VALUE self, VALUE handle, VALUE sid, VALUE destination, VALUE event) {
272
- Check_Type(handle, T_FIXNUM);
273
- Check_Type(sid, T_STRING);
274
- Check_Type(destination, T_STRING);
275
- Check_Type(event, T_STRING);
276
-
277
- unsigned int result = queueMessage(FIX2INT(handle), QUEUE_ASSERT_FACT, RSTRING_PTR(sid), RSTRING_PTR(destination), RSTRING_PTR(event));
278
- if (result != RULES_OK) {
279
- if (result == ERR_OUT_OF_MEMORY) {
280
- rb_raise(rb_eNoMemError, "Out of memory");
281
- } else {
282
- rb_raise(rb_eException, "Could not queue assert fact, error code: %d", result);
283
- }
284
- }
285
-
286
- return Qnil;
287
- }
288
-
289
- static VALUE rbStartAssertFacts(VALUE self, VALUE handle, VALUE facts) {
290
- Check_Type(handle, T_FIXNUM);
291
- Check_Type(facts, T_STRING);
292
-
293
- unsigned int replyCount;
294
- void *rulesBinding = NULL;
295
- unsigned int result = startAssertFacts(FIX2INT(handle), RSTRING_PTR(facts), &rulesBinding, &replyCount);
296
- if (result == RULES_OK || result == ERR_EVENT_NOT_HANDLED) {
297
90
  VALUE output = rb_ary_new();
298
- rb_ary_push(output, INT2FIX(rulesBinding));
299
- rb_ary_push(output, INT2FIX(replyCount));
300
- return output;
91
+ rb_ary_push(output, INT2FIX(result));
92
+ rb_ary_push(output, INT2FIX(stateOffset));
93
+ return output;
301
94
  } else {
302
95
  if (result == ERR_OUT_OF_MEMORY) {
303
96
  rb_raise(rb_eNoMemError, "Out of memory");
304
97
  } else {
305
- rb_raise(rb_eException, "Could not assert facts, error code: %d", result);
98
+ rb_raise(rb_eException, "Could not assert fact, error code: %d", result);
306
99
  }
307
100
  }
308
101
 
@@ -313,37 +106,18 @@ static VALUE rbAssertFacts(VALUE self, VALUE handle, VALUE facts) {
313
106
  Check_Type(handle, T_FIXNUM);
314
107
  Check_Type(facts, T_STRING);
315
108
 
316
- unsigned int result = assertFacts(FIX2INT(handle), RSTRING_PTR(facts));
109
+ unsigned int stateOffset;
110
+ unsigned int result = assertFacts(FIX2INT(handle), RSTRING_PTR(facts), &stateOffset);
317
111
  if (result == RULES_OK || result == ERR_EVENT_NOT_HANDLED || result == ERR_EVENT_OBSERVED) {
318
- return INT2FIX(result);
319
- } else {
320
- if (result == ERR_OUT_OF_MEMORY) {
321
- rb_raise(rb_eNoMemError, "Out of memory");
322
- } else {
323
- rb_raise(rb_eException, "Could not assert facts, error code: %d", result);
324
- }
325
- }
326
-
327
- return Qnil;
328
- }
329
-
330
- static VALUE rbStartRetractFact(VALUE self, VALUE handle, VALUE fact) {
331
- Check_Type(handle, T_FIXNUM);
332
- Check_Type(fact, T_STRING);
333
-
334
- unsigned int replyCount;
335
- void *rulesBinding = NULL;
336
- unsigned int result = startRetractFact(FIX2INT(handle), RSTRING_PTR(fact), &rulesBinding, &replyCount);
337
- if (result == RULES_OK || result == ERR_EVENT_NOT_HANDLED) {
338
112
  VALUE output = rb_ary_new();
339
- rb_ary_push(output, INT2FIX(rulesBinding));
340
- rb_ary_push(output, INT2FIX(replyCount));
341
- return output;
113
+ rb_ary_push(output, INT2FIX(result));
114
+ rb_ary_push(output, INT2FIX(stateOffset));
115
+ return output;
342
116
  } else {
343
117
  if (result == ERR_OUT_OF_MEMORY) {
344
118
  rb_raise(rb_eNoMemError, "Out of memory");
345
119
  } else {
346
- rb_raise(rb_eException, "Could not retract fact, error code: %d", result);
120
+ rb_raise(rb_eException, "Could not assert facts, error code: %d", result);
347
121
  }
348
122
  }
349
123
 
@@ -354,9 +128,13 @@ static VALUE rbRetractFact(VALUE self, VALUE handle, VALUE fact) {
354
128
  Check_Type(handle, T_FIXNUM);
355
129
  Check_Type(fact, T_STRING);
356
130
 
357
- unsigned int result = retractFact(FIX2INT(handle), RSTRING_PTR(fact));
131
+ unsigned int stateOffset;
132
+ unsigned int result = retractFact(FIX2INT(handle), RSTRING_PTR(fact), &stateOffset);
358
133
  if (result == RULES_OK || result == ERR_EVENT_NOT_HANDLED || result == ERR_EVENT_OBSERVED) {
359
- return INT2FIX(result);
134
+ VALUE output = rb_ary_new();
135
+ rb_ary_push(output, INT2FIX(result));
136
+ rb_ary_push(output, INT2FIX(stateOffset));
137
+ return output;
360
138
  } else {
361
139
  if (result == ERR_OUT_OF_MEMORY) {
362
140
  rb_raise(rb_eNoMemError, "Out of memory");
@@ -368,36 +146,17 @@ static VALUE rbRetractFact(VALUE self, VALUE handle, VALUE fact) {
368
146
  return Qnil;
369
147
  }
370
148
 
371
- static VALUE rbQueueRetractFact(VALUE self, VALUE handle, VALUE sid, VALUE destination, VALUE event) {
372
- Check_Type(handle, T_FIXNUM);
373
- Check_Type(sid, T_STRING);
374
- Check_Type(destination, T_STRING);
375
- Check_Type(event, T_STRING);
376
-
377
- unsigned int result = queueMessage(FIX2INT(handle), QUEUE_RETRACT_FACT, RSTRING_PTR(sid), RSTRING_PTR(destination), RSTRING_PTR(event));
378
- if (result != RULES_OK) {
379
- if (result == ERR_OUT_OF_MEMORY) {
380
- rb_raise(rb_eNoMemError, "Out of memory");
381
- } else {
382
- rb_raise(rb_eException, "Could not queue retract fact, error code: %d", result);
383
- }
384
- }
385
-
386
- return Qnil;
387
- }
388
-
389
- static VALUE rbStartRetractFacts(VALUE self, VALUE handle, VALUE facts) {
149
+ static VALUE rbRetractFacts(VALUE self, VALUE handle, VALUE facts) {
390
150
  Check_Type(handle, T_FIXNUM);
391
151
  Check_Type(facts, T_STRING);
392
152
 
393
- unsigned int replyCount;
394
- void *rulesBinding = NULL;
395
- unsigned int result = startRetractFacts(FIX2INT(handle), RSTRING_PTR(facts), &rulesBinding, &replyCount);
396
- if (result == RULES_OK || result == ERR_EVENT_NOT_HANDLED) {
153
+ unsigned int stateOffset;
154
+ unsigned int result = retractFacts(FIX2INT(handle), RSTRING_PTR(facts), &stateOffset);
155
+ if (result == RULES_OK || result == ERR_EVENT_NOT_HANDLED || result == ERR_EVENT_OBSERVED) {
397
156
  VALUE output = rb_ary_new();
398
- rb_ary_push(output, INT2FIX(rulesBinding));
399
- rb_ary_push(output, INT2FIX(replyCount));
400
- return output;
157
+ rb_ary_push(output, INT2FIX(result));
158
+ rb_ary_push(output, INT2FIX(stateOffset));
159
+ return output;
401
160
  } else {
402
161
  if (result == ERR_OUT_OF_MEMORY) {
403
162
  rb_raise(rb_eNoMemError, "Out of memory");
@@ -409,30 +168,14 @@ static VALUE rbStartRetractFacts(VALUE self, VALUE handle, VALUE facts) {
409
168
  return Qnil;
410
169
  }
411
170
 
412
- static VALUE rbRetractFacts(VALUE self, VALUE handle, VALUE facts) {
171
+ static VALUE rbUpdateState(VALUE self, VALUE handle, VALUE state) {
413
172
  Check_Type(handle, T_FIXNUM);
414
- Check_Type(facts, T_STRING);
415
-
416
- unsigned int result = retractFacts(FIX2INT(handle), RSTRING_PTR(facts));
417
- if (result != RULES_OK) {
418
- if (result == ERR_OUT_OF_MEMORY) {
419
- rb_raise(rb_eNoMemError, "Out of memory");
420
- } else {
421
- rb_raise(rb_eException, "Could not retract facts, error code: %d", result);
422
- }
423
- }
424
-
425
- return Qnil;
426
- }
427
-
428
- static VALUE rbAssertState(VALUE self, VALUE handle, VALUE sid, VALUE state) {
429
- Check_Type(handle, T_FIXNUM);
430
- Check_Type(sid, T_STRING);
431
173
  Check_Type(state, T_STRING);
432
174
 
433
- unsigned int result = assertState(FIX2INT(handle), RSTRING_PTR(sid), RSTRING_PTR(state));
175
+ unsigned int stateOffset;
176
+ unsigned int result = updateState(FIX2INT(handle), RSTRING_PTR(state), &stateOffset);
434
177
  if (result == RULES_OK || result == ERR_EVENT_NOT_HANDLED || result == ERR_EVENT_OBSERVED) {
435
- return INT2FIX(result);
178
+ return INT2FIX(stateOffset);
436
179
  } else {
437
180
  if (result == ERR_OUT_OF_MEMORY) {
438
181
  rb_raise(rb_eNoMemError, "Out of memory");
@@ -444,38 +187,13 @@ static VALUE rbAssertState(VALUE self, VALUE handle, VALUE sid, VALUE state) {
444
187
  return Qnil;
445
188
  }
446
189
 
447
- static VALUE rbStartUpdateState(VALUE self, VALUE handle, VALUE actionHandle, VALUE state) {
448
- Check_Type(handle, T_FIXNUM);
449
- Check_Type(actionHandle, T_FIXNUM);
450
- Check_Type(state, T_STRING);
451
-
452
- unsigned int replyCount;
453
- void *rulesBinding = NULL;
454
- unsigned int result = startUpdateState(FIX2INT(handle), (void *)FIX2LONG(actionHandle), RSTRING_PTR(state), &rulesBinding, &replyCount);
455
- if (result == RULES_OK || result == ERR_EVENT_NOT_HANDLED) {
456
- VALUE output = rb_ary_new();
457
- rb_ary_push(output, INT2FIX(rulesBinding));
458
- rb_ary_push(output, INT2FIX(replyCount));
459
- return output;
460
- } else {
461
- if (result == ERR_OUT_OF_MEMORY) {
462
- rb_raise(rb_eNoMemError, "Out of memory");
463
- } else {
464
- rb_raise(rb_eException, "Could not start update state, error code: %d", result);
465
- }
466
- }
467
-
468
- return Qnil;
469
- }
470
-
471
190
  static VALUE rbStartAction(VALUE self, VALUE handle) {
472
191
  Check_Type(handle, T_FIXNUM);
473
192
 
474
193
  char *state;
475
194
  char *messages;
476
- void *actionHandle;
477
- void *actionBinding;
478
- unsigned int result = startAction(FIX2INT(handle), &state, &messages, &actionHandle, &actionBinding);
195
+ unsigned int stateOffset;
196
+ unsigned int result = startAction(FIX2INT(handle), &state, &messages, &stateOffset);
479
197
  if (result == ERR_NO_ACTION_AVAILABLE) {
480
198
  return Qnil;
481
199
  } else if (result != RULES_OK) {
@@ -489,35 +207,39 @@ static VALUE rbStartAction(VALUE self, VALUE handle) {
489
207
  VALUE output = rb_ary_new();
490
208
  rb_ary_push(output, rb_str_new2(state));
491
209
  rb_ary_push(output, rb_str_new2(messages));
492
- rb_ary_push(output, INT2FIX(actionHandle));
493
- rb_ary_push(output, INT2FIX(actionBinding));
210
+ rb_ary_push(output, INT2FIX(stateOffset));
494
211
  return output;
495
212
  }
496
213
 
497
- static VALUE rbCompleteAction(VALUE self, VALUE handle, VALUE actionHandle, VALUE state) {
214
+ static VALUE rbStartActionForState(VALUE self, VALUE handle, VALUE stateOffset) {
498
215
  Check_Type(handle, T_FIXNUM);
499
- Check_Type(actionHandle, T_FIXNUM);
500
- Check_Type(state, T_STRING);
216
+ Check_Type(stateOffset, T_FIXNUM);
501
217
 
502
- unsigned int result = completeAction(FIX2INT(handle), (void *)FIX2LONG(actionHandle), RSTRING_PTR(state));
503
- if (result != RULES_OK) {
218
+ char *state;
219
+ char *messages;
220
+ unsigned int result = startActionForState(FIX2INT(handle), FIX2INT(stateOffset), &state, &messages);
221
+ if (result == ERR_NO_ACTION_AVAILABLE) {
222
+ return Qnil;
223
+ } else if (result != RULES_OK) {
504
224
  if (result == ERR_OUT_OF_MEMORY) {
505
225
  rb_raise(rb_eNoMemError, "Out of memory");
506
226
  } else {
507
- rb_raise(rb_eException, "Could not complete action, error code: %d", result);
227
+ rb_raise(rb_eException, "Could not start action, error code: %d", result);
508
228
  }
509
229
  }
510
230
 
511
- return Qnil;
231
+ VALUE output = rb_ary_new();
232
+ rb_ary_push(output, rb_str_new2(state));
233
+ rb_ary_push(output, rb_str_new2(messages));
234
+ return output;
512
235
  }
513
236
 
514
- static VALUE rbCompleteAndStartAction(VALUE self, VALUE handle, VALUE expectedReplies, VALUE actionHandle) {
237
+ static VALUE rbCompleteAndStartAction(VALUE self, VALUE handle, VALUE stateOffset) {
515
238
  Check_Type(handle, T_FIXNUM);
516
- Check_Type(expectedReplies, T_FIXNUM);
517
- Check_Type(actionHandle, T_FIXNUM);
239
+ Check_Type(stateOffset, T_FIXNUM);
518
240
 
519
241
  char *messages;
520
- unsigned int result = completeAndStartAction(FIX2INT(handle), FIX2LONG(expectedReplies), (void *)FIX2LONG(actionHandle), &messages);
242
+ unsigned int result = completeAndStartAction(FIX2INT(handle), FIX2INT(stateOffset), &messages);
521
243
  if (result == ERR_NO_ACTION_AVAILABLE) {
522
244
  return Qnil;
523
245
  } else if (result != RULES_OK) {
@@ -531,12 +253,11 @@ static VALUE rbCompleteAndStartAction(VALUE self, VALUE handle, VALUE expectedRe
531
253
  return rb_str_new2(messages);
532
254
  }
533
255
 
534
-
535
- static VALUE rbAbandonAction(VALUE self, VALUE handle, VALUE actionHandle) {
256
+ static VALUE rbAbandonAction(VALUE self, VALUE handle, VALUE stateOffset) {
536
257
  Check_Type(handle, T_FIXNUM);
537
- Check_Type(actionHandle, T_FIXNUM);
258
+ Check_Type(stateOffset, T_FIXNUM);
538
259
 
539
- unsigned int result = abandonAction(FIX2INT(handle), (void *)FIX2LONG(actionHandle));
260
+ unsigned int result = abandonAction(FIX2INT(handle), FIX2INT(stateOffset));
540
261
  if (result != RULES_OK) {
541
262
  if (result == ERR_OUT_OF_MEMORY) {
542
263
  rb_raise(rb_eNoMemError, "Out of memory");
@@ -588,11 +309,7 @@ static VALUE rbAssertTimers(VALUE self, VALUE handle) {
588
309
  Check_Type(handle, T_FIXNUM);
589
310
 
590
311
  unsigned int result = assertTimers(FIX2INT(handle));
591
- if (result == RULES_OK) {
592
- return INT2FIX(1);
593
- } else if (result == ERR_NO_TIMERS_AVAILABLE) {
594
- return INT2FIX(0);
595
- } else {
312
+ if (result != RULES_OK) {
596
313
  if (result == ERR_OUT_OF_MEMORY) {
597
314
  rb_raise(rb_eNoMemError, "Out of memory");
598
315
  } else {
@@ -656,33 +373,18 @@ static VALUE rbRenewActionLease(VALUE self, VALUE handle, VALUE sid) {
656
373
 
657
374
  void Init_rules() {
658
375
  rulesModule = rb_define_module("Rules");
659
- rb_define_singleton_method(rulesModule, "create_ruleset", rbCreateRuleset, 3);
376
+ rb_define_singleton_method(rulesModule, "create_ruleset", rbCreateRuleset, 2);
660
377
  rb_define_singleton_method(rulesModule, "delete_ruleset", rbDeleteRuleset, 1);
661
- rb_define_singleton_method(rulesModule, "create_client", rbCreateClient, 2);
662
- rb_define_singleton_method(rulesModule, "delete_client", rbDeleteClient, 1);
663
- rb_define_singleton_method(rulesModule, "bind_ruleset", rbBindRuleset, 5);
664
- rb_define_singleton_method(rulesModule, "complete", rbComplete, 2);
665
378
  rb_define_singleton_method(rulesModule, "assert_event", rbAssertEvent, 2);
666
- rb_define_singleton_method(rulesModule, "queue_assert_event", rbQueueAssertEvent, 4);
667
- rb_define_singleton_method(rulesModule, "start_assert_event", rbStartAssertEvent, 2);
668
379
  rb_define_singleton_method(rulesModule, "assert_events", rbAssertEvents, 2);
669
- rb_define_singleton_method(rulesModule, "start_assert_events", rbStartAssertEvents, 2);
670
- rb_define_singleton_method(rulesModule, "retract_event", rbRetractEvent, 2);
671
- rb_define_singleton_method(rulesModule, "start_assert_fact", rbStartAssertFact, 2);
672
380
  rb_define_singleton_method(rulesModule, "assert_fact", rbAssertFact, 2);
673
- rb_define_singleton_method(rulesModule, "queue_assert_fact", rbQueueAssertFact, 4);
674
- rb_define_singleton_method(rulesModule, "start_assert_facts", rbStartAssertFacts, 2);
675
381
  rb_define_singleton_method(rulesModule, "assert_facts", rbAssertFacts, 2);
676
- rb_define_singleton_method(rulesModule, "start_retract_fact", rbStartRetractFact, 2);
677
382
  rb_define_singleton_method(rulesModule, "retract_fact", rbRetractFact, 2);
678
- rb_define_singleton_method(rulesModule, "queue_retract_fact", rbQueueRetractFact, 4);
679
- rb_define_singleton_method(rulesModule, "start_retract_facts", rbStartRetractFacts, 2);
680
383
  rb_define_singleton_method(rulesModule, "retract_facts", rbRetractFacts, 2);
681
- rb_define_singleton_method(rulesModule, "assert_state", rbAssertState, 3);
682
- rb_define_singleton_method(rulesModule, "start_update_state", rbStartUpdateState, 3);
384
+ rb_define_singleton_method(rulesModule, "update_state", rbUpdateState, 2);
683
385
  rb_define_singleton_method(rulesModule, "start_action", rbStartAction, 1);
684
- rb_define_singleton_method(rulesModule, "complete_action", rbCompleteAction, 3);
685
- rb_define_singleton_method(rulesModule, "complete_and_start_action", rbCompleteAndStartAction, 3);
386
+ rb_define_singleton_method(rulesModule, "start_action_for_state", rbStartActionForState, 2);
387
+ rb_define_singleton_method(rulesModule, "complete_and_start_action", rbCompleteAndStartAction, 2);
686
388
  rb_define_singleton_method(rulesModule, "abandon_action", rbAbandonAction, 2);
687
389
  rb_define_singleton_method(rulesModule, "start_timer", rbStartTimer, 5);
688
390
  rb_define_singleton_method(rulesModule, "cancel_timer", rbCancelTimer, 3);