public class RiveScript
extends java.lang.Object
SYNOPSIS:
import com.rivescript.Config;
import com.rivescript.RiveScript;
// Create a new bot with the default settings.
RiveScript bot = new RiveScript();
// To enable UTF-8 mode, you'd have initialized the bot like:
RiveScript bot = new RiveScript(Config.utf8());
// Load a directory full of RiveScript documents (.rive files)
bot.loadDirectory("./replies");
// Load an individual file.
bot.LoadFile("./testsuite.rive");
// Sort the replies after loading them!
bot.sortReplies();
// Get a reply.
String reply = bot.reply("user", "Hello bot!");
Constructor and Description |
---|
RiveScript()
Creates a new
RiveScript interpreter. |
RiveScript(Config config)
Creates a new
RiveScript interpreter with the given Config . |
Modifier and Type | Method and Description |
---|---|
void |
clearAllUservars()
Clears all variables for all users.
|
void |
clearUservars(java.lang.String username)
Clears a user's variables.
|
java.lang.String |
currentUser()
Returns the current user's ID.
|
void |
dumpSorted()
Dumps the trigger sort buffers to the standard output stream.
|
void |
dumpTopics()
Dumps the entire topic/trigger/reply structure to the standard output stream.
|
void |
freezeUservars(java.lang.String username)
Makes a snapshot of a user's variables.
|
int |
getDepth()
Returns the recursion depth limit.
|
java.util.Map<java.lang.String,java.lang.String> |
getErrorMessages()
Returns the error messages (unmodifiable).
|
java.lang.String |
getGlobal(java.lang.String name)
Returns a global variable.
|
java.lang.String |
getPerson(java.lang.String name)
Returns a person substitution pattern.
|
java.lang.String |
getSubstitution(java.lang.String name)
Returns a substitution pattern.
|
java.lang.String |
getUnicodePunctuation()
Returns the unicode punctuation pattern.
|
java.lang.String |
getUservar(java.lang.String username,
java.lang.String name)
Returns a user variable.
|
UserData |
getUservars(java.lang.String username)
Returns all variables for a user.
|
java.lang.String |
getVariable(java.lang.String name)
Returns a bot variable.
|
static java.lang.String |
getVersion()
Returns the RiveScript library version, or
null if it cannot be determined. |
boolean |
isForceCase()
Returns whether forcing triggers to lowercase is enabled.
|
boolean |
isStrict()
Returns whether strict syntax checking is enabled.
|
boolean |
isThrowExceptions()
Returns whether exception throwing is enabled.
|
boolean |
isUtf8()
Returns whether UTF-8 mode is enabled for user messages and triggers.
|
java.lang.String |
lastMatch(java.lang.String username)
Returns a user's last matched trigger.
|
void |
loadDirectory(java.io.File directory,
java.lang.String... extensions)
Loads multiple RiveScript documents from a directory on disk.
|
void |
loadDirectory(java.nio.file.Path path,
java.lang.String... extensions)
Loads multiple RiveScript documents from a directory on disk.
|
void |
loadDirectory(java.lang.String path,
java.lang.String... extensions)
Loads multiple RiveScript documents from a directory on disk.
|
void |
loadFile(java.io.File file)
Loads a single RiveScript document from disk.
|
void |
loadFile(java.nio.file.Path path)
Loads a single RiveScript document from disk.
|
void |
loadFile(java.lang.String path)
Loads a single RiveScript document from disk.
|
void |
removeHandler(java.lang.String name)
Removes an object macro language handler.
|
void |
removeSubroutine(java.lang.String name)
Removes a Java object macro.
|
java.lang.String |
reply(java.lang.String username,
java.lang.String message)
Returns a reply from the bot for a user's message.
|
void |
setGlobal(java.lang.String name,
java.lang.String value)
Sets a global variable.
|
void |
setHandler(java.lang.String name,
ObjectHandler handler)
Sets a custom language handler for RiveScript object macros.
|
void |
setPerson(java.lang.String name,
java.lang.String value)
Sets a person substitution pattern.
|
void |
setSubroutine(java.lang.String name,
Subroutine subroutine)
Defines a Java object macro.
|
void |
setSubstitution(java.lang.String name,
java.lang.String value)
Sets a substitution pattern.
|
void |
setUservar(java.lang.String username,
java.lang.String name,
java.lang.String value)
Sets a user variable.
|
void |
setUservars(java.lang.String username,
java.util.Map<java.lang.String,java.lang.String> vars)
Set a user's variables.
|
void |
setVariable(java.lang.String name,
java.lang.String value)
Sets a bot variable.
|
void |
sortReplies()
Sorts the reply structures in memory for optimal matching.
|
void |
stream(java.lang.String code)
Loads RiveScript source code from a text buffer, with line breaks after each line.
|
void |
stream(java.lang.String[] code)
Loads RiveScript source code from a
String array, one line per item. |
void |
thawUservars(java.lang.String username,
ThawAction action)
Unfreezes a user's variables.
|
public RiveScript()
RiveScript
interpreter.public RiveScript(Config config)
RiveScript
interpreter with the given Config
.config
- the configpublic static java.lang.String getVersion()
null
if it cannot be determined.Package.getImplementationVersion()
public boolean isThrowExceptions()
public boolean isStrict()
public boolean isUtf8()
public boolean isForceCase()
public int getDepth()
public java.lang.String getUnicodePunctuation()
public java.util.Map<java.lang.String,java.lang.String> getErrorMessages()
public void setHandler(java.lang.String name, ObjectHandler handler)
name
- the name of the programming languagehandler
- the implementationpublic void removeHandler(java.lang.String name)
name
- the name of the programming languagepublic void setSubroutine(java.lang.String name, Subroutine subroutine)
Because Java is a compiled language, this method must be used to create an object macro written in Java.
name
- the name of the object macro for the `subroutine
- the subroutinepublic void removeSubroutine(java.lang.String name)
name
- the name of the object macropublic void setGlobal(java.lang.String name, java.lang.String value)
This is equivalent to ! global
in RiveScript. Set the value to null
to delete a global.
name
- the variable namevalue
- the variable value or null
public java.lang.String getGlobal(java.lang.String name)
This is equivalent to <env>
in RiveScript. Returns null
if the variable isn't defined.
name
- the variable namenull
public void setVariable(java.lang.String name, java.lang.String value)
This is equivalent to ! vars
in RiveScript. Set the value to null
to delete a bot variable.
name
- the variable namevalue
- the variable value or null
public java.lang.String getVariable(java.lang.String name)
This is equivalent to <bot>
in RiveScript. Returns null
if the variable isn't defined.
name
- the variable namenull
public void setSubstitution(java.lang.String name, java.lang.String value)
This is equivalent to ! sub
in RiveScript. Set the value to null
to delete a substitution.
name
- the substitution namevalue
- the substitution pattern or null
public java.lang.String getSubstitution(java.lang.String name)
Returns null
if the substitution isn't defined.
name
- the substitution namenull
public void setPerson(java.lang.String name, java.lang.String value)
This is equivalent to ! person
in RiveScript. Set the value to null
to delete a person substitution.
name
- the person substitution namevalue
- the person substitution pattern or null
public java.lang.String getPerson(java.lang.String name)
This is equivalent to <person>
in RiveScript. Returns null
if the person substitution isn't defined.
name
- the person substitution namenull
public void loadFile(java.io.File file) throws RiveScriptException, ParserException
file
- the RiveScript fileRiveScriptException
- in case of a loading errorParserException
- in case of a parsing errorpublic void loadFile(java.nio.file.Path path) throws RiveScriptException, ParserException
path
- the path to the RiveScript documentRiveScriptException
- in case of a loading errorParserException
- in case of a parsing errorpublic void loadFile(java.lang.String path) throws RiveScriptException, ParserException
path
- the path to the RiveScript documentRiveScriptException
- in case of a loading errorParserException
- in case of a parsing errorpublic void loadDirectory(java.io.File directory, java.lang.String... extensions) throws RiveScriptException, ParserException
directory
- the directory containing the RiveScript documentsRiveScriptException
- in case of a loading errorParserException
- in case of a parsing errorpublic void loadDirectory(java.nio.file.Path path, java.lang.String... extensions) throws RiveScriptException, ParserException
path
- The path to the directory containing the RiveScript documentsRiveScriptException
- in case of a loading errorParserException
- in case of a parsing errorpublic void loadDirectory(java.lang.String path, java.lang.String... extensions) throws RiveScriptException, ParserException
path
- The path to the directory containing the RiveScript documentsRiveScriptException
- in case of a loading errorParserException
- in case of a parsing errorpublic void stream(java.lang.String code) throws ParserException
code
- the RiveScript source codeParserException
- in case of a parsing errorpublic void stream(java.lang.String[] code) throws ParserException
String
array, one line per item.code
- the lines of RiveScript source codeParserException
- in case of a parsing errorpublic void sortReplies()
After finishing loading the RiveScript code, this method needs to be called to populate the various sort buffers. This is absolutely necessary for reply matching to work efficiently!
public java.lang.String reply(java.lang.String username, java.lang.String message) throws RiveScriptException
In case of an exception and exception throwing is enabled a RiveScriptException
is thrown.
Check the subclasses to see which types exceptions can be thrown.
username
- the usernamemessage
- the user's messageRiveScriptException
- in case of an exception and exception throwing is enabledpublic void setUservar(java.lang.String username, java.lang.String name, java.lang.String value)
This is equivalent to <set>
in RiveScript. Set the value to null
to delete a user variable.
username
- the usernamename
- the variable namevalue
- the variable valuepublic void setUservars(java.lang.String username, java.util.Map<java.lang.String,java.lang.String> vars)
Set multiple user variables by providing a Map
of key/value pairs.
Equivalent to calling setUservar(String, String, String)
for each pair in the map.
username
- the namevars
- the user variablespublic java.lang.String getUservar(java.lang.String username, java.lang.String name)
This is equivalent to <get name>
in RiveScript. Returns "undefined"
if the variable isn't defined.
username
- the usernamename
- the variable namepublic UserData getUservars(java.lang.String username)
username
- the usernamepublic void clearAllUservars()
public void clearUservars(java.lang.String username)
username
- the usernamepublic void freezeUservars(java.lang.String username)
username
- the usernamepublic void thawUservars(java.lang.String username, ThawAction action)
username
- the usernameaction
- the thaw actionThawAction
public java.lang.String lastMatch(java.lang.String username)
username
- the usernamepublic java.lang.String currentUser()
This is only useful from within a (Java) object macro, to get the ID of the user who invoked the macro.
This value is set at the beginning of reply(String, String)
and unset at the end, so this method will return null
outside of a reply context.
null
public void dumpSorted()
public void dumpTopics()