public OutgoingTransition(
     TElementDistribution elementDistribution, Weight weight, WeightedStateSet destinations)
 {
     this.ElementDistribution = elementDistribution;
     this.Weight       = weight;
     this.Destinations = destinations;
 }
            public Transition(TElementDistribution elementDistribution, Weight weight, int destinationStateIndex, byte group = 0)
                : this()
            {
                Argument.CheckIfInRange(destinationStateIndex >= 0, "destinationStateIndex", "A destination state index cannot be negative.");

                this.ElementDistribution   = elementDistribution;
                this.DestinationStateIndex = destinationStateIndex;
                this.Weight = weight;
                this.Group  = group;
            }
            /// <summary>
            /// Merges outgoing transitions with the same destination state.
            /// </summary>
            public void MergeParallelTransitions()
            {
                for (var stateIndex = 0; stateIndex < this.builder.StatesCount; ++stateIndex)
                {
                    var state = this.builder[stateIndex];
                    for (var iterator1 = state.TransitionIterator; iterator1.Ok; iterator1.Next())
                    {
                        var transition1 = iterator1.Value;
                        var iterator2   = iterator1;
                        iterator2.Next();
                        for (; iterator2.Ok; iterator2.Next())
                        {
                            var transition2 = iterator2.Value;
                            if (transition1.DestinationStateIndex == transition2.DestinationStateIndex && transition1.Group == transition2.Group)
                            {
                                var removeTransition2 = false;
                                if (transition1.IsEpsilon && transition2.IsEpsilon)
                                {
                                    transition1.Weight = Weight.Sum(transition1.Weight, transition2.Weight);
                                    iterator1.Value    = transition1;
                                    removeTransition2  = true;
                                }
                                else if (!transition1.IsEpsilon && !transition2.IsEpsilon)
                                {
                                    var newElementDistribution = new TElementDistribution();
                                    if (double.IsInfinity(transition1.Weight.Value) && double.IsInfinity(transition2.Weight.Value))
                                    {
                                        newElementDistribution.SetToSum(
                                            1.0, transition1.ElementDistribution.Value, 1.0, transition2.ElementDistribution.Value);
                                    }
                                    else
                                    {
                                        newElementDistribution.SetToSum(
                                            transition1.Weight.Value, transition1.ElementDistribution.Value, transition2.Weight.Value, transition2.ElementDistribution.Value);
                                    }

                                    transition1.ElementDistribution = newElementDistribution;
                                    transition1.Weight = Weight.Sum(transition1.Weight, transition2.Weight);
                                    iterator1.Value    = transition1;
                                    removeTransition2  = true;
                                }

                                if (removeTransition2)
                                {
                                    iterator2.Remove();
                                }
                            }
                        }
                    }
                }
            }
Exemple #4
0
            /// <summary>
            /// Adds a transition to the current state.
            /// </summary>
            /// <param name="elementDistribution">
            /// The element distribution associated with the transition.
            /// If the value of this parameter is <see langword="null"/>, an epsilon transition will be created.
            /// </param>
            /// <param name="weight">The transition weight.</param>
            /// <param name="destinationState">
            /// The destination state of the added transition.
            /// If the value of this parameter is <see langword="null"/>, a new state will be created.</param>
            /// <param name="group">The group of the added transition.</param>
            /// <returns>The destination state of the added transition.</returns>
            public State AddTransition(TElementDistribution elementDistribution, Weight weight, State destinationState = null, byte group = 0)
            {
                if (destinationState == null)
                {
                    destinationState = this.Owner.AddState();
                }
                else if (!ReferenceEquals(destinationState.Owner, this.Owner))
                {
                    throw new ArgumentException("The given state belongs to another automaton.");
                }

                this.AddTransition(new Transition(elementDistribution, weight, destinationState.Index, group));
                return(destinationState);
            }
            public Transition(Option <TElementDistribution> elementDistribution, Weight weight, int destinationStateIndex, int group = 0)
                : this()
            {
                Argument.CheckIfInRange(destinationStateIndex >= 0, nameof(destinationStateIndex), "A destination state index cannot be negative.");

                this.hasElementDistribution = (short)(elementDistribution.HasValue ? 1 : 0);
                if (elementDistribution.HasValue)
                {
                    this.elementDistribution = elementDistribution.Value;
                }

                this.DestinationStateIndex = destinationStateIndex;
                this.Weight = weight;
                this.Group  = (short)group;
            }
