public virtual TransducerGraph.Arc ProcessArc(TransducerGraph.Arc a)
 {
     a = new TransducerGraph.Arc(a);
     a.SetInput(Generics.NewPair(a.GetInput(), a.GetOutput()));
     a.SetOutput(null);
     return(a);
 }
        /// <returns>
        /// true if and only if it added Arc a to the graph.
        /// determinism.
        /// </returns>
        protected internal virtual bool AddArc(TransducerGraph.Arc a)
        {
            object source = a.GetSourceNode();
            object target = a.GetTargetNode();
            object input  = a.GetInput();

            if (source == null || target == null || input == null)
            {
                return(false);
            }
            // add to data structures
            if (arcs.Contains(a))
            {
                return(false);
            }
            // it's new, so add to the rest of the data structures
            // add to source and input map
            Pair p = Generics.NewPair(source, input);

            if (arcsBySourceAndInput.Contains(p) && checkDeterminism)
            {
                throw new Exception("Creating nondeterminism while inserting arc " + a + " because it already has arc " + arcsBySourceAndInput[p] + checkDeterminism);
            }
            arcsBySourceAndInput[p] = a;
            Maps.PutIntoValueHashSet(arcsBySource, source, a);
            p = Generics.NewPair(target, input);
            Maps.PutIntoValueHashSet(arcsByTargetAndInput, p, a);
            Maps.PutIntoValueHashSet(arcsByTarget, target, a);
            Maps.PutIntoValueHashSet(arcsByInput, input, a);
            // add to arcs
            arcs.Add(a);
            return(true);
        }
        public virtual bool RemoveArc(TransducerGraph.Arc a)
        {
            object source = a.GetSourceNode();
            object target = a.GetTargetNode();
            object input  = a.GetInput();

            // remove from arcs
            if (!arcs.Remove(a))
            {
                return(false);
            }
            // remove from arcsBySourceAndInput
            Pair p = Generics.NewPair(source, input);

            if (!arcsBySourceAndInput.Contains(p))
            {
                return(false);
            }
            Sharpen.Collections.Remove(arcsBySourceAndInput, p);
            // remove from arcsBySource
            ICollection <TransducerGraph.Arc> s = arcsBySource[source];

            if (s == null)
            {
                return(false);
            }
            if (!s.Remove(a))
            {
                return(false);
            }
            // remove from arcsByTargetAndInput
            p = Generics.NewPair(target, input);
            s = arcsByTargetAndInput[p];
            if (s == null)
            {
                return(false);
            }
            if (!s.Remove(a))
            {
                return(false);
            }
            // remove from arcsByTarget
            s = arcsByTarget[target];
            if (s == null)
            {
                return(false);
            }
            s = arcsByInput[input];
            if (s == null)
            {
                return(false);
            }
            if (!s.Remove(a))
            {
                return(false);
            }
            return(true);
        }
            public virtual TransducerGraph.Arc ProcessArc(TransducerGraph.Arc a)
            {
                a = new TransducerGraph.Arc(a);
                Pair p = (Pair)a.GetInput();

                a.SetInput(p.first);
                a.SetOutput(p.second);
                return(a);
            }
        /// <summary>for testing only.</summary>
        /// <remarks>for testing only. doubles combined by addition.</remarks>
        public virtual IList SampleUniformPathFromGraph()
        {
            IList  list     = new ArrayList();
            object node     = this.GetStartNode();
            ISet   endNodes = this.GetEndNodes();

            while (!endNodes.Contains(node))
            {
                IList <TransducerGraph.Arc> arcs = new List <TransducerGraph.Arc>(this.GetArcsBySource(node));
                TransducerGraph.Arc         arc  = arcs[r.NextInt(arcs.Count)];
                list.Add(arc.GetInput());
                node = arc.GetTargetNode();
            }
            return(list);
        }
示例#6
0
        protected internal virtual void AddSplits(FastExactAutomatonMinimizer.Block block)
        {
            IDictionary symbolToTarget = new Hashtable();

            foreach (object member in block.GetMembers())
            {
                foreach (object o in GetInverseArcs(member))
                {
                    TransducerGraph.Arc arc = (TransducerGraph.Arc)o;
                    object symbol           = arc.GetInput();
                    object target           = arc.GetTargetNode();
                    Maps.PutIntoValueArrayList(symbolToTarget, symbol, target);
                }
            }
            foreach (object symbol_1 in symbolToTarget.Keys)
            {
                AddSplit(new FastExactAutomatonMinimizer.Split((IList)symbolToTarget[symbol_1], symbol_1, block));
            }
        }
示例#7
0
        /// <summary>Takes time linear in number of arcs.</summary>
        public static ClassicCounter ComputeLambda(TransducerGraph graph)
        {
            ArrayList      queue  = new ArrayList();
            ClassicCounter lambda = new ClassicCounter();
            ClassicCounter length = new ClassicCounter();
            IDictionary    first  = new Hashtable();
            ISet           nodes  = graph.GetNodes();

            foreach (object node in nodes)
            {
                lambda.SetCount(node, 0);
                length.SetCount(node, double.PositiveInfinity);
            }
            ISet endNodes = graph.GetEndNodes();

            foreach (object o in endNodes)
            {
                lambda.SetCount(o, 0);
                length.SetCount(o, 0);
                queue.AddLast(o);
            }
            // Breadth first search
            // get the first node from the queue
            object node_1 = null;

            try
            {
                node_1 = queue.RemoveFirst();
            }
            catch (NoSuchElementException)
            {
            }
            while (node_1 != null)
            {
                double oldLen = length.GetCount(node_1);
                ISet   arcs   = graph.GetArcsByTarget(node_1);
                if (arcs != null)
                {
                    foreach (object arc1 in arcs)
                    {
                        TransducerGraph.Arc arc = (TransducerGraph.Arc)arc1;
                        object      newNode     = arc.GetSourceNode();
                        IComparable a           = (IComparable)arc.GetInput();
                        double      k           = ((double)arc.GetOutput());
                        double      newLen      = length.GetCount(newNode);
                        if (newLen == double.PositiveInfinity)
                        {
                            // we are discovering this
                            queue.AddLast(newNode);
                        }
                        IComparable f = (IComparable)first[newNode];
                        if (newLen == double.PositiveInfinity || (newLen == oldLen + 1 && a.CompareTo(f) < 0))
                        {
                            // f can't be null, since we have a newLen
                            // we do this to this to newNode when we have new info, possibly many times
                            first[newNode] = a;
                            // ejecting old one if necessary
                            length.SetCount(newNode, oldLen + 1);
                            // this may already be the case
                            lambda.SetCount(newNode, k + lambda.GetCount(node_1));
                        }
                    }
                }
                // get a new node from the queue
                node_1 = null;
                try
                {
                    node_1 = queue.RemoveFirst();
                }
                catch (NoSuchElementException)
                {
                }
            }
            return(lambda);
        }
 protected internal Arc(TransducerGraph.Arc <NODE, IN, OUT> a)
     : this(a.GetSourceNode(), a.GetTargetNode(), a.GetInput(), a.GetOutput())
 {
 }