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

Dialogs package #922

Merged
merged 30 commits into from
Jan 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
e2b3ab9
dialogs.choices
tracyboehrer Aug 25, 2020
51b1103
bot-dialogs: periodic push (mostly functional ObjectPath)
tracyboehrer Sep 10, 2020
44daf13
Dialog Updates
LeeParrishMSFT Nov 13, 2020
8333320
Interim commit and push to protect work in progress
LeeParrishMSFT Nov 17, 2020
dbce8f5
Pushing to protect work in progress
LeeParrishMSFT Nov 19, 2020
0c4a42f
Safety Checkin
LeeParrishMSFT Dec 3, 2020
013325e
Setting up unit tests for Dialogs
LeeParrishMSFT Dec 3, 2020
d46c9f3
Merge branch 'main' into lparrish/dialog
LeeParrishMSFT Dec 3, 2020
76e87d4
Updates to get VS Code test runner working
LeeParrishMSFT Dec 3, 2020
4757483
Completed DialogManager and DialogStateManager
LeeParrishMSFT Dec 9, 2020
093b8e3
Updates to match namespaces and file structure.
LeeParrishMSFT Dec 9, 2020
3f6983e
First unit test for DialogStateManager now working.
LeeParrishMSFT Dec 14, 2020
29d03bb
Addin DialogStateManager unit tests and fixes.
LeeParrishMSFT Jan 5, 2021
680d665
Waterfall dialogs and Prompts
LeeParrishMSFT Jan 8, 2021
a296c08
Fixes and unit tests
LeeParrishMSFT Jan 20, 2021
b7ba635
Choice Prompt unit tests
LeeParrishMSFT Jan 20, 2021
089aab4
Additional unit tests and fixes
LeeParrishMSFT Jan 22, 2021
094355c
Unit tests and fixes.
LeeParrishMSFT Jan 22, 2021
0d12f46
ComponentDialog tests and fixes
LeeParrishMSFT Jan 23, 2021
8de94b2
DialogContainerTests and fixes to Dialog
LeeParrishMSFT Jan 23, 2021
26e226a
Additional tests and fixes
LeeParrishMSFT Jan 25, 2021
37999d4
Final unit tests
LeeParrishMSFT Jan 26, 2021
438732a
Correct failing unit test.
LeeParrishMSFT Jan 26, 2021
4cccba3
Merge branch 'main' into lparrish/dialog
LeeParrishMSFT Jan 27, 2021
9d4431a
Fixes for merge issues.
LeeParrishMSFT Jan 27, 2021
5ab664c
Update to new exception handling pattern.
LeeParrishMSFT Jan 27, 2021
7eea25e
Remove uneeded unit test inports.
LeeParrishMSFT Jan 28, 2021
3b5fe18
Added copyright notices.
LeeParrishMSFT Jan 28, 2021
ad8b358
Update DialogContext
LeeParrishMSFT Jan 28, 2021
20c1173
Added recognizer libraries
LeeParrishMSFT Jan 29, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
4 changes: 3 additions & 1 deletion etc/bot-checkstyle.xml
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,9 @@
<!-- See http://checkstyle.sourceforge.net/config_coding.html -->
<module name="EmptyStatement"/>
<module name="EqualsHashCode"/>
<module name="HiddenField"/>
<module name="HiddenField">
<property name="tokens" value="VARIABLE_DEF"/>
</module>
<module name="IllegalInstantiation"/>
<module name="InnerAssignment"/>
<module name="MagicNumber"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
import org.apache.commons.lang3.NotImplementedException;
import org.apache.commons.lang3.StringUtils;

/**
* Represents a bot adapter that can connect a bot to a service endpoint. This
Expand Down Expand Up @@ -194,6 +195,10 @@ protected CompletableFuture<Void> runPipeline(

// Call any registered Middleware Components looking for ReceiveActivity()
if (context.getActivity() != null) {
if (!StringUtils.isEmpty(context.getActivity().getLocale())) {
context.setLocale(context.getActivity().getLocale());
}

return middlewareSet.receiveActivityWithStatus(context, callback)
.exceptionally(exception -> {
if (onTurnError != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,9 @@ public CompletableFuture<Void> saveChanges(TurnContext turnContext, boolean forc
*/
public CompletableFuture<Void> clearState(TurnContext turnContext) {
if (turnContext == null) {
throw new IllegalArgumentException("turnContext cannot be null");
return Async.completeExceptionally(new IllegalArgumentException(
"TurnContext cannot be null."
));
}

turnContext.getTurnState().replace(contextServiceKey, new CachedBotState());
Expand All @@ -190,7 +192,9 @@ public CompletableFuture<Void> clearState(TurnContext turnContext) {
*/
public CompletableFuture<Void> delete(TurnContext turnContext) {
if (turnContext == null) {
throw new IllegalArgumentException("turnContext cannot be null");
return Async.completeExceptionally(new IllegalArgumentException(
"TurnContext cannot be null."
));
}

String storageKey = getStorageKey(turnContext);
Expand Down Expand Up @@ -220,6 +224,20 @@ public JsonNode get(TurnContext turnContext) {
return new ObjectMapper().valueToTree(cachedState.state);
}

/**
* Gets the cached bot state instance that wraps the raw cached data for this BotState from the turn context.
*
* @param turnContext The context object for this turn.
* @return The cached bot state instance.
*/
public CachedBotState getCachedState(TurnContext turnContext) {
if (turnContext == null) {
throw new IllegalArgumentException("turnContext cannot be null");
}

return turnContext.getTurnState().get(contextServiceKey);
}

/**
* When overridden in a derived class, gets the key to use when reading and
* writing state to and from storage.
Expand Down Expand Up @@ -274,11 +292,15 @@ protected CompletableFuture<Void> deletePropertyValue(
String propertyName
) {
if (turnContext == null) {
throw new IllegalArgumentException("turnContext cannot be null");
return Async.completeExceptionally(new IllegalArgumentException(
"TurnContext cannot be null."
));
}

if (StringUtils.isEmpty(propertyName)) {
throw new IllegalArgumentException("propertyName cannot be empty");
return Async.completeExceptionally(new IllegalArgumentException(
"propertyName cannot be empty"
));
}

CachedBotState cachedState = turnContext.getTurnState().get(contextServiceKey);
Expand All @@ -300,11 +322,15 @@ protected CompletableFuture<Void> setPropertyValue(
Object value
) {
if (turnContext == null) {
throw new IllegalArgumentException("turnContext cannot be null");
return Async.completeExceptionally(new IllegalArgumentException(
"turnContext cannot be null."
));
}

if (StringUtils.isEmpty(propertyName)) {
throw new IllegalArgumentException("propertyName cannot be empty");
return Async.completeExceptionally(new IllegalArgumentException(
"propertyName cannot be empty"
));
}

CachedBotState cachedState = turnContext.getTurnState().get(contextServiceKey);
Expand All @@ -315,7 +341,7 @@ protected CompletableFuture<Void> setPropertyValue(
/**
* Internal cached bot state.
*/
private static class CachedBotState {
public static class CachedBotState {
/**
* In memory cache of BotState properties.
*/
Expand Down Expand Up @@ -348,26 +374,46 @@ private static class CachedBotState {
hash = computeHash(withState);
}

Map<String, Object> getState() {
/**
* @return The Map of key value pairs which are the state.
*/
public Map<String, Object> getState() {
return state;
}

/**
* @param withState The key value pairs to set the state with.
*/
void setState(Map<String, Object> withState) {
state = withState;
}

/**
* @return The hash value for the state.
*/
String getHash() {
return hash;
}

/**
* @param witHashCode Set the hash value.
*/
void setHash(String witHashCode) {
hash = witHashCode;
}

/**
*
* @return Boolean to tell if the state has changed.
*/
boolean isChanged() {
return !StringUtils.equals(hash, computeHash(state));
}

/**
* @param obj The object to compute the hash for.
* @return The computed has for the provided object.
*/
String computeHash(Object obj) {
if (obj == null) {
return "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,18 @@ void trackException(
*/
void trackTrace(String message, Severity severityLevel, Map<String, String> properties);

/**
* Log a DialogView using the TrackPageView method on the IBotTelemetryClient if
* IBotPageViewTelemetryClient has been implemented. Alternatively log the information out via
* TrackTrace.
*
* @param dialogName The name of the dialog to log the entry / start for.
* @param properties Named string values you can use to search and classify
* events.
* @param metrics Measurements associated with this event.
*/
void trackDialogView(String dialogName, Map<String, String> properties, Map<String, Double> metrics);

/**
* Flushes the in-memory buffer and any metrics being pre-aggregated.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.microsoft.bot.builder;

import java.util.concurrent.ConcurrentHashMap;

/**
* ComponentRegistration is a signature class for discovering assets from components.
*/
@SuppressWarnings("checkstyle:HideUtilityClassConstructor")
public class ComponentRegistration {

private static final ConcurrentHashMap<Class<?>, ComponentRegistration> COMPONENTS =
new ConcurrentHashMap<Class<?>, ComponentRegistration>();

/**
* Add a component which implements registration methods.
*
* @param componentRegistration The component to add to the registration.
*/
public static void add(ComponentRegistration componentRegistration) {
COMPONENTS.put(componentRegistration.getClass(), componentRegistration);
}

/**
* Gets list of all ComponentRegistration objects registered.
*
* @return A array of ComponentRegistration objects.
*/
public static Iterable<ComponentRegistration> getComponents() {
return COMPONENTS.values();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,24 @@ public DelegatingTurnContext(TurnContext withTurnContext) {
innerTurnContext = withTurnContext;
}

/**
* Gets the locale on this context object.
* @return The string of locale on this context object.
*/
@Override
public String getLocale() {
return innerTurnContext.getLocale();
}

/**
* Set the locale on this context object.
* @param withLocale The string of locale on this context object.
*/
@Override
public void setLocale(String withLocale) {
innerTurnContext.setLocale(withLocale);
}

/**
* Gets the inner context's activity.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.microsoft.bot.builder;

import java.util.concurrent.CompletableFuture;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,9 @@ public void trackTrace(String message, Severity severityLevel, Map<String, Strin
public void flush() {

}

@Override
public void trackDialogView(String dialogName, Map<String, String> properties, Map<String, Double> metrics) {

}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.microsoft.bot.builder;

import java.util.concurrent.CompletableFuture;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.microsoft.bot.builder;

import java.util.concurrent.CompletableFuture;

import com.nimbusds.oauth2.sdk.util.StringUtils;

/**
* Middleware for adding an object to or registering a service with the current
* turn context.
*
* @param <T> The typeof service to add.
*/
public class RegisterClassMiddleware<T> implements Middleware {
private String key;

/**
* Initializes a new instance of the RegisterClassMiddleware class.
*
* @param service The Service to register.
*/
public RegisterClassMiddleware(T service) {
this.service = service;
}

/**
* Initializes a new instance of the RegisterClassMiddleware class.
*
* @param service The Service to register.
* @param key optional key for service object in turn state. Default is name
* of service.
*/
public RegisterClassMiddleware(T service, String key) {
this.service = service;
this.key = key;
}

private T service;

/**
* Gets the Service.
*
* @return The Service.
*/
public T getService() {
return service;
}

/**
* Sets the Service.
*
* @param withService The value to set the Service to.
*/
public void setService(T withService) {
this.service = withService;
}

@Override
/**
* Adds the associated object or service to the current turn context.
* @param turnContext The context object for this turn.
* @param next The delegate to call to continue the bot middleware pipeline.
*/
public CompletableFuture<Void> onTurn(TurnContext turnContext, NextDelegate next) {
if (!StringUtils.isBlank(key)) {
turnContext.getTurnState().add(key, service);
} else {
turnContext.getTurnState().add(service);
}
return next.next();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,31 @@ static CompletableFuture<ResourceResponse> traceActivity(
String valueType,
String label
) {

return turnContext
.sendActivity(turnContext.getActivity().createTrace(name, value, valueType, label));
}

/**
* @param turnContext The turnContext.
* @param name The name of the activity.
* @return A future with the ResourceReponse.
*/
static CompletableFuture<ResourceResponse> traceActivity(TurnContext turnContext, String name) {
return traceActivity(turnContext, name, null, null, null);
}

/**
* Gets the locale on this context object.
* @return The string of locale on this context object.
*/
String getLocale();

/**
* Set the locale on this context object.
* @param withLocale The string of locale on this context object.
*/
void setLocale(String withLocale);

/**
* Gets the bot adapter that created this context object.
*
Expand Down
Loading