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

Commit

Permalink
Lparrish/dialog (#922)
Browse files Browse the repository at this point in the history
* dialogs.choices

* bot-dialogs: periodic push (mostly functional ObjectPath)

* Dialog Updates

* Interim commit and push to protect work in progress

* Pushing to protect work in progress

* Safety Checkin

* Setting up unit tests for Dialogs

* Updates to get VS Code test runner working

* Completed DialogManager and DialogStateManager

* Updates to match namespaces and file structure.

* First unit test for DialogStateManager now working.

* Addin DialogStateManager unit tests and fixes.

* Waterfall dialogs and Prompts

* Fixes and unit tests

* Choice Prompt unit tests

* Additional unit tests and fixes

* Unit tests and fixes.

* ComponentDialog tests and fixes

* DialogContainerTests and fixes to Dialog

* Additional tests and fixes

* Final unit tests

* Correct failing unit test.

* Fixes for merge issues.

* Update to new exception handling pattern.

* Remove uneeded unit test inports.

* Added copyright notices.

* Update DialogContext

* Added recognizer libraries

Co-authored-by: tracyboehrer <[email protected]>
  • Loading branch information
LeeParrishMSFT and tracyboehrer authored Jan 29, 2021
1 parent 1b478b4 commit 5e1ca81
Show file tree
Hide file tree
Showing 688 changed files with 79,326 additions and 498 deletions.
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

0 comments on commit 5e1ca81

Please sign in to comment.