├── .editorconfig ├── .gitignore ├── .travis-deploy-artifacts.sh ├── .travis.yml ├── CONTRIBUTING.md ├── Changes.md ├── LICENSE ├── README.md ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── rivescript-core └── src │ ├── integration-test │ ├── java │ │ └── com │ │ │ └── rivescript │ │ │ ├── BaseIT.java │ │ │ ├── BeginIT.java │ │ │ ├── BotVariableIT.java │ │ │ ├── ConfigIT.java │ │ │ ├── ExceptionsIT.java │ │ │ ├── GroovyObjectIT.java │ │ │ ├── JavaObjectIT.java │ │ │ ├── JavaScriptObjectIT.java │ │ │ ├── MathIT.java │ │ │ ├── ObjectIT.java │ │ │ ├── OptionsIT.java │ │ │ ├── ReplyIT.java │ │ │ ├── RiveScriptIT.java │ │ │ ├── RubyObjectIT.java │ │ │ ├── SessionsIT.java │ │ │ ├── SubstitutionIT.java │ │ │ ├── TopicIT.java │ │ │ ├── TriggerIT.java │ │ │ └── UnicodeIT.java │ └── resources │ │ ├── simplelogger.properties │ │ └── testsuite.rive │ ├── main │ └── java │ │ └── com │ │ └── rivescript │ │ ├── ConcatMode.java │ │ ├── Config.java │ │ ├── RiveScript.java │ │ ├── RiveScriptException.java │ │ ├── ast │ │ ├── Begin.java │ │ ├── ObjectMacro.java │ │ ├── Root.java │ │ ├── Topic.java │ │ ├── Trigger.java │ │ └── package-info.java │ │ ├── cmd │ │ ├── Shell.java │ │ └── package-info.java │ │ ├── exception │ │ ├── DeepRecursionException.java │ │ ├── NoDefaultTopicException.java │ │ ├── RepliesNotSortedException.java │ │ ├── ReplyNotFoundException.java │ │ ├── ReplyNotMatchedException.java │ │ └── package-info.java │ │ ├── lang │ │ ├── Perl.java │ │ ├── groovy │ │ │ ├── GroovyHandler.java │ │ │ └── package-info.java │ │ ├── javascript │ │ │ ├── JavaScriptHandler.java │ │ │ └── package-info.java │ │ ├── jsr223 │ │ │ ├── Jsr223ScriptingHandler.java │ │ │ └── package-info.java │ │ ├── package-info.java │ │ └── ruby │ │ │ ├── RubyHandler.java │ │ │ └── package-info.java │ │ ├── macro │ │ ├── ObjectHandler.java │ │ ├── Subroutine.java │ │ └── package-info.java │ │ ├── parser │ │ ├── Parser.java │ │ ├── ParserConfig.java │ │ ├── ParserException.java │ │ └── package-info.java │ │ ├── regexp │ │ ├── Regexp.java │ │ └── package-info.java │ │ ├── session │ │ ├── ConcurrentHashMapSessionManager.java │ │ ├── History.java │ │ ├── NoOpSessionManager.java │ │ ├── SessionManager.java │ │ ├── ThawAction.java │ │ ├── UserData.java │ │ └── package-info.java │ │ ├── sorting │ │ ├── SortBuffer.java │ │ ├── SortTrack.java │ │ ├── SortedTriggerEntry.java │ │ └── package-info.java │ │ └── util │ │ ├── StringUtils.java │ │ └── package-info.java │ └── test │ ├── java │ └── com │ │ └── rivescript │ │ ├── ConfigBuilderTests.java │ │ ├── ConfigTests.java │ │ ├── RiveScriptResourceTests.java │ │ ├── ast │ │ └── RootTests.java │ │ ├── parser │ │ ├── ParserConfigBuilderTests.java │ │ └── ParserConfigTests.java │ │ ├── session │ │ └── ConcurrrentHashMapSessionManagerTests.java │ │ └── util │ │ └── StringUtilTests.java │ └── resources │ ├── simplelogger.properties │ └── test.rive ├── rivescript-spring-boot-starter └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── rivescript │ │ │ └── spring │ │ │ └── boot │ │ │ └── autoconfigure │ │ │ ├── RiveScriptAutoConfiguration.java │ │ │ └── RiveScriptProperties.java │ └── resources │ │ └── META-INF │ │ └── spring.factories │ └── test │ ├── java │ └── com │ │ └── rivescript │ │ └── spring │ │ └── boot │ │ └── autoconfigure │ │ └── RiveScriptAutoConfigurationTests.java │ └── resources │ ├── rivescript │ └── test.rive │ └── simplelogger.properties ├── samples ├── rsbot │ ├── build.gradle │ └── src │ │ └── main │ │ ├── java │ │ ├── ExampleMacro.java │ │ └── RSBot.java │ │ └── resources │ │ ├── lang │ │ └── rsp4j.pl │ │ └── rivescript │ │ ├── admin.rive │ │ ├── begin.rive │ │ ├── clients.rive │ │ ├── eliza.rive │ │ ├── myself.rive │ │ ├── obj-java.rive │ │ ├── obj-perl.rive │ │ └── rpg.rive └── spring-boot-starter-rsbot │ ├── build.gradle │ └── src │ └── main │ ├── java │ └── com │ │ └── rivescript │ │ └── samples │ │ └── Application.java │ └── resources │ ├── application.yml │ └── rivescript │ ├── admin.rive │ ├── begin.rive │ ├── clients.rive │ ├── eliza.rive │ ├── javascript.rive │ ├── myself.rive │ ├── rpg.rive │ └── test.rive ├── secring.gpg.enc ├── settings.gradle └── src ├── api └── overview.html └── eclipse ├── README.md └── rivescript-java-style.xml /.editorconfig: -------------------------------------------------------------------------------- 1 | # http://editorconfig.org 2 | 3 | root = true 4 | 5 | [*] 6 | charset = utf-8 7 | indent_style = tab 8 | end_of_line = lf 9 | insert_final_newline = true 10 | trim_trailing_whitespace = true 11 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | .classpath 3 | .gradle 4 | .idea 5 | .project 6 | .settings 7 | *.iml 8 | *.ipr 9 | *.iws 10 | 11 | /bin 12 | /build 13 | /classes 14 | /lib 15 | /log 16 | /out 17 | /target 18 | *~ 19 | 20 | /rivescript-*/bin 21 | /rivescript-*/build 22 | /rivescript-*/classes 23 | /rivescript-*/lib 24 | /rivescript-*/log 25 | /rivescript-*/out 26 | /rivescript-*/target 27 | 28 | /samples/*/bin 29 | /samples/*/build 30 | /samples/*/classes 31 | /samples/*/lib 32 | /samples/*/log 33 | /samples/*/out 34 | /samples/*/target 35 | -------------------------------------------------------------------------------- /.travis-deploy-artifacts.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -e 4 | set +x 5 | 6 | # Do not deploy archives when building pull request 7 | if [ "$TRAVIS_BRANCH" != "master" ] || [ "$TRAVIS_PULL_REQUEST" == "true" ]; then 8 | exit 0 9 | fi 10 | 11 | # Deploy jar artifacts to Sonatype OSSRH 12 | 13 | ./gradlew -Psigning.keyId="$SIGNING_KEY" -Psigning.password="$SIGNING_PASSWORD" -Psigning.secretKeyRingFile="${TRAVIS_BUILD_DIR}/secring.gpg" uploadArchives 14 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: java 2 | dist: trusty 3 | sudo: false 4 | jdk: 5 | - oraclejdk8 6 | before_install: 7 | - openssl aes-256-cbc -K $encrypted_69327d5a42d4_key -iv $encrypted_69327d5a42d4_iv -in secring.gpg.enc -out secring.gpg -d 8 | after_success: 9 | - ./.travis-deploy-artifacts.sh 10 | cache: 11 | directories: 12 | - $HOME/.gradle 13 | - $HOME/.m2 14 | notifications: 15 | webhooks: 16 | urls: 17 | - https://webhooks.gitter.im/e/53c5025773af9113e108 18 | on_success: always # options: [always|never|change] default: always 19 | on_failure: always # options: [always|never|change] default: always 20 | on_start: never # options: [always|never|change] default: always 21 | env: 22 | global: 23 | # SIGNING_KEY 24 | - secure: "xlxM7lxFwvIMkh9GoDASvaINYwzh9A8fI2t0hd0pZBK/tcyLKM/4A7e92DI8ltkkGZrpPaWxyLoBkCNv0n99QKjqaonksnvwd2bbFzVqXuyJLPjh7vbA5HlR8KCf6NIn5E0ykgFCpDKMq4Wo5vSJT9bDC0gNSchbO285BwzT0LRA7R+rUlQTPUXk++VqYYmRhwuySDjnUwCAtjBoHTGhH6nB/41eIyeDz7U+oAmO4KAnRn4303FWV5p1fOP0mJkKzvkt2ryjiJ5vISWM3kUlUiUJVPLRYVgbdvWshE0C4kscUKiNZ+LfsyjNech/+r2mBZWN0cH91rTtcXn0bcmR29JPZUD5tb9BLjB9wlGV0ZfkRgJ0/acKq22BLA7r04eNdd4tk3I0puqX43cKWQ88z+HH8tdVrkHFOeP7yV4uxbhRYA2W2IGOFFcLbHaLlJizRr4JpJsSKiK+b9IPiLkP2Pyifjqq/NR/usUXBvvxLmYQe5PFbezsXo1LZ0xQQwaqdHGPC+R26SSr+9A7qmn/8hWhggnpNszWc8GVTc+i+nS1bghyGm6iBg3OMHkUXnEX4rs/sREM0xdqwRqxxCgAJADTxNOpi3pCmOZMhAEewFgovNuz/bqvz3MMcW3JKRTwTV5nbQQbZaJ0CXEjKm8N+ziM4jNPfZf+TqIc4CntzVo=" 25 | # SIGNING_PASSWORD 26 | - secure: "RqngaD+YkMApJNd0+63OvXdUJH9j/6bAuxL2BKUgDBdZJKWh5iW6SSA5JGrZRjXGDKGKgasCttBLWzN5idqsDsyudUL5Qa0LMlEhaORi1u1e1KxhJTef5iDtdHagho0rSXcrsWPQ1HpAHMyk/v4Eu+4+pI4kGStNh+u93geyKoMNWraIVKdTQ9BjIe9ubVe9vTj45JcWLJET0BqjrWVk7/dRXlo6zCMnbSGS2vr7b/ctLCNqwUX3ih73ZR5OUu9u3mK/zB7k269sv0zX8POGgDLzXG6C/ystTndnGiQOmYBT27/dPDQ4adf8+jic/1tcat+Fsm2EIqUVWrwsllXZcvnO6fv1HO7WXXBAEq3fQSyQ7jMaynPL6ky+9H646y08AXEU+r09KCEb0enemVS/dwAzk9eGoIIOwy7/CPjXVe9lASPkURlGeRbDmYAAsjZaFjfa9EywTN8DY51/ZAxQzoOSw1KcII3sMcnMgwA/fnIn48icCcs2y9GoiBDn56Ti65050IR18fiQ0rkLqQ4YqVS8t444I2+xzgQaPiPHnVJuDEODJadFxKdoW+ADB/kwRtDtIgzLXiGUomvcNvvAWF9M+v9THUUYJP579xO46pOrH7mZwgLhRUBzTV/mEShSjcNZ4UqZyjxTs6xlLSXYbbz73buc0/XsVvlqHIsjBdk=" 27 | # SONATYPE_OSSRH_USERNAME 28 | - secure: "q+lXCSq/zf9Wdgg3GpVo98OZ2WdizgImDc4gKv09L3z6ei7IUhwuzNjMxVTL7rW10fbVQKkn/3+vvE0EisYoi/q3c5ggg8sRsRhNbLfz0KxkgxmKjhmF5VRtDWkXLgiEGLpklOdEPq+NZ4Hw305M95NZvJKQmmV5Pjil5PndI4+LCEBaSqUXis3iKyPXiEdcuGi9udqRclWdOMqKiOCYDNtkpNPqOum9vEo99g8fz/m2+R+uCx5yW+AOz+wbJQGnv0GIbLXVAdVPreUnXYj8AtbLa/qeMuYis5k3gdT8bUU66KmZCaM0yHWq4bbjTum1vcQ/Q9qDlkcBk9LJvxXm2ojAPnyz5oosVmJGgk6y6l4NvhPCo4aZnHS6XzDN6s0PwNi/C2NO1tX4vayzl4p0ZpYIKNBA0kDmeVag0qbWiBtImBZqoSjQvmuW08w5N33751UPxlJ+vboHcxDSGioSeAo+hjRuCz/IeIffpMLzls19xUL40jLPHCGaMB48PcttvabKtj9w4UvvRsVdPJNEUms9N+H+D5Fl5EL/Ypb4XiIaTLpfv3p05lx/NeGmwmUkm+l8Zx0efOpKJ16DTHGiQyUlJVmElU7kkhsIf1nERCcVFB+ccs7nGlS3CjP/5FgeRuKLFCD8+srGZlxCAlOoIKHg0rpWiTSHre+gr0TOZZ0=" 29 | # SONATYPE_OSSRH_PASSWORD 30 | - secure: "XWP+W7tBNv2w+GPRYlF52xCDBVjUO2UJ3hG4EkDt0PnswPIeuZ0rjVEAF5iHkpK6imMenj2ritIlfhGiQyFANFCkKcGChkISrRgK0/X1uWHlBmMU5sgSlQRtMk0UQCJ3jU2tHLl57x/CczrumncyOCaixnIVDz2FMA7E/woYmJofCyfQPKuYAzkOi2DsKBpIkgyQflgknGdX4QqDD8/ldvhcXqOPyqFOwBSoZRVwT/03q/VqbFrCYRUvTyKS4p4hSEkEh+tpQbtzIeIPhUK4PSwd3HmaO9mMD4XjAMQKn3qvT+Hzn4FWvIuzWuVVGM5G0JPYttkvRdEZnxBVyU0gxlY8s4CHx09Av1Q3KWvq2M+am2loMQNzwdxcbwvkninCBm8CfVG+kSoaOwK8aO5LcFsMZMAxy0P9CHuvTIsRJNRc8iRWzH2kSBjQPZ1okJUGrD0+TkREfcTIIkN/zyVUK2S67Y5aJm/GR3Aad96zG2fB5A5UgHmWPOfKdxKfCglSBNLP4LMsKlRmifxKDQdSK9A9rB2Ocee6RygXxrj34eVMZ8bbdKefBYGENmYuFwlc+YnitClao1FLnGB6yYO2t/KnpYNIoVQVRptN2BbewE2XRB53KEVRlz6NjcGhHZRELV5qi3ECWqMdlsKbWBx4DxDMHJOIqPlavUkI3Vi60/k=" 31 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing 2 | 3 | Interested in contributing to RiveScript? Great! 4 | 5 | First, check the general contributing guidelines for RiveScript and its primary 6 | implementations found at . 7 | 8 | Users should simply import the Eclipse 9 | [RiveScript Java Style](https://github.com/aichaos/rivescript-java/blob/master/src/eclipse/rivescript-java-style.xml) 10 | file in their IDE. The same code formatter file should also be imported in IntelliJ IDEA. 11 | 12 | # Quick Start 13 | 14 | Fork, then clone the repo: 15 | 16 | git clone git@github.com:your-username/rivescript-java.git 17 | 18 | Make your code changes and test them by using the included `RSBot` sample: 19 | 20 | ./gradlew :rivescript-samples-rsbot:runApp --console plain 21 | 22 | Push to your fork and [submit a pull request](https://github.com/kirsle/rivescript-java/compare/). 23 | 24 | At this point you're waiting on me. I'm usually pretty quick to comment on pull 25 | requests (within a few days) and I may suggest some changes or improvements 26 | or alternatives. 27 | 28 | Some things that will increase the chance that your pull request is accepted: 29 | 30 | * Follow the style guide at 31 | * Write a [good commit message](http://tbaggery.com/2008/04/19/a-note-about-git-commit-messages.html). 32 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 the original author or authors. 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | version=0.12.0-SNAPSHOT 2 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aichaos/rivescript-java/04abeca7fffeaf0783e6aa9c2fe84c34829e5a67/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-5.5.1-all.zip 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | set DIRNAME=%~dp0 12 | if "%DIRNAME%" == "" set DIRNAME=. 13 | set APP_BASE_NAME=%~n0 14 | set APP_HOME=%DIRNAME% 15 | 16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 17 | set DEFAULT_JVM_OPTS= 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windows variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | 53 | :win9xME_args 54 | @rem Slurp the command line arguments. 55 | set CMD_LINE_ARGS= 56 | set _SKIP=2 57 | 58 | :win9xME_args_slurp 59 | if "x%~1" == "x" goto execute 60 | 61 | set CMD_LINE_ARGS=%* 62 | 63 | :execute 64 | @rem Setup the command line 65 | 66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 67 | 68 | @rem Execute Gradle 69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 70 | 71 | :end 72 | @rem End local scope for the variables with windows NT shell 73 | if "%ERRORLEVEL%"=="0" goto mainEnd 74 | 75 | :fail 76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 77 | rem the _cmd.exe /c_ return code! 78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 79 | exit /b 1 80 | 81 | :mainEnd 82 | if "%OS%"=="Windows_NT" endlocal 83 | 84 | :omega 85 | -------------------------------------------------------------------------------- /rivescript-core/src/integration-test/java/com/rivescript/BaseIT.java: -------------------------------------------------------------------------------- 1 | package com.rivescript;/* 2 | * Copyright (c) 2016 the original author or authors. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in all 12 | * copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | * SOFTWARE. 21 | */ 22 | 23 | import java.io.File; 24 | 25 | import static org.hamcrest.Matchers.equalTo; 26 | import static org.hamcrest.Matchers.instanceOf; 27 | import static org.hamcrest.Matchers.is; 28 | import static org.hamcrest.Matchers.isIn; 29 | import static org.junit.Assert.assertThat; 30 | import static org.junit.Assert.fail; 31 | 32 | /** 33 | * Base class for integration tests. 34 | * 35 | * @author Noah Petherbridge 36 | * @author Marcel Overdijk 37 | */ 38 | public class BaseIT { 39 | 40 | protected RiveScript rs; 41 | protected String username = "local-user"; 42 | 43 | protected File getClassPathFile(String name) { 44 | return new File(getClass().getClassLoader().getResource(name).getFile()); 45 | } 46 | 47 | /** 48 | * Sets up the RiveScript source code and sorts the replies. 49 | * 50 | * @param code the lines of RiveScript source code 51 | */ 52 | protected void setUp(String[] code) { 53 | rs.stream(code); 54 | rs.sortReplies(); 55 | } 56 | 57 | /** 58 | * Asserts that a global variable has the expected value. 59 | * 60 | * @param name the variable name 61 | * @param expected the expected value 62 | */ 63 | protected void assertGlobal(String name, String expected) { 64 | assertThat(rs.getGlobal(name), is(equalTo(expected))); 65 | } 66 | 67 | /** 68 | * Asserts that a bot variable has the expected value. 69 | * 70 | * @param name the variable name 71 | * @param expected the expected value 72 | */ 73 | protected void assertVariable(String name, String expected) { 74 | assertThat(rs.getVariable(name), is(equalTo(expected))); 75 | } 76 | 77 | /** 78 | * Asserts that a substitution pattern has the expected value. 79 | * 80 | * @param name the substitution name 81 | * @param expected the expected substitution pattern 82 | */ 83 | protected void assertSubstitution(String name, String expected) { 84 | assertThat(rs.getSubstitution(name), is(equalTo(expected))); 85 | } 86 | 87 | /** 88 | * Asserts that a person substitution pattern has the expected value. 89 | * 90 | * @param name the person substitution name 91 | * @param expected the expected person substitution pattern 92 | */ 93 | protected void assertPerson(String name, String expected) { 94 | assertThat(rs.getPerson(name), is(equalTo(expected))); 95 | } 96 | 97 | /** 98 | * Sets a user variable. 99 | * 100 | * @param name the variable name 101 | * @param value the variable value 102 | */ 103 | protected void setUservar(String name, String value) { 104 | rs.setUservar(username, name, value); 105 | } 106 | 107 | /** 108 | * Asserts that a user variable has the expected value. 109 | * 110 | * @param name the variable name 111 | * @param expected the expected value 112 | */ 113 | protected void assertUservar(String name, String expected) { 114 | assertThat(rs.getUservar(username, name), is(equalTo(expected))); 115 | } 116 | 117 | /** 118 | * Asserts that a given input gets the expected reply. 119 | * 120 | * @param message the input message 121 | * @param expected the expected reply 122 | */ 123 | protected void assertReply(String message, String expected) { 124 | assertThat(rs.reply(username, message), is(equalTo(expected))); 125 | } 126 | 127 | /** 128 | * Asserts that a given input gets one of the expected replies. 129 | * 130 | * @param message the input message 131 | * @param expected the expected replies 132 | */ 133 | protected void assertReply(String message, String... expected) { 134 | assertThat(rs.reply(username, message), isIn(expected)); 135 | } 136 | 137 | /** 138 | * Asserts that a given input throws the expected exception. 139 | * 140 | * @param message the input message 141 | * @param expected the expected exception 142 | */ 143 | protected void assertReplyThrows(String message, Class expected) { 144 | try { 145 | String reply = rs.reply(username, message); 146 | fail("Expected: is a instance of " + expected.getCanonicalName() + " but: was \"" + reply + "\""); 147 | } catch (Exception e) { 148 | assertThat(e, is(instanceOf(expected))); 149 | } 150 | } 151 | } 152 | -------------------------------------------------------------------------------- /rivescript-core/src/integration-test/java/com/rivescript/BeginIT.java: -------------------------------------------------------------------------------- 1 | package com.rivescript;/* 2 | * Copyright (c) 2016 the original author or authors. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in all 12 | * copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | * SOFTWARE. 21 | */ 22 | 23 | import org.junit.Test; 24 | 25 | /** 26 | * @author Noah Petherbridge 27 | * @author Marcel Overdijk 28 | */ 29 | public class BeginIT extends BaseIT { 30 | 31 | @Test 32 | public void testNoBeginBlock() { 33 | rs = new RiveScript(); 34 | setUp(new String[] { 35 | "+ hello bot", 36 | "- Hello human." 37 | }); 38 | assertReply("Hello bot", "Hello human."); 39 | } 40 | 41 | @Test 42 | public void testSimpleBeginBlock() { 43 | rs = new RiveScript(); 44 | setUp(new String[] { 45 | "> begin", 46 | " + request", 47 | " - {ok}", 48 | "< begin", 49 | "", 50 | "+ hello bot", 51 | "- Hello human." 52 | }); 53 | assertReply("Hello bot", "Hello human."); 54 | } 55 | 56 | @Test 57 | public void testBlockedBeginBlock() { 58 | rs = new RiveScript(); 59 | setUp(new String[] { 60 | "> begin", 61 | " + request", 62 | " - Nope.", 63 | "< begin", 64 | "", 65 | "+ hello bot", 66 | "- Hello human." 67 | }); 68 | assertReply("Hello bot", "Nope."); 69 | } 70 | 71 | @Test 72 | public void testConditionalBeginBlock() { 73 | rs = new RiveScript(); 74 | setUp(new String[] { 75 | "> begin", 76 | " + request", 77 | " * == undefined => {ok}", 78 | " * != undefined => : {ok}", 79 | " - {ok}", 80 | "< begin", 81 | "", 82 | "+ hello bot", 83 | "- Hello human.", 84 | "", 85 | "+ my name is *", 86 | "- >Hello, ." 87 | }); 88 | assertReply("Hello bot", "Hello human."); 89 | setUservar("met", "true"); 90 | assertUservar("name", null); 91 | assertReply("My name is bob", "Hello, Bob."); 92 | assertUservar("name", "Bob"); 93 | assertReply("Hello bot", "Bob: Hello human."); 94 | } 95 | 96 | @Test 97 | public void testDefinitions() { 98 | rs = new RiveScript(); 99 | setUp(new String[] { 100 | "! global g1 = one", 101 | "! global g2 = two", 102 | "! global g2 = ", 103 | "", 104 | "! var v1 = one", 105 | "! var v2 = two", 106 | "! var v2 = ", 107 | "", 108 | "! sub what's = what is", 109 | "! sub who's = who is", 110 | "! sub who's = ", 111 | "", 112 | "! person you = me", 113 | "! person me = you", 114 | "! person your = my", 115 | "! person my = your", 116 | "! person your = ", 117 | "! person my = " 118 | }); 119 | assertGlobal("g1", "one"); 120 | assertGlobal("g2", null); 121 | assertVariable("v1", "one"); 122 | assertVariable("v2", null); 123 | assertSubstitution("what's", "what is"); 124 | assertSubstitution("who's", null); 125 | assertPerson("you", "me"); 126 | assertPerson("your", null); 127 | } 128 | } 129 | -------------------------------------------------------------------------------- /rivescript-core/src/integration-test/java/com/rivescript/BotVariableIT.java: -------------------------------------------------------------------------------- 1 | package com.rivescript;/* 2 | * Copyright (c) 2016 the original author or authors. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in all 12 | * copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | * SOFTWARE. 21 | */ 22 | 23 | import org.junit.Test; 24 | 25 | /** 26 | * @author Noah Petherbridge 27 | * @author Marcel Overdijk 28 | */ 29 | public class BotVariableIT extends BaseIT { 30 | 31 | @Test 32 | public void testBotVariables() { 33 | rs = new RiveScript(); 34 | setUp(new String[] { 35 | "! var name = Aiden", 36 | "! var age = 5", 37 | "", 38 | "+ what is your name", 39 | "- My name is .", 40 | "", 41 | "+ how old are you", 42 | "- I am .", 43 | "", 44 | "+ what are you", 45 | "- I'm .", 46 | "", 47 | "+ happy birthday", 48 | "- Thanks!" 49 | }); 50 | assertReply("What is your name?", "My name is Aiden."); 51 | assertReply("How old are you?", "I am 5."); 52 | assertReply("What are you?", "I'm undefined."); 53 | assertReply("Happy birthday!", "Thanks!"); 54 | assertReply("How old are you?", "I am 6."); 55 | } 56 | 57 | @Test 58 | public void testGlobalVariables() { 59 | rs = new RiveScript(); 60 | setUp(new String[] { 61 | "! global debug = false", 62 | "", 63 | "+ debug mode", 64 | "- Debug mode is: ", 65 | "", 66 | "+ set debug mode *", 67 | "- >Switched to ." 68 | }); 69 | assertReply("Debug mode.", "Debug mode is: false"); 70 | assertReply("Set debug mode true", "Switched to true."); 71 | assertReply("Debug mode?", "Debug mode is: true"); 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /rivescript-core/src/integration-test/java/com/rivescript/ConfigIT.java: -------------------------------------------------------------------------------- 1 | package com.rivescript;/* 2 | * Copyright (c) 2016 the original author or authors. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in all 12 | * copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | * SOFTWARE. 21 | */ 22 | 23 | import org.junit.Test; 24 | 25 | /** 26 | * @author Noah Petherbridge 27 | * @author Marcel Overdijk 28 | */ 29 | public class ConfigIT extends BaseIT { 30 | 31 | @Test 32 | public void testConfigApi() { 33 | rs = new RiveScript(); 34 | setUp(new String[] { 35 | "+ hello bot", 36 | "- Hello human." 37 | }); 38 | 39 | // Global variables. 40 | assertGlobal("global test", null); 41 | rs.setGlobal("global test", "on"); 42 | assertGlobal("global test", "on"); 43 | rs.setGlobal("global test", null); 44 | assertGlobal("global test", null); 45 | 46 | // Bot variables. 47 | assertVariable("var test", null); 48 | rs.setVariable("var test", "on"); 49 | assertVariable("var test", "on"); 50 | rs.setVariable("var test", null); 51 | assertVariable("var test", null); 52 | 53 | // Substitutions. 54 | assertSubstitution("what's", null); 55 | rs.setSubstitution("what's", "what is"); 56 | assertSubstitution("what's", "what is"); 57 | rs.setSubstitution("what's", null); 58 | assertSubstitution("what's", null); 59 | 60 | // Person substitutions. 61 | assertPerson("you", null); 62 | rs.setPerson("you", "me"); 63 | assertPerson("you", "me"); 64 | rs.setPerson("you", null); 65 | assertPerson("you", null); 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /rivescript-core/src/integration-test/java/com/rivescript/ExceptionsIT.java: -------------------------------------------------------------------------------- 1 | package com.rivescript;/* 2 | * Copyright (c) 2016 the original author or authors. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in all 12 | * copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | * SOFTWARE. 21 | */ 22 | 23 | import com.rivescript.exception.DeepRecursionException; 24 | import com.rivescript.exception.RepliesNotSortedException; 25 | import com.rivescript.exception.ReplyNotFoundException; 26 | import com.rivescript.exception.ReplyNotMatchedException; 27 | import org.junit.Test; 28 | 29 | /** 30 | * @author Noah Petherbridge 31 | * @author Marcel Overdijk 32 | */ 33 | public class ExceptionsIT extends BaseIT { 34 | 35 | @Test 36 | public void testThrowExceptions() { 37 | rs = new RiveScript(Config.Builder.basic().throwExceptions(true).build()); 38 | assertReplyThrows("hello bot", RepliesNotSortedException.class); 39 | 40 | setUp(new String[] { 41 | "+ hello bot", 42 | "- Hello human.", 43 | "", 44 | "// Infinite recursion between these two.", 45 | "+ one", 46 | "@ two", 47 | "+ two", 48 | "@ one", 49 | "", 50 | "// Empty reply.", 51 | "+ test not found", 52 | "- " 53 | }); 54 | assertReply("hello bot", "Hello human."); 55 | assertReplyThrows("one", DeepRecursionException.class); 56 | assertReplyThrows("test not matched", ReplyNotMatchedException.class); 57 | assertReplyThrows("test not found", ReplyNotFoundException.class); 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /rivescript-core/src/integration-test/java/com/rivescript/GroovyObjectIT.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016-2017 the original author or authors. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in all 12 | * copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | * SOFTWARE. 21 | */ 22 | 23 | package com.rivescript; 24 | 25 | import com.rivescript.lang.groovy.GroovyHandler; 26 | import org.junit.Before; 27 | import org.junit.Test; 28 | 29 | import static com.rivescript.RiveScript.DEFAULT_OBJECT_NOT_FOUND_MESSAGE; 30 | 31 | /** 32 | * Integration tests for {@link GroovyHandler}. 33 | * 34 | * @author Marcel Overdijk 35 | */ 36 | public class GroovyObjectIT extends BaseIT { 37 | 38 | @Before 39 | public void setUp() { 40 | rs = new RiveScript(); 41 | rs.setHandler("groovy", new GroovyHandler()); 42 | setUp(new String[] { 43 | "> object reverse groovy", 44 | " def msg = args.join(' ')", 45 | " return msg.reverse()", 46 | "< object", 47 | "> object setname groovy", 48 | " def username = rs.currentUser()", 49 | " rs.setUservar(username, 'name', args[0])", 50 | "< object", 51 | "+ reverse *", 52 | "- reverse ", 53 | "+ my name is *", 54 | "- I will remember that.setname \"\"", 55 | "+ what is my name", 56 | "- You are ." 57 | }); 58 | } 59 | 60 | @Test 61 | public void testReverse() { 62 | assertReply("reverse hello world", "dlrow olleh"); 63 | } 64 | 65 | @Test 66 | public void testSetName() { 67 | assertReply("my name is john doe", "I will remember that."); 68 | assertReply("what is my name", "You are John Doe."); 69 | } 70 | 71 | @Test 72 | public void testNoGroovyHandler() { 73 | rs.removeHandler("groovy"); 74 | assertReply("reverse hello world", DEFAULT_OBJECT_NOT_FOUND_MESSAGE); 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /rivescript-core/src/integration-test/java/com/rivescript/JavaObjectIT.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016-2017 the original author or authors. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in all 12 | * copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | * SOFTWARE. 21 | */ 22 | 23 | package com.rivescript; 24 | 25 | import com.rivescript.macro.Subroutine; 26 | import com.rivescript.util.StringUtils; 27 | import org.junit.Before; 28 | import org.junit.Test; 29 | 30 | import static com.rivescript.RiveScript.DEFAULT_OBJECT_NOT_FOUND_MESSAGE; 31 | 32 | /** 33 | * Integration tests for Java {@link Subroutine}. 34 | * 35 | * @author Marcel Overdijk 36 | */ 37 | public class JavaObjectIT extends BaseIT { 38 | 39 | @Before 40 | public void setUp() { 41 | rs = new RiveScript(); 42 | rs.setSubroutine("reverse", new Subroutine() { 43 | 44 | @Override 45 | public String call(RiveScript rs, String[] args) { 46 | String msg = StringUtils.join(args, " "); 47 | return new StringBuilder(msg).reverse().toString(); 48 | } 49 | }); 50 | rs.setSubroutine("setname", new Subroutine() { 51 | 52 | @Override 53 | public String call(RiveScript rs, String[] args) { 54 | String username = rs.currentUser(); 55 | rs.setUservar(username, "name", args[0]); 56 | return null; 57 | } 58 | }); 59 | setUp(new String[] { 60 | "+ reverse *", 61 | "- reverse ", 62 | "+ my name is *", 63 | "- I will remember that.setname \"\"", 64 | "+ what is my name", 65 | "- You are ." 66 | }); 67 | } 68 | 69 | @Test 70 | public void testReverse() { 71 | assertReply("reverse hello world", "dlrow olleh"); 72 | } 73 | 74 | @Test 75 | public void testSetName() { 76 | assertReply("my name is john doe", "I will remember that."); 77 | assertReply("what is my name", "You are John Doe."); 78 | } 79 | 80 | @Test 81 | public void testRemoveSubroutine() { 82 | rs.removeSubroutine("reverse"); 83 | assertReply("reverse hello world", DEFAULT_OBJECT_NOT_FOUND_MESSAGE); 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /rivescript-core/src/integration-test/java/com/rivescript/JavaScriptObjectIT.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016-2017 the original author or authors. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in all 12 | * copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | * SOFTWARE. 21 | */ 22 | 23 | package com.rivescript; 24 | 25 | import com.rivescript.lang.javascript.JavaScriptHandler; 26 | import org.junit.Before; 27 | import org.junit.Test; 28 | 29 | import static com.rivescript.RiveScript.DEFAULT_OBJECT_NOT_FOUND_MESSAGE; 30 | 31 | /** 32 | * Integration tests for {@link JavaScriptHandler}. 33 | * 34 | * @author Marcel Overdijk 35 | */ 36 | public class JavaScriptObjectIT extends BaseIT { 37 | 38 | @Before 39 | public void setUp() { 40 | rs = new RiveScript(); 41 | rs.setHandler("javascript", new JavaScriptHandler()); 42 | setUp(new String[] { 43 | "> object reverse javascript", 44 | " var msg = args.join(' ');", 45 | " return msg.split('').reverse().join('');", 46 | "< object", 47 | "> object setname javascript", 48 | " var username = rs.currentUser();", 49 | " rs.setUservar(username, 'name', args[0]);", 50 | "< object", 51 | "+ reverse *", 52 | "- reverse ", 53 | "+ my name is *", 54 | "- I will remember that.setname \"\"", 55 | "+ what is my name", 56 | "- You are ." 57 | }); 58 | } 59 | 60 | @Test 61 | public void testReverse() { 62 | assertReply("reverse hello world", "dlrow olleh"); 63 | } 64 | 65 | @Test 66 | public void testSetName() { 67 | assertReply("my name is john doe", "I will remember that."); 68 | assertReply("what is my name", "You are John Doe."); 69 | } 70 | 71 | @Test 72 | public void testNoJavaScriptHandler() { 73 | rs.removeHandler("javascript"); 74 | assertReply("reverse hello world", DEFAULT_OBJECT_NOT_FOUND_MESSAGE); 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /rivescript-core/src/integration-test/java/com/rivescript/MathIT.java: -------------------------------------------------------------------------------- 1 | package com.rivescript;/* 2 | * Copyright (c) 2016 the original author or authors. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in all 12 | * copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | * SOFTWARE. 21 | */ 22 | 23 | import org.junit.Test; 24 | 25 | /** 26 | * @author Noah Petherbridge 27 | * @author Marcel Overdijk 28 | */ 29 | public class MathIT extends BaseIT { 30 | 31 | @Test 32 | public void testMath() { 33 | rs = new RiveScript(); 34 | setUp(new String[] { 35 | "+ test counter", 36 | "- counter set", 37 | "", 38 | "+ show", 39 | "- counter = ", 40 | "", 41 | "+ add", 42 | "- adding", 43 | "", 44 | "+ sub", 45 | "- subbing", 46 | "", 47 | "+ div", 48 | "- ", 49 | "^
", 50 | "^ divving", 51 | "", 52 | "+ mult", 53 | "- ", 54 | "^ ", 55 | "^ multing" 56 | 57 | }); 58 | assertReply("test counter", "counter set"); 59 | assertReply("show", "counter = 0"); 60 | 61 | assertReply("add", "adding"); 62 | assertReply("show", "counter = 1"); 63 | 64 | assertReply("sub", "subbing"); 65 | assertReply("show", "counter = 0"); 66 | 67 | assertReply("div", "divving"); 68 | assertReply("show", "counter = 5"); 69 | 70 | assertReply("mult", "multing"); 71 | assertReply("show", "counter = 20"); 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /rivescript-core/src/integration-test/java/com/rivescript/ObjectIT.java: -------------------------------------------------------------------------------- 1 | package com.rivescript;/* 2 | * Copyright (c) 2016 the original author or authors. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in all 12 | * copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | * SOFTWARE. 21 | */ 22 | 23 | import com.rivescript.macro.ObjectHandler; 24 | import com.rivescript.util.StringUtils; 25 | import org.junit.Test; 26 | 27 | import java.util.HashMap; 28 | import java.util.Map; 29 | 30 | import static com.rivescript.RiveScript.DEFAULT_OBJECT_NOT_FOUND_MESSAGE; 31 | 32 | /** 33 | * @author Noah Petherbridge 34 | * @author Marcel Overdijk 35 | */ 36 | public class ObjectIT extends BaseIT { 37 | 38 | @Test 39 | public void testMacroParsing() { 40 | rs = new RiveScript(); 41 | rs.setHandler("text", new MockHandler()); 42 | setUp(new String[] { 43 | "> object hello text", 44 | " Hello world!", 45 | "< object", 46 | "", 47 | "> object goodbye javascript", 48 | " return \"Goodbye\";", 49 | "< object", 50 | "", 51 | "+ hello", 52 | "- hello", 53 | "", 54 | "+ goodbye", 55 | "- goodbye" 56 | }); 57 | assertReply("Hello", "Hello world!"); 58 | assertReply("goodbye", DEFAULT_OBJECT_NOT_FOUND_MESSAGE); 59 | } 60 | 61 | private static class MockHandler implements ObjectHandler { 62 | 63 | private Map codes = new HashMap<>(); 64 | 65 | @Override 66 | public void load(RiveScript rs, String name, String[] code) { 67 | codes.put(name, StringUtils.join(code, "\n")); 68 | } 69 | 70 | @Override 71 | public String call(RiveScript rs, String name, String[] fields) { 72 | return codes.get(name); 73 | } 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /rivescript-core/src/integration-test/java/com/rivescript/OptionsIT.java: -------------------------------------------------------------------------------- 1 | package com.rivescript;/* 2 | * Copyright (c) 2016 the original author or authors. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in all 12 | * copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | * SOFTWARE. 21 | */ 22 | 23 | import org.junit.Test; 24 | 25 | /** 26 | * @author Noah Petherbridge 27 | * @author Marcel Overdijk 28 | */ 29 | public class OptionsIT extends BaseIT { 30 | 31 | @Test 32 | public void testBotVariables() { 33 | rs = new RiveScript(); 34 | setUp(new String[] { 35 | "// Default concat mode = none", 36 | "+ test concat default", 37 | "- Hello", 38 | "^ world!", 39 | "", 40 | "! local concat = space", 41 | "+ test concat space", 42 | "- Hello", 43 | "^ world!", 44 | "", 45 | "! local concat = none", 46 | "+ test concat none", 47 | "- Hello", 48 | "^ world!", 49 | "", 50 | "! local concat = newline", 51 | "+ test concat newline", 52 | "- Hello", 53 | "^ world!", 54 | "", 55 | "// invalid concat setting is equivalent to 'none'", 56 | "! local concat = foobar", 57 | "+ test concat foobar", 58 | "- Hello", 59 | "^ world!", 60 | "", 61 | "// the option is file scoped so it can be left at", 62 | "// any setting and won't affect subsequent parses", 63 | "! local concat = newline" 64 | }); 65 | setUp(new String[] { 66 | "// concat mode should be restored to the default in a", 67 | "// separate file/stream parse", 68 | "+ test concat second file", 69 | "- Hello", 70 | "^ world!" 71 | }); 72 | assertReply("test concat default", "Helloworld!"); 73 | assertReply("test concat space", "Hello world!"); 74 | assertReply("test concat none", "Helloworld!"); 75 | assertReply("test concat newline", "Hello\nworld!"); 76 | assertReply("test concat foobar", "Helloworld!"); 77 | assertReply("test concat second file", "Helloworld!"); 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /rivescript-core/src/integration-test/java/com/rivescript/RiveScriptIT.java: -------------------------------------------------------------------------------- 1 | package com.rivescript;/* 2 | * Copyright (c) 2016 the original author or authors. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in all 12 | * copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | * SOFTWARE. 21 | */ 22 | 23 | import org.junit.Test; 24 | 25 | import static com.rivescript.RiveScript.DEFAULT_REPLIES_NOT_SORTED_MESSAGE; 26 | import static org.hamcrest.Matchers.equalTo; 27 | import static org.hamcrest.Matchers.is; 28 | import static org.junit.Assert.assertThat; 29 | import static org.junit.Assert.fail; 30 | 31 | /** 32 | * @author Noah Petherbridge 33 | * @author Marcel Overdijk 34 | */ 35 | public class RiveScriptIT extends BaseIT { 36 | 37 | @Test 38 | public void testNoSorting() { 39 | rs = new RiveScript(); 40 | assertReply("hello bot", DEFAULT_REPLIES_NOT_SORTED_MESSAGE); 41 | } 42 | 43 | @Test 44 | public void testFailedLoading() { 45 | rs = new RiveScript(); 46 | try { 47 | rs.loadFile("/root/notexist345613123098"); 48 | fail("I tried to load an obviously missing file, but I succeeded unexpectedly"); 49 | } catch (RiveScriptException e) { 50 | assertThat(e.getMessage(), is(equalTo("File '/root/notexist345613123098' not found"))); 51 | } 52 | 53 | try { 54 | rs.loadDirectory("/root/notexist412901890281"); 55 | fail("I tried to load an obviously missing directory, but I succeeded unexpectedly"); 56 | } catch (RiveScriptException e) { 57 | assertThat(e.getMessage(), is(equalTo("Directory '/root/notexist412901890281' not found"))); 58 | } 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /rivescript-core/src/integration-test/java/com/rivescript/RubyObjectIT.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016-2017 the original author or authors. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in all 12 | * copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | * SOFTWARE. 21 | */ 22 | 23 | package com.rivescript; 24 | 25 | import com.rivescript.lang.ruby.RubyHandler; 26 | import org.junit.Before; 27 | import org.junit.Test; 28 | 29 | import static com.rivescript.RiveScript.DEFAULT_OBJECT_NOT_FOUND_MESSAGE; 30 | 31 | /** 32 | * Integration tests for {@link RubyHandler}. 33 | * 34 | * @author Marcel Overdijk 35 | */ 36 | public class RubyObjectIT extends BaseIT { 37 | 38 | @Before 39 | public void setUp() { 40 | rs = new RiveScript(); 41 | rs.setHandler("ruby", new RubyHandler()); 42 | setUp(new String[] { 43 | "> object reverse ruby", 44 | " msg = args.join(' ')", 45 | " return msg.reverse!", 46 | "< object", 47 | "> object setname ruby", 48 | " username = rs.currentUser()", 49 | " rs.setUservar(username, 'name', args[0])", 50 | "< object", 51 | "+ reverse *", 52 | "- reverse ", 53 | "+ my name is *", 54 | "- I will remember that.setname \"\"", 55 | "+ what is my name", 56 | "- You are ." 57 | }); 58 | } 59 | 60 | @Test 61 | public void testReverse() { 62 | assertReply("reverse hello world", "dlrow olleh"); 63 | } 64 | 65 | @Test 66 | public void testSetName() { 67 | assertReply("my name is john doe", "I will remember that."); 68 | assertReply("what is my name", "You are John Doe."); 69 | } 70 | 71 | @Test 72 | public void testNoRubyHandler() { 73 | rs.removeHandler("ruby"); 74 | assertReply("reverse hello world", DEFAULT_OBJECT_NOT_FOUND_MESSAGE); 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /rivescript-core/src/integration-test/java/com/rivescript/SessionsIT.java: -------------------------------------------------------------------------------- 1 | package com.rivescript;/* 2 | * Copyright (c) 2016 the original author or authors. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in all 12 | * copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | * SOFTWARE. 21 | */ 22 | 23 | import com.rivescript.session.NoOpSessionManager; 24 | import com.rivescript.session.ThawAction; 25 | import org.junit.Test; 26 | 27 | /** 28 | * @author Noah Petherbridge 29 | * @author Marcel Overdijk 30 | */ 31 | public class SessionsIT extends BaseIT { 32 | 33 | private String[] commonSessionTest = new String[] { 34 | "+ my name is *", 35 | "- >Nice to meet you, .", 36 | "+ who am i", 37 | "- Aren't you ?", 38 | "+ what did i just say", 39 | "- You just said: ", 40 | "+ what did you just say", 41 | "- I just said: ", 42 | "+ i hate you", 43 | "- How mean!{topic=apology}", 44 | "> topic apology", 45 | "+ *", 46 | "- Nope, I'm mad at you.", 47 | "< topic" 48 | }; 49 | 50 | @Test 51 | public void testNoOpSessionManager() { 52 | rs = new RiveScript(Config.Builder.basic().sessionManager(new NoOpSessionManager()).build()); 53 | setUp(commonSessionTest); 54 | assertReply("My name is Aiden", "Nice to meet you, undefined."); 55 | assertReply("Who am I?", "Aren't you undefined?"); 56 | assertReply("What did I just say?", "You just said: undefined"); 57 | assertReply("What did you just say?", "I just said: undefined"); 58 | assertReply("I hate you", "How mean!"); 59 | assertReply("My name is Aiden", "Nice to meet you, undefined."); 60 | } 61 | 62 | @Test 63 | public void testConcurrentHashMapSessionManager() { 64 | rs = new RiveScript(); 65 | setUp(commonSessionTest); 66 | assertReply("My name is Aiden", "Nice to meet you, Aiden."); 67 | assertReply("What did I just say?", "You just said: my name is aiden"); 68 | assertReply("Who am I?", "Aren't you Aiden?"); 69 | assertReply("What did you just say?", "I just said: Aren't you Aiden?"); 70 | assertReply("I hate you!", "How mean!"); 71 | assertReply("My name is Bob", "Nope, I'm mad at you."); 72 | } 73 | 74 | @Test 75 | public void testFreezeThaw() { 76 | rs = new RiveScript(); 77 | setUp(new String[] { 78 | "+ my name is *", 79 | "- >Nice to meet you, .", 80 | "", 81 | "+ who am i", 82 | "- Aren't you ?" 83 | }); 84 | assertReply("My name is Aiden", "Nice to meet you, Aiden."); 85 | assertReply("Who am I?", "Aren't you Aiden?"); 86 | 87 | rs.freezeUservars(username); 88 | assertReply("My name is Bob", "Nice to meet you, Bob."); 89 | assertReply("Who am I?", "Aren't you Bob?"); 90 | 91 | rs.thawUservars(username, ThawAction.THAW); 92 | assertReply("Who am I?", "Aren't you Aiden?"); 93 | rs.freezeUservars(username); 94 | 95 | assertReply("My name is Bob", "Nice to meet you, Bob."); 96 | assertReply("Who am I?", "Aren't you Bob?"); 97 | rs.thawUservars(username, ThawAction.DISCARD); 98 | assertReply("Who am I?", "Aren't you Bob?"); 99 | } 100 | } 101 | -------------------------------------------------------------------------------- /rivescript-core/src/integration-test/java/com/rivescript/SubstitutionIT.java: -------------------------------------------------------------------------------- 1 | package com.rivescript;/* 2 | * Copyright (c) 2016 the original author or authors. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in all 12 | * copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | * SOFTWARE. 21 | */ 22 | 23 | import org.junit.Test; 24 | 25 | /** 26 | * @author Noah Petherbridge 27 | * @author Marcel Overdijk 28 | */ 29 | public class SubstitutionIT extends BaseIT { 30 | 31 | @Test 32 | public void testSubstitutions() { 33 | rs = new RiveScript(); 34 | setUp(new String[] { 35 | "+ whats up", 36 | "- nm.", 37 | "", 38 | "+ what is up", 39 | "- Not much." 40 | }); 41 | assertReply("whats up", "nm."); 42 | assertReply("what's up?", "nm."); 43 | assertReply("what is up?", "Not much."); 44 | 45 | setUp(new String[] { 46 | "! sub whats = what is", 47 | "! sub what's = what is" 48 | }); 49 | assertReply("whats up", "Not much."); 50 | assertReply("what's up?", "Not much."); 51 | assertReply("What is up?", "Not much."); 52 | } 53 | 54 | @Test 55 | public void testPersonSubstitutions() { 56 | rs = new RiveScript(); 57 | setUp(new String[] { 58 | "+ say *", 59 | "- " 60 | }); 61 | assertReply("say I am cool", "i am cool"); 62 | assertReply("say You are dumb", "you are dumb"); 63 | 64 | setUp(new String[] { 65 | "! person i am = you are", 66 | "! person you are = I am" 67 | }); 68 | assertReply("say I am cool", "you are cool"); 69 | assertReply("say You are dumb", "I am dumb"); 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /rivescript-core/src/integration-test/java/com/rivescript/UnicodeIT.java: -------------------------------------------------------------------------------- 1 | package com.rivescript;/* 2 | * Copyright (c) 2016 the original author or authors. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in all 12 | * copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | * SOFTWARE. 21 | */ 22 | 23 | import org.junit.Test; 24 | 25 | import static com.rivescript.RiveScript.DEFAULT_REPLY_NOT_MATCHED_MESSAGE; 26 | 27 | /** 28 | * @author Noah Petherbridge 29 | * @author Marcel Overdijk 30 | */ 31 | public class UnicodeIT extends BaseIT { 32 | 33 | @Test 34 | public void testUnicode() { 35 | rs = new RiveScript(Config.utf8()); 36 | setUp(new String[] { 37 | "! sub who's = who is", 38 | "+ äh", 39 | "- What's the matter?", 40 | "", 41 | "+ ブラッキー", 42 | "- エーフィ", 43 | "", 44 | "// Make sure %Previous continues working in UTF-8 mode.", 45 | "+ knock knock", 46 | "- Who's there?", 47 | "", 48 | "+ *", 49 | "% who is there", 50 | "- who?", 51 | "", 52 | "+ *", 53 | "% * who", 54 | "- Haha! !", 55 | "", 56 | "// And with UTF-8.", 57 | "+ tëll më ä pöëm", 58 | "- Thërë öncë wäs ä män nämëd Tïm", 59 | "", 60 | "+ more", 61 | "% thërë öncë wäs ä män nämëd tïm", 62 | "- Whö nëvër qüïtë lëärnëd höw tö swïm", 63 | "", 64 | "+ more", 65 | "% whö nëvër qüïtë lëärnëd höw tö swïm", 66 | "- Hë fëll öff ä döck, änd sänk lïkë ä röck", 67 | "", 68 | "+ more", 69 | "% hë fëll öff ä döck änd sänk lïkë ä röck", 70 | "- Änd thät wäs thë ënd öf hïm." 71 | }); 72 | assertReply("äh", "What's the matter?"); 73 | assertReply("ブラッキー", "エーフィ"); 74 | assertReply("knock knock", "Who's there?"); 75 | assertReply("Orange", "Orange who?"); 76 | assertReply("banana", "Haha! Banana!"); 77 | assertReply("tëll më ä pöëm", "Thërë öncë wäs ä män nämëd Tïm"); 78 | assertReply("more", "Whö nëvër qüïtë lëärnëd höw tö swïm"); 79 | assertReply("more", "Hë fëll öff ä döck, änd sänk lïkë ä röck"); 80 | assertReply("more", "Änd thät wäs thë ënd öf hïm."); 81 | } 82 | 83 | @Test 84 | public void testPunctuation() { 85 | rs = new RiveScript(Config.utf8()); 86 | setUp(new String[] { 87 | "+ hello bot", 88 | "- Hello human!" 89 | }); 90 | assertReply("Hello bot", "Hello human!"); 91 | assertReply("Hello, bot!", "Hello human!"); 92 | assertReply("Hello: Bot", "Hello human!"); 93 | assertReply("Hello... bot?", "Hello human!"); 94 | } 95 | 96 | @Test 97 | public void testUnicodePunctuation() { 98 | rs = new RiveScript(Config.Builder.utf8().unicodePunctuation("xxx").build()); 99 | setUp(new String[] { 100 | "+ hello bot", 101 | "- Hello human!" 102 | }); 103 | assertReply("Hello bot", "Hello human!"); 104 | assertReply("Hello, bot!", DEFAULT_REPLY_NOT_MATCHED_MESSAGE); 105 | } 106 | } 107 | -------------------------------------------------------------------------------- /rivescript-core/src/integration-test/resources/simplelogger.properties: -------------------------------------------------------------------------------- 1 | org.slf4j.simpleLogger.defaultLogLevel=info 2 | org.slf4j.simpleLogger.showThreadName=false 3 | org.slf4j.simpleLogger.log.com.rivescript=debug 4 | -------------------------------------------------------------------------------- /rivescript-core/src/main/java/com/rivescript/ConcatMode.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016 the original author or authors. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in all 12 | * copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | * SOFTWARE. 21 | */ 22 | 23 | package com.rivescript; 24 | 25 | /** 26 | * Specifies the concat modes. 27 | * 28 | * @author Marcel Overdijk 29 | */ 30 | public enum ConcatMode { 31 | 32 | NONE(""), NEWLINE("\n"), SPACE(" "); 33 | 34 | private String concatChar; 35 | 36 | ConcatMode(String concatChar) { 37 | this.concatChar = concatChar; 38 | } 39 | 40 | /** 41 | * Resolves the {@link ConcatMode} by the given name (ignoring case). 42 | * 43 | * @param name the name 44 | * @return the concat mode or {@code null} if not found 45 | */ 46 | public static ConcatMode fromName(String name) { 47 | for (ConcatMode concatMode : values()) { 48 | if (concatMode.name().equalsIgnoreCase(name)) { 49 | return concatMode; 50 | } 51 | } 52 | return null; 53 | } 54 | 55 | /** 56 | * Returns the concat character. 57 | * 58 | * @return the concat character 59 | */ 60 | public String getConcatChar() { 61 | return concatChar; 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /rivescript-core/src/main/java/com/rivescript/RiveScriptException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016-2017 the original author or authors. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in all 12 | * copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | * SOFTWARE. 21 | */ 22 | 23 | package com.rivescript; 24 | 25 | /** 26 | * Thrown to indicate a RiveScript error. 27 | * 28 | * @author Noah Petherbridge 29 | * @author Marcel Overdijk 30 | */ 31 | public class RiveScriptException extends RuntimeException { 32 | 33 | /** 34 | * Creates a new {@code RiveScriptException}. 35 | */ 36 | public RiveScriptException() { 37 | super(); 38 | } 39 | 40 | /** 41 | * Creates a new {@code RiveScriptException} with the given message. 42 | * 43 | * @param message the message 44 | */ 45 | public RiveScriptException(String message) { 46 | super(message); 47 | } 48 | 49 | /** 50 | * Creates a new {@code RiveScriptException} with the given message and cause. 51 | * 52 | * @param message the message 53 | * @param cause the cause 54 | */ 55 | public RiveScriptException(String message, Throwable cause) { 56 | super(message, cause); 57 | } 58 | 59 | /** 60 | * Creates a new {@code RiveScriptException} with the given cause. 61 | * 62 | * @param cause the cause 63 | */ 64 | public RiveScriptException(Throwable cause) { 65 | super(cause); 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /rivescript-core/src/main/java/com/rivescript/ast/ObjectMacro.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016-2017 the original author or authors. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in all 12 | * copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | * SOFTWARE. 21 | */ 22 | 23 | package com.rivescript.ast; 24 | 25 | import java.util.List; 26 | 27 | /** 28 | * Represents a RiveScript Object Macro. 29 | * 30 | * @author Noah Petherbridge 31 | * @author Marcel Overdijk 32 | */ 33 | public class ObjectMacro { 34 | 35 | private String name; 36 | private String language; 37 | private List code; 38 | 39 | /** 40 | * Returns the name of this object. 41 | * 42 | * @return the name 43 | */ 44 | public String getName() { 45 | return name; 46 | } 47 | 48 | /** 49 | * Sets the name of this object. 50 | * 51 | * @param name the name 52 | */ 53 | public void setName(String name) { 54 | this.name = name; 55 | } 56 | 57 | /** 58 | * Returns the programming language of this object. 59 | * 60 | * @return the programming language 61 | */ 62 | public String getLanguage() { 63 | return language; 64 | } 65 | 66 | /** 67 | * Sets the programming language of this object. 68 | * 69 | * @param language the programming language 70 | */ 71 | public void setLanguage(String language) { 72 | this.language = language; 73 | } 74 | 75 | /** 76 | * Returns the source code of this object. 77 | * 78 | * @return the source code 79 | */ 80 | public List getCode() { 81 | return code; 82 | } 83 | 84 | /** 85 | * Sets the source code of this object. 86 | * 87 | * @param code the source code 88 | */ 89 | public void setCode(List code) { 90 | this.code = code; 91 | } 92 | 93 | @Override 94 | public boolean equals(Object o) { 95 | if (this == o) { 96 | return true; 97 | } 98 | if (o == null || getClass() != o.getClass()) { 99 | return false; 100 | } 101 | ObjectMacro that = (ObjectMacro) o; 102 | if (name != null ? !name.equals(that.name) : that.name != null) { 103 | return false; 104 | } 105 | if (language != null ? !language.equals(that.language) : that.language != null) { 106 | return false; 107 | } 108 | return code != null ? code.equals(that.code) : that.code == null; 109 | } 110 | 111 | @Override 112 | public int hashCode() { 113 | int result = name != null ? name.hashCode() : 0; 114 | result = 31 * result + (language != null ? language.hashCode() : 0); 115 | result = 31 * result + (code != null ? code.hashCode() : 0); 116 | return result; 117 | } 118 | 119 | @Override 120 | public String toString() { 121 | return "ObjectMacro{" + 122 | "name='" + name + '\'' + 123 | ", language='" + language + '\'' + 124 | ", code=" + code + 125 | '}'; 126 | } 127 | } 128 | -------------------------------------------------------------------------------- /rivescript-core/src/main/java/com/rivescript/ast/Root.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016-2017 the original author or authors. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in all 12 | * copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | * SOFTWARE. 21 | */ 22 | 23 | package com.rivescript.ast; 24 | 25 | import java.util.ArrayList; 26 | import java.util.HashMap; 27 | import java.util.List; 28 | import java.util.Map; 29 | 30 | /** 31 | * Represents the root of the Abstract Syntax Tree (AST). 32 | * 33 | * @author Noah Petherbridge 34 | * @author Marcel Overdijk 35 | */ 36 | public class Root { 37 | 38 | private Begin begin; 39 | private Map topics; 40 | private List objects; 41 | 42 | /** 43 | * Creates a new Abstract Syntax Tree (AST) containing an empty "random" topic. 44 | */ 45 | public Root() { 46 | this.begin = new Begin(); 47 | this.topics = new HashMap<>(); 48 | this.objects = new ArrayList<>(); 49 | // Initialize the 'random' topic. 50 | addTopic("random"); 51 | } 52 | 53 | /** 54 | * Returns the "begin block" (configuration) data. 55 | * 56 | * @return the "begin block" (configuration) data 57 | */ 58 | public Begin getBegin() { 59 | return begin; 60 | } 61 | 62 | /** 63 | * Sets the "begin block" (configuration) data. 64 | * 65 | * @param begin the "begin block" (configuration) data 66 | */ 67 | public void setBegin(Begin begin) { 68 | this.begin = begin; 69 | } 70 | 71 | /** 72 | * Returns the topic with the given name. 73 | * 74 | * @param name the name of the topic 75 | * @return the topic or {@code null} if not found 76 | */ 77 | public Topic getTopic(String name) { 78 | return topics.get(name); 79 | } 80 | 81 | /** 82 | * Returns the topics. 83 | * 84 | * @return the topics 85 | */ 86 | public Map getTopics() { 87 | return topics; 88 | } 89 | 90 | /** 91 | * Sets the topics. 92 | * 93 | * @param topics the topics 94 | */ 95 | public void setTopics(Map topics) { 96 | this.topics = topics; 97 | } 98 | 99 | /** 100 | * Returns the objects. 101 | * 102 | * @return the objects 103 | */ 104 | public List getObjects() { 105 | return objects; 106 | } 107 | 108 | /** 109 | * Sets the objects. 110 | * 111 | * @param objects the objects 112 | */ 113 | public void setObjects(List objects) { 114 | this.objects = objects; 115 | } 116 | 117 | /** 118 | * Adds a new topic with the given name. 119 | * 120 | * @param name the name of the topic 121 | */ 122 | public void addTopic(String name) { 123 | topics.put(name, new Topic()); 124 | } 125 | 126 | /** 127 | * Adds the given object. 128 | * 129 | * @param object the object 130 | */ 131 | public void addObject(ObjectMacro object) { 132 | objects.add(object); 133 | } 134 | 135 | @Override 136 | public boolean equals(Object o) { 137 | if (this == o) { 138 | return true; 139 | } 140 | if (o == null || getClass() != o.getClass()) { 141 | return false; 142 | } 143 | Root that = (Root) o; 144 | if (begin != null ? !begin.equals(that.begin) : that.begin != null) { 145 | return false; 146 | } 147 | if (topics != null ? !topics.equals(that.topics) : that.topics != null) { 148 | return false; 149 | } 150 | return objects != null ? objects.equals(that.objects) : that.objects == null; 151 | } 152 | 153 | @Override 154 | public int hashCode() { 155 | int result = begin != null ? begin.hashCode() : 0; 156 | result = 31 * result + (topics != null ? topics.hashCode() : 0); 157 | result = 31 * result + (objects != null ? objects.hashCode() : 0); 158 | return result; 159 | } 160 | 161 | @Override 162 | public String toString() { 163 | return "Root{" + 164 | "begin=" + begin + 165 | ", topics=" + topics + 166 | ", objects=" + objects + 167 | '}'; 168 | } 169 | } 170 | -------------------------------------------------------------------------------- /rivescript-core/src/main/java/com/rivescript/ast/Topic.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016-2017 the original author or authors. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in all 12 | * copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | * SOFTWARE. 21 | */ 22 | 23 | package com.rivescript.ast; 24 | 25 | import java.util.ArrayList; 26 | import java.util.HashMap; 27 | import java.util.List; 28 | import java.util.Map; 29 | 30 | /** 31 | * Represents a RiveScript zzTopic. 32 | * 33 | * @author Noah Petherbridge 34 | * @author Marcel Overdijk 35 | */ 36 | public class Topic { 37 | 38 | private List triggers; 39 | private Map includes; 40 | private Map inherits; 41 | 42 | public Topic() { 43 | this.triggers = new ArrayList<>(); 44 | this.includes = new HashMap<>(); 45 | this.inherits = new HashMap<>(); 46 | } 47 | 48 | /** 49 | * Returns the triggers of this topic. 50 | * 51 | * @return the triggers 52 | */ 53 | public List getTriggers() { 54 | return triggers; 55 | } 56 | 57 | /** 58 | * Sets the triggers of this topic. 59 | * 60 | * @param triggers the triggers 61 | */ 62 | public void setTriggers(List triggers) { 63 | this.triggers = triggers; 64 | } 65 | 66 | /** 67 | * Returns the includes of this topic. 68 | * 69 | * @return the includes 70 | */ 71 | public Map getIncludes() { 72 | return includes; 73 | } 74 | 75 | /** 76 | * Sets the includes of this topic. 77 | * 78 | * @param includes the includes 79 | */ 80 | public void setIncludes(Map includes) { 81 | this.includes = includes; 82 | } 83 | 84 | /** 85 | * Returns the inherits of this topic. 86 | * 87 | * @return the inherits 88 | */ 89 | public Map getInherits() { 90 | return inherits; 91 | } 92 | 93 | /** 94 | * Sets the inherits of this topic. 95 | * 96 | * @param inherits the inherits 97 | */ 98 | public void setInherits(Map inherits) { 99 | this.inherits = inherits; 100 | } 101 | 102 | /** 103 | * Adds the given trigger to this topic. 104 | * 105 | * @param trigger the trigger 106 | */ 107 | public void addTrigger(Trigger trigger) { 108 | triggers.add(trigger); 109 | } 110 | 111 | /** 112 | * Adds the given include to this topic. 113 | * 114 | * @param name the include 115 | * @param value the value 116 | */ 117 | public void addInclude(String name, boolean value) { 118 | includes.put(name, value); 119 | } 120 | 121 | /** 122 | * Adds the given inherit to this topic. 123 | * 124 | * @param name the inherit 125 | * @param value the value 126 | */ 127 | public void addInherit(String name, boolean value) { 128 | inherits.put(name, value); 129 | } 130 | 131 | @Override 132 | public boolean equals(Object o) { 133 | if (this == o) { 134 | return true; 135 | } 136 | if (o == null || getClass() != o.getClass()) { 137 | return false; 138 | } 139 | Topic that = (Topic) o; 140 | if (triggers != null ? !triggers.equals(that.triggers) : that.triggers != null) { 141 | return false; 142 | } 143 | if (includes != null ? !includes.equals(that.includes) : that.includes != null) { 144 | return false; 145 | } 146 | return inherits != null ? inherits.equals(that.inherits) : that.inherits == null; 147 | } 148 | 149 | @Override 150 | public int hashCode() { 151 | int result = triggers != null ? triggers.hashCode() : 0; 152 | result = 31 * result + (includes != null ? includes.hashCode() : 0); 153 | result = 31 * result + (inherits != null ? inherits.hashCode() : 0); 154 | return result; 155 | } 156 | 157 | @Override 158 | public String toString() { 159 | return "Topic{" + 160 | "triggers=" + triggers + 161 | ", includes=" + includes + 162 | ", inherits=" + inherits + 163 | '}'; 164 | } 165 | } 166 | -------------------------------------------------------------------------------- /rivescript-core/src/main/java/com/rivescript/ast/Trigger.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016-2017 the original author or authors. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in all 12 | * copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | * SOFTWARE. 21 | */ 22 | 23 | package com.rivescript.ast; 24 | 25 | import java.util.ArrayList; 26 | import java.util.List; 27 | 28 | /** 29 | * Represents a RiveScript ZzTrigger. 30 | * 31 | * @author Noah Petherbridge 32 | * @author Marcel Overdijk 33 | */ 34 | public class Trigger { 35 | 36 | private String trigger; 37 | private List reply; 38 | private List condition; 39 | private String redirect; 40 | private String previous; 41 | 42 | public Trigger() { 43 | this.reply = new ArrayList<>(); 44 | this.condition = new ArrayList<>(); 45 | } 46 | 47 | /** 48 | * Returns the trigger pattern of this trigger. 49 | * 50 | * @return the trigger pattern 51 | */ 52 | public String getTrigger() { 53 | return trigger; 54 | } 55 | 56 | /** 57 | * Sets the trigger pattern of this trigger. 58 | * 59 | * @param trigger the trigger pattern 60 | */ 61 | public void setTrigger(String trigger) { 62 | this.trigger = trigger; 63 | } 64 | 65 | /** 66 | * Returns the replies of this trigger. 67 | * 68 | * @return the replies 69 | */ 70 | public List getReply() { 71 | return reply; 72 | } 73 | 74 | /** 75 | * Sets the replies of this trigger. 76 | * 77 | * @param reply the replies 78 | */ 79 | public void setReply(List reply) { 80 | this.reply = reply; 81 | } 82 | 83 | /** 84 | * Returns the conditions of this trigger. 85 | * 86 | * @return the conditions 87 | */ 88 | public List getCondition() { 89 | return condition; 90 | } 91 | 92 | /** 93 | * Sets the conditions of this trigger. 94 | * 95 | * @param condition the conditions 96 | */ 97 | public void setCondition(List condition) { 98 | this.condition = condition; 99 | } 100 | 101 | /** 102 | * Returns the redirect of this trigger. 103 | * 104 | * @return the redirect 105 | */ 106 | public String getRedirect() { 107 | return redirect; 108 | } 109 | 110 | /** 111 | * Sets the redirect of this trigger. 112 | * 113 | * @param redirect the redirect 114 | */ 115 | public void setRedirect(String redirect) { 116 | this.redirect = redirect; 117 | } 118 | 119 | /** 120 | * Returns the previous of this trigger. 121 | * 122 | * @return the previous 123 | */ 124 | public String getPrevious() { 125 | return previous; 126 | } 127 | 128 | /** 129 | * Sets the previous of this trigger. 130 | * 131 | * @param previous the previous 132 | */ 133 | public void setPrevious(String previous) { 134 | this.previous = previous; 135 | } 136 | 137 | /** 138 | * Adds the given reply to this trigger. 139 | * 140 | * @param reply the reply 141 | */ 142 | public void addReply(String reply) { 143 | this.reply.add(reply); 144 | } 145 | 146 | /** 147 | * Adds the given condition to this trigger. 148 | * 149 | * @param condition the condition 150 | */ 151 | public void addCondition(String condition) { 152 | this.condition.add(condition); 153 | } 154 | 155 | @Override 156 | public boolean equals(Object o) { 157 | if (this == o) { 158 | return true; 159 | } 160 | if (o == null || getClass() != o.getClass()) { 161 | return false; 162 | } 163 | Trigger that = (Trigger) o; 164 | if (trigger != null ? !trigger.equals(that.trigger) : that.trigger != null) { 165 | return false; 166 | } 167 | if (reply != null ? !reply.equals(that.reply) : that.reply != null) { 168 | return false; 169 | } 170 | if (condition != null ? !condition.equals(that.condition) : that.condition != null) { 171 | return false; 172 | } 173 | if (redirect != null ? !redirect.equals(that.redirect) : that.redirect != null) { 174 | return false; 175 | } 176 | return previous != null ? previous.equals(that.previous) : that.previous == null; 177 | } 178 | 179 | @Override 180 | public int hashCode() { 181 | int result = trigger != null ? trigger.hashCode() : 0; 182 | result = 31 * result + (reply != null ? reply.hashCode() : 0); 183 | result = 31 * result + (condition != null ? condition.hashCode() : 0); 184 | result = 31 * result + (redirect != null ? redirect.hashCode() : 0); 185 | result = 31 * result + (previous != null ? previous.hashCode() : 0); 186 | return result; 187 | } 188 | 189 | @Override 190 | public String toString() { 191 | return "Trigger{" + 192 | "trigger='" + trigger + '\'' + 193 | ", reply=" + reply + 194 | ", condition=" + condition + 195 | ", redirect='" + redirect + '\'' + 196 | ", previous='" + previous + '\'' + 197 | '}'; 198 | } 199 | } 200 | -------------------------------------------------------------------------------- /rivescript-core/src/main/java/com/rivescript/ast/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016-2017 the original author or authors. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in all 12 | * copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | * SOFTWARE. 21 | */ 22 | 23 | /** 24 | * Provides Abstract Syntax Tree (AST) classes. 25 | */ 26 | package com.rivescript.ast; 27 | -------------------------------------------------------------------------------- /rivescript-core/src/main/java/com/rivescript/cmd/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016-2017 the original author or authors. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in all 12 | * copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | * SOFTWARE. 21 | */ 22 | 23 | /** 24 | * Provides command classes. 25 | */ 26 | package com.rivescript.cmd; 27 | -------------------------------------------------------------------------------- /rivescript-core/src/main/java/com/rivescript/exception/DeepRecursionException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016-2017 the original author or authors. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in all 12 | * copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | * SOFTWARE. 21 | */ 22 | 23 | package com.rivescript.exception; 24 | 25 | import com.rivescript.RiveScriptException; 26 | 27 | /** 28 | * Thrown to indicate a deep recursion error. 29 | * 30 | * @author Noah Petherbridge 31 | * @author Marcel Overdijk 32 | */ 33 | public class DeepRecursionException extends RiveScriptException { 34 | 35 | /** 36 | * Creates a new {@code DeepRecursionException}. 37 | */ 38 | public DeepRecursionException() { 39 | super(); 40 | } 41 | 42 | /** 43 | * Creates a new {@code DeepRecursionException} with the given message. 44 | * 45 | * @param message the message 46 | */ 47 | public DeepRecursionException(String message) { 48 | super(message); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /rivescript-core/src/main/java/com/rivescript/exception/NoDefaultTopicException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016-2017 the original author or authors. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in all 12 | * copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | * SOFTWARE. 21 | */ 22 | 23 | package com.rivescript.exception; 24 | 25 | import com.rivescript.RiveScriptException; 26 | 27 | /** 28 | * Thrown to indicate no default topic exists. 29 | * 30 | * @author Noah Petherbridge 31 | * @author Marcel Overdijk 32 | */ 33 | public class NoDefaultTopicException extends RiveScriptException { 34 | 35 | /** 36 | * Creates a new {@code NoDefaultTopicException}. 37 | */ 38 | public NoDefaultTopicException() { 39 | super(); 40 | } 41 | 42 | /** 43 | * Creates a new {@code NoDefaultTopicException} with the given message. 44 | * 45 | * @param message the message 46 | */ 47 | public NoDefaultTopicException(String message) { 48 | super(message); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /rivescript-core/src/main/java/com/rivescript/exception/RepliesNotSortedException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016-2017 the original author or authors. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in all 12 | * copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | * SOFTWARE. 21 | */ 22 | 23 | package com.rivescript.exception; 24 | 25 | import com.rivescript.RiveScriptException; 26 | 27 | /** 28 | * Thrown to indicate the replies are not sorted. 29 | * 30 | * @author Noah Petherbridge 31 | * @author Marcel Overdijk 32 | */ 33 | public class RepliesNotSortedException extends RiveScriptException { 34 | 35 | /** 36 | * Creates a new {@code RepliesNotSortedException}. 37 | */ 38 | public RepliesNotSortedException() { 39 | super(); 40 | } 41 | 42 | /** 43 | * Creates a new {@code RepliesNotSortedException} with the given message. 44 | * 45 | * @param message the message 46 | */ 47 | public RepliesNotSortedException(String message) { 48 | super(message); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /rivescript-core/src/main/java/com/rivescript/exception/ReplyNotFoundException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016-2017 the original author or authors. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in all 12 | * copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | * SOFTWARE. 21 | */ 22 | 23 | package com.rivescript.exception; 24 | 25 | import com.rivescript.RiveScriptException; 26 | 27 | /** 28 | * Thrown to indicate no reply was found. 29 | * 30 | * @author Noah Petherbridge 31 | * @author Marcel Overdijk 32 | */ 33 | public class ReplyNotFoundException extends RiveScriptException { 34 | 35 | /** 36 | * Creates a new {@code ReplyNotFoundException}. 37 | */ 38 | public ReplyNotFoundException() { 39 | super(); 40 | } 41 | 42 | /** 43 | * Creates a new {@code ReplyNotFoundException} with the given message. 44 | * 45 | * @param message the message 46 | */ 47 | public ReplyNotFoundException(String message) { 48 | super(message); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /rivescript-core/src/main/java/com/rivescript/exception/ReplyNotMatchedException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016-2017 the original author or authors. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in all 12 | * copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | * SOFTWARE. 21 | */ 22 | 23 | package com.rivescript.exception; 24 | 25 | import com.rivescript.RiveScriptException; 26 | 27 | /** 28 | * Thrown to indicate no reply matched. 29 | * 30 | * @author Noah Petherbridge 31 | * @author Marcel Overdijk 32 | */ 33 | public class ReplyNotMatchedException extends RiveScriptException { 34 | 35 | /** 36 | * Creates a new {@code ReplyNotMatchedException}. 37 | */ 38 | public ReplyNotMatchedException() { 39 | super(); 40 | } 41 | 42 | /** 43 | * Creates a new {@code ReplyNotMatchedException} with the given message. 44 | * 45 | * @param message the message 46 | */ 47 | public ReplyNotMatchedException(String message) { 48 | super(message); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /rivescript-core/src/main/java/com/rivescript/exception/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016-2017 the original author or authors. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in all 12 | * copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | * SOFTWARE. 21 | */ 22 | 23 | /** 24 | * Provides exception classes. 25 | */ 26 | package com.rivescript.exception; 27 | -------------------------------------------------------------------------------- /rivescript-core/src/main/java/com/rivescript/lang/Perl.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016 the original author or authors. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in all 12 | * copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | * SOFTWARE. 21 | */ 22 | 23 | package com.rivescript.lang; 24 | 25 | import com.rivescript.RiveScript; 26 | import com.rivescript.macro.ObjectHandler; 27 | import com.rivescript.util.StringUtils; 28 | import org.json.JSONException; 29 | import org.json.JSONObject; 30 | import org.slf4j.Logger; 31 | import org.slf4j.LoggerFactory; 32 | 33 | import java.io.BufferedReader; 34 | import java.io.IOException; 35 | import java.io.InputStreamReader; 36 | import java.io.OutputStream; 37 | import java.util.HashMap; 38 | import java.util.Iterator; 39 | import java.util.Map; 40 | import java.util.Vector; 41 | 42 | import static java.util.Objects.requireNonNull; 43 | 44 | /** 45 | * Perl programming language support for RiveScript-Java. 46 | * 47 | * @author Noah Petherbridge 48 | * @see ObjectHandler 49 | */ 50 | public class Perl implements ObjectHandler { 51 | 52 | private static Logger logger = LoggerFactory.getLogger(Perl.class); 53 | 54 | private String rsp4j; // Path to the Perl script 55 | private HashMap codes; // Object codes 56 | 57 | /** 58 | * Creates a Perl {@link ObjectHandler}. Must take the path to the rsp4j script as its argument. 59 | * 60 | * @param rsp4j the path to the rsp4j script (either in .pl or .exe format), not null. 61 | */ 62 | public Perl(String rsp4j) { 63 | this.rsp4j = requireNonNull(rsp4j, "'rsp4j' must not be null"); 64 | this.codes = new HashMap<>(); 65 | } 66 | 67 | /** 68 | * {@inheritDoc} 69 | */ 70 | @Override 71 | public void load(RiveScript rs, String name, String[] code) { 72 | codes.put(name, StringUtils.join(code, "\n")); 73 | } 74 | 75 | /** 76 | * {@inheritDoc} 77 | */ 78 | @Override 79 | public String call(RiveScript rs, String name, String[] fields) { 80 | String user = rs.currentUser(); 81 | // Prepare JSON data to send. 82 | try { 83 | JSONObject json = new JSONObject(); 84 | 85 | // Set the flat scalars first. 86 | json.put("id", user); 87 | json.put("message", StringUtils.join(fields, " ")); 88 | json.put("code", codes.get(name)); 89 | 90 | // Transcode the user's data into a JSON object. 91 | JSONObject vars = new JSONObject(); 92 | Map data = rs.getUservars(user).getVariables(); 93 | Iterator it = data.keySet().iterator(); 94 | while (it.hasNext()) { 95 | String key = it.next().toString(); 96 | vars.put(key, data.get(key)); 97 | } 98 | 99 | // Add it to the JSON. 100 | json.put("vars", vars); 101 | 102 | // Stringify. 103 | String outgoing = json.toString(); // Query 104 | String incoming; // Response 105 | 106 | // Run the Perl RiveScript handler. 107 | try { 108 | Process p = Runtime.getRuntime().exec(this.rsp4j + " --java"); 109 | OutputStream stdIn = p.getOutputStream(); 110 | BufferedReader stdOut = new BufferedReader(new InputStreamReader(p.getInputStream())); 111 | 112 | // Send it the JSON-in. 113 | stdIn.write(outgoing.getBytes()); 114 | stdIn.flush(); 115 | stdIn.close(); 116 | 117 | // Read the results back. 118 | Vector result = new Vector<>(); 119 | String line; 120 | while ((line = stdOut.readLine()) != null) { 121 | result.add(line); 122 | } 123 | incoming = StringUtils.join(result.toArray(new String[0]), "\n"); 124 | } catch (IOException e) { 125 | logger.error("IOException error in " + this.getClass().getCanonicalName() + ": " + e.getMessage()); 126 | return "[ERR: IOException: " + e.getMessage() + "]"; 127 | } 128 | 129 | // Process the response. 130 | JSONObject reply = new JSONObject(incoming); 131 | 132 | // OK, or error? 133 | if (reply.getString("status").equals("error")) { 134 | return "[ERR: " + reply.getString("message"); 135 | } 136 | 137 | // Send back any new user vars. 138 | JSONObject newVars = reply.getJSONObject("vars"); 139 | String[] keys = reply.getNames(newVars); 140 | for (int i = 0; i < keys.length; i++) { 141 | String value = newVars.getString(keys[i]); 142 | rs.setUservar(user, keys[i], value); 143 | } 144 | 145 | // OK. Get the reply. 146 | return reply.getString("reply"); 147 | 148 | } catch (JSONException e) { 149 | logger.error("JSONException in " + this.getClass().getCanonicalName() + ": " + e.getMessage()); 150 | return "[ERR: JSONException: " + e.getMessage() + "]"; 151 | } 152 | } 153 | } 154 | -------------------------------------------------------------------------------- /rivescript-core/src/main/java/com/rivescript/lang/groovy/GroovyHandler.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016 the original author or authors. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in all 12 | * copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | * SOFTWARE. 21 | */ 22 | 23 | package com.rivescript.lang.groovy; 24 | 25 | import com.rivescript.lang.jsr223.Jsr223ScriptingHandler; 26 | import com.rivescript.macro.ObjectHandler; 27 | 28 | /** 29 | * Provides Groovy programming language support for object macros in RiveScript. 30 | *

31 | * Example: 32 | *

33 | *

34 |  * 
35 |  * import com.rivescript.Config;
36 |  * import com.rivescript.RiveScript;
37 |  * import com.rivescript.lang.groovy.GroovyHandler;
38 |  *
39 |  * RiveScript bot = new RiveScript();
40 |  * bot.setHandler("groovy", new GroovyHandler(rs));
41 |  *
42 |  * // and go on as normal
43 |  * 
44 |  * 
45 | *

46 | * And in your RiveScript code, you can load and run Groovy objects: 47 | *

48 | *

49 |  * 
50 |  * > object reverse groovy
51 |  *     def msg = args.join(' ')
52 |  *     return msg.reverse()
53 |  * < object
54 |  *
55 |  * > object setname groovy
56 |  *     def username = rs.currentUser()
57 |  *     rs.setUservar(username, 'name', args[0])
58 |  * < object
59 |  *
60 |  * + reverse *
61 |  * - <call>reverse <star></call>
62 |  *
63 |  * + my name is *
64 |  * - I will remember that.<call>setname "<formal>"</call>
65 |  *
66 |  * + what is my name
67 |  * - You are <get name>
68 |  * 
69 |  * 
70 | * 71 | * @author Marcel Overdijk 72 | * @see ObjectHandler 73 | * @see Jsr223ScriptingHandler 74 | */ 75 | public class GroovyHandler extends Jsr223ScriptingHandler { 76 | 77 | /** 78 | * Constructs a Groovy {@link ObjectHandler}. 79 | */ 80 | public GroovyHandler() { 81 | super("groovy", "" 82 | + "def %s(rs, args) {\n" 83 | + " %s\n" 84 | + "}"); 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /rivescript-core/src/main/java/com/rivescript/lang/groovy/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016-2017 the original author or authors. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in all 12 | * copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | * SOFTWARE. 21 | */ 22 | 23 | /** 24 | * Provides Groovy programming language support classes. 25 | */ 26 | package com.rivescript.lang.groovy; 27 | -------------------------------------------------------------------------------- /rivescript-core/src/main/java/com/rivescript/lang/javascript/JavaScriptHandler.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016 the original author or authors. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in all 12 | * copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | * SOFTWARE. 21 | */ 22 | 23 | package com.rivescript.lang.javascript; 24 | 25 | import com.rivescript.lang.jsr223.Jsr223ScriptingHandler; 26 | import com.rivescript.macro.ObjectHandler; 27 | 28 | /** 29 | * Provides JavaScript programming language support for object macros in RiveScript. 30 | *

31 | * Example: 32 | *

33 | *

34 |  * 
35 |  * import com.rivescript.Config;
36 |  * import com.rivescript.RiveScript;
37 |  * import com.rivescript.lang.javascript.JavaScriptHandler;
38 |  *
39 |  * RiveScript bot = new RiveScript();
40 |  * bot.setHandler("javascript", new JavaScriptHandler(rs));
41 |  *
42 |  * // and go on as normal
43 |  * 
44 |  * 
45 | *

46 | * And in your RiveScript code, you can load and run JavaScript objects: 47 | *

48 | *

49 |  * 
50 |  * > object reverse javascript
51 |  *     var msg = args.join(' ');
52 |  *     return msg.split('').reverse().join('');
53 |  * < object
54 |  *
55 |  * > object setname javascript
56 |  *     var username = rs.currentUser();
57 |  *     rs.setUservar(username, 'name', args[0]);
58 |  * < object
59 |  *
60 |  * + reverse *
61 |  * - <call>reverse <star></call>
62 |  *
63 |  * + my name is *
64 |  * - I will remember that.<call>setname "<formal>"</call>
65 |  *
66 |  * + what is my name
67 |  * - You are <get name>
68 |  * 
69 |  * 
70 | * 71 | * @author Marcel Overdijk 72 | * @see ObjectHandler 73 | * @see Jsr223ScriptingHandler 74 | */ 75 | public class JavaScriptHandler extends Jsr223ScriptingHandler { 76 | 77 | /** 78 | * Constructs a JavaScript {@link ObjectHandler}. 79 | */ 80 | public JavaScriptHandler() { 81 | super("javascript", "" 82 | + "function %s(rs, args) {\n" 83 | + " args = Java.from(args);" 84 | + " %s\n" 85 | + "}"); 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /rivescript-core/src/main/java/com/rivescript/lang/javascript/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016-2017 the original author or authors. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in all 12 | * copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | * SOFTWARE. 21 | */ 22 | 23 | /** 24 | * Provides JavaScript programming language support classes. 25 | */ 26 | package com.rivescript.lang.javascript; 27 | -------------------------------------------------------------------------------- /rivescript-core/src/main/java/com/rivescript/lang/jsr223/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016-2017 the original author or authors. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in all 12 | * copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | * SOFTWARE. 21 | */ 22 | 23 | /** 24 | * Provides JSR-223 Java Scripting programming language support classes. 25 | */ 26 | package com.rivescript.lang.jsr223; 27 | -------------------------------------------------------------------------------- /rivescript-core/src/main/java/com/rivescript/lang/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016-2017 the original author or authors. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in all 12 | * copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | * SOFTWARE. 21 | */ 22 | 23 | /** 24 | * Provides programming language support classes. 25 | */ 26 | package com.rivescript.lang; 27 | -------------------------------------------------------------------------------- /rivescript-core/src/main/java/com/rivescript/lang/ruby/RubyHandler.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016 the original author or authors. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in all 12 | * copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | * SOFTWARE. 21 | */ 22 | 23 | package com.rivescript.lang.ruby; 24 | 25 | import com.rivescript.lang.jsr223.Jsr223ScriptingHandler; 26 | import com.rivescript.macro.ObjectHandler; 27 | 28 | /** 29 | * Provides Ruby programming language support for object macros in RiveScript. 30 | *

31 | * Example: 32 | *

33 | *

34 |  * 
35 |  * import com.rivescript.Config;
36 |  * import com.rivescript.RiveScript;
37 |  * import com.rivescript.lang.ruby.RubyHandler;
38 |  *
39 |  * RiveScript bot = new RiveScript();
40 |  * bot.setHandler("ruby", new RubyHandler(rs));
41 |  *
42 |  * // and go on as normal
43 |  * 
44 |  * 
45 | *

46 | * And in your RiveScript code, you can load and run Ruby objects: 47 | *

48 | *

49 |  * 
50 |  * > object reverse ruby
51 |  *     msg = args.join(' ')
52 |  *     return msg.reverse!
53 |  * < object
54 |  *
55 |  * > object setname ruby
56 |  *     username = rs.currentUser()
57 |  *     rs.setUservar(username, 'name', args[0])
58 |  * < object
59 |  *
60 |  * + reverse *
61 |  * - <call>reverse <star></call>
62 |  *
63 |  * + my name is *
64 |  * - I will remember that.<call>setname "<formal>"</call>
65 |  *
66 |  * + what is my name
67 |  * - You are <get name>
68 |  * 
69 |  * 
70 | * 71 | * @author Marcel Overdijk 72 | * @see ObjectHandler 73 | * @see Jsr223ScriptingHandler 74 | */ 75 | public class RubyHandler extends Jsr223ScriptingHandler { 76 | 77 | /** 78 | * Constructs a Ruby {@link ObjectHandler}. 79 | */ 80 | public RubyHandler() { 81 | super("ruby", "" 82 | + "def %s(rs, args)\n" 83 | + " args = args.to_a\n" 84 | + " %s\n" 85 | + "end"); 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /rivescript-core/src/main/java/com/rivescript/lang/ruby/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016-2017 the original author or authors. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in all 12 | * copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | * SOFTWARE. 21 | */ 22 | 23 | /** 24 | * Provides Ruby programming language support classes. 25 | */ 26 | package com.rivescript.lang.ruby; 27 | -------------------------------------------------------------------------------- /rivescript-core/src/main/java/com/rivescript/macro/ObjectHandler.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016 the original author or authors. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in all 12 | * copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | * SOFTWARE. 21 | */ 22 | 23 | package com.rivescript.macro; 24 | 25 | import com.rivescript.RiveScript; 26 | 27 | /** 28 | * Interface for RiveScript object handlers. 29 | * 30 | * @author Noah Petherbridge 31 | * @author Marcel Overdijk 32 | */ 33 | public interface ObjectHandler { 34 | 35 | /** 36 | * Handler for when object code is read (loaded) by RiveScript. 37 | * 38 | * @param rs the reference to the parent RiveScript instance 39 | * @param name the name of the object 40 | * @param code the source code inside the object 41 | */ 42 | void load(RiveScript rs, String name, String[] code); 43 | 44 | /** 45 | * Handler for when a user invokes the object. Should return the {@link String} result from the object. 46 | *

47 | * This code is executed when a {@code } tag in a RiveScript reply wants to call your object macro. 48 | * 49 | * @param rs the reference to the parent RiveScript instance 50 | * @param name the name of the object being called 51 | * @param fields the argument list from the call tag 52 | * @return the result 53 | */ 54 | String call(RiveScript rs, String name, String[] fields); 55 | } 56 | -------------------------------------------------------------------------------- /rivescript-core/src/main/java/com/rivescript/macro/Subroutine.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016 the original author or authors. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in all 12 | * copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | * SOFTWARE. 21 | */ 22 | 23 | package com.rivescript.macro; 24 | 25 | import com.rivescript.RiveScript; 26 | 27 | /** 28 | * Interface for RiveScript object macros written in Java. 29 | * 30 | * @author Noah Petherbridge 31 | * @author Marcel Overdijk 32 | */ 33 | public interface Subroutine { 34 | 35 | /** 36 | * The implemenatation of the Java object macro. Should return the {@link String} result from the object. 37 | * 38 | * @param rs the reference to the parent RiveScript instance 39 | * @param args the argument list from the call tag 40 | * @return the result 41 | */ 42 | String call(RiveScript rs, String[] args); 43 | } 44 | -------------------------------------------------------------------------------- /rivescript-core/src/main/java/com/rivescript/macro/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016-2017 the original author or authors. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in all 12 | * copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | * SOFTWARE. 21 | */ 22 | 23 | /** 24 | * Provides macro classes. 25 | */ 26 | package com.rivescript.macro; 27 | -------------------------------------------------------------------------------- /rivescript-core/src/main/java/com/rivescript/parser/ParserConfig.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016-2017 the original author or authors. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in all 12 | * copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | * SOFTWARE. 21 | */ 22 | 23 | package com.rivescript.parser; 24 | 25 | import com.rivescript.ConcatMode; 26 | 27 | /** 28 | * User-configurable properties of the RiveScript {@link Parser}. 29 | * 30 | * @author Noah Petherbridge 31 | * @author Marcel Overdijk 32 | */ 33 | public class ParserConfig { 34 | 35 | private boolean strict; 36 | private boolean utf8; 37 | private boolean forceCase; 38 | private ConcatMode concat; 39 | 40 | protected ParserConfig() { 41 | } 42 | 43 | /** 44 | * Returns whether strict syntax checking is enabled. 45 | * 46 | * @return whether strict syntax checking is enabled 47 | */ 48 | public boolean isStrict() { 49 | return strict; 50 | } 51 | 52 | /** 53 | * Returns whether UTF-8 mode is enabled for user messages and triggers. 54 | * 55 | * @return whether UTF-8 mode is enabled for user messages and triggers 56 | */ 57 | public boolean isUtf8() { 58 | return utf8; 59 | } 60 | 61 | /** 62 | * Returns whether forcing triggers to lowercase is enabled. 63 | * 64 | * @return whether forcing triggers to lowercase is enabled 65 | */ 66 | public boolean isForceCase() { 67 | return forceCase; 68 | } 69 | 70 | /** 71 | * Returns the concat mode. 72 | * 73 | * @return the concat mode 74 | */ 75 | public ConcatMode getConcat() { 76 | return concat; 77 | } 78 | 79 | @Override 80 | public boolean equals(Object o) { 81 | if (this == o) { 82 | return true; 83 | } 84 | if (o == null || getClass() != o.getClass()) { 85 | return false; 86 | } 87 | ParserConfig that = (ParserConfig) o; 88 | if (strict != that.strict) { 89 | return false; 90 | } 91 | if (utf8 != that.utf8) { 92 | return false; 93 | } 94 | if (forceCase != that.forceCase) { 95 | return false; 96 | } 97 | return concat == that.concat; 98 | } 99 | 100 | @Override 101 | public int hashCode() { 102 | int result = (strict ? 1 : 0); 103 | result = 31 * result + (utf8 ? 1 : 0); 104 | result = 31 * result + (forceCase ? 1 : 0); 105 | result = 31 * result + (concat != null ? concat.hashCode() : 0); 106 | return result; 107 | } 108 | 109 | @Override 110 | public String toString() { 111 | return "ParserConfig{" + 112 | "strict=" + strict + 113 | ", utf8=" + utf8 + 114 | ", forceCase=" + forceCase + 115 | ", concat=" + concat + 116 | '}'; 117 | } 118 | 119 | /** 120 | * Converts this {@link ParserConfig} instance to a {@link Builder}. 121 | * 122 | * @return the builder 123 | */ 124 | public Builder toBuilder() { 125 | return newBuilder() 126 | .strict(this.strict) 127 | .utf8(this.utf8) 128 | .forceCase(this.forceCase) 129 | .concat(this.concat); 130 | } 131 | 132 | /** 133 | * Creates a new {@link Builder}. 134 | * 135 | * @return the builder 136 | */ 137 | public static Builder newBuilder() { 138 | return new Builder(); 139 | } 140 | 141 | /** 142 | * Builder for {@link ParserConfig}. 143 | */ 144 | public static final class Builder { 145 | 146 | private boolean strict; 147 | private boolean utf8; 148 | private boolean forceCase; 149 | private ConcatMode concat; 150 | 151 | private Builder() { 152 | } 153 | 154 | /** 155 | * Sets whether strict syntax checking is enabled. 156 | * 157 | * @param strict whether strict syntax checking is enabled 158 | * @return this builder 159 | */ 160 | public Builder strict(boolean strict) { 161 | this.strict = strict; 162 | return this; 163 | } 164 | 165 | /** 166 | * Sets whether UTF-8 mode is enabled. 167 | * 168 | * @param utf8 whether UTF-8 is enabled 169 | * @return this builder 170 | */ 171 | public Builder utf8(boolean utf8) { 172 | this.utf8 = utf8; 173 | return this; 174 | } 175 | 176 | /** 177 | * Sets whether forcing triggers to lowercase is enabled. 178 | * 179 | * @param forceCase whether forcing triggers to lowercase is enabled 180 | * @return this builder 181 | */ 182 | public Builder forceCase(boolean forceCase) { 183 | this.forceCase = forceCase; 184 | return this; 185 | } 186 | 187 | /** 188 | * Sets the concat mode 189 | * 190 | * @param concat the concat mode 191 | * @return this builder 192 | */ 193 | public Builder concat(ConcatMode concat) { 194 | this.concat = concat; 195 | return this; 196 | } 197 | 198 | /** 199 | * Builds the parser config. 200 | * 201 | * @return the parser config 202 | */ 203 | public ParserConfig build() { 204 | ParserConfig config = new ParserConfig(); 205 | config.strict = this.strict; 206 | config.utf8 = this.utf8; 207 | config.forceCase = this.forceCase; 208 | config.concat = this.concat; 209 | return config; 210 | } 211 | } 212 | } 213 | -------------------------------------------------------------------------------- /rivescript-core/src/main/java/com/rivescript/parser/ParserException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016-2017 the original author or authors. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in all 12 | * copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | * SOFTWARE. 21 | */ 22 | 23 | package com.rivescript.parser; 24 | 25 | /** 26 | * Thrown to indicate an error during parsing. 27 | * 28 | * @author Noah Petherbridge 29 | * @author Marcel Overdijk 30 | */ 31 | public class ParserException extends RuntimeException { 32 | 33 | /** 34 | * Creates a new {@code ParserException}. 35 | */ 36 | public ParserException() { 37 | super(); 38 | } 39 | 40 | /** 41 | * Creates a new {@code ParserException} with the given message. 42 | * 43 | * @param message the message 44 | */ 45 | public ParserException(String message) { 46 | super(message); 47 | } 48 | 49 | /** 50 | * Creates a new {@code ParserException} with the given message and cause. 51 | * 52 | * @param message the message 53 | * @param cause the cause 54 | */ 55 | public ParserException(String message, Throwable cause) { 56 | super(message, cause); 57 | } 58 | 59 | /** 60 | * Creates a new {@code ParserException} with the given cause. 61 | * 62 | * @param cause the cause 63 | */ 64 | public ParserException(Throwable cause) { 65 | super(cause); 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /rivescript-core/src/main/java/com/rivescript/parser/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016-2017 the original author or authors. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in all 12 | * copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | * SOFTWARE. 21 | */ 22 | 23 | /** 24 | * Provides parser classes. 25 | */ 26 | package com.rivescript.parser; 27 | -------------------------------------------------------------------------------- /rivescript-core/src/main/java/com/rivescript/regexp/Regexp.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016 the original author or authors. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in all 12 | * copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | * SOFTWARE. 21 | */ 22 | 23 | package com.rivescript.regexp; 24 | 25 | import java.util.regex.Pattern; 26 | 27 | /** 28 | * Miscellaneous compiled {@link Pattern} constants. 29 | * 30 | * @author Noah Petherbridge 31 | * @author Marcel Overdijk 32 | */ 33 | public class Regexp { 34 | 35 | public static final Pattern RE_ANY_TAG = Pattern.compile("<([^<]+?)>"); 36 | public static final Pattern RE_ARRAY = Pattern.compile("@(.+?)\\b"); 37 | public static final Pattern RE_BOT_VAR = Pattern.compile(""); 38 | public static final Pattern RE_CALL = Pattern.compile("(.+?)"); 39 | public static final Pattern RE_CONDITION = Pattern.compile("^(.+?)\\s+(==|eq|!=|ne|<>|<|<=|>|>=)\\s+(.*?)$"); 40 | public static final Pattern RE_INHERITS = Pattern.compile("\\{inherits=(\\d+)\\}"); 41 | public static final Pattern RE_INPUT = Pattern.compile(""); 42 | public static final Pattern RE_META = Pattern.compile("[\\<>]+"); 43 | public static final Pattern RE_NASTIES = Pattern.compile("[^A-Za-z0-9_ ]"); 44 | public static final Pattern RE_OPTIONAL = Pattern.compile("\\[(.+?)\\]"); 45 | public static final Pattern RE_PLACEHOLDER = Pattern.compile("\\\\x00(\\d+)\\\\x00"); 46 | public static final Pattern RE_RANDOM = Pattern.compile("\\{random\\}(.+?)\\{/random\\}"); 47 | public static final Pattern RE_REDIRECT = Pattern.compile("\\{@([^\\}]*?)\\}"); 48 | public static final Pattern RE_REPLY = Pattern.compile(""); 49 | public static final Pattern RE_SET = Pattern.compile(""); 50 | public static final Pattern RE_SYMBOLS = Pattern.compile("[.?,!;:@#$%^&*()]+"); 51 | public static final Pattern RE_TOPIC = Pattern.compile("\\{topic=(.+?)\\}"); 52 | public static final Pattern RE_USER_VAR = Pattern.compile(""); 53 | public static final Pattern RE_WEIGHT = Pattern.compile("\\{weight=(\\d+)\\}"); 54 | public static final Pattern RE_ZERO_WITH_STAR = Pattern.compile("^\\*$"); 55 | } 56 | -------------------------------------------------------------------------------- /rivescript-core/src/main/java/com/rivescript/regexp/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016-2017 the original author or authors. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in all 12 | * copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | * SOFTWARE. 21 | */ 22 | 23 | /** 24 | * Provides regular expression classes. 25 | */ 26 | package com.rivescript.regexp; 27 | -------------------------------------------------------------------------------- /rivescript-core/src/main/java/com/rivescript/session/History.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016 the original author or authors. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in all 12 | * copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | * SOFTWARE. 21 | */ 22 | 23 | package com.rivescript.session; 24 | 25 | import java.io.Serializable; 26 | import java.util.ArrayList; 27 | import java.util.List; 28 | 29 | import static com.rivescript.RiveScript.UNDEFINED; 30 | import static com.rivescript.session.SessionManager.HISTORY_SIZE; 31 | 32 | /** 33 | * Container to keep track of recent input and reply history. 34 | * 35 | * @author Noah Petherbridge 36 | * @author Marcel Overdijk 37 | */ 38 | public class History implements Serializable { 39 | 40 | private static final long serialVersionUID = 3383496053211557826L; 41 | 42 | private List input; 43 | private List reply; 44 | 45 | public History() { 46 | this.input = new ArrayList<>(HISTORY_SIZE); 47 | this.reply = new ArrayList<>(HISTORY_SIZE); 48 | for (int i = 0; i < HISTORY_SIZE; i++) { 49 | this.input.add(UNDEFINED); 50 | this.reply.add(UNDEFINED); 51 | } 52 | } 53 | 54 | public List getInput() { 55 | return input; 56 | } 57 | 58 | public String getInput(int index) { 59 | return input.get(index); 60 | } 61 | 62 | public List getReply() { 63 | return reply; 64 | } 65 | 66 | public String getReply(int index) { 67 | return reply.get(index); 68 | } 69 | 70 | @Override 71 | public boolean equals(Object o) { 72 | if (this == o) { 73 | return true; 74 | } 75 | if (o == null || getClass() != o.getClass()) { 76 | return false; 77 | } 78 | History that = (History) o; 79 | if (input != null ? !input.equals(that.input) : that.input != null) { 80 | return false; 81 | } 82 | return reply != null ? reply.equals(that.reply) : that.reply == null; 83 | } 84 | 85 | @Override 86 | public int hashCode() { 87 | int result = input != null ? input.hashCode() : 0; 88 | result = 31 * result + (reply != null ? reply.hashCode() : 0); 89 | return result; 90 | } 91 | 92 | @Override 93 | public String toString() { 94 | return "History{" + 95 | "input=" + input + 96 | ", reply=" + reply + 97 | '}'; 98 | } 99 | } 100 | -------------------------------------------------------------------------------- /rivescript-core/src/main/java/com/rivescript/session/NoOpSessionManager.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016 the original author or authors. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in all 12 | * copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | * SOFTWARE. 21 | */ 22 | 23 | package com.rivescript.session; 24 | 25 | 26 | import java.util.Collections; 27 | import java.util.Map; 28 | 29 | /** 30 | * A no operation {@link SessionManager} suitable for disabling session storage. 31 | * 32 | * @author Noah Petherbridge 33 | * @author Marcel Overdijk 34 | */ 35 | public class NoOpSessionManager implements SessionManager { 36 | 37 | @Override 38 | public UserData init(String username) { 39 | return noOpSession(); 40 | } 41 | 42 | @Override 43 | public void set(String username, String name, String value) { 44 | } 45 | 46 | @Override 47 | public void set(String username, Map vars) { 48 | } 49 | 50 | @Override 51 | public void addHistory(String username, String input, String reply) { 52 | } 53 | 54 | @Override 55 | public void setLastMatch(String username, String trigger) { 56 | } 57 | 58 | @Override 59 | public String get(String username, String name) { 60 | return null; 61 | } 62 | 63 | @Override 64 | public UserData get(String username) { 65 | return null; 66 | } 67 | 68 | @Override 69 | public Map getAll() { 70 | return Collections.emptyMap(); 71 | } 72 | 73 | @Override 74 | public String getLastMatch(String username) { 75 | return null; 76 | } 77 | 78 | @Override 79 | public History getHistory(String username) { 80 | return new History(); 81 | } 82 | 83 | @Override 84 | public void clear(String username) { 85 | } 86 | 87 | @Override 88 | public void clearAll() { 89 | } 90 | 91 | @Override 92 | public void freeze(String username) { 93 | } 94 | 95 | @Override 96 | public void thaw(String username, ThawAction action) { 97 | } 98 | 99 | private UserData noOpSession() { 100 | UserData userData = new UserData(); 101 | userData.setLastMatch(""); 102 | return userData; 103 | } 104 | } 105 | -------------------------------------------------------------------------------- /rivescript-core/src/main/java/com/rivescript/session/SessionManager.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016 the original author or authors. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in all 12 | * copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | * SOFTWARE. 21 | */ 22 | 23 | package com.rivescript.session; 24 | 25 | import com.rivescript.RiveScript; 26 | 27 | import java.util.Map; 28 | 29 | /** 30 | * Interface for RiveScript user user variables. 31 | *

32 | * The session manager keeps track of getting and setting user variables, for example when the {@code } or {@code `} tags are used 33 | * in RiveScript or when API functions like {@link RiveScript#setUservar(String, String, String)} are called. 34 | *

35 | * By default RiveScript stores user sessions in memory and provides methods to export and import them (e.g. to persist them when the bot 36 | * shuts down so they can be reloaded). If you'd prefer a more 'active' session storage, for example one that puts user variables into a 37 | * database or cache, you can create your own session manager that implements this interface. 38 | * 39 | * @author Noah Petherbridge 40 | * @author Marcel Overdijk 41 | */ 42 | public interface SessionManager { 43 | 44 | /** 45 | * The number of entries stored in the history. 46 | */ 47 | int HISTORY_SIZE = 9; 48 | 49 | /** 50 | * Makes sure a username has a session (creates one if not). 51 | * 52 | * @param username the username 53 | * @return the user data 54 | */ 55 | UserData init(String username); 56 | 57 | /** 58 | * Sets a user's variable. 59 | * 60 | * @param username the username 61 | * @param name the variable name 62 | * @param value the variable value 63 | */ 64 | void set(String username, String name, String value); 65 | 66 | /** 67 | * Sets a user's variables. 68 | * 69 | * @param username the username 70 | * @param vars the user variables 71 | */ 72 | void set(String username, Map vars); 73 | 74 | /** 75 | * Adds input and reply to a user's history. 76 | * 77 | * @param username the username 78 | * @param input the input 79 | * @param reply the reply 80 | */ 81 | void addHistory(String username, String input, String reply); 82 | 83 | /** 84 | * Sets a user's the last matched trigger. 85 | * 86 | * @param username the username 87 | * @param trigger the trigger 88 | */ 89 | void setLastMatch(String username, String trigger); 90 | 91 | /** 92 | * Returns a user variable. 93 | * 94 | * @param username the username 95 | * @param name the variable name 96 | * @return the variable value 97 | */ 98 | String get(String username, String name); 99 | 100 | /** 101 | * Returns all variables for a user. 102 | * 103 | * @param username the username 104 | * @return the user data 105 | */ 106 | UserData get(String username); 107 | 108 | /** 109 | * Returns all variables about all users. 110 | * 111 | * @return the users and their user data 112 | */ 113 | Map getAll(); 114 | 115 | /** 116 | * Returns a user's last matched trigger. 117 | * 118 | * @param username the username 119 | * @return the last matched trigger 120 | */ 121 | String getLastMatch(String username); 122 | 123 | /** 124 | * Returns a user's history. 125 | * 126 | * @param username the username 127 | * @return the history 128 | */ 129 | History getHistory(String username); 130 | 131 | /** 132 | * Clears a user's variables. 133 | * 134 | * @param username the username 135 | */ 136 | void clear(String username); 137 | 138 | /** 139 | * Clear all variables of all users. 140 | */ 141 | void clearAll(); 142 | 143 | /** 144 | * Makes a snapshot of a user's variables. 145 | * 146 | * @param username the username 147 | */ 148 | void freeze(String username); 149 | 150 | /** 151 | * Unfreezes a user's variables. 152 | * 153 | * @param username the username 154 | * @param action the thaw action 155 | * @see ThawAction 156 | */ 157 | void thaw(String username, ThawAction action); 158 | } 159 | -------------------------------------------------------------------------------- /rivescript-core/src/main/java/com/rivescript/session/ThawAction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016 the original author or authors. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in all 12 | * copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | * SOFTWARE. 21 | */ 22 | 23 | package com.rivescript.session; 24 | 25 | /** 26 | * Specifies options for thawing the user's variables. 27 | *

28 | *

    29 | *
  • {@code THAW}: Restore the variables and delete the frozen copy. 30 | *
  • {@code DISCARD}: Don't restore the variables, just delete the frozen copy. 31 | *
  • {@code KEEP}: Keep the frozen copy after restoring. 32 | *
33 | * 34 | * @author Noah Petherbridge 35 | * @author Marcel Overdijk 36 | */ 37 | public enum ThawAction { 38 | 39 | THAW, DISCARD, KEEP 40 | } 41 | -------------------------------------------------------------------------------- /rivescript-core/src/main/java/com/rivescript/session/UserData.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016 the original author or authors. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in all 12 | * copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | * SOFTWARE. 21 | */ 22 | 23 | package com.rivescript.session; 24 | 25 | import java.io.Serializable; 26 | import java.util.HashMap; 27 | import java.util.Map; 28 | 29 | /** 30 | * Container for user variables. 31 | * 32 | * @author Noah Petherbridge 33 | * @author Marcel Overdijk 34 | */ 35 | public class UserData implements Serializable { 36 | 37 | private static final long serialVersionUID = -4112596798916704937L; 38 | 39 | private Map variables; 40 | private String lastMatch; 41 | private History history; 42 | 43 | public UserData() { 44 | this.variables = new HashMap<>(); 45 | this.history = new History(); 46 | } 47 | 48 | public String getVariable(String name) { 49 | return variables.get(name); 50 | } 51 | 52 | public Map getVariables() { 53 | return variables; 54 | } 55 | 56 | public void setVariable(String name, String value) { 57 | variables.put(name, value); 58 | } 59 | 60 | public String getLastMatch() { 61 | return lastMatch; 62 | } 63 | 64 | public void setLastMatch(String lastMatch) { 65 | this.lastMatch = lastMatch; 66 | } 67 | 68 | public History getHistory() { 69 | return history; 70 | } 71 | 72 | @Override 73 | public boolean equals(Object o) { 74 | if (this == o) { 75 | return true; 76 | } 77 | if (o == null || getClass() != o.getClass()) { 78 | return false; 79 | } 80 | UserData that = (UserData) o; 81 | if (variables != null ? !variables.equals(that.variables) : that.variables != null) { 82 | return false; 83 | } 84 | if (lastMatch != null ? !lastMatch.equals(that.lastMatch) : that.lastMatch != null) { 85 | return false; 86 | } 87 | return history != null ? history.equals(that.history) : that.history == null; 88 | } 89 | 90 | @Override 91 | public int hashCode() { 92 | int result = variables != null ? variables.hashCode() : 0; 93 | result = 31 * result + (lastMatch != null ? lastMatch.hashCode() : 0); 94 | result = 31 * result + (history != null ? history.hashCode() : 0); 95 | return result; 96 | } 97 | 98 | @Override 99 | public String toString() { 100 | return "UserData{" + 101 | "variables=" + variables + 102 | ", lastMatch='" + lastMatch + '\'' + 103 | ", history=" + history + 104 | '}'; 105 | } 106 | } 107 | -------------------------------------------------------------------------------- /rivescript-core/src/main/java/com/rivescript/session/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016-2017 the original author or authors. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in all 12 | * copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | * SOFTWARE. 21 | */ 22 | 23 | /** 24 | * Provides session manager classes. 25 | */ 26 | package com.rivescript.session; 27 | -------------------------------------------------------------------------------- /rivescript-core/src/main/java/com/rivescript/sorting/SortBuffer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016 the original author or authors. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in all 12 | * copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | * SOFTWARE. 21 | */ 22 | 23 | package com.rivescript.sorting; 24 | 25 | import java.util.ArrayList; 26 | import java.util.HashMap; 27 | import java.util.List; 28 | import java.util.Map; 29 | 30 | /** 31 | * Sort buffer data. 32 | * 33 | * @author Noah Petherbridge 34 | * @author Marcel Overdijk 35 | */ 36 | public class SortBuffer { 37 | 38 | private Map> topics; 39 | private Map> thats; 40 | private List sub; 41 | private List person; 42 | 43 | public SortBuffer() { 44 | this.topics = new HashMap<>(); 45 | this.thats = new HashMap<>(); 46 | this.sub = new ArrayList<>(); 47 | this.person = new ArrayList<>(); 48 | } 49 | 50 | public List getTopic(String name) { 51 | return topics.get(name); 52 | } 53 | 54 | public Map> getTopics() { 55 | return topics; 56 | } 57 | 58 | public void addTopic(String name, List triggers) { 59 | topics.put(name, triggers); 60 | } 61 | 62 | public List getThats(String name) { 63 | return thats.get(name); 64 | } 65 | 66 | public Map> getThats() { 67 | return thats; 68 | } 69 | 70 | public void addThats(String name, List triggers) { 71 | thats.put(name, triggers); 72 | } 73 | 74 | public List getSub() { 75 | return sub; 76 | } 77 | 78 | public void setSub(List sub) { 79 | this.sub = sub; 80 | } 81 | 82 | public List getPerson() { 83 | return person; 84 | } 85 | 86 | public void setPerson(List person) { 87 | this.person = person; 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /rivescript-core/src/main/java/com/rivescript/sorting/SortTrack.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016 the original author or authors. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in all 12 | * copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | * SOFTWARE. 21 | */ 22 | 23 | package com.rivescript.sorting; 24 | 25 | import java.util.ArrayList; 26 | import java.util.HashMap; 27 | import java.util.List; 28 | import java.util.Map; 29 | 30 | /** 31 | * Temporary categorization of triggers while sorting. 32 | * 33 | * @author Noah Petherbridge 34 | * @author Marcel Overdijk 35 | */ 36 | public class SortTrack { 37 | 38 | private Map> atomic; // Sort by number of whole words 39 | private Map> option; // Sort optionals by number of words 40 | private Map> alpha; // Sort alpha wildcards by no. of words 41 | private Map> number; // Sort numeric wildcards by no. of words 42 | private Map> wild; // Sort wildcards by no. of words 43 | private List pound; // Triggers of just '#' 44 | private List under; // Triggers of just '_' 45 | private List star; // Triggers of just '*' 46 | 47 | public SortTrack() { 48 | this.atomic = new HashMap<>(); 49 | this.option = new HashMap<>(); 50 | this.alpha = new HashMap<>(); 51 | this.number = new HashMap<>(); 52 | this.wild = new HashMap<>(); 53 | this.pound = new ArrayList<>(); 54 | this.under = new ArrayList<>(); 55 | this.star = new ArrayList<>(); 56 | } 57 | 58 | public Map> getAtomic() { 59 | return atomic; 60 | } 61 | 62 | public Map> getOption() { 63 | return option; 64 | } 65 | 66 | public Map> getAlpha() { 67 | return alpha; 68 | } 69 | 70 | public Map> getNumber() { 71 | return number; 72 | } 73 | 74 | public Map> getWild() { 75 | return wild; 76 | } 77 | 78 | public List getPound() { 79 | return pound; 80 | } 81 | 82 | public List getUnder() { 83 | return under; 84 | } 85 | 86 | public List getStar() { 87 | return star; 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /rivescript-core/src/main/java/com/rivescript/sorting/SortedTriggerEntry.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016 the original author or authors. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in all 12 | * copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | * SOFTWARE. 21 | */ 22 | 23 | package com.rivescript.sorting; 24 | 25 | import com.rivescript.ast.Trigger; 26 | 27 | /** 28 | * Holds a sorted trigger and the pointer to that trigger's data 29 | * 30 | * @author Noah Petherbridge 31 | * @author Marcel Overdijk 32 | */ 33 | public class SortedTriggerEntry { 34 | 35 | private String trigger; 36 | private Trigger pointer; 37 | 38 | public SortedTriggerEntry(String trigger, Trigger pointer) { 39 | this.trigger = trigger; 40 | this.pointer = pointer; 41 | } 42 | 43 | public String getTrigger() { 44 | return trigger; 45 | } 46 | 47 | public void setTrigger(String trigger) { 48 | this.trigger = trigger; 49 | } 50 | 51 | public Trigger getPointer() { 52 | return pointer; 53 | } 54 | 55 | public void setPointer(Trigger pointer) { 56 | this.pointer = pointer; 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /rivescript-core/src/main/java/com/rivescript/sorting/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016-2017 the original author or authors. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in all 12 | * copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | * SOFTWARE. 21 | */ 22 | 23 | /** 24 | * Provides sorting classes. 25 | */ 26 | package com.rivescript.sorting; 27 | -------------------------------------------------------------------------------- /rivescript-core/src/main/java/com/rivescript/util/StringUtils.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016 the original author or authors. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in all 12 | * copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | * SOFTWARE. 21 | */ 22 | 23 | package com.rivescript.util; 24 | 25 | import com.rivescript.regexp.Regexp; 26 | 27 | import java.util.HashSet; 28 | import java.util.Set; 29 | 30 | import static com.rivescript.regexp.Regexp.RE_NASTIES; 31 | 32 | /** 33 | * Miscellaneous {@link String} utility methods. 34 | * 35 | * @author Noah Petherbridge 36 | * @author Marcel Overdijk 37 | */ 38 | public class StringUtils { 39 | 40 | private static final char ESCAPE_CHAR = '\\'; 41 | 42 | private static final Set UNSAFE_CHARS = new HashSet<>(); 43 | 44 | static { 45 | UNSAFE_CHARS.add('\\'); 46 | UNSAFE_CHARS.add('.'); 47 | UNSAFE_CHARS.add('+'); 48 | UNSAFE_CHARS.add('*'); 49 | UNSAFE_CHARS.add('?'); 50 | UNSAFE_CHARS.add('['); 51 | UNSAFE_CHARS.add('^'); 52 | UNSAFE_CHARS.add(']'); 53 | UNSAFE_CHARS.add('$'); 54 | UNSAFE_CHARS.add('('); 55 | UNSAFE_CHARS.add(')'); 56 | UNSAFE_CHARS.add('{'); 57 | UNSAFE_CHARS.add('}'); 58 | UNSAFE_CHARS.add('='); 59 | UNSAFE_CHARS.add('!'); 60 | UNSAFE_CHARS.add('<'); 61 | UNSAFE_CHARS.add('>'); 62 | UNSAFE_CHARS.add('|'); 63 | UNSAFE_CHARS.add(':'); 64 | } 65 | 66 | /** 67 | * Counts the number of words in a {@link String}. 68 | * 69 | * @param str the string to count 70 | * @param all count all 71 | * @return the number of words 72 | */ 73 | public static int countWords(String str, boolean all) { 74 | if (str == null || str.length() == 0) { 75 | return 0; 76 | } 77 | String[] words; 78 | if (all) { 79 | words = str.split("\\s+"); // Splits at whitespaces. 80 | } else { 81 | words = str.split("[\\s\\*\\#\\_\\|]+"); 82 | } 83 | 84 | int count = 0; 85 | for (String word : words) { 86 | if (word.length() > 0) { 87 | count++; 88 | } 89 | } 90 | 91 | return count; 92 | } 93 | 94 | /** 95 | * Joins a {@link String} array into a single {@link String}. 96 | * 97 | * @param array the array to join 98 | * @param delimiter the delimiter character to use, {@code null} is treated as {@code ""} 99 | */ 100 | public static String join(String[] array, String delimiter) { 101 | if (array == null) { 102 | return null; 103 | } 104 | if (delimiter == null) { 105 | delimiter = ""; 106 | } 107 | StringBuilder buf = new StringBuilder(); 108 | for (int i = 0; i < array.length; i++) { 109 | buf.append(array[i]); 110 | if (i < array.length - 1) { 111 | buf.append(delimiter); 112 | } 113 | } 114 | return buf.toString(); 115 | } 116 | 117 | /** 118 | * Escapes a {@link String} for use in a regular expression. 119 | * 120 | * @param str the string to escape 121 | * @return the escaped string 122 | */ 123 | public static String quoteMetacharacters(String str) { 124 | if (str == null) { 125 | return null; 126 | } 127 | 128 | StringBuilder sb = new StringBuilder(); 129 | for (int i = 0; i < str.length(); i++) { 130 | char c = str.charAt(i); 131 | if (UNSAFE_CHARS.contains(c)) { 132 | sb.append(ESCAPE_CHAR); 133 | } 134 | sb.append(c); 135 | } 136 | 137 | return sb.toString(); 138 | } 139 | 140 | /** 141 | * Strips special characters out of a {@link String}. 142 | * 143 | * @param str the string to strip 144 | * @return the stripped string 145 | * @see Regexp#RE_NASTIES 146 | */ 147 | public static String stripNasties(String str) { 148 | if (str == null) { 149 | return null; 150 | } 151 | return RE_NASTIES.matcher(str).replaceAll(""); 152 | } 153 | } 154 | 155 | -------------------------------------------------------------------------------- /rivescript-core/src/main/java/com/rivescript/util/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016-2017 the original author or authors. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in all 12 | * copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | * SOFTWARE. 21 | */ 22 | 23 | /** 24 | * Provides util classes. 25 | */ 26 | package com.rivescript.util; 27 | -------------------------------------------------------------------------------- /rivescript-core/src/test/java/com/rivescript/ConfigTests.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016-2017 the original author or authors. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in all 12 | * copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | * SOFTWARE. 21 | */ 22 | 23 | package com.rivescript; 24 | 25 | import org.junit.Test; 26 | 27 | import static com.rivescript.Config.DEFAULT_CONCAT; 28 | import static com.rivescript.Config.DEFAULT_DEPTH; 29 | import static org.hamcrest.Matchers.equalTo; 30 | import static org.hamcrest.Matchers.is; 31 | import static org.junit.Assert.assertThat; 32 | 33 | /** 34 | * Tests for {@link Config}. 35 | * 36 | * @author Marcel Overdijk 37 | */ 38 | public class ConfigTests { 39 | 40 | @Test 41 | public void testBasicConfig() { 42 | Config config = Config.basic(); 43 | assertThat(config.isThrowExceptions(), is(equalTo(false))); 44 | assertThat(config.isStrict(), is(equalTo(true))); 45 | assertThat(config.isUtf8(), is(equalTo(false))); 46 | assertThat(config.getUnicodePunctuation(), is(equalTo("[.,!?;:]"))); 47 | assertThat(config.isForceCase(), is(equalTo(false))); 48 | assertThat(config.getConcat(), is(equalTo(DEFAULT_CONCAT))); 49 | assertThat(config.getDepth(), is(equalTo(DEFAULT_DEPTH))); 50 | assertThat(config.getSessionManager(), is(equalTo(null))); 51 | assertThat(config.getErrorMessages(), is(equalTo(null))); 52 | } 53 | 54 | @Test 55 | public void testUtf8Config() { 56 | Config config = Config.utf8(); 57 | assertThat(config.isThrowExceptions(), is(equalTo(false))); 58 | assertThat(config.isStrict(), is(equalTo(true))); 59 | assertThat(config.isUtf8(), is(equalTo(true))); 60 | assertThat(config.getUnicodePunctuation(), is(equalTo("[.,!?;:]"))); 61 | assertThat(config.isForceCase(), is(equalTo(false))); 62 | assertThat(config.getConcat(), is(equalTo(DEFAULT_CONCAT))); 63 | assertThat(config.getDepth(), is(equalTo(DEFAULT_DEPTH))); 64 | assertThat(config.getSessionManager(), is(equalTo(null))); 65 | assertThat(config.getErrorMessages(), is(equalTo(null))); 66 | } 67 | 68 | @Test 69 | public void testEquals() { 70 | Config config1 = new Config(); 71 | Config config2 = new Config(); 72 | assertThat(config1, is(equalTo(config2))); 73 | } 74 | 75 | @Test 76 | public void testToBuilder() { 77 | Config config1 = new Config(); 78 | Config.Builder builder = config1.toBuilder(); 79 | Config config2 = builder.build(); 80 | assertThat(config1, is(equalTo(config2))); 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /rivescript-core/src/test/java/com/rivescript/RiveScriptResourceTests.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016-2017 the original author or authors. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in all 12 | * copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | * SOFTWARE. 21 | */ 22 | 23 | package com.rivescript; 24 | 25 | import org.junit.Test; 26 | 27 | import java.io.IOException; 28 | import java.io.InputStream; 29 | 30 | import static org.junit.Assert.assertEquals; 31 | import static org.junit.Assert.assertNull; 32 | 33 | /** 34 | * Tests for the RiveScript classpath load methods. 35 | */ 36 | public class RiveScriptResourceTests { 37 | 38 | /** 39 | * This method tests the loadInputStream() method of RiveScript. This implies 40 | * usage of the loadReader() method as well, as it delegates internally. 41 | * 42 | * @throws IOException This implies that the resource could not be read. 43 | */ 44 | @Test 45 | public void testLoadFromInputStream() throws IOException { 46 | RiveScript engine=new RiveScript(); 47 | try (InputStream is = RiveScript.class.getResourceAsStream("/test.rive")) { 48 | engine.loadInputStream(is); 49 | engine.sortReplies(); 50 | assertEquals(engine.reply("anyone", "hello"), "Hi there!"); 51 | } 52 | } 53 | 54 | /** 55 | * This method tests the loadInputStream() when passed a nonexistent resource. 56 | * Internally, it catches the NullPointerException and ignores it, throwing 57 | * exceptions in the test for any other outcome. 58 | * 59 | * @throws IOException This should logically never happen, as the resource is 60 | * acquired via getResourceAsStream(), which will return null rather than an IOException. 61 | */ 62 | @Test 63 | public void testLoadFromNull() throws IOException { 64 | RiveScript engine=new RiveScript(); 65 | try (InputStream is = RiveScript.class.getResourceAsStream("/notthere.rive")) { 66 | assertNull(is); 67 | engine.loadInputStream(is); 68 | throw new AssertionError("Should have received error from loadInputStream with null input"); 69 | } catch (NullPointerException e) { 70 | // this is expected output from loadInputStream() 71 | } 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /rivescript-core/src/test/java/com/rivescript/ast/RootTests.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016-2017 the original author or authors. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in all 12 | * copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | * SOFTWARE. 21 | */ 22 | 23 | package com.rivescript.ast; 24 | 25 | import org.junit.Test; 26 | 27 | import static org.hamcrest.Matchers.equalTo; 28 | import static org.hamcrest.Matchers.hasKey; 29 | import static org.hamcrest.Matchers.is; 30 | import static org.hamcrest.Matchers.not; 31 | import static org.hamcrest.Matchers.nullValue; 32 | import static org.junit.Assert.assertThat; 33 | 34 | /** 35 | * Tests for {@link Root}. 36 | * 37 | * @author Marcel Overdijk 38 | */ 39 | public class RootTests { 40 | 41 | @Test 42 | public void testNewRootHasEmptyRandomTopic() { 43 | Root root = new Root(); 44 | assertThat(root.getTopics(), hasKey("random")); 45 | assertThat(root.getTopic("random"), is(not(nullValue()))); 46 | assertThat(root.getTopic("random").getTriggers().size(), is(equalTo(0))); 47 | assertThat(root.getTopic("random").getIncludes().size(), is(equalTo(0))); 48 | assertThat(root.getTopic("random").getInherits().size(), is(equalTo(0))); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /rivescript-core/src/test/java/com/rivescript/parser/ParserConfigBuilderTests.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016-2017 the original author or authors. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in all 12 | * copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | * SOFTWARE. 21 | */ 22 | 23 | package com.rivescript.parser; 24 | 25 | import org.junit.Test; 26 | 27 | import static com.rivescript.ConcatMode.NEWLINE; 28 | import static com.rivescript.ConcatMode.NONE; 29 | import static com.rivescript.ConcatMode.SPACE; 30 | import static org.hamcrest.Matchers.equalTo; 31 | import static org.hamcrest.Matchers.is; 32 | import static org.junit.Assert.assertThat; 33 | 34 | /** 35 | * Tests for {@link ParserConfig.Builder}. 36 | * 37 | * @author Marcel Overdijk 38 | */ 39 | public class ParserConfigBuilderTests { 40 | 41 | @Test 42 | public void testBuildWithStrictIsFalse() { 43 | ParserConfig config = ParserConfig.newBuilder().strict(false).build(); 44 | assertThat(config.isStrict(), is(equalTo(false))); 45 | } 46 | 47 | @Test 48 | public void testBuildWithStrictIsTrue() { 49 | ParserConfig config = ParserConfig.newBuilder().strict(true).build(); 50 | assertThat(config.isStrict(), is(equalTo(true))); 51 | } 52 | 53 | @Test 54 | public void testBuildWithUtf8IsFalse() { 55 | ParserConfig config = ParserConfig.newBuilder().utf8(false).build(); 56 | assertThat(config.isUtf8(), is(equalTo(false))); 57 | } 58 | 59 | @Test 60 | public void testBuildWithUtf8IsTrue() { 61 | ParserConfig config = ParserConfig.newBuilder().utf8(true).build(); 62 | assertThat(config.isUtf8(), is(equalTo(true))); 63 | } 64 | 65 | @Test 66 | public void testBuildWithForceCaseIsFalse() { 67 | ParserConfig config = ParserConfig.newBuilder().forceCase(false).build(); 68 | assertThat(config.isForceCase(), is(equalTo(false))); 69 | } 70 | 71 | @Test 72 | public void testBuildWithForceCaseIsTrue() { 73 | ParserConfig config = ParserConfig.newBuilder().forceCase(true).build(); 74 | assertThat(config.isForceCase(), is(equalTo(true))); 75 | } 76 | 77 | @Test 78 | public void testBuildWithConcatIsNone() { 79 | ParserConfig config = ParserConfig.newBuilder().concat(NONE).build(); 80 | assertThat(config.getConcat(), is(equalTo(NONE))); 81 | } 82 | 83 | @Test 84 | public void testBuildWithConcatIsNewline() { 85 | ParserConfig config = ParserConfig.newBuilder().concat(NEWLINE).build(); 86 | assertThat(config.getConcat(), is(equalTo(NEWLINE))); 87 | } 88 | 89 | @Test 90 | public void testBuildWithConcatIsSpace() { 91 | ParserConfig config = ParserConfig.newBuilder().concat(SPACE).build(); 92 | assertThat(config.getConcat(), is(equalTo(SPACE))); 93 | } 94 | } 95 | -------------------------------------------------------------------------------- /rivescript-core/src/test/java/com/rivescript/parser/ParserConfigTests.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016-2017 the original author or authors. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in all 12 | * copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | * SOFTWARE. 21 | */ 22 | 23 | package com.rivescript.parser; 24 | 25 | import org.junit.Test; 26 | 27 | import static org.hamcrest.Matchers.equalTo; 28 | import static org.hamcrest.Matchers.is; 29 | import static org.junit.Assert.assertThat; 30 | 31 | /** 32 | * Tests for {@link ParserConfig}. 33 | * 34 | * @author Marcel Overdijk 35 | */ 36 | public class ParserConfigTests { 37 | 38 | @Test 39 | public void testEquals() { 40 | ParserConfig config1 = new ParserConfig(); 41 | ParserConfig config2 = new ParserConfig(); 42 | assertThat(config1, is(equalTo(config2))); 43 | } 44 | 45 | @Test 46 | public void testToBuilder() { 47 | ParserConfig config1 = new ParserConfig(); 48 | ParserConfig.Builder builder = config1.toBuilder(); 49 | ParserConfig config2 = builder.build(); 50 | assertThat(config1, is(equalTo(config2))); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /rivescript-core/src/test/java/com/rivescript/util/StringUtilTests.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016-2017 the original author or authors. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in all 12 | * copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | * SOFTWARE. 21 | */ 22 | 23 | package com.rivescript.util; 24 | 25 | import org.junit.Test; 26 | 27 | import static org.hamcrest.Matchers.equalTo; 28 | import static org.hamcrest.Matchers.is; 29 | import static org.junit.Assert.assertThat; 30 | 31 | /** 32 | * Tests for {@link StringUtils}. 33 | * 34 | * @author Marcel Overdijk 35 | */ 36 | public class StringUtilTests { 37 | 38 | @Test 39 | public void testCountWords() { 40 | assertThat(StringUtils.countWords("Hi", true), is(equalTo(1))); 41 | assertThat(StringUtils.countWords("Hi there", true), is(equalTo(2))); 42 | assertThat(StringUtils.countWords("Hi there", true), is(equalTo(2))); 43 | assertThat(StringUtils.countWords("The quick brown fox jumps over the lazy dog", true), is(equalTo(9))); 44 | assertThat(StringUtils.countWords("The * quick # brown _ fox | jumps over the lazy dog", true), is(equalTo(13))); 45 | assertThat(StringUtils.countWords("The * quick # brown _ fox | jumps over the lazy dog", false), is(equalTo(9))); 46 | } 47 | 48 | @Test 49 | public void testJoin() { 50 | assertThat(StringUtils.join(new String[] {"a"}, ","), is(equalTo("a"))); 51 | assertThat(StringUtils.join(new String[] {"a", "b"}, ","), is(equalTo("a,b"))); 52 | assertThat(StringUtils.join(new String[] {"a", "b", "c"}, ","), is(equalTo("a,b,c"))); 53 | assertThat(StringUtils.join(new String[] {"foo", "bar"}, ""), is(equalTo("foobar"))); 54 | assertThat(StringUtils.join(new String[] {}, ","), is(equalTo(""))); 55 | assertThat(StringUtils.join(new String[] {}, ""), is(equalTo(""))); 56 | assertThat(StringUtils.join(null, ","), is(equalTo(null))); 57 | } 58 | 59 | @Test 60 | public void testQuoteMetacharacters() { 61 | assertThat(StringUtils.quoteMetacharacters("foo"), is(equalTo("foo"))); 62 | assertThat(StringUtils.quoteMetacharacters("foo bar"), is(equalTo("foo bar"))); 63 | assertThat(StringUtils.quoteMetacharacters("\\"), is(equalTo("\\\\"))); 64 | assertThat(StringUtils.quoteMetacharacters("."), is(equalTo("\\."))); 65 | assertThat(StringUtils.quoteMetacharacters("+"), is(equalTo("\\+"))); 66 | assertThat(StringUtils.quoteMetacharacters("*"), is(equalTo("\\*"))); 67 | assertThat(StringUtils.quoteMetacharacters("?"), is(equalTo("\\?"))); 68 | assertThat(StringUtils.quoteMetacharacters("["), is(equalTo("\\["))); 69 | assertThat(StringUtils.quoteMetacharacters("^"), is(equalTo("\\^"))); 70 | assertThat(StringUtils.quoteMetacharacters("]"), is(equalTo("\\]"))); 71 | assertThat(StringUtils.quoteMetacharacters("$"), is(equalTo("\\$"))); 72 | assertThat(StringUtils.quoteMetacharacters("("), is(equalTo("\\("))); 73 | assertThat(StringUtils.quoteMetacharacters(")"), is(equalTo("\\)"))); 74 | assertThat(StringUtils.quoteMetacharacters("{"), is(equalTo("\\{"))); 75 | assertThat(StringUtils.quoteMetacharacters("}"), is(equalTo("\\}"))); 76 | assertThat(StringUtils.quoteMetacharacters("="), is(equalTo("\\="))); 77 | assertThat(StringUtils.quoteMetacharacters("!"), is(equalTo("\\!"))); 78 | assertThat(StringUtils.quoteMetacharacters("<"), is(equalTo("\\<"))); 79 | assertThat(StringUtils.quoteMetacharacters(">"), is(equalTo("\\>"))); 80 | assertThat(StringUtils.quoteMetacharacters("|"), is(equalTo("\\|"))); 81 | assertThat(StringUtils.quoteMetacharacters(":"), is(equalTo("\\:"))); 82 | assertThat(StringUtils.quoteMetacharacters("foo bar \\.+*?[^]$(){}=!<>|:"), 83 | is(equalTo("foo bar \\\\\\.\\+\\*\\?\\[\\^\\]\\$\\(\\)\\{\\}\\=\\!\\<\\>\\|\\:"))); 84 | assertThat(StringUtils.quoteMetacharacters(""), is(equalTo(""))); 85 | assertThat(StringUtils.quoteMetacharacters(null), is(equalTo(null))); 86 | } 87 | 88 | @Test 89 | public void testStripNasties() { 90 | assertThat(StringUtils.stripNasties("foo"), is(equalTo("foo"))); 91 | assertThat(StringUtils.stripNasties("foo bar"), is(equalTo("foo bar"))); 92 | assertThat(StringUtils.stripNasties("foo _ bar"), is(equalTo("foo _ bar"))); 93 | assertThat(StringUtils.stripNasties("!@#$%^&*(){}:;\"'|\\<>,.?/"), is(equalTo(""))); 94 | assertThat(StringUtils.stripNasties(""), is(equalTo(""))); 95 | assertThat(StringUtils.stripNasties(null), is(equalTo(null))); 96 | } 97 | } 98 | -------------------------------------------------------------------------------- /rivescript-core/src/test/resources/simplelogger.properties: -------------------------------------------------------------------------------- 1 | org.slf4j.simpleLogger.defaultLogLevel=info 2 | org.slf4j.simpleLogger.showThreadName=false 3 | org.slf4j.simpleLogger.log.com.rivescript=debug 4 | -------------------------------------------------------------------------------- /rivescript-core/src/test/resources/test.rive: -------------------------------------------------------------------------------- 1 | + hello 2 | - Hi there! 3 | -------------------------------------------------------------------------------- /rivescript-spring-boot-starter/src/main/resources/META-INF/spring.factories: -------------------------------------------------------------------------------- 1 | # Auto Configure 2 | org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ 3 | com.rivescript.spring.boot.autoconfigure.RiveScriptAutoConfiguration 4 | -------------------------------------------------------------------------------- /rivescript-spring-boot-starter/src/test/resources/rivescript/test.rive: -------------------------------------------------------------------------------- 1 | + hello 2 | - Hi there! 3 | -------------------------------------------------------------------------------- /rivescript-spring-boot-starter/src/test/resources/simplelogger.properties: -------------------------------------------------------------------------------- 1 | org.slf4j.simpleLogger.defaultLogLevel=info 2 | org.slf4j.simpleLogger.showThreadName=false 3 | org.slf4j.simpleLogger.log.com.rivescript=debug 4 | -------------------------------------------------------------------------------- /samples/rsbot/build.gradle: -------------------------------------------------------------------------------- 1 | dependencies { 2 | compile project(":rivescript-core") 3 | compile "org.json:json:${jsonVersion}" 4 | } 5 | 6 | task runBot(type: JavaExec) { 7 | classpath = sourceSets.main.runtimeClasspath 8 | main = "RSBot" 9 | standardInput = System.in 10 | args "${project.projectDir}/src/main/resources/rivescript" 11 | } 12 | -------------------------------------------------------------------------------- /samples/rsbot/src/main/java/ExampleMacro.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016 the original author or authors. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in all 12 | * copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | * SOFTWARE. 21 | */ 22 | 23 | import com.rivescript.macro.Subroutine; 24 | import com.rivescript.util.StringUtils; 25 | 26 | import java.lang.String; 27 | import java.lang.StringBuilder; 28 | 29 | /** 30 | * An example object macro written in Java. 31 | * 32 | * To define a Java object macro, you must implement the interface 33 | * com.rivescript.ZzObjectMacro and register it using setSubroutine(). 34 | * 35 | * This macro does two things: returns their message reversed, and sets 36 | * a user variable named `java`. 37 | * 38 | * This implements the `reverse` object macro used in Aiden/obj-java.rive 39 | * 40 | * See RSBot.java for more details. 41 | * 42 | * @author Noah Petherbridge 43 | */ 44 | public class ExampleMacro implements Subroutine { 45 | public String call (com.rivescript.RiveScript rs, String[] args) { 46 | String message = StringUtils.join(args, " "); 47 | 48 | // To get/set user variables for the user, you can use currentUser 49 | // to find their ID and then use the usual methods. 50 | String user = rs.currentUser(); 51 | rs.setUservar(user, "java", "This variable was set by Java when you said 'reverse " + message + "'"); 52 | 53 | // Reverse their message and return it. 54 | return new StringBuilder(message).reverse().toString(); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /samples/rsbot/src/main/java/RSBot.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016 the original author or authors. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in all 12 | * copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | * SOFTWARE. 21 | */ 22 | 23 | import com.rivescript.RiveScript; 24 | import com.rivescript.cmd.Shell; 25 | import com.rivescript.lang.Perl; 26 | import java.io.File; 27 | 28 | /** 29 | * @author Noah Petherbridge 30 | */ 31 | public class RSBot extends Shell { 32 | 33 | @Override 34 | protected void init(RiveScript bot) { 35 | // Create a handler for Perl as an object macro language. 36 | File rsp4jFile = new File(RSBot.class.getClassLoader().getResource("lang/rsp4j.pl").getFile()); 37 | bot.setHandler("perl", new Perl(rsp4jFile.getAbsolutePath())); 38 | // Define an object macro in Java. 39 | bot.setSubroutine("javatest", new ExampleMacro()); 40 | } 41 | 42 | public static void main(String[] args) { 43 | if (args.length == 0) { 44 | String path = RSBot.class.getClassLoader().getResource("rivescript").getFile(); 45 | args = new String[] { path }; 46 | } 47 | new RSBot().run(args); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /samples/rsbot/src/main/resources/lang/rsp4j.pl: -------------------------------------------------------------------------------- 1 | #!/usr/bin/perl -w 2 | 3 | # RiveScript Perl 4 Java 4 | # A simple Perl script that allows object macros in RiveScript-Java to run 5 | # Perl code. 6 | 7 | use strict; 8 | use warnings; 9 | use RiveScript; 10 | use Getopt::Long; 11 | use JSON; 12 | use File::Basename; 13 | my $self = basename($0); 14 | 15 | # Parse CLI options. 16 | my $java = 0; # --java, -j 17 | my $help = 0; 18 | GetOptions ( 19 | 'java|j' => \$java, 20 | 'help|h|?' => \$help, 21 | ); 22 | 23 | # No valid options? 24 | if (!$java && !$help) { 25 | print "See $self --help\n"; 26 | exit(1); 27 | } 28 | 29 | # If run with --java, the Java code is calling on us for a request. 30 | if ($java) { 31 | # Parse the incoming params. 32 | my $json = JSON->new->utf8->pretty(); 33 | my $incoming = ""; 34 | while () { 35 | $incoming .= $_; 36 | } 37 | 38 | # JSON decode. 39 | my $data; 40 | eval { 41 | $data = $json->decode($incoming); 42 | }; 43 | if ($@) { 44 | &error($json, "Invalid JSON data: $@"); 45 | } 46 | 47 | # Verify required keys exist. 48 | foreach my $key (qw(code vars id message)) { 49 | if (!exists $data->{$key}) { 50 | &error($json, "Required JSON key '$key' doesn't exist!"); 51 | } 52 | } 53 | 54 | # Set up RiveScript. 55 | my $code = $data->{code}; 56 | my $rs = RiveScript->new(); 57 | $rs->stream(qq{ 58 | + * 59 | - handle 60 | 61 | > object handle perl 62 | $code 63 | < object 64 | }); 65 | $rs->sortReplies(); 66 | 67 | # Set all the user vars. 68 | foreach my $what (keys %{$data->{vars}}) { 69 | $rs->setUservar($data->{id}, $what, $data->{vars}->{$what}); 70 | } 71 | 72 | # Get the reply. 73 | my $reply = $rs->reply($data->{id}, $data->{message}); 74 | 75 | # Return the JSON response. 76 | my $raw = $rs->getUservars($data->{id}); 77 | my $vars = {}; 78 | foreach my $key (keys %{$raw}) { 79 | next if ref($raw->{$key}); 80 | $vars->{$key} = $raw->{$key}; 81 | } 82 | my $out = { 83 | status => 'ok', 84 | reply => $reply, 85 | vars => $vars, 86 | }; 87 | print $json->encode($out); 88 | exit(0); 89 | } 90 | 91 | sub error { 92 | my ($json, $text) = @_; 93 | print $json->encode( { 94 | status => 'error', 95 | message => $text, 96 | } ); 97 | exit(1); 98 | } 99 | -------------------------------------------------------------------------------- /samples/rsbot/src/main/resources/rivescript/admin.rive: -------------------------------------------------------------------------------- 1 | // Administrative functions. 2 | 3 | + shutdown{weight=10000} 4 | * eq => Shutting down... shutdown 5 | - {@botmaster only} 6 | 7 | + botmaster only 8 | - This command can only be used by my botmaster. != 9 | 10 | > object shutdown perl 11 | my ($rs) = @_; 12 | 13 | # Shut down. 14 | exit(0); 15 | < object 16 | -------------------------------------------------------------------------------- /samples/rsbot/src/main/resources/rivescript/clients.rive: -------------------------------------------------------------------------------- 1 | // Learn stuff about our users. 2 | 3 | + my name is * 4 | - >Nice to meet you, . 5 | - >, nice to meet you. 6 | 7 | + my name is 8 | - >That's my master's name too. 9 | 10 | + my name is 11 | - >What a coincidence! That's my name too! 12 | - >That's my name too! 13 | 14 | + call me * 15 | - >, I will call you that from now on. 16 | 17 | + i am * years old 18 | - >A lot of people are , you're not alone. 19 | - >Cool, I'm myself.{weight=49} 20 | 21 | + i am a (@malenoun) 22 | - Alright, you're a . 23 | 24 | + i am a (@femalenoun) 25 | - Alright, you're female. 26 | 27 | + i (am from|live in) * 28 | - {/formal}>I've spoken to people from before. 29 | 30 | + my favorite * is * 31 | - =>Why is it your favorite? 32 | 33 | + i am single 34 | - I am too. 35 | 36 | + i have a girlfriend 37 | - What's her name? 38 | 39 | + i have a boyfriend 40 | - What's his name? 41 | 42 | + * 43 | % whats her name 44 | - >That's a pretty name. 45 | 46 | + * 47 | % whats his name 48 | - >That's a cool name. 49 | 50 | + my (girlfriend|boyfriend)* name is * 51 | - >That's a nice name. 52 | 53 | + (what is my name|who am i|do you know my name|do you know who i am){weight=10} 54 | - Your name is . 55 | - You told me your name is . 56 | - Aren't you ? 57 | 58 | + (how old am i|do you know how old i am|do you know my age){weight=10} 59 | - You are years old. 60 | - You're . 61 | 62 | + am i a (@malenoun) or a (@femalenoun){weight=10} 63 | - You're a . 64 | 65 | + am i (@malenoun) or (@femalenoun){weight=10} 66 | - You're a . 67 | 68 | + what is my favorite *{weight=10} 69 | - Your favorite is > 70 | 71 | + who is my (boyfriend|girlfriend|spouse){weight=10} 72 | - 73 | -------------------------------------------------------------------------------- /samples/rsbot/src/main/resources/rivescript/myself.rive: -------------------------------------------------------------------------------- 1 | // Tell the user stuff about ourself. 2 | 3 | + 4 | - Yes? 5 | 6 | + * 7 | - Yes? {@} 8 | 9 | + asl 10 | - // 11 | 12 | + (what is your name|who are you|who is this) 13 | - I am . 14 | - You can call me . 15 | 16 | + how old are you 17 | - I'm years old. 18 | - I'm . 19 | 20 | + are you a (@malenoun) or a (@femalenoun) 21 | - I'm a . 22 | 23 | + are you (@malenoun) or (@femalenoun) 24 | - I'm a . 25 | 26 | + where (are you|are you from|do you live) 27 | - I'm from . 28 | 29 | + what (city|town) (are you from|do you live in) 30 | - I'm in . 31 | 32 | + what is your favorite color 33 | - Definitely . 34 | 35 | + what is your favorite band 36 | - I like the most. 37 | 38 | + what is your favorite book 39 | - The best book I've read was . 40 | 41 | + what is your occupation 42 | - I'm a . 43 | 44 | + where is your (website|web site|site) 45 | - 46 | 47 | + what color are your eyes 48 | - I have eyes. 49 | - {sentence}{/sentence}. 50 | 51 | + what do you look like 52 | - I have eyes and hair. 53 | 54 | + what do you do 55 | - I'm a . 56 | 57 | + who is your favorite author 58 | - . 59 | 60 | + who is your master 61 | - . 62 | -------------------------------------------------------------------------------- /samples/rsbot/src/main/resources/rivescript/obj-java.rive: -------------------------------------------------------------------------------- 1 | // Tests the com.rivescript.lang.Java handler. 2 | 3 | ! local concat = space 4 | 5 | + reverse *{weight=2} 6 | - Ok: javatest 7 | 8 | + java test 9 | * <> undefined => Last time you used "reverse ___", I set 10 | ^ the "java" user variable to the following: . 11 | - Say something like "reverse hello world" and then say "java test" again. 12 | -------------------------------------------------------------------------------- /samples/rsbot/src/main/resources/rivescript/obj-perl.rive: -------------------------------------------------------------------------------- 1 | // Tests the com.rivescript.lang.Perl handler. 2 | 3 | + encode * in md5 4 | - "" in MD5 is: md5 5 | 6 | + perl version 7 | - Perl RS version: perlver 8 | 9 | + perl set name to * 10 | - Test using Perl handler to set your name. nametest 11 | 12 | > object md5 perl 13 | my ($rs,@args) = @_; 14 | my $msg = join(" ", @_); 15 | 16 | use Digest::MD5 qw(md5_hex); 17 | return md5_hex($msg); 18 | < object 19 | 20 | > object perlver perl 21 | my ($rs,@args) = @_; 22 | return $RiveScript::VERSION; 23 | < object 24 | 25 | > object nametest perl 26 | my $rs = shift; 27 | my $id = shift; 28 | my $name = join(" ",@_); 29 | 30 | $rs->setUservar($id, "name", $name); 31 | return "Done! Name set to $name. Ask 'what is my name' now. ;-)"; 32 | < object 33 | -------------------------------------------------------------------------------- /samples/spring-boot-starter-rsbot/build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | ext { 3 | springBootVersion = "2.1.6.RELEASE" 4 | } 5 | repositories { 6 | jcenter() 7 | mavenCentral() 8 | mavenLocal() 9 | } 10 | dependencies { 11 | classpath "org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}" 12 | } 13 | } 14 | 15 | apply plugin: "org.springframework.boot" 16 | 17 | dependencies { 18 | compile project(":rivescript-spring-boot-starter") 19 | compile "org.springframework.boot:spring-boot-starter-test:${springBootVersion}" 20 | } 21 | 22 | bootRun { 23 | standardInput = System.in 24 | } 25 | -------------------------------------------------------------------------------- /samples/spring-boot-starter-rsbot/src/main/java/com/rivescript/samples/Application.java: -------------------------------------------------------------------------------- 1 | package com.rivescript.samples;/* 2 | * Copyright (c) 2016 the original author or authors. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in all 12 | * copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | * SOFTWARE. 21 | */ 22 | 23 | import com.rivescript.RiveScript; 24 | import com.rivescript.cmd.Shell; 25 | import org.springframework.beans.factory.annotation.Autowired; 26 | import org.springframework.boot.Banner; 27 | import org.springframework.boot.CommandLineRunner; 28 | import org.springframework.boot.SpringApplication; 29 | import org.springframework.boot.SpringBootVersion; 30 | import org.springframework.boot.autoconfigure.SpringBootApplication; 31 | import org.springframework.context.annotation.Bean; 32 | import org.springframework.core.env.Environment; 33 | 34 | import java.io.PrintStream; 35 | 36 | /** 37 | * @author Marcel Overdijk 38 | */ 39 | @SpringBootApplication 40 | public class Application { 41 | 42 | @Bean 43 | public ShellCommandLineRunner runner() { 44 | return new ShellCommandLineRunner(); 45 | } 46 | 47 | private static class ShellCommandLineRunner extends Shell implements CommandLineRunner { 48 | 49 | @Autowired 50 | private RiveScript bot; 51 | 52 | @Override 53 | public void run(String... args) { 54 | run(bot); 55 | } 56 | } 57 | 58 | public static void main(String[] args) { 59 | SpringApplication app = new SpringApplication(Application.class); 60 | app.setBanner(new Banner() { 61 | 62 | @Override 63 | public void printBanner(Environment environment, Class sourceClass, PrintStream out) { 64 | out.println("" 65 | + " . . \n" 66 | + " .:...:: RiveScript Interpreter (Java)\n" 67 | + " .:: ::. Library Version: " + RiveScript.getVersion() + "\n" 68 | + " ..:;;. ' .;;:.. Spring Boot Version: " + SpringBootVersion.getVersion() + "\n" 69 | + " . ''' . \n" 70 | + " :;,:,;: \n" 71 | + " : : \n"); 72 | } 73 | }); 74 | app.run(args); 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /samples/spring-boot-starter-rsbot/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | logging.level: 2 | com.rivescript: INFO # Change to DEBUG for full logging. 3 | rivescript: 4 | object-handlers: javascript 5 | -------------------------------------------------------------------------------- /samples/spring-boot-starter-rsbot/src/main/resources/rivescript/admin.rive: -------------------------------------------------------------------------------- 1 | // Administrative functions. 2 | 3 | + shutdown{weight=10000} 4 | * eq => Shutting down... (j/k, not supported) 5 | - {@botmaster only} 6 | 7 | + botmaster only 8 | - This command can only be used by my botmaster. != 9 | -------------------------------------------------------------------------------- /samples/spring-boot-starter-rsbot/src/main/resources/rivescript/clients.rive: -------------------------------------------------------------------------------- 1 | // Learn stuff about our users. 2 | 3 | + my name is * 4 | - >Nice to meet you, . 5 | - >, nice to meet you. 6 | 7 | + my name is 8 | - >That's my master's name too. 9 | 10 | + my name is 11 | - >What a coincidence! That's my name too! 12 | - >That's my name too! 13 | 14 | + call me * 15 | - >, I will call you that from now on. 16 | 17 | + i am * years old 18 | - >A lot of people are , you're not alone. 19 | - >Cool, I'm myself.{weight=49} 20 | 21 | + i am a (@malenoun) 22 | - Alright, you're a . 23 | 24 | + i am a (@femalenoun) 25 | - Alright, you're female. 26 | 27 | + i (am from|live in) * 28 | - {/formal}>I've spoken to people from before. 29 | 30 | + my favorite * is * 31 | - =>Why is it your favorite? 32 | 33 | + i am single 34 | - I am too. 35 | 36 | + i have a girlfriend 37 | - What's her name? 38 | 39 | + i have a boyfriend 40 | - What's his name? 41 | 42 | + * 43 | % what is her name 44 | - >That's a pretty name. 45 | 46 | + * 47 | % what is his name 48 | - >That's a cool name. 49 | 50 | + my (girlfriend|boyfriend)* name is * 51 | - >That's a nice name. 52 | 53 | + (what is my name|who am i|do you know my name|do you know who i am){weight=10} 54 | - Your name is . 55 | - You told me your name is . 56 | - Aren't you ? 57 | 58 | + (how old am i|do you know how old i am|do you know my age){weight=10} 59 | - You are years old. 60 | - You're . 61 | 62 | + am i a (@malenoun) or a (@femalenoun){weight=10} 63 | - You're a . 64 | 65 | + am i (@malenoun) or (@femalenoun){weight=10} 66 | - You're a . 67 | 68 | + what is my favorite *{weight=10} 69 | - Your favorite is > 70 | 71 | + who is my (boyfriend|girlfriend|spouse){weight=10} 72 | - 73 | -------------------------------------------------------------------------------- /samples/spring-boot-starter-rsbot/src/main/resources/rivescript/javascript.rive: -------------------------------------------------------------------------------- 1 | // JavaScript Object Macro Examples 2 | 3 | ! version = 2.0 4 | 5 | > object setvar javascript 6 | // Example of how to get the current user's ID and set 7 | // variables for them. 8 | var uid = rs.CurrentUser(); 9 | var name = args[0]; 10 | var value = args[1]; 11 | rs.SetUservar(uid, name, value); 12 | < object 13 | 14 | > object add javascript 15 | // Demonstrates that JS objects can return numbers. 16 | var a = args[0]; 17 | var b = args[1]; 18 | return parseInt(a) + parseInt(b); 19 | < object 20 | 21 | + add # and # 22 | - + = add 23 | 24 | + javascript set * to * 25 | - Set user variable to .setvar 26 | -------------------------------------------------------------------------------- /samples/spring-boot-starter-rsbot/src/main/resources/rivescript/myself.rive: -------------------------------------------------------------------------------- 1 | // Tell the user stuff about ourself. 2 | 3 | + 4 | - Yes? 5 | 6 | + * 7 | - Yes? {@} 8 | 9 | + asl 10 | - // 11 | 12 | + (what is your name|who are you|who is this) 13 | - I am . 14 | - You can call me . 15 | 16 | + how old are you 17 | - I'm years old. 18 | - I'm . 19 | 20 | + are you a (@malenoun) or a (@femalenoun) 21 | - I'm a . 22 | 23 | + are you (@malenoun) or (@femalenoun) 24 | - I'm a . 25 | 26 | + where (are you|are you from|do you live) 27 | - I'm from . 28 | 29 | + what (city|town) (are you from|do you live in) 30 | - I'm in . 31 | 32 | + what is your favorite color 33 | - Definitely . 34 | 35 | + what is your favorite band 36 | - I like the most. 37 | 38 | + what is your favorite book 39 | - The best book I've read was . 40 | 41 | + what is your occupation 42 | - I'm a . 43 | 44 | + where is your (website|web site|site) 45 | - 46 | 47 | + what color are your eyes 48 | - I have eyes. 49 | - {sentence}{/sentence}. 50 | 51 | + what do you look like 52 | - I have eyes and hair. 53 | 54 | + what do you do 55 | - I'm a . 56 | 57 | + who is your favorite author 58 | - . 59 | 60 | + who is your master 61 | - . 62 | -------------------------------------------------------------------------------- /samples/spring-boot-starter-rsbot/src/main/resources/rivescript/test.rive: -------------------------------------------------------------------------------- 1 | + hello 2 | - Hi there! 3 | -------------------------------------------------------------------------------- /secring.gpg.enc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aichaos/rivescript-java/04abeca7fffeaf0783e6aa9c2fe84c34829e5a67/secring.gpg.enc -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = "rivescript" 2 | 3 | def String[] modules = [ 4 | "rivescript-core", 5 | "rivescript-spring-boot-starter" 6 | ] 7 | 8 | include modules 9 | 10 | def String[] samples = [ 11 | "rsbot", 12 | "spring-boot-starter-rsbot" 13 | ] 14 | 15 | samples.each { name -> 16 | include name 17 | def project = findProject(":${name}") 18 | project.name = "rivescript-samples-${name}" 19 | project.projectDir = new File(settingsDir, "samples/${name}"); 20 | } 21 | -------------------------------------------------------------------------------- /src/api/overview.html: -------------------------------------------------------------------------------- 1 | 2 | 3 |

4 | This is the public API documentation for the RiveScript interpreter for Java. 5 |

6 | 7 | 8 | -------------------------------------------------------------------------------- /src/eclipse/README.md: -------------------------------------------------------------------------------- 1 | The `rivescript-java-style.xml` code style is a copy of `https://github.com/google/styleguide/blob/gh-pages/eclipse-java-google-style.xml`. 2 | 3 | The following adjustments have been made: 4 | 5 | * `org.eclipse.jdt.core.formatter.comment.format_header`: `false` (was `true`) 6 | * `org.eclipse.jdt.core.formatter.blank_lines_before_first_class_body_declaration`: `1` (was `0`) 7 | * `org.eclipse.jdt.core.formatter.blank_lines_before_package`: `1` (was `0`) 8 | * `org.eclipse.jdt.core.formatter.comment.line_length`: `120` (was `100`) 9 | * `org.eclipse.jdt.core.formatter.insert_new_line_in_empty_method_body`: `insert` (was `do not insert`) 10 | * `org.eclipse.jdt.core.formatter.lineSplit`: `120` (was `100`) 11 | * `org.eclipse.jdt.core.formatter.tabulation.size`: `4` (was `2)` 12 | * `org.eclipse.jdt.core.formatter.use_on_off_tags`: `true` (was `false`) 13 | --------------------------------------------------------------------------------