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); }
protected internal virtual ICollection GetInverseImages(FastExactAutomatonMinimizer.Split split) { IList inverseImages = new ArrayList(); object symbol = split.GetSymbol(); FastExactAutomatonMinimizer.Block block = split.GetBlock(); foreach (object member in split.GetMembers()) { if (!block.GetMembers().Contains(member)) { continue; } ICollection arcs = GetInverseArcs(member, symbol); foreach (object arc1 in arcs) { TransducerGraph.Arc arc = (TransducerGraph.Arc)arc1; object source = arc.GetSourceNode(); inverseImages.Add(source); } } return(inverseImages); }
/// <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()) { }