Exemple #1
0
        /// <summary>
        /// Builds the action from a contextual action.
        /// </summary>
        /// <param name="action">The contextual action.</param>
        /// <param name="intentName">The LUIS intent name of the parent action.</param>
        /// <returns>The parent action.</returns>
        public static ILuisAction BuildContextForContextualAction(ILuisAction action, out string intentName)
        {
            if (action == null)
            {
                throw new ArgumentNullException(nameof(action));
            }

            if (!LuisActionResolver.IsContextualAction(action))
            {
                intentName = null;
                return(null);
            }

            var contextType = action
                              .GetType()
                              .GetInterfaces()
                              .First(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(ILuisContextualAction <>))
                              .GetGenericArguments()[0];

            var result = Activator.CreateInstance(contextType) as ILuisAction;

            intentName = LuisActionResolver.GetActionDefinition(result).IntentName;

            var isContextual = false;

            LuisActionResolver.IsValidContextualAction(action, result, out isContextual);

            return(result);
        }
Exemple #2
0
        /// <summary>
        /// If the action is contextual and related to the current context action, set the contextual action to the current context.
        /// </summary>
        /// <param name="action">The contextual action to validate.</param>
        /// <param name="context">The current action context.</param>
        /// <param name="isContextual">Indicates if the contextual action is related to the current action.</param>
        public static bool IsValidContextualAction(ILuisAction action, ILuisAction context, out bool isContextual)
        {
            if (action == null)
            {
                throw new ArgumentNullException(nameof(action));
            }

            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            isContextual = LuisActionResolver.IsContextualAction(action);

            if (!isContextual)
            {
                return(false);
            }

            var validContextualType = typeof(ILuisContextualAction <>).MakeGenericType(context.GetType());

            if (validContextualType.IsAssignableFrom(action.GetType()))
            {
                var prop = validContextualType.GetProperty("Context", BindingFlags.Public | BindingFlags.Instance);
                prop.SetValue(action, context);

                return(true);
            }

            return(false);
        }
Exemple #3
0
        /// <summary>
        /// Indicates if the action can be started, either is not a contextual action or it can create the contextual action directly.
        /// </summary>
        /// <param name="action">The action to validate.</param>
        /// <param name="actionDefinition">The action definition.</param>
        public static bool CanStartWithNoContextAction(ILuisAction action, out LuisActionBindingAttribute actionDefinition)
        {
            if (action == null)
            {
                throw new ArgumentNullException(nameof(action));
            }

            if (!LuisActionResolver.IsContextualAction(action))
            {
                actionDefinition = null;
                return(true);
            }

            actionDefinition = LuisActionResolver.GetActionDefinition(action);

            return(actionDefinition.CanExecuteWithNoContext);
        }
Exemple #4
0
        /// <summary>
        /// If the action is contextual and related to the current context action, set the contextual action to the current context.
        /// </summary>
        /// <param name="action">The contextual action to validate.</param>
        /// <param name="context">The current action context.</param>
        /// <param name="isContextual">Indicates if the contextual action is related to the current action.</param>
        public static bool UpdateIfValidContextualAction(ILuisAction action, ILuisAction context, out bool isContextual)
        {
            if (action == null)
            {
                throw new ArgumentNullException(nameof(action));
            }

            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            isContextual = false;

            if (!LuisActionResolver.IsContextualAction(context))
            {
                return(true);
            }

            var prop          = context.GetType().GetProperty("Context", BindingFlags.Public | BindingFlags.Instance);
            var actionContext = prop.GetValue(context) as ILuisAction;

            return(LuisActionResolver.IsValidContextualAction(action, actionContext, out isContextual));
        }