示例#1
0
        /// <summary>
        /// Gets the next available memory address for each scope and <see cref="SemanticCubeUtilities.DataTypes"/> value.
        /// Called by <see cref="QuadrupleManager"/> to allot a space in memory to a value.
        /// </summary>
        /// <param name="scope"></param>
        /// <param name="type"></param>
        /// <returns>A memory address.</returns>
        public static int GetNextAvailable(MemoryScope scope, SemanticCubeUtilities.DataTypes type)
        {
            switch (scope)
            {
            case MemoryScope.global:
                switch (type)
                {
                case SemanticCubeUtilities.DataTypes.number:
                    if (currentGlobalIntAddress <= highestGlobalIntAddress)
                    {
                        return(currentGlobalIntAddress);
                    }
                    break;

                case SemanticCubeUtilities.DataTypes.text:
                    if (currentGlobalStringAddress <= highestGlobalStringAddress)
                    {
                        return(currentGlobalStringAddress);
                    }
                    break;

                case SemanticCubeUtilities.DataTypes.boolean:
                    if (currentGlobalBoolAddress <= highestGlobalBoolAddress)
                    {
                        return(currentGlobalBoolAddress);
                    }
                    break;

                default:
                    break;
                }
                break;

            case MemoryScope.temporary:
                switch (type)
                {
                case SemanticCubeUtilities.DataTypes.number:
                    if (currentTempIntAddress <= highestTempIntAddress)
                    {
                        return(currentTempIntAddress);
                    }
                    break;

                case SemanticCubeUtilities.DataTypes.text:
                    if (currentTempStringAddress <= highestTempStringAddress)
                    {
                        return(currentTempStringAddress);
                    }
                    break;

                case SemanticCubeUtilities.DataTypes.boolean:
                    if (currentTempBoolAddress <= highestTempBoolAddress)
                    {
                        return(currentTempBoolAddress);
                    }
                    break;

                default:
                    break;
                }
                break;

            case MemoryScope.constant:
                switch (type)
                {
                case SemanticCubeUtilities.DataTypes.number:
                    if (currentConstantIntAddress <= highestConstantIntAddress)
                    {
                        return(currentConstantIntAddress);
                    }
                    break;

                case SemanticCubeUtilities.DataTypes.text:
                    if (currentConstantStringAddress <= highestConstantStringAddress)
                    {
                        return(currentConstantStringAddress);
                    }
                    break;

                case SemanticCubeUtilities.DataTypes.boolean:
                    if (currentConstantBoolAddress <= highestConstantBoolAddress)
                    {
                        return(currentConstantBoolAddress);
                    }
                    break;

                default:
                    break;
                }
                break;

            case MemoryScope.local:
                if (currentLocalAddress <= highestLocalAddress)
                {
                    return(currentLocalAddress);
                }
                break;

            default:
                break;
            }
            return(-1);
        }
示例#2
0
        /// <summary>
        /// Runs dialog system in the context of an ITurnContext.
        /// </summary>
        /// <param name="context">turn context.</param>
        /// <param name="state">stored state.</param>
        /// <param name="cancellationToken">cancelation token.</param>
        /// <returns>result of the running the logic against the activity.</returns>
        public async Task <DialogManagerResult> OnTurnAsync(ITurnContext context, PersistedState state = null, CancellationToken cancellationToken = default(CancellationToken))
        {
            var saveState = false;
            var keys      = GetKeys(context);
            var storage   = context.TurnState.Get <IStorage>();

            if (state == null)
            {
                if (storage == null)
                {
                    throw new Exception("DialogManager: unable to load the bots state.Bot.storage not assigned.");
                }

                state = await LoadStateAsync(storage, keys).ConfigureAwait(false);

                saveState = true;
            }

            // Clone state to preserve original state
            var newState = ObjectPath.Clone(state);

            // Check for expired conversation
            var now = DateTime.UtcNow;

            if (this.ExpireAfter.HasValue && newState.ConversationState.ContainsKey(LASTACCESS))
            {
                var lastAccess = DateTime.Parse(newState.ConversationState[LASTACCESS] as string);
                if ((DateTime.UtcNow - lastAccess) >= TimeSpan.FromMilliseconds((double)this.ExpireAfter))
                {
                    // Clear conversation state
                    state.ConversationState       = new Dictionary <string, object>();
                    state.ConversationState[ETAG] = newState.ConversationState[ETAG];
                }
            }

            newState.ConversationState[LASTACCESS] = DateTime.UtcNow.ToString("u");

            // Ensure dialog stack populated
            DialogState dialogState;

            if (!newState.ConversationState.ContainsKey(DIALOGS))
            {
                dialogState = new DialogState();
                newState.ConversationState[DIALOGS] = dialogState;
            }
            else
            {
                dialogState = (DialogState)newState.ConversationState[DIALOGS];
            }

            var namedScopes = MemoryScope.GetScopesMemory(context);

            namedScopes[ScopePath.USER]         = newState.UserState;
            namedScopes[ScopePath.CONVERSATION] = newState.ConversationState;
            namedScopes[ScopePath.TURN]         = new Dictionary <string, object>(StringComparer.InvariantCultureIgnoreCase);

            // Create DialogContext
            var dc = new DialogContext(
                this.dialogSet,
                context,
                dialogState);

            DialogTurnResult turnResult = null;

            if (dc.ActiveDialog == null)
            {
                // start root dialog
                turnResult = await dc.BeginDialogAsync(this.rootDialogId, cancellationToken : cancellationToken).ConfigureAwait(false);
            }
            else
            {
                // Continue execution
                // - This will apply any queued up interruptions and execute the current/next step(s).
                turnResult = await dc.ContinueDialogAsync(cancellationToken : cancellationToken).ConfigureAwait(false);

                if (turnResult.Status == DialogTurnStatus.Empty)
                {
                    // restart root dialog
                    turnResult = await dc.BeginDialogAsync(this.rootDialogId, cancellationToken : cancellationToken).ConfigureAwait(false);
                }
            }

            // send trace of memory
            await dc.Context.SendActivityAsync((Activity)Activity.CreateTraceActivity("BotState", "https://www.botframework.com/schemas/botState", dc.State.GetMemorySnapshot(), "Bot State")).ConfigureAwait(false);

            // Save state if loaded from storage
            if (saveState)
            {
                await DialogManager.SaveStateAsync(storage, keys : keys, newState : newState, oldState : state, eTag : "*").ConfigureAwait(false);

                return(new DialogManagerResult()
                {
                    TurnResult = turnResult
                });
            }
            else
            {
                return(new DialogManagerResult()
                {
                    TurnResult = turnResult, NewState = newState
                });
            }
        }
示例#3
0
 private bool CheckParameter(KernelParameter param, string name, bool isArray, int id, DataVectorTypes type, MemoryScope scope)
 {
     return(param.IsArray == isArray &&
            param.DataType == type &&
            param.Id == id &&
            param.MemScope == scope &&
            param.Name == name);
 }
        /// <summary>
        /// Runs dialog system in the context of an ITurnContext.
        /// </summary>
        /// <param name="context">turn context.</param>
        /// <param name="cancellationToken">cancelation token.</param>
        /// <returns>result of the running the logic against the activity.</returns>
        public async Task <DialogManagerResult> OnTurnAsync(ITurnContext context, CancellationToken cancellationToken = default(CancellationToken))
        {
            ConversationState conversationState = context.TurnState.Get <ConversationState>() ?? throw new ArgumentNullException($"{nameof(ConversationState)} is not found in the turn context. Have you called adapter.UseState() with a configured ConversationState object?");
            UserState         userState         = context.TurnState.Get <UserState>() ?? throw new ArgumentNullException($"{nameof(UserState)} is not found in the turn context. Have you called adapter.UseState() with a configured UserState object?");

            // create property accessors
            var lastAccessProperty = conversationState.CreateProperty <DateTime>(LASTACCESS);
            var dialogsProperty    = conversationState.CreateProperty <DialogState>(DIALOGS);
            var userScope          = userState.CreateProperty <object>($"{ScopePath.USER}{nameof(MemoryScope)}");
            var conversationScope  = conversationState.CreateProperty <object>($"{ScopePath.CONVERSATION}{nameof(MemoryScope)}");

            var lastAccess = await lastAccessProperty.GetAsync(context, () => DateTime.UtcNow, cancellationToken : cancellationToken).ConfigureAwait(false);

            // Check for expired conversation
            var now = DateTime.UtcNow;

            if (this.ExpireAfter.HasValue && (DateTime.UtcNow - lastAccess) >= TimeSpan.FromMilliseconds((double)this.ExpireAfter))
            {
                // Clear conversation state
                await conversationState.ClearStateAsync(context, cancellationToken : cancellationToken).ConfigureAwait(false);
            }

            lastAccess = DateTime.UtcNow;
            await lastAccessProperty.SetAsync(context, lastAccess, cancellationToken : cancellationToken).ConfigureAwait(false);

            // get dialog stack
            DialogState dialogState = await dialogsProperty.GetAsync(context, () => new DialogState(), cancellationToken : cancellationToken).ConfigureAwait(false);

            var namedScopes = MemoryScope.GetScopesMemory(context);

            namedScopes[ScopePath.USER] = await userScope.GetAsync(context, () => new Dictionary <string, object>(StringComparer.InvariantCultureIgnoreCase), cancellationToken : cancellationToken).ConfigureAwait(false);

            namedScopes[ScopePath.CONVERSATION] = await conversationScope.GetAsync(context, () => new Dictionary <string, object>(StringComparer.InvariantCultureIgnoreCase), cancellationToken : cancellationToken).ConfigureAwait(false);

            namedScopes[ScopePath.TURN] = new Dictionary <string, object>(StringComparer.InvariantCultureIgnoreCase);

            // Create DialogContext
            var dc = new DialogContext(this.dialogSet, context, dialogState);

            DialogTurnResult turnResult = null;

            if (dc.ActiveDialog == null)
            {
                // start root dialog
                turnResult = await dc.BeginDialogAsync(this.rootDialogId, cancellationToken : cancellationToken).ConfigureAwait(false);
            }
            else
            {
                // Continue execution
                // - This will apply any queued up interruptions and execute the current/next step(s).
                turnResult = await dc.ContinueDialogAsync(cancellationToken : cancellationToken).ConfigureAwait(false);

                if (turnResult.Status == DialogTurnStatus.Empty)
                {
                    // restart root dialog
                    turnResult = await dc.BeginDialogAsync(this.rootDialogId, cancellationToken : cancellationToken).ConfigureAwait(false);
                }
            }

            // send trace of memory
            await dc.Context.SendActivityAsync((Activity)Activity.CreateTraceActivity("BotState", "https://www.botframework.com/schemas/botState", dc.State.GetMemorySnapshot(), "Bot State")).ConfigureAwait(false);

            return(new DialogManagerResult()
            {
                TurnResult = turnResult
            });
        }