-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added long term session attributes and functions. * Upgraded blade-ink to v0.4.3. * Added tests for long term session attributes. * Updated version.
- Loading branch information
1 parent
166b4be
commit 6485fa7
Showing
22 changed files
with
588 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
mutters-core/src/test/java/com/rabidgremlin/mutters/core/session/TestSession.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package com.rabidgremlin.mutters.core.session; | ||
|
||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.hamcrest.CoreMatchers.notNullValue; | ||
import static org.hamcrest.CoreMatchers.nullValue; | ||
import static org.junit.Assert.assertThat; | ||
|
||
import org.junit.Test; | ||
|
||
|
||
public class TestSession | ||
{ | ||
@Test | ||
public void testAttributeLifeCycle() | ||
{ | ||
Session session = new Session(); | ||
|
||
session.setAttribute("bob", "alice"); | ||
assertThat(session.getAttribute("bob"), is("alice")); | ||
|
||
session.removeAttribute("bob"); | ||
assertThat(session.getAttribute("bob"), is(nullValue())); | ||
} | ||
|
||
@Test | ||
public void testLongTermAttributeLifeCycle() | ||
{ | ||
Session session = new Session(); | ||
|
||
session.setLongTermAttribute("bobLT", "aliceLT"); | ||
assertThat(session.getLongTermAttribute("bobLT"), is("aliceLT")); | ||
|
||
session.removeLongTermAttribute("bobLT"); | ||
assertThat(session.getLongTermAttribute("bobLT"), is(nullValue())); | ||
} | ||
|
||
@Test | ||
public void testReset() | ||
{ | ||
Session session = new Session(); | ||
|
||
session.setAttribute("bob", "alice"); | ||
session.setLongTermAttribute("bobLT", "aliceLT"); | ||
|
||
session.reset(); | ||
|
||
assertThat(session.getLongTermAttribute("bob"), is(nullValue())); | ||
assertThat(session.getLongTermAttribute("bobLT"), is("aliceLT")); | ||
} | ||
|
||
@Test | ||
public void testResetAll() | ||
{ | ||
Session session = new Session(); | ||
|
||
session.setAttribute("bob", "alice"); | ||
session.setLongTermAttribute("bobLT", "aliceLT"); | ||
|
||
session.resetAll(); | ||
|
||
assertThat(session.getLongTermAttribute("bob"), is(nullValue())); | ||
assertThat(session.getLongTermAttribute("bobLT"), is(nullValue())); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
...rc/main/java/com/rabidgremlin/mutters/bot/ink/functions/GetLongTermAttributeFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package com.rabidgremlin.mutters.bot.ink.functions; | ||
|
||
import com.bladecoder.ink.runtime.Story; | ||
import com.rabidgremlin.mutters.bot.ink.CurrentResponse; | ||
import com.rabidgremlin.mutters.bot.ink.InkBotFunction; | ||
import com.rabidgremlin.mutters.core.IntentMatch; | ||
import com.rabidgremlin.mutters.core.session.Session; | ||
|
||
/** | ||
* This function gets the value of a long term session attribute. These attributes are not removed at the end of a | ||
* conversation so they can be used to share context between conversations in the same session. | ||
* | ||
* For example in your Ink script you could have: | ||
* ``` | ||
* VAR current_order = "" | ||
* | ||
* === check_order_status === | ||
* ::GET_LONG_TERM_ATTR name::currentorder var::current_order | ||
* { | ||
* - current_order == "": | ||
* -> get_order_number_for_status_check // this would prompt for order number and store in current_order then jump to check_order_status | ||
* - else: | ||
* -> check_order_status // retrieves and displays status for order number in current_order | ||
* } | ||
* -> END | ||
* ``` | ||
* | ||
* Note: If there is no long term attribute with the specified name in the session then the specified Ink variable | ||
* will be set to "". | ||
* | ||
* | ||
* @author rabidgremlin | ||
* | ||
*/ | ||
public class GetLongTermAttributeFunction | ||
implements InkBotFunction | ||
{ | ||
|
||
/* | ||
* (non-Javadoc) | ||
* | ||
* @see com.rabidgremlin.mutters.bot.ink.InkBotFunction#getFunctionName() | ||
*/ | ||
@Override | ||
public String getFunctionName() | ||
{ | ||
return "GET_LONG_TERM_ATTR"; | ||
} | ||
|
||
/* | ||
* (non-Javadoc) | ||
* | ||
* @see com.rabidgremlin.mutters.bot.ink.InkBotFunction#respondexecute(CurrentResponse currentResponse, Session | ||
* session, IntentMatch intentMatch, Story story, String param) | ||
*/ | ||
@Override | ||
public void execute(CurrentResponse currentResponse, Session session, IntentMatch intentMatch, Story story, String param) | ||
{ | ||
FunctionDetails details = FunctionHelper.parseFunctionString(param); | ||
|
||
if (details.getFunctionParams() == null) | ||
{ | ||
throw new IllegalArgumentException("Missing name and variable value for GET_LONG_TERM_ATTR"); | ||
} | ||
|
||
String name = details.getFunctionParams().get("name"); | ||
if (name == null) | ||
{ | ||
throw new IllegalArgumentException("Missing name value for GET_LONG_TERM_ATTR"); | ||
} | ||
|
||
String var = details.getFunctionParams().get("var"); | ||
if (var == null) | ||
{ | ||
throw new IllegalArgumentException("Missing var value for GET_LONG_TERM_ATTR"); | ||
} | ||
|
||
try | ||
{ | ||
Object value = session.getLongTermAttribute(name); | ||
if (value == null) | ||
{ | ||
story.getVariablesState().set(var, ""); | ||
} | ||
else | ||
{ | ||
story.getVariablesState().set(var, value); | ||
} | ||
} | ||
catch(Exception e) | ||
{ | ||
throw new RuntimeException("Failed to get long term attribute",e); | ||
} | ||
} | ||
|
||
} |
73 changes: 73 additions & 0 deletions
73
...main/java/com/rabidgremlin/mutters/bot/ink/functions/RemoveLongTermAttributeFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package com.rabidgremlin.mutters.bot.ink.functions; | ||
|
||
import com.bladecoder.ink.runtime.Story; | ||
import com.rabidgremlin.mutters.bot.ink.CurrentResponse; | ||
import com.rabidgremlin.mutters.bot.ink.InkBotFunction; | ||
import com.rabidgremlin.mutters.core.IntentMatch; | ||
import com.rabidgremlin.mutters.core.session.Session; | ||
|
||
/** | ||
* This function removes the specified long term session attribute from the session. | ||
* These attributes are not removed at the end of a conversation so they can be used | ||
* to share context between conversations in the same session. | ||
* | ||
* For example in your Ink script you could have: | ||
* ``` | ||
* VAR current_order = "" | ||
* | ||
* === check_order_status === | ||
* ::GET_LONG_TERM_ATTR name::currentorder var::current_order | ||
* For order {current_order} ? | ||
* + YesIntent | ||
* -> display_order_details | ||
* + NoIntent | ||
* ::REMOVE_LONG_TERM_ATTR name::currentorder | ||
* -> get_order_number | ||
* -> END | ||
* ``` | ||
* | ||
* | ||
* @author rabidgremlin | ||
* | ||
*/ | ||
public class RemoveLongTermAttributeFunction | ||
implements InkBotFunction | ||
{ | ||
|
||
/* | ||
* (non-Javadoc) | ||
* | ||
* @see com.rabidgremlin.mutters.bot.ink.InkBotFunction#getFunctionName() | ||
*/ | ||
@Override | ||
public String getFunctionName() | ||
{ | ||
return "REMOVE_LONG_TERM_ATTR"; | ||
} | ||
|
||
/* | ||
* (non-Javadoc) | ||
* | ||
* @see com.rabidgremlin.mutters.bot.ink.InkBotFunction#respondexecute(CurrentResponse currentResponse, Session | ||
* session, IntentMatch intentMatch, Story story, String param) | ||
*/ | ||
@Override | ||
public void execute(CurrentResponse currentResponse, Session session, IntentMatch intentMatch, Story story, String param) | ||
{ | ||
FunctionDetails details = FunctionHelper.parseFunctionString(param); | ||
|
||
if (details.getFunctionParams() == null) | ||
{ | ||
throw new IllegalArgumentException("Missing name and value values for REMOVE_LONG_TERM_ATTR"); | ||
} | ||
|
||
String name = details.getFunctionParams().get("name"); | ||
if (name == null) | ||
{ | ||
throw new IllegalArgumentException("Missing name value for REMOVE_LONG_TERM_ATTR"); | ||
} | ||
|
||
session.removeLongTermAttribute(name); | ||
} | ||
|
||
} |
Oops, something went wrong.