durable_rules 0.34.51 → 0.34.52

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9db587bb8df6b72055812d37c3b3db5e1bebba8f
4
- data.tar.gz: 3f35d2c234db4ae3c5d50619a52e743b0146f051
3
+ metadata.gz: 72027204669b6fba94f9092c272f05e27e6d7b1f
4
+ data.tar.gz: 377a4eb20f2ae9fb966380bdcfb1f91e72b85de4
5
5
  SHA512:
6
- metadata.gz: 55c856dd6335c44e34fb5b85d88d74c1443e3fde82ee1b3c798feb1bd5b52c06bda645c1922bbcb69ee0a6ff2ec98592bcb68e864b1a57e77a9ec043f356c46e
7
- data.tar.gz: b74146ddba7bd15e620b652ca0a0138244dfc4bce8dc95356fc082e37b1ee89c1dac7e0fe469aee5a75045414e8a6861cc725117b34678667db7a7a2b091bfe9
6
+ metadata.gz: 2b61ad9e9c4343ec0edaa7ebd0cc5d68c9613b206331c1d64eb31c5bde6b5f617d6a7f38ea0181a83e956edebcaa82aab5ad4ecd3d66da1bbba9202f24c621a9
7
+ data.tar.gz: bd8b882feea47d4ba40f40301e310499c37f3828fb3400a9e766432faf311ca184d936099da9b424c14b073c0c3311d0b6e26f5031f1f1159e455b17962620c3
data/librb/durable.rb CHANGED
@@ -449,6 +449,10 @@ module Durable
449
449
  {:cap => value}
450
450
  end
451
451
 
452
+ def distinct(value)
453
+ {:dist => value}
454
+ end
455
+
452
456
  def timeout(name)
453
457
  expression = Expression.new(:$m, :$t)
454
458
  expression == name
@@ -507,6 +511,14 @@ module Durable
507
511
  rule["cap"] = options[:cap]
508
512
  end
509
513
 
514
+ if options.key? :dist
515
+ if (options[:dist])
516
+ rule["dist"] = 1
517
+ else
518
+ rule["dist"] = 0
519
+ end
520
+ end
521
+
510
522
  @rules[rule_name] = rule
511
523
  rule
512
524
  end
data/src/rules/net.c CHANGED
@@ -282,8 +282,14 @@ static unsigned int createTest(ruleset *tree, expression *expr, char **test, cha
282
282
  }
283
283
 
284
284
  if (first) {
285
- if (asprintf(test, "%scompare_array(message[\"%s\"], %s, %s, %s)", *test, leftProperty, idiomString, op, par) == -1) {
286
- return ERR_OUT_OF_MEMORY;
285
+ if (expr->distinct) {
286
+ if (asprintf(test, "is_distinct_message(frame, message) and %scompare_array(message[\"%s\"], %s, %s, %s)", *test, leftProperty, idiomString, op, par) == -1) {
287
+ return ERR_OUT_OF_MEMORY;
288
+ }
289
+ } else {
290
+ if (asprintf(test, "%scompare_array(message[\"%s\"], %s, %s, %s)", *test, leftProperty, idiomString, op, par) == -1) {
291
+ return ERR_OUT_OF_MEMORY;
292
+ }
287
293
  }
288
294
 
289
295
  first = 0;
@@ -330,9 +336,16 @@ static unsigned int createTest(ruleset *tree, expression *expr, char **test, cha
330
336
 
331
337
  char *oldTest = *test;
332
338
  if (first) {
333
- if (asprintf(test, "%smessage[\"%s\"] %s %s", *test, leftProperty, op, idiomString) == -1) {
334
- return ERR_OUT_OF_MEMORY;
339
+ if (expr->distinct) {
340
+ if (asprintf(test, "is_distinct_message(frame, message) and %smessage[\"%s\"] %s %s", *test, leftProperty, op, idiomString) == -1) {
341
+ return ERR_OUT_OF_MEMORY;
342
+ }
343
+ } else {
344
+ if (asprintf(test, "%smessage[\"%s\"] %s %s", *test, leftProperty, op, idiomString) == -1) {
345
+ return ERR_OUT_OF_MEMORY;
346
+ }
335
347
  }
348
+
336
349
  first = 0;
337
350
  } else {
338
351
  if (asprintf(test, "%s %s message[\"%s\"] %s %s", *test, comp, leftProperty, op, idiomString) == -1) {
@@ -768,6 +781,45 @@ static unsigned int loadAddMessageCommand(ruleset *tree, binding *rulesBinding)
768
781
  "local message = {}\n"
769
782
  "local primary_message_keys = {}\n"
770
783
  "local input_keys = {}\n"
784
+ "local is_distinct_message = function(frame, message)\n"
785
+ " for name, frame_message in pairs(frame) do\n"
786
+ " if frame_message[\"id\"] == message[\"id\"] then\n"
787
+ " return false\n"
788
+ " end\n"
789
+ " end\n"
790
+ " return true\n"
791
+ "end\n"
792
+ "local compare_array = function(left_array, right_value, op, compare_all)\n"
793
+ " if not left_array or type(left_array) ~= \"table\" then\n"
794
+ " return false\n"
795
+ " end\n"
796
+ " for i = 1, #left_array, 1 do\n"
797
+ " local comparison = false\n"
798
+ " if op == 0 then\n"
799
+ " comparison = (left_array[i] < right_value)\n"
800
+ " elseif op == 1 then\n"
801
+ " comparison = (left_array[i] <= right_value)\n"
802
+ " elseif op == 2 then\n"
803
+ " comparison = (left_array[i] > right_value)\n"
804
+ " elseif op == 3 then\n"
805
+ " comparison = (left_array[i] >= right_value)\n"
806
+ " elseif op == 4 then\n"
807
+ " comparison = (left_array[i] == right_value)\n"
808
+ " elseif op == 5 then\n"
809
+ " comparison = (left_array[i] ~= right_value)\n"
810
+ " end\n"
811
+ " if not compare_all and comparison then\n"
812
+ " return true\n"
813
+ " end\n"
814
+ " if compare_all and not comparison then\n"
815
+ " return false\n"
816
+ " end\n"
817
+ " end"
818
+ " if compare_all then\n"
819
+ " return true\n"
820
+ " end\n"
821
+ " return false\n"
822
+ "end\n"
771
823
  "local save_message = function(current_key, message, events_key, messages_key, save_hashset)\n"
772
824
  " if save_hashset == 1 then\n"
773
825
  " redis.call(\"hsetnx\", messages_key, message[\"id\"], cmsgpack.pack(message))\n"
@@ -1045,6 +1097,14 @@ static unsigned int loadPeekActionCommand(ruleset *tree, binding *rulesBinding)
1045
1097
  "local facts_mids_cache = {}\n"
1046
1098
  "local events_mids_cache = {}\n"
1047
1099
  "local get_context\n"
1100
+ "local is_distinct_message = function(frame, message)\n"
1101
+ " for name, frame_message in pairs(frame) do\n"
1102
+ " if frame_message[\"id\"] == message[\"id\"] then\n"
1103
+ " return false\n"
1104
+ " end\n"
1105
+ " end\n"
1106
+ " return true\n"
1107
+ "end\n"
1048
1108
  "local compare_array = function(left_array, right_value, op, compare_all)\n"
1049
1109
  " if not left_array or type(left_array) ~= \"table\" then\n"
1050
1110
  " return false\n"
@@ -1837,6 +1897,14 @@ static unsigned int loadEvalMessageCommand(ruleset *tree, binding *rulesBinding)
1837
1897
  "local results_key\n"
1838
1898
  "local inverse_directory\n"
1839
1899
  "local key\n"
1900
+ "local is_distinct_message = function(frame, message)\n"
1901
+ " for name, frame_message in pairs(frame) do\n"
1902
+ " if frame_message[\"id\"] == message[\"id\"] then\n"
1903
+ " return false\n"
1904
+ " end\n"
1905
+ " end\n"
1906
+ " return true\n"
1907
+ "end\n"
1840
1908
  "local compare_array = function(left_array, right_value, op, compare_all)\n"
1841
1909
  " if not left_array or type(left_array) ~= \"table\" then\n"
1842
1910
  " return false\n"
data/src/rules/rete.c CHANGED
@@ -13,6 +13,7 @@
13
13
  #define HASH_PRI 1450887882 // pri
14
14
  #define HASH_COUNT 967958004 // count
15
15
  #define HASH_CAP 41178555 // cap
16
+ #define HASH_DIST 1281379241 // dist
16
17
  #define HASH_LT 542787579 // $lt
17
18
  #define HASH_LTE 2350824890 // $lte
18
19
  #define HASH_GT 507407960 // $gt
@@ -67,6 +68,7 @@ static unsigned int validateAlgebra(char *rule);
67
68
  static unsigned int createBeta(ruleset *tree,
68
69
  char *rule,
69
70
  unsigned char operator,
71
+ unsigned short distinct,
70
72
  unsigned int nextOffset,
71
73
  path *nextPath,
72
74
  path **outPath);
@@ -796,6 +798,11 @@ static unsigned int validateRuleset(char *rules) {
796
798
  return result;
797
799
  }
798
800
 
801
+ result = validateSetting(HASH_DIST, first, JSON_INT);
802
+ if (result != PARSE_OK && result != ERR_SETTING_NOT_FOUND) {
803
+ return result;
804
+ }
805
+
799
806
  result = readNextName(first, &first, &last, &hash);
800
807
  while (result == PARSE_OK) {
801
808
  result = readNextValue(last, &first, &last, &type);
@@ -808,7 +815,8 @@ static unsigned int validateRuleset(char *rules) {
808
815
  if (result != RULES_OK && result != PARSE_END) {
809
816
  return result;
810
817
  }
811
- } else if (hash != HASH_COUNT && hash != HASH_PRI && hash != HASH_CAP) {
818
+ } else if (hash != HASH_COUNT && hash != HASH_PRI && hash != HASH_CAP && hash != HASH_DIST) {
819
+ printf("%d\n", hash);
812
820
  return ERR_UNEXPECTED_NAME;
813
821
  }
814
822
 
@@ -1360,6 +1368,7 @@ static unsigned int createAlpha(ruleset *tree,
1360
1368
  static unsigned int createBetaConnector(ruleset *tree,
1361
1369
  char *rule,
1362
1370
  path *betaPath,
1371
+ unsigned short distinct,
1363
1372
  unsigned int nextOffset) {
1364
1373
  char *first;
1365
1374
  char *last;
@@ -1429,6 +1438,7 @@ static unsigned int createBetaConnector(ruleset *tree,
1429
1438
  expr->not = (operator == OP_NOT) ? 1 : 0;
1430
1439
  expr->termsLength = 0;
1431
1440
  expr->t.termsPointer = NULL;
1441
+ expr->distinct = (distinct != 0) ? 1 : 0;
1432
1442
  if (operator == OP_NOP || operator == OP_NOT) {
1433
1443
  unsigned int resultOffset = NODE_M_OFFSET;
1434
1444
  readNextValue(last, &first, &last, &type);
@@ -1436,7 +1446,7 @@ static unsigned int createBetaConnector(ruleset *tree,
1436
1446
  }
1437
1447
  else {
1438
1448
  readNextValue(last, &first, &last, &type);
1439
- result = createBeta(tree, first, operator, connectorOffset, betaPath, NULL);
1449
+ result = createBeta(tree, first, operator, distinct, connectorOffset, betaPath, NULL);
1440
1450
  }
1441
1451
 
1442
1452
  if (result != RULES_OK) {
@@ -1452,6 +1462,7 @@ static unsigned int createBetaConnector(ruleset *tree,
1452
1462
  static unsigned int createBeta(ruleset *tree,
1453
1463
  char *rule,
1454
1464
  unsigned char operator,
1465
+ unsigned short distinct,
1455
1466
  unsigned int nextOffset,
1456
1467
  path *nextPath,
1457
1468
  path **outPath) {
@@ -1496,7 +1507,7 @@ static unsigned int createBeta(ruleset *tree,
1496
1507
  *outPath = betaPath;
1497
1508
  }
1498
1509
 
1499
- return createBetaConnector(tree, rule, betaPath, nextOffset);
1510
+ return createBetaConnector(tree, rule, betaPath, distinct, nextOffset);
1500
1511
  }
1501
1512
 
1502
1513
  static unsigned int add(any *right, any *left, any **result) {
@@ -1604,6 +1615,7 @@ static unsigned int createSingleQuery(ruleset *tree,
1604
1615
  newExpression->aliasOffset = expr->aliasOffset;
1605
1616
  newExpression->termsLength = expr->termsLength;
1606
1617
  newExpression->not = expr->not;
1618
+ newExpression->distinct = expr->distinct;
1607
1619
  if (expr->termsLength) {
1608
1620
  result = allocateNext(tree, expr->termsLength, &newExpression->t.termsOffset);
1609
1621
  if (result != RULES_OK) {
@@ -1851,15 +1863,17 @@ static unsigned int createTree(ruleset *tree, char *rules) {
1851
1863
  ruleAction->value.c.count = 1;
1852
1864
  }
1853
1865
 
1866
+ unsigned short distinct = 1;
1867
+ getSetting(HASH_DIST, first, &distinct);
1854
1868
  result = readNextName(first, &first, &last, &hash);
1855
1869
  while (result == PARSE_OK) {
1856
1870
  readNextValue(last, &first, &last, &type);
1857
1871
  switch (hash) {
1858
1872
  case HASH_ANY:
1859
- result = createBeta(tree, first, OP_ANY, actionOffset, NULL, &betaPath);
1873
+ result = createBeta(tree, first, OP_ANY, distinct, actionOffset, NULL, &betaPath);
1860
1874
  break;
1861
1875
  case HASH_ALL:
1862
- result = createBeta(tree, first, OP_ALL, actionOffset, NULL, &betaPath);
1876
+ result = createBeta(tree, first, OP_ALL, distinct, actionOffset, NULL, &betaPath);
1863
1877
  break;
1864
1878
  }
1865
1879
  if (result != RULES_OK) {
data/src/rules/rete.h CHANGED
@@ -66,6 +66,7 @@ typedef struct expression {
66
66
  unsigned int nameOffset;
67
67
  unsigned int aliasOffset;
68
68
  unsigned short termsLength;
69
+ unsigned char distinct;
69
70
  unsigned char not;
70
71
  union {
71
72
  unsigned int termsOffset;
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: durable_rules
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.34.51
4
+ version: 0.34.52
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jesus Ruiz
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-07-01 00:00:00.000000000 Z
11
+ date: 2018-09-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake