示例#1
0
        /// <summary>
        /// Get a nondet choice.
        /// This may replay a nondet choice or make (and record) a new nondet choice.
        /// </summary>
        /// <param name="isBoolChoice">If true, a boolean choice; otherwise, an int choice.</param>
        /// <param name="rand">Random</param>
        /// <param name="contract">IContract</param>
        public int MakeOrReplayNondetChoice(bool isBoolChoice, IRandomNumberGenerator rand, IContract contract)
        {
            contract.Assert(
                rand != null || isBoolChoice,
                "A DFS DPOR exploration of int nondeterminstic choices " +
                "is not currently supported because this won't scale.");

            if (NondetChoices == null)
            {
                NondetChoices = new List <NonDetChoice>();
            }


            if (NextNondetChoiceIndex < NondetChoices.Count)
            {
                // Replay:
                NonDetChoice choice = NondetChoices[NextNondetChoiceIndex];
                ++NextNondetChoiceIndex;
                contract.Assert(choice.IsBoolChoice == isBoolChoice);
                return(choice.Choice);
            }

            // Adding a choice.
            contract.Assert(NextNondetChoiceIndex == NondetChoices.Count);
            NonDetChoice ndc = new NonDetChoice
            {
                IsBoolChoice = isBoolChoice,
                Choice       = rand == null ? 0 : (isBoolChoice ? rand.Next(2) : rand.Next())
            };

            NondetChoices.Add(ndc);
            ++NextNondetChoiceIndex;
            return(ndc.Choice);
        }
示例#2
0
        /// <summary>
        /// This method is used in a DFS exploration of nondet choice.
        /// It will pop off bool choices that are 1 until
        /// it reaches a 0 that will then be changed to a 1.
        /// The NextNondetChoiceIndex will be reset ready for replay.
        /// </summary>
        /// <param name="contract">IContract</param>
        /// <returns>false if there are no more nondet choices to explore</returns>
        public bool BacktrackNondetChoices(IContract contract)
        {
            if (NondetChoices == null)
            {
                return(false);
            }

            contract.Assert(NextNondetChoiceIndex == NondetChoices.Count);

            NextNondetChoiceIndex = 0;

            while (NondetChoices.Count > 0)
            {
                NonDetChoice choice = NondetChoices[NondetChoices.Count - 1];
                contract.Assert(choice.IsBoolChoice, "DFS DPOR only supports bool choices.");
                if (choice.Choice == 0)
                {
                    choice.Choice = 1;
                    NondetChoices[NondetChoices.Count - 1] = choice;
                    return(true);
                }
                contract.Assert(choice.Choice == 1, "Unexpected choice value.");
                NondetChoices.RemoveAt(NondetChoices.Count - 1);
            }

            return(false);
        }