Skip to content

Commit 2c91ce6

Browse files
author
Marcel Overdijk
committed
Introduces static contstants for custom error message keys and undefined string which closes #41
1 parent fac8051 commit 2c91ce6

2 files changed

Lines changed: 50 additions & 39 deletions

File tree

rivescript-core/src/main/java/com/rivescript/RiveScript.java

Lines changed: 45 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,17 @@
124124
*/
125125
public class RiveScript {
126126

127+
public static final String MSG_DEEP_RECURSION_KEY = "deepRecursion";
128+
public static final String MSG_REPLIES_NOT_SORTED_KEY = "repliesNotSorted";
129+
public static final String MSG_DEFAULT_TOPIC_NOT_FOUND_KEY = "defaultTopicNotFound";
130+
public static final String MSG_REPLY_NOT_MATCHED_KEY = "replyNotMatched";
131+
public static final String MSG_REPLY_NOT_FOUND_KEY = "replyNotFound";
132+
public static final String MSG_OBJECT_NOT_FOUND_KEY = "objectNotFound";
133+
public static final String MSG_CANNOT_DIVIDE_BY_ZERO_KEY = "cannotDivideByZero";
134+
public static final String MSG_CANNOT_MATH_VARIABLE_KEY = "cannotMathVariable";
135+
public static final String MSG_CANNOT_MATH_VALUE_KEY = "cannotMathValue";
136+
public static final String UNDEFINED = "undefined";
137+
127138
private static final String[] DEFAULT_FILE_EXTENSIONS = new String[] {".rive", ".rs"};
128139
private static final Random RANDOM = new Random();
129140
private static final String UNDEF_TAG = "<undef>";
@@ -192,16 +203,15 @@ public RiveScript(Config config) {
192203
this.unicodePunctuation = Pattern.compile(unicodePunctuation);
193204

194205
this.errorMessages = new HashMap<>();
195-
this.errorMessages.put("deepRecursion", "ERR: Deep Recursion Detected");
196-
this.errorMessages.put("repliesNotSorted", "ERR: Replies Not Sorted");
197-
this.errorMessages.put("defaultTopicNotFound", "ERR: No default topic 'random' was found");
198-
this.errorMessages.put("replyNotMatched", "ERR: No Reply Matched");
199-
this.errorMessages.put("replyNotFound", "ERR: No Reply Found");
200-
this.errorMessages.put("objectNotFound", "[ERR: Object Not Found]");
201-
this.errorMessages.put("cannotDivideByZero", "[ERR: Can't Divide By Zero]");
202-
this.errorMessages.put("cannotMathVariable", "[ERR: Can't perform math operation on non-numeric variable]");
203-
this.errorMessages.put("cannotMathValue", "[ERR: Can't perform math operation on non-numeric value]");
204-
this.errorMessages.put("undefined", "undefined");
206+
this.errorMessages.put(MSG_DEEP_RECURSION_KEY, "ERR: Deep Recursion Detected");
207+
this.errorMessages.put(MSG_REPLIES_NOT_SORTED_KEY, "ERR: Replies Not Sorted");
208+
this.errorMessages.put(MSG_DEFAULT_TOPIC_NOT_FOUND_KEY, "ERR: No default topic 'random' was found");
209+
this.errorMessages.put(MSG_REPLY_NOT_MATCHED_KEY, "ERR: No Reply Matched");
210+
this.errorMessages.put(MSG_REPLY_NOT_FOUND_KEY, "ERR: No Reply Found");
211+
this.errorMessages.put(MSG_OBJECT_NOT_FOUND_KEY, "[ERR: Object Not Found]");
212+
this.errorMessages.put(MSG_CANNOT_DIVIDE_BY_ZERO_KEY, "[ERR: Can't Divide By Zero]");
213+
this.errorMessages.put(MSG_CANNOT_MATH_VARIABLE_KEY, "[ERR: Can't perform math operation on non-numeric variable]");
214+
this.errorMessages.put(MSG_CANNOT_MATH_VALUE_KEY, "[ERR: Can't perform math operation on non-numeric value]");
205215

206216
if (config.getErrorMessages() != null) {
207217
for (Map.Entry<String, String> entry : config.getErrorMessages().entrySet()) {
@@ -1309,7 +1319,7 @@ private String getReply(String username, String message, boolean isBegin, int st
13091319
// Needed to sort replies?
13101320
if (this.sorted.getTopics().size() == 0) {
13111321
logger.warn("You forgot to call sortReplies()!");
1312-
String errorMessage = this.errorMessages.get("repliesNotSorted");
1322+
String errorMessage = this.errorMessages.get(MSG_REPLIES_NOT_SORTED_KEY);
13131323
if (this.throwExceptions) {
13141324
throw new RepliesNotSortedException(errorMessage);
13151325
}
@@ -1334,7 +1344,7 @@ private String getReply(String username, String message, boolean isBegin, int st
13341344

13351345
// Avoid deep recursion.
13361346
if (checkDeepRecursion(step, "Deep recursion while getting reply!")) {
1337-
return this.errorMessages.get("deepRecursion");
1347+
return this.errorMessages.get(MSG_DEEP_RECURSION_KEY);
13381348
}
13391349

13401350
// Are we in the BEGIN block?
@@ -1345,7 +1355,7 @@ private String getReply(String username, String message, boolean isBegin, int st
13451355
// More topic sanity checking.
13461356
if (!this.topics.containsKey(topic)) {
13471357
// This was handled before, which would mean topic=random and it doesn't exist. Serious issue!
1348-
String errorMessage = this.errorMessages.get("defaultTopicNotFound");
1358+
String errorMessage = this.errorMessages.get(MSG_DEFAULT_TOPIC_NOT_FOUND_KEY);
13491359
if (this.throwExceptions) {
13501360
throw new NoDefaultTopicException(errorMessage);
13511361
}
@@ -1513,10 +1523,10 @@ private String getReply(String username, String message, boolean isBegin, int st
15131523

15141524
// Defaults?
15151525
if (left.length() == 0) {
1516-
left = "undefined";
1526+
left = UNDEFINED;
15171527
}
15181528
if (right.length() == 0) {
1519-
right = "undefined";
1529+
right = UNDEFINED;
15201530
}
15211531

15221532
logger.debug("Check if {} {} {}", left, eq, right);
@@ -1597,13 +1607,13 @@ private String getReply(String username, String message, boolean isBegin, int st
15971607

15981608
// Still no reply?? Give up with the fallback error replies.
15991609
if (!foundMatch) {
1600-
String errorMessage = this.errorMessages.get("replyNotMatched");
1610+
String errorMessage = this.errorMessages.get(MSG_REPLY_NOT_MATCHED_KEY);
16011611
if (this.throwExceptions) {
16021612
throw new ReplyNotMatchedException(errorMessage);
16031613
}
16041614
reply = errorMessage;
16051615
} else if (reply == null || reply.length() == 0) {
1606-
String errorMessage = this.errorMessages.get("replyNotFound");
1616+
String errorMessage = this.errorMessages.get(MSG_REPLY_NOT_FOUND_KEY);
16071617
if (this.throwExceptions) {
16081618
throw new ReplyNotFoundException(errorMessage);
16091619
}
@@ -1690,13 +1700,13 @@ private String formatMessage(String message, boolean botReply) {
16901700
/**
16911701
* Processes tags in a reply element.
16921702
*
1693-
* @param username
1694-
* @param message
1695-
* @param reply
1696-
* @param st
1697-
* @param bst
1698-
* @param step
1699-
* @return
1703+
* @param username the username
1704+
* @param message the user's message
1705+
* @param reply the reply
1706+
* @param st the stars
1707+
* @param bst the bot stars
1708+
* @param step the recursion depth counter
1709+
* @return the processed reply
17001710
*/
17011711
private String processTags(String username, String message, String reply, List<String> st, List<String> bst, int step) {
17021712
// Prepare the stars and botstars.
@@ -1707,10 +1717,10 @@ private String processTags(String username, String message, String reply, List<S
17071717
botstars.add("");
17081718
botstars.addAll(bst);
17091719
if (stars.size() == 1) {
1710-
stars.add("undefined");
1720+
stars.add(UNDEFINED);
17111721
}
17121722
if (botstars.size() == 1) {
1713-
botstars.add("undefined");
1723+
botstars.add(UNDEFINED);
17141724
}
17151725

17161726
// Turn arrays into randomized sets.
@@ -1883,7 +1893,7 @@ private String processTags(String username, String message, String reply, List<S
18831893
if (target.containsKey(data)) {
18841894
insert = target.get(data);
18851895
} else {
1886-
insert = "undefined";
1896+
insert = UNDEFINED;
18871897
}
18881898
}
18891899
} else if (tag.equals("set")) {
@@ -1928,24 +1938,24 @@ private String processTags(String username, String message, String reply, List<S
19281938
// Don't divide by zero.
19291939
if (value == 0) {
19301940
logger.warn("Can't divide by zero");
1931-
insert = this.errorMessages.get("cannotDivideByZero");
1941+
insert = this.errorMessages.get(MSG_CANNOT_DIVIDE_BY_ZERO_KEY);
19321942
}
19331943
result /= value;
19341944
}
19351945
this.sessions.set(username, name, Integer.toString(result));
19361946
} catch (NumberFormatException e) {
19371947
logger.warn("Math can't " + tag + " non-numeric variable " + name);
1938-
insert = this.errorMessages.get("cannotMathVariable");
1948+
insert = this.errorMessages.get(MSG_CANNOT_MATH_VARIABLE_KEY);
19391949
}
19401950
} catch (NumberFormatException e) {
19411951
logger.warn("Math can't " + tag + " non-numeric value " + strValue);
1942-
insert = this.errorMessages.get("cannotMathValue");
1952+
insert = this.errorMessages.get(MSG_CANNOT_MATH_VALUE_KEY);
19431953
}
19441954
} else if (tag.equals("get")) {
19451955
// <get> user vars.
19461956
insert = this.sessions.get(username, data);
19471957
if (insert == null) {
1948-
insert = "undefined";
1958+
insert = UNDEFINED;
19491959
}
19501960
} else {
19511961
// Unrecognized tag; preserve it.
@@ -2018,7 +2028,7 @@ private String processTags(String username, String message, String reply, List<S
20182028
String language = this.objectLanguages.get(obj);
20192029
output = this.handlers.get(language).call(obj, args);
20202030
} else {
2021-
output = this.errorMessages.get("objectNotFound");
2031+
output = this.errorMessages.get(MSG_OBJECT_NOT_FOUND_KEY);
20222032
}
20232033
if (output == null) {
20242034
output = "";
@@ -2257,7 +2267,7 @@ private String triggerRegexp(String username, String pattern) {
22572267
}
22582268

22592269
String name = matcher.group(1);
2260-
String rep = "undefined";
2270+
String rep = UNDEFINED;
22612271
String value = this.sessions.get(username, name);
22622272
if (value != null) {
22632273
rep = value;
@@ -2284,8 +2294,8 @@ private String triggerRegexp(String username, String pattern) {
22842294
pattern = pattern.replace(inputPattern, history.getInput(i - 1));
22852295
pattern = pattern.replace(replyPattern, history.getReply(i - 1));
22862296
} else {
2287-
pattern = pattern.replace(inputPattern, "undefined");
2288-
pattern = pattern.replace(replyPattern, "undefined");
2297+
pattern = pattern.replace(inputPattern, UNDEFINED);
2298+
pattern = pattern.replace(replyPattern, UNDEFINED);
22892299
}
22902300
}
22912301
}

rivescript-core/src/main/java/com/rivescript/session/History.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.util.ArrayList;
2727
import java.util.List;
2828

29+
import static com.rivescript.RiveScript.UNDEFINED;
2930
import static com.rivescript.session.SessionManager.HISTORY_SIZE;
3031

3132
/**
@@ -42,11 +43,11 @@ public class History implements Serializable {
4243
private List<String> reply;
4344

4445
public History() {
45-
this.input = new ArrayList<>();
46-
this.reply = new ArrayList<>();
46+
this.input = new ArrayList<>(HISTORY_SIZE);
47+
this.reply = new ArrayList<>(HISTORY_SIZE);
4748
for (int i = 0; i < HISTORY_SIZE; i++) {
48-
this.input.add("undefined");
49-
this.reply.add("undefined");
49+
this.input.add(UNDEFINED);
50+
this.reply.add(UNDEFINED);
5051
}
5152
}
5253

0 commit comments

Comments
 (0)