Skip to content

Commit

Permalink
refactor provider hierarchy (refs xatkit-bot-platform/xatkit-runtime#221
Browse files Browse the repository at this point in the history
)

ChatPlatform now contains 2 providers that extend the ones from the runtime component:
- ChatIntentProvider that can be extended by providers based on third-party libraries (e.g. Slack)
- JsonWebhookChatIntentProvider that can be extended by providers receiving messages as JSON payloads
  • Loading branch information
gdaniel committed Sep 3, 2019
1 parent bea8594 commit cee7fad
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -1,26 +1,24 @@
package com.xatkit.plugins.chat.platform.io;

import com.xatkit.core.platform.io.RuntimeIntentProvider;
import com.xatkit.core.platform.io.RuntimeEventProvider;
import com.xatkit.core.session.XatkitSession;
import com.xatkit.intent.EventInstance;
import com.xatkit.plugins.chat.ChatUtils;
import com.xatkit.plugins.chat.platform.ChatPlatform;
import org.apache.commons.configuration2.Configuration;

import java.util.Map;

import static fr.inria.atlanmod.commons.Preconditions.checkState;
import static java.util.Objects.nonNull;

/**
* An abstract chat {@link RuntimeIntentProvider}.
* An abstract chat intent provider.
* <p>
* This class is extended by provider of platforms extending the {@link ChatPlatform}, and ensures that all the
* chat-related context variables have been set before triggering actions associated to the computed event.
* This class is designed to be extended by concrete providers relying on third-party libraries to receive chat
* events (e.g. a dedicated library connecting to Slack). To receive chat events from Json webhooks check
* {@link JsonWebhookChatIntentProvider}.
*
* @param <T> the concrete {@link ChatPlatform} that contains this provider
*
* @see JsonWebhookChatIntentProvider
*/
public abstract class ChatIntentProvider<T extends ChatPlatform> extends RuntimeIntentProvider<T> {
public abstract class ChatIntentProvider<T extends ChatPlatform> extends RuntimeEventProvider<T> {

/**
* Constructs a new {@link ChatIntentProvider} with the provided {@code runtimePlatform} and {@code configuration}.
Expand All @@ -41,19 +39,10 @@ public ChatIntentProvider(T runtimePlatform, Configuration configuration) {
*/
@Override
public void sendEventInstance(EventInstance eventInstance, XatkitSession session) {
Map<String, Object> contextVariables =
session.getRuntimeContexts().getContextVariables(ChatUtils.CHAT_CONTEXT_KEY);
checkState(nonNull(contextVariables), "Intent provider %s did not define the context %s",
this.getClass().getSimpleName(), ChatUtils.CHAT_CONTEXT_KEY);
checkState(nonNull(contextVariables.get(ChatUtils.CHAT_CHANNEL_CONTEXT_KEY)), "Intent provider %s did not " +
"define the context variable %s.%s", this.getClass().getSimpleName(),
ChatUtils.CHAT_CONTEXT_KEY,
ChatUtils.CHAT_CHANNEL_CONTEXT_KEY);
checkState(nonNull(contextVariables.get(ChatUtils.CHAT_USERNAME_CONTEXT_KEY)), "Intent provider %s did not " +
"define the context variable %s.%s", this.getClass().getSimpleName(),
ChatUtils.CHAT_USERNAME_CONTEXT_KEY);
checkState(nonNull(contextVariables.get(ChatUtils.CHAT_RAW_MESSAGE_CONTEXT_KEY)), "Intent provider %s did not" +
" define the context variable %s.%s", this.getClass().getSimpleName());
/*
* TODO should we set this override as final?
*/
ChatProviderUtils.checkSession(session);
super.sendEventInstance(eventInstance, session);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.xatkit.plugins.chat.platform.io;

import com.xatkit.core.session.XatkitSession;
import com.xatkit.plugins.chat.ChatUtils;

import java.util.Map;

import static fr.inria.atlanmod.commons.Preconditions.checkState;
import static java.util.Objects.nonNull;

/**
* Provides utility methods to check session contents.
*/
public class ChatProviderUtils {

/**
* Checks that the context variables defined in {@link ChatUtils} have been set in the given {@code session}.
* <p>
* This method is called when sending the event to the {@link com.xatkit.core.XatkitCore} component, and ensures
* that each concrete implementation of the {@link com.xatkit.plugins.chat.platform.ChatPlatform} sets the same
* context variables.
*
* @param session the {@link XatkitSession} to check
* @throws IllegalStateException if a context or context variable has not been set in the session
*/
protected static void checkSession(XatkitSession session) {
Map<String, Object> contextVariables =
session.getRuntimeContexts().getContextVariables(ChatUtils.CHAT_CONTEXT_KEY);
checkState(nonNull(contextVariables), "The context %s has not been defined by the intent provider",
ChatUtils.CHAT_CONTEXT_KEY);
checkState(nonNull(contextVariables.get(ChatUtils.CHAT_CHANNEL_CONTEXT_KEY)), "The context variable %s" +
".%s has not been defined by the intent provider", ChatUtils.CHAT_CONTEXT_KEY,
ChatUtils.CHAT_CHANNEL_CONTEXT_KEY);
checkState(nonNull(contextVariables.get(ChatUtils.CHAT_USERNAME_CONTEXT_KEY)), "The context variable %s" +
".%s has not been defined by the intent provider",
ChatUtils.CHAT_CONTEXT_KEY, ChatUtils.CHAT_USERNAME_CONTEXT_KEY);
checkState(nonNull(contextVariables.get(ChatUtils.CHAT_RAW_MESSAGE_CONTEXT_KEY)), "The context variable %s" +
".%s has not been defined by the intent provider", ChatUtils.CHAT_CONTEXT_KEY,
ChatUtils.CHAT_RAW_MESSAGE_CONTEXT_KEY);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.xatkit.plugins.chat.platform.io;

import com.xatkit.core.platform.io.JsonWebhookEventProvider;
import com.xatkit.core.session.XatkitSession;
import com.xatkit.intent.EventInstance;
import com.xatkit.plugins.chat.ChatUtils;
import com.xatkit.plugins.chat.platform.ChatPlatform;
import org.apache.commons.configuration2.Configuration;

/**
* An abstract chat intent provider that extracts intents from received Json payloads.
* <p>
* This class is designed to be extended by concrete providers extracting user intents from received Json payloads.
* The class provides utility methods to navigate the Json payload and the received HTTP headers. To receive chat
* from third-party libraries check and {@link ChatIntentProvider}.
*
* @param <T> the concrete {@link ChatPlatform} that contains this provider
* @see ChatIntentProvider
*/
public abstract class JsonWebhookChatIntentProvider<T extends ChatPlatform> extends JsonWebhookEventProvider<T> {

/**
* Constructs a new {@link JsonWebhookChatIntentProvider} with the provided {@code runtimePlatform} and {@code
* configuration}.
*
* @param runtimePlatform the {@link ChatPlatform} containing this provider
* @param configuration the {@link Configuration} used to initialize this provider
*/
public JsonWebhookChatIntentProvider(T runtimePlatform, Configuration configuration) {
super(runtimePlatform, configuration);
}

/**
* {@inheritDoc}
* <p>
* This method checks that the context variables defined in {@link ChatUtils} have been initialized by the
* concrete provider. This ensures that all the providers extending {@link ChatIntentProvider} set the same set
* of variables and can be used transparently in execution models.
*/
@Override
public void sendEventInstance(EventInstance eventInstance, XatkitSession session) {
/*
* TODO should we set this override as final?
*/
ChatProviderUtils.checkSession(session);
super.sendEventInstance(eventInstance, session);
}
}

0 comments on commit cee7fad

Please sign in to comment.