Exemple #6
0
 internal TransitionElement(int destIndex, Weight weight, TElementDistribution distribution)
 {
     this.destIndex    = destIndex;
     this.distribution = distribution;
     this.weight       = weight;
 }
Exemple #7
0
 /// <summary>
 /// Adds a self-transition to the current state.
 /// </summary>
 /// <param name="elementDistribution">
 /// The element distribution associated with the transition.
 /// If the value of this parameter is <see langword="null"/>, an epsilon transition will be created.
 /// </param>
 /// <param name="weight">The transition weight.</param>
 /// <param name="group">The group of the added transition.</param>
 /// <returns>The current state.</returns>
 public State AddSelfTransition(TElementDistribution elementDistribution, Weight weight, byte group = 0)
 {
     return(this.AddTransition(elementDistribution, weight, this, group));
 }
Exemple #8
0
            /// <summary>
            /// Merges outgoing transitions with the same destination state.
            /// </summary>
            public void MergeParallelTransitions()
            {
                for (var stateIndex = 0; stateIndex < this.builder.StatesCount; ++stateIndex)
                {
                    var state = this.builder[stateIndex];
                    for (var iterator1 = state.TransitionIterator; iterator1.Ok; iterator1.Next())
                    {
                        var transition1 = iterator1.Value;
                        var iterator2   = iterator1;
                        iterator2.Next();
                        for (; iterator2.Ok; iterator2.Next())
                        {
                            var transition2      = iterator2.Value;
                            var mergedTransition = TryMergeTransitions(transition1, transition2);
                            if (mergedTransition != null)
                            {
                                iterator1.Value = mergedTransition.Value;
                                transition1     = mergedTransition.Value;
                                iterator2.Remove();
                            }
                        }
                    }
                }

                Transition?TryMergeTransitions(Transition transition1, Transition transition2)
                {
                    if (transition1.DestinationStateIndex != transition2.DestinationStateIndex ||
                        transition1.Group != transition2.Group)
                    {
                        return(null);
                    }

                    if (transition1.IsEpsilon && transition2.IsEpsilon)
                    {
                        transition1.Weight += transition2.Weight;
                        return(transition1);
                    }

                    if (!transition1.IsEpsilon && !transition2.IsEpsilon)
                    {
                        var newElementDistribution = new TElementDistribution();
                        if (transition1.Weight.IsInfinity && transition2.Weight.IsInfinity)
                        {
                            newElementDistribution.SetToSum(
                                1.0,
                                transition1.ElementDistribution.Value,
                                1.0,
                                transition2.ElementDistribution.Value);
                        }
                        else if (transition1.Weight > transition2.Weight)
                        {
                            newElementDistribution.SetToSum(
                                1,
                                transition1.ElementDistribution.Value,
                                (transition2.Weight / transition1.Weight).Value,
                                transition2.ElementDistribution.Value);
                        }
                        else
                        {
                            newElementDistribution.SetToSum(
                                (transition1.Weight / transition2.Weight).Value,
                                transition1.ElementDistribution.Value,
                                1,
                                transition2.ElementDistribution.Value);
                        }

                        return(new Transition(
                                   newElementDistribution,
                                   transition1.Weight + transition2.Weight,
                                   transition1.DestinationStateIndex,
                                   transition1.Group));
                    }

                    return(null);
                }
            }
Exemple #9
0
                public MultiRepresentationWeightFunction <TDictionary> ConstantLog(double logValue, TElementDistribution allowedElements)
                {
                    if (double.IsNegativeInfinity(allowedElements.GetLogAverageOf(allowedElements)))
                    {
                        return(Zero());
                    }
                    var automaton = new TAutomaton();

                    automaton.SetToConstantLog(logValue, allowedElements);
                    return(FromAutomaton(automaton));
                }
Exemple #10
0
                /// <inheritdoc/>
                public MultiRepresentationWeightFunction <TDictionary> ConstantLog(double logValue, TElementDistribution allowedElements)
                {
                    if (double.IsNegativeInfinity(allowedElements.GetLogAverageOf(allowedElements)))
                    {
                        return(Zero());
                    }
                    var automaton = Automaton <TSequence, TElement, TElementDistribution, TSequenceManipulator, TAutomaton> .ConstantLog(logValue, allowedElements);

                    return(FromAutomaton(automaton));
                }