Esempio n. 1
0
 public Alternative(AlternativeType type, string text, bool isAnswer)
 {
     this.Type     = type;
     this.Text     = text;
     this.IsAnswer = isAnswer;
     this.Id       = System.Guid.NewGuid().ToString();
 }
Esempio n. 2
0
        private void PriTraverse()
        {
            try
            {
                while (traverseIndex < alts.Length)
                {
                    AlternativeType type = alts[traverseIndex].Enable(this);
                    if (type == AlternativeType.False)
                    {
                        lock (lockObject)
                        {
                            if (choiceStatus == choiceState.INACTIVE)
                            {
                                return;
                            }

                            traverseIndex++;
                            continue;
                        }
                    }
                    if (type == AlternativeType.Channel)
                    {
                        lock (lockObject)
                        {
                            if (choiceStatus != choiceState.INACTIVE)
                            {
                                selected     = traverseIndex;
                                choiceStatus = choiceState.INACTIVE;
                                alts[traverseIndex].Reserve(choiceThreadId);
                                return;
                            }
                            alts[traverseIndex].Reserve(-1);
                            return;
                        }
                    }
                    lock (lockObject)
                    {
                        if (choiceStatus != choiceState.INACTIVE)
                        {
                            selected     = traverseIndex;
                            choiceStatus = choiceState.INACTIVE;
                            return;
                        }
                        return;
                    }
                }
            }
            catch (PoisonException)
            {
                SignalPoison();
            }
        }
Esempio n. 3
0
            public void SetAlternative <TSample>(DestinationSampler destinationSampler, Dictionary <TSample, int> sample,
                                                 bool isChosenAlternative, AlternativeType alternativeType = AlternativeType.Default, bool skipChoiceProbabilityCalculator = false) where TSample : ISampleItem
            {
                TSample key = (TSample)(object)this;

                Available = true;
                IsChosen  = isChosenAlternative;

                if (sample.ContainsKey(key))
                {
                    sample[key] += 1;
                }
                else
                {
                    int alternativeIndex;

                    switch (alternativeType)
                    {
                    case AlternativeType.Chosen:
                        if (sample.Count == destinationSampler._sampleSize)
                        {
                            sample.Remove(sample.Last().Key);
                        }

                        alternativeIndex = sample.Count;

                        break;

                    case AlternativeType.Usual:
                        //                            alternativeIndex = sample.Count;
                        alternativeIndex = destinationSampler._sampleSize;

                        break;

                    default:
                        alternativeIndex = destinationSampler._alternativeIndex++;

                        break;
                    }

                    sample.Add(key, 1);

                    if (!skipChoiceProbabilityCalculator)
                    {
                        Alternative = destinationSampler._choiceProbabilityCalculator.GetAlternative(alternativeIndex, true, isChosenAlternative);
                    }

                    SetProbability(destinationSampler, skipChoiceProbabilityCalculator);
                }
            }
Esempio n. 4
0
        private void FairTraverse(int end)
        {
            try
            {
                bool wrapped = false;

                while (traverseIndex < end)
                {
                    AlternativeType type = alts[traverseIndex].Enable(this);

                    if (type == AlternativeType.False)
                    {
                        lock (lockObject)
                        {
                            if (choiceStatus == choiceState.INACTIVE)
                            {
                                return;
                            }

                            traverseIndex++;

                            if (traverseIndex == end && !wrapped)
                            {
                                traverseIndex = 0;
                                end           = fairIndex;
                                wrapped       = true;
                            }
                            continue;
                        }
                    }

                    if (type == AlternativeType.Channel)
                    {
                        lock (lockObject)
                        {
                            if (choiceStatus != choiceState.INACTIVE)
                            {
                                selected     = traverseIndex;
                                choiceStatus = choiceState.INACTIVE;
                                alts[traverseIndex].Reserve(choiceThreadId);
                                return;
                            }

                            alts[traverseIndex].Reserve(-1);
                            return;
                        }
                    }
                    lock (lockObject)
                    {
                        if (choiceStatus != choiceState.INACTIVE)
                        {
                            selected     = traverseIndex;
                            choiceStatus = choiceState.INACTIVE;
                            return;
                        }
                        return;
                    }
                }
            }
            catch (PoisonException)
            {
                SignalPoison();
            }
        }