/// <summary>For testing only.</summary>
        /// <remarks>For testing only.  Doubles combined by multiplication.</remarks>
        private bool CanAddPath(IList path)
        {
            object node = this.GetStartNode();

            for (int j = 0; j < path.Count - 1; j++)
            {
                object input            = path[j];
                TransducerGraph.Arc arc = this.GetArcBySourceAndInput(node, input);
                // next input in path
                if (arc == null)
                {
                    return(true);
                }
                node = arc.GetTargetNode();
            }
            object input_1 = path[path.Count - 1];

            // last element
            TransducerGraph.Arc arc_1 = this.GetArcBySourceAndInput(node, input_1);
            // next input in path
            if (arc_1 == null)
            {
                return(true);
            }
            else
            {
                return(GetEndNodes().Contains(arc_1.GetTargetNode()));
            }
        }
 public virtual TransducerGraph.Arc ProcessArc(TransducerGraph.Arc a)
 {
     a = new TransducerGraph.Arc(a);
     a.SetSourceNode(nodeProcessor.ProcessNode(a.GetSourceNode()));
     a.SetTargetNode(nodeProcessor.ProcessNode(a.GetTargetNode()));
     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);
        }
        /// <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);
        }
        /// <summary>For testing only.</summary>
        /// <remarks>For testing only.  Doubles combined by addition.</remarks>
        public virtual double GetOutputOfPathInGraph(IList path)
        {
            double score = 0.0;
            object node  = GetStartNode();

            foreach (object input in path)
            {
                TransducerGraph.Arc arc = GetArcBySourceAndInput(node, input);
                // next input in path
                if (arc == null)
                {
                    System.Console.Out.WriteLine(" NOT ACCEPTED :" + path);
                    return(double.NegativeInfinity);
                }
                score += ((double)arc.GetOutput());
                node   = arc.GetTargetNode();
            }
            return(score);
        }
Exemplo n.º 7
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));
            }
        }
 protected internal Arc(TransducerGraph.Arc <NODE, IN, OUT> a)
     : this(a.GetSourceNode(), a.GetTargetNode(), a.GetInput(), a.GetOutput())
 {
 }