durable_rules 0.34.19 → 0.34.20

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/src/rules/events.c +0 -1
  3. data/src/rules/regex.c +162 -33
  4. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8c9e5f1398313b056da22ae2f5f3f2e530290b13
4
- data.tar.gz: 4c855b1f826d50b56b092c5c7f16a45ad26fe4d4
3
+ metadata.gz: 79a74c49e4e60c55ef7d89ecdade38fce29f2b47
4
+ data.tar.gz: 2ba0939e717d8b66d6b1e981eb1703ceb038efae
5
5
  SHA512:
6
- metadata.gz: 8ed7203164847dbb9a410f737150dbb0e4b65a2cbe786066e97eb0f7aab9e008cbbd56c89a2a7fda94a44706c07a8334f856ade7590801e76a94403670898c08
7
- data.tar.gz: c9a0aa9a35890f73c1236716287a7ec380b2ccaa5fd3d7a843b753f45d3debb79dc975f7666ef76c58ef24650c7d9ad441a34e676ad511c8d0972eabd78fb1cf
6
+ metadata.gz: ddfbc0d6fd48dd54449e7cf7f7b61fb87a97a74bdbf527fdbc7796d55a27b5a4ac7aa841b159b6833614422603729d26b4c6569ffd1eac6a82aa90bdb0a68e26
7
+ data.tar.gz: 48bf8d7b62d9c46b923224cffa54c2ce36b6d1fa8623d0ac5311e1071b868deb87056f09fcf70661eb47de4d8fe88a0eea226e528f8abe37020f539996afdb0b
data/src/rules/events.c CHANGED
@@ -204,7 +204,6 @@ static unsigned char compareStringAndStringProperty(char *leftFirst,
204
204
  int result = strcmp(leftFirst, rightFirst);
205
205
  rightFirst[rightLength] = rightTemp;
206
206
  leftFirst[leftLength] = leftTemp;
207
-
208
207
  switch(op) {
209
208
  case OP_LT:
210
209
  return (result < 0);
data/src/rules/regex.c CHANGED
@@ -68,7 +68,7 @@
68
68
  type hset[MAX_HSET] = {0}; \
69
69
 
70
70
  #define HSET(value) do { \
71
- unsigned int size = 0; \
71
+ unsigned short size = 0; \
72
72
  unsigned short index = value->hash % MAX_HSET; \
73
73
  while (hset[index]) { \
74
74
  index = (index + 1) % MAX_HSET; \
@@ -81,12 +81,14 @@
81
81
  } while(0)
82
82
 
83
83
  #define HGET(valueHash, value) do { \
84
+ unsigned short size = 0; \
84
85
  unsigned short index = valueHash % MAX_HSET; \
85
86
  *value = NULL; \
86
- while (hset[index] && !*value) { \
87
+ while (hset[index] && !*value && size < MAX_HSET) { \
87
88
  if (hset[index]->hash == valueHash) { \
88
89
  *value = hset[index]; \
89
90
  } \
91
+ ++size; \
90
92
  index = (index + 1) % MAX_HSET; \
91
93
  } \
92
94
  } while(0)
@@ -97,8 +99,8 @@
97
99
  type set[MAX_SET] = {0}; \
98
100
 
99
101
  #define SET(value) do { \
100
- unsigned int size = 0; \
101
- unsigned int i = value % MAX_SET; \
102
+ unsigned short size = 0; \
103
+ unsigned short i = value % MAX_SET; \
102
104
  while (set[i]) { \
103
105
  i = (i + 1) % MAX_SET; \
104
106
  ++size; \
@@ -110,13 +112,15 @@
110
112
  } while(0)
111
113
 
112
114
  #define EXISTS(value, result) do { \
113
- unsigned int i = value % MAX_SET; \
115
+ unsigned short size = 0; \
116
+ unsigned short i = value % MAX_SET; \
114
117
  *result = 0; \
115
- while (set[i] && !*result) { \
118
+ while (set[i] && !*result && size < MAX_SET) { \
116
119
  if (set[i] == value) { \
117
120
  *result = 1; \
118
121
  } \
119
- i = (i + 1) % MAX_HSET; \
122
+ ++size; \
123
+ i = (i + 1) % MAX_SET; \
120
124
  } \
121
125
  } while(0)
122
126
 
@@ -128,7 +132,14 @@
128
132
  } while (0)
129
133
 
130
134
  #define LINK_STATES(previousState, nextState, tokenSymbol) do { \
131
- unsigned int result = linkStates(previousState, nextState, tokenSymbol); \
135
+ unsigned int result = linkStates(previousState, nextState, tokenSymbol, 0); \
136
+ if (result != RULES_OK) { \
137
+ return result; \
138
+ } \
139
+ } while (0)
140
+
141
+ #define LINK_STATES_D(previousState, nextState, tokenSymbol) do { \
142
+ unsigned int result = linkStates(previousState, nextState, tokenSymbol, 1); \
132
143
  if (result != RULES_OK) { \
133
144
  return result; \
134
145
  } \
@@ -137,6 +148,7 @@
137
148
  struct state;
138
149
 
139
150
  typedef struct transition {
151
+ unsigned short deterministic;
140
152
  unsigned int symbol;
141
153
  struct state *next;
142
154
  } transition;
@@ -570,7 +582,8 @@ static unsigned int createState(unsigned short *stateId,
570
582
 
571
583
  static unsigned int linkStates(state *previousState,
572
584
  state *nextState,
573
- unsigned int tokenSymbol) {
585
+ unsigned int tokenSymbol,
586
+ unsigned short deterministic) {
574
587
  for (int i = 0; i < previousState->transitionsLength; ++i) {
575
588
  if (previousState->transitions[i].symbol == tokenSymbol &&
576
589
  previousState->transitions[i].next->id == nextState->id) {
@@ -578,6 +591,7 @@ static unsigned int linkStates(state *previousState,
578
591
  }
579
592
  }
580
593
 
594
+ previousState->transitions[previousState->transitionsLength].deterministic = deterministic;
581
595
  previousState->transitions[previousState->transitionsLength].symbol = tokenSymbol;
582
596
  previousState->transitions[previousState->transitionsLength].next = nextState;
583
597
  ++previousState->transitionsLength;
@@ -597,6 +611,7 @@ static void deleteTransition(state *previousState, unsigned short index) {
597
611
  }
598
612
 
599
613
  for (unsigned short i = index + 1; i < previousState->transitionsLength; ++i) {
614
+ previousState->transitions[i - 1].deterministic = previousState->transitions[i].deterministic;
600
615
  previousState->transitions[i - 1].symbol = previousState->transitions[i].symbol;
601
616
  previousState->transitions[i - 1].next = previousState->transitions[i].next;
602
617
  }
@@ -631,7 +646,7 @@ static unsigned int printGraph(state *start) {
631
646
  }
632
647
  for (int i = 0; i < currentState->transitionsLength; ++ i) {
633
648
  transition *currentTransition = &currentState->transitions[i];
634
- printf(" transition %x to state %d\n", currentTransition->symbol, currentTransition->next->id);
649
+ printf(" transition %x to state %d deterministic %d\n", currentTransition->symbol, currentTransition->next->id, currentTransition->deterministic);
635
650
  if (!visited[currentTransition->next->id]) {
636
651
  visited[currentTransition->next->id] = 1;
637
652
  ENQUEUE(currentTransition->next);
@@ -709,7 +724,7 @@ static unsigned int createGraph(char **first,
709
724
  CREATE_STATE(id, &currentState);
710
725
  currentState->isReject = 1;
711
726
  for (unsigned short i = 0; i < currentToken.inverseSymbolsLength; ++i) {
712
- LINK_STATES(previousState, currentState, currentToken.inverseSymbols[i]);
727
+ LINK_STATES_D(previousState, currentState, currentToken.inverseSymbols[i]);
713
728
  }
714
729
 
715
730
  CREATE_STATE(id, &currentState);
@@ -871,6 +886,14 @@ static unsigned short calculateHash(state **list,
871
886
  return hash;
872
887
  }
873
888
 
889
+ static unsigned int calculateHashFromTwo(state *first,
890
+ state *second) {
891
+ CREATE_LIST(state*);
892
+ ADD(first);
893
+ ADD(second);
894
+ return calculateHash(LIST);
895
+ }
896
+
874
897
  static unsigned int ensureState(unsigned short *id,
875
898
  state **list,
876
899
  unsigned short stateListLength,
@@ -880,7 +903,10 @@ static unsigned int ensureState(unsigned short *id,
880
903
  state *targetState = list[i];
881
904
  for (unsigned short ii = 0; ii < targetState->transitionsLength; ++ii) {
882
905
  transition *targetTransition = &targetState->transitions[ii];
883
- LINK_STATES(*newState, targetTransition->next, targetTransition->symbol);
906
+ unsigned int result = linkStates(*newState, targetTransition->next, targetTransition->symbol, targetTransition->deterministic);
907
+ if (result != RULES_OK) {
908
+ return result;
909
+ }
884
910
  }
885
911
 
886
912
  if (targetState->isAccept) {
@@ -895,10 +921,19 @@ static unsigned int ensureState(unsigned short *id,
895
921
  return ERR_REGEX_CONFLICT;
896
922
  }
897
923
  }
898
-
899
924
  return RULES_OK;
900
925
  }
901
926
 
927
+ static unsigned int ensureStateFromTwo(unsigned short *id,
928
+ state *first,
929
+ state *second,
930
+ state **newState) {
931
+ CREATE_LIST(state*);
932
+ ADD(first);
933
+ ADD(second);
934
+ return ensureState(id, LIST, newState);
935
+ }
936
+
902
937
  static unsigned int consolidateStates(state *currentState,
903
938
  unsigned short *id) {
904
939
  for (unsigned short i = 0; i < currentState->transitionsLength; ++i) {
@@ -908,7 +943,10 @@ static unsigned int consolidateStates(state *currentState,
908
943
  if (nextState != currentState) {
909
944
  for (unsigned short ii = 0; ii < nextState->transitionsLength; ++ii) {
910
945
  transition *nextTransition = &nextState->transitions[ii];
911
- LINK_STATES(currentState, nextTransition->next, nextTransition->symbol);
946
+ unsigned int result = linkStates(currentState, nextTransition->next, nextTransition->symbol, nextTransition->deterministic);
947
+ if (result != RULES_OK) {
948
+ return result;
949
+ }
912
950
  if (nextState->refCount == 1) {
913
951
  --nextTransition->next->refCount;
914
952
  }
@@ -954,14 +992,8 @@ static unsigned int consolidateTransitions(state *currentState,
954
992
  SET(currentTransition->symbol);
955
993
  for (unsigned short ii = i + 1; ii < currentState->transitionsLength; ++ ii) {
956
994
  transition *targetTransition = &currentState->transitions[ii];
957
- if ((currentTransition->symbol == targetTransition->symbol) ||
958
- (currentTransition->symbol == REGEX_DOT && !targetTransition->next->isReject) ||
959
- (targetTransition->symbol == REGEX_DOT && !currentTransition->next->isReject)) {
995
+ if (currentTransition->symbol == targetTransition->symbol) {
960
996
  foundSymbol = currentTransition->symbol;
961
- if (foundSymbol == REGEX_DOT) {
962
- foundSymbol = targetTransition->symbol;
963
- }
964
-
965
997
  if (LIST_EMPTY()) {
966
998
  ADD(currentTransition->next);
967
999
  oldTransitions[oldTransitionsLength].symbol = currentTransition->symbol;
@@ -1008,14 +1040,64 @@ static unsigned int consolidateTransitions(state *currentState,
1008
1040
  return RULES_OK;
1009
1041
  }
1010
1042
 
1043
+ static unsigned int consolidateDot(state *currentState,
1044
+ unsigned short *id,
1045
+ state **hset) {
1046
+ transition oldTransitions[MAX_TRANSITIONS];
1047
+ unsigned short oldTransitionsLength = 0;
1048
+ transition newTransitions[MAX_TRANSITIONS];
1049
+ unsigned short newTransitionsLength = 0;
1050
+
1051
+ for (unsigned short i = 0; i < currentState->transitionsLength; ++i) {
1052
+ transition *currentTransition = &currentState->transitions[i];
1053
+ for (unsigned short ii = i + 1; ii < currentState->transitionsLength; ++ ii) {
1054
+ transition *targetTransition = &currentState->transitions[ii];
1055
+ if ((currentTransition->symbol == REGEX_DOT && targetTransition->symbol != REGEX_DOT && !targetTransition->deterministic) ||
1056
+ (currentTransition->symbol != REGEX_DOT && !currentTransition->deterministic && targetTransition->symbol == REGEX_DOT)){
1057
+ state *newState;
1058
+ unsigned int newStateHash = calculateHashFromTwo(currentTransition->next, targetTransition->next);
1059
+ HGET(newStateHash, &newState);
1060
+ if (!newState) {
1061
+ unsigned int result = ensureStateFromTwo(id, currentTransition->next, targetTransition->next, &newState);
1062
+ if (result != REGEX_PARSE_OK) {
1063
+ return result;
1064
+ }
1065
+
1066
+ newState->hash = newStateHash;
1067
+ HSET(newState);
1068
+ }
1069
+
1070
+ if (currentTransition->symbol == REGEX_DOT) {
1071
+ oldTransitions[oldTransitionsLength].symbol = targetTransition->symbol;
1072
+ oldTransitions[oldTransitionsLength].next = targetTransition->next;
1073
+ } else {
1074
+ oldTransitions[oldTransitionsLength].symbol = currentTransition->symbol;
1075
+ oldTransitions[oldTransitionsLength].next = currentTransition->next;
1076
+ }
1077
+
1078
+ newTransitions[newTransitionsLength].symbol = oldTransitions[oldTransitionsLength].symbol;
1079
+ newTransitions[newTransitionsLength].next = newState;
1080
+ ++oldTransitionsLength;
1081
+ ++newTransitionsLength;
1082
+
1083
+ }
1084
+ }
1085
+ }
1086
+
1087
+ for (unsigned short i = 0; i < oldTransitionsLength; ++i) {
1088
+ unlinkStates(currentState, oldTransitions[i].next, oldTransitions[i].symbol);
1089
+ }
1090
+
1091
+ for (unsigned short i = 0; i < newTransitionsLength; ++i) {
1092
+ LINK_STATES_D(currentState, newTransitions[i].next, newTransitions[i].symbol);
1093
+ }
1094
+
1095
+ return RULES_OK;
1096
+ }
1097
+
1011
1098
  static unsigned int transformToDFA(state *nfa,
1012
1099
  unsigned short *id) {
1013
1100
 
1014
- #ifdef _PRINT
1015
- printf("*** NFA ***\n");
1016
- printGraph(nfa);
1017
- #endif
1018
-
1019
1101
  CREATE_HASHSET(state*);
1020
1102
  CREATE_QUEUE(state*);
1021
1103
  unsigned char visited[MAX_STATES] = {0};
@@ -1032,6 +1114,7 @@ static unsigned int transformToDFA(state *nfa,
1032
1114
  return result;
1033
1115
  }
1034
1116
 
1117
+
1035
1118
  for (int i = 0; i < currentState->transitionsLength; ++ i) {
1036
1119
  transition *currentTransition = &currentState->transitions[i];
1037
1120
  if (!visited[currentTransition->next->id]) {
@@ -1043,10 +1126,33 @@ static unsigned int transformToDFA(state *nfa,
1043
1126
  DEQUEUE(&currentState);
1044
1127
  }
1045
1128
 
1046
- #ifdef _PRINT
1047
- printf("*** DFA ***\n");
1048
- printGraph(nfa);
1049
- #endif
1129
+ return RULES_OK;
1130
+ }
1131
+
1132
+ static unsigned int expandDot(state *nfa,
1133
+ unsigned short *id) {
1134
+
1135
+ CREATE_HASHSET(state*);
1136
+ CREATE_QUEUE(state*);
1137
+ unsigned char visited[MAX_STATES] = {0};
1138
+ state *currentState = nfa;
1139
+ visited[currentState->id] = 1;
1140
+ while (currentState) {
1141
+ unsigned int result = consolidateDot(currentState, id, HASHSET);
1142
+ if (result != REGEX_PARSE_OK) {
1143
+ return result;
1144
+ }
1145
+
1146
+ for (int i = 0; i < currentState->transitionsLength; ++ i) {
1147
+ transition *currentTransition = &currentState->transitions[i];
1148
+ if (!visited[currentTransition->next->id]) {
1149
+ visited[currentTransition->next->id] = 1;
1150
+ ENQUEUE(currentTransition->next);
1151
+ }
1152
+ }
1153
+
1154
+ DEQUEUE(&currentState);
1155
+ }
1050
1156
 
1051
1157
  return RULES_OK;
1052
1158
  }
@@ -1171,10 +1277,32 @@ unsigned int compileRegex(void *tree,
1171
1277
  }
1172
1278
  end->isAccept = 1;
1173
1279
  ++start->refCount;
1280
+
1281
+ #ifdef _PRINT
1282
+ printf("*** NFA ***\n");
1283
+ printGraph(start);
1284
+ #endif
1285
+
1174
1286
  result = transformToDFA(start, &id);
1175
1287
  if (result != RULES_OK) {
1176
1288
  return result;
1177
1289
  }
1290
+
1291
+ result = expandDot(start, &id);
1292
+ if (result != RULES_OK) {
1293
+ return result;
1294
+ }
1295
+
1296
+ result = transformToDFA(start, &id);
1297
+ if (result != RULES_OK) {
1298
+ return result;
1299
+ }
1300
+
1301
+ #ifdef _PRINT
1302
+ printf("*** DFA ***\n");
1303
+ printGraph(start);
1304
+ #endif
1305
+
1178
1306
  result = calculateGraphDimensions(start,
1179
1307
  vocabularyLength,
1180
1308
  statesLength);
@@ -1224,8 +1352,10 @@ unsigned char evaluateRegex(void *tree,
1224
1352
  return 0;
1225
1353
  }
1226
1354
  } else {
1227
- currentState = stateTable[statesLength * (currentSymbol - 1) + (currentState - 1)];
1228
- if (!currentState) {
1355
+ unsigned short futureState = stateTable[statesLength * (currentSymbol - 1) + (currentState - 1)];
1356
+ if (futureState) {
1357
+ currentState = futureState;
1358
+ } else {
1229
1359
  currentSymbol = getIndex(symbolHashSet, vocabularyLength, REGEX_DOT);
1230
1360
  if (!currentSymbol) {
1231
1361
  return 0;
@@ -1239,6 +1369,5 @@ unsigned char evaluateRegex(void *tree,
1239
1369
  }
1240
1370
  }
1241
1371
  }
1242
-
1243
1372
  return acceptVector[currentState - 1];
1244
1373
  }
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.19
4
+ version: 0.34.20
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jesus Ruiz
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-02-06 00:00:00.000000000 Z
11
+ date: 2017-02-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake