Skip to content
This repository has been archived by the owner on Dec 4, 2023. It is now read-only.

Pass through StateConfiguration in DialogManager #1139

Merged
merged 2 commits into from
Apr 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.microsoft.bot.connector.authentication.GovernmentAuthenticationConstants;
import com.microsoft.bot.connector.authentication.SkillValidation;
import com.microsoft.bot.dialogs.memory.DialogStateManager;
import com.microsoft.bot.dialogs.memory.DialogStateManagerConfiguration;
import com.microsoft.bot.schema.Activity;
import com.microsoft.bot.schema.ActivityTypes;
import com.microsoft.bot.schema.EndOfConversationCodes;
Expand Down Expand Up @@ -322,24 +323,25 @@ public static CompletableFuture<Void> run(Dialog dialog, TurnContext turnContext
dialogSet.add(dialog);

return dialogSet.createContext(turnContext)
.thenAccept(dialogContext -> innerRun(turnContext, dialog.getId(), dialogContext));
.thenAccept(dialogContext -> innerRun(turnContext, dialog.getId(), dialogContext, null));
}

/**
* Shared implementation of run with Dialog and DialogManager.
*
* @param turnContext The turnContext.
* @param dialogId The Id of the Dialog.
* @param dialogContext The DialogContext.
* @param turnContext The turnContext.
* @param dialogId The Id of the Dialog.
* @param dialogContext The DialogContext.
* @param stateConfiguration The DialogStateManagerConfiguration.
* @return A DialogTurnResult.
*/
protected static CompletableFuture<DialogTurnResult> innerRun(TurnContext turnContext, String dialogId,
DialogContext dialogContext) {
DialogContext dialogContext, DialogStateManagerConfiguration stateConfiguration) {
for (Entry<String, Object> entry : turnContext.getTurnState().getTurnStateServices().entrySet()) {
dialogContext.getServices().replace(entry.getKey(), entry.getValue());
}

DialogStateManager dialogStateManager = new DialogStateManager(dialogContext);
DialogStateManager dialogStateManager = new DialogStateManager(dialogContext, stateConfiguration);
return dialogStateManager.loadAllScopes().thenCompose(result -> {
dialogContext.getContext().getTurnState().add(dialogStateManager);
DialogTurnResult dialogTurnResult = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ public CompletableFuture<DialogManagerResult> onTurn(TurnContext context) {
// Create DialogContext
DialogContext dc = new DialogContext(dialogs, context, dialogState);

return Dialog.innerRun(context, rootDialogId, dc).thenCompose(turnResult -> {
return Dialog.innerRun(context, rootDialogId, dc, getStateManagerConfiguration()).thenCompose(turnResult -> {
return botStateSet.saveAllChanges(dc.getContext(), false).thenCompose(saveResult -> {
DialogManagerResult result = new DialogManagerResult();
result.setTurnResult(turnResult);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,20 @@
import com.microsoft.bot.builder.UserState;
import com.microsoft.bot.builder.adapters.TestAdapter;
import com.microsoft.bot.builder.adapters.TestFlow;
import com.microsoft.bot.dialogs.memory.DialogStateManager;
import com.microsoft.bot.dialogs.memory.DialogStateManagerConfiguration;
import com.microsoft.bot.dialogs.memory.PathResolver;
import com.microsoft.bot.dialogs.memory.scopes.MemoryScope;
import com.microsoft.bot.dialogs.prompts.PromptOptions;
import com.microsoft.bot.dialogs.prompts.TextPrompt;
import com.microsoft.bot.schema.Activity;
import com.microsoft.bot.schema.ActivityTypes;
import com.microsoft.bot.schema.ChannelAccount;
import com.microsoft.bot.schema.ConversationAccount;
import com.microsoft.bot.schema.ResourceResponse;
import com.microsoft.bot.schema.ResultPair;

import org.apache.commons.lang3.NotImplementedException;
import org.apache.commons.lang3.StringUtils;
import org.junit.Assert;
import org.junit.Test;
Expand Down Expand Up @@ -462,6 +468,44 @@ public void SkillShouldReturnEmptyOnRepromptWithNoDialog() {
Assert.assertEquals(DialogTurnStatus.EMPTY, _dmTurnResult.getTurnResult().getStatus());
}

@Test
public void DialogManager_StateConfigurationTest() {
// Arrange
WaterfallDialog dialog = new WaterfallDialog("test-dialog", null);

CustomMemoryScope memoryScope = new CustomMemoryScope();
CustomPathResolver pathResolver = new CustomPathResolver();

DialogManager dialogManager = new DialogManager(dialog, null);
dialogManager.setStateManagerConfiguration(new DialogStateManagerConfiguration());
dialogManager.getStateManagerConfiguration().getMemoryScopes().add(memoryScope);
dialogManager.getStateManagerConfiguration().getPathResolvers().add(pathResolver);

// The test dialog being used here happens to not send anything so we only need to mock the type.
TestAdapter adapter = new TestAdapter();

// ChannelId and Conversation.Id are required for ConversationState and
// ChannelId and From.Id are required for UserState.
Activity activity = new Activity(ActivityTypes.MESSAGE);
activity.setChannelId("test-channel");
ConversationAccount conversation = new ConversationAccount();
conversation.setId("test-conversation-id");
ChannelAccount channelAccount = new ChannelAccount();
channelAccount.setId("test-id");
activity.setConversation(conversation);
activity.setFrom(channelAccount);

// Act
TurnContext turnContext = new TurnContextImpl(adapter, activity);
turnContext.getTurnState().add(new ConversationState(new MemoryStorage()));
dialogManager.onTurn(turnContext).join();
DialogStateManager actual = turnContext.getTurnState().get(DialogStateManager.class);

// Assert
Assert.assertTrue(actual.getConfiguration().getMemoryScopes().contains(memoryScope));
Assert.assertTrue(actual.getConfiguration().getPathResolvers().contains(pathResolver));
}

private Dialog CreateTestDialog(String property) {
return new AskForNameDialog(property.replace(".", ""), property);
}
Expand Down Expand Up @@ -539,6 +583,33 @@ public CompletableFuture<ResourceResponse[]> invoke(TurnContext context, List<Ac
}
}

private class CustomMemoryScope extends MemoryScope {
CustomMemoryScope() {
super("custom", false);
}

@Override
public Object getMemory(DialogContext dialogContext) {
throw new NotImplementedException("Not implemented");
}

@Override
public void setMemory(DialogContext dialogContext, Object memory) {
throw new NotImplementedException("Not implemented");
}
}

private class CustomPathResolver implements PathResolver {
CustomPathResolver() {
}

@Override
public String transformPath(String path) {
throw new NotImplementedException("Not implemented");
}

}

private class AskForNameDialog extends ComponentDialog implements DialogDependencies {
private final String property;

Expand Down