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 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));
        }