//Return an ArrayList of PatchRegex[] pairs static private ArrayList SplitRight(PatchRegex s, int overlap) { SpecialFunctions.CheckCondition(0 < overlap && overlap < s.CoreLength); //!!!raise error ArrayList rgrg = s.SplitLeft(s.CoreLength - (int)overlap); return(rgrg); }
PatchRegex ReunifyWithFlanking(PatchRegex left, PatchRegex unification, PatchRegex right) { ArrayList rgStringDisjuncts = new ArrayList(); foreach (Disjunct disjunctUnification in unification.DisjunctCollection) { foreach (Disjunct disjunctLeft in left.DisjunctCollection) { AASetSequence newLeftDisjunctOrNull = UnifyWithBeforeOrNull(disjunctLeft.Core.Count, disjunctLeft.FullAsAASetSequence, disjunctUnification.Before); if (newLeftDisjunctOrNull != null) { Debug.Assert(newLeftDisjunctOrNull[0] != AASet.OptionalAny && newLeftDisjunctOrNull[newLeftDisjunctOrNull.Count - 1] != AASet.OptionalAny); // real assert - disjuncts never start or end with AASet.OptionalAny foreach (Disjunct disjunctRight in right.DisjunctCollection) { AASetSequence newRightDisjunctOrNull = UnifyWithAfterOrNull(disjunctRight.Core.Count, disjunctRight.FullAsAASetSequence, disjunctUnification.After); if (newRightDisjunctOrNull != null) { Debug.Assert(newRightDisjunctOrNull[0] != AASet.OptionalAny && newRightDisjunctOrNull[newRightDisjunctOrNull.Count - 1] != AASet.OptionalAny); // real assert - disjuncts never start or end with AASet.OptionalAny Concatenate(newLeftDisjunctOrNull, disjunctUnification, newRightDisjunctOrNull, ref rgStringDisjuncts); } } } } } if (rgStringDisjuncts.Count == 0) { return(null); } else { PatchRegex newRegex = PatchRegex.GetInstance(rgStringDisjuncts, PatchRegexFactory); return(newRegex); } }
/// <summary> /// Example: Suppose "1" is "[TNMSQC]" and the patchPattern is "11T11" and the other pattern is "T1111Z" /// then UnifyOrNull returns "T1T11Z" because that is the most general pattern /// such that "11T11".IsMatch("T1T11Z") is true and "T1111Z".IsMatch("T1T11Z") is true. /// Example: Suppose "1" is "[TNMSQC]" and the patchPattern is "11T11" and the other pattern is "A1111" /// then UnifyOrNull returns null because there is no pattern X such that /// such that "11T11".IsMatch(X) is true and "A1111".IsMatch(X) is true. /// </summary> override public PatchPattern UnifyOrNull(PatchPattern other) { SpecialFunctions.CheckCondition(CoreLength <= other.CoreLength); //!!!raise error SpecialFunctions.CheckCondition(other is PatchRegex); //!!!raise error ArrayList newDisjuncts = new ArrayList(); foreach (Disjunct disjunct in DisjunctCollection) { foreach (Disjunct disjunctOther in (other as PatchRegex).DisjunctCollection) { disjunct.AppendUnifications(disjunctOther, ref newDisjuncts); } } if (newDisjuncts.Count == 0) { string sMiniVaccine = string.Format("{0}{1}{0}", new string('_', (int)Math.Max(MaxLength, other.MaxLength)), other.CoreRealization()); if (IsMatch(sMiniVaccine)) { Debug.WriteLine(string.Format("Warning: two patterns would unify except for flanking regions that could be ignored by requiring that nothing be appending to the component. {0}, {1}", this, other)); } return(null); } else { PatchPattern patchPatternUnified = PatchRegex.GetInstance(newDisjuncts, PatchRegexFactory); string sMiniVaccine = string.Format("{0}{1}{0}", new string('_', (int)Math.Max(MaxLength, other.MaxLength)), patchPatternUnified.CoreRealization()); Debug.Assert(IsMatch(sMiniVaccine)); // real assert Debug.Assert(other.IsMatch(sMiniVaccine)); // real assert return(patchPatternUnified); } }
override public PatchPattern UnifyOnRightOrNull(int overlap, PatchPattern other) { SpecialFunctions.CheckCondition(overlap < CoreLength && overlap < other.CoreLength); //!!!raise error SpecialFunctions.CheckCondition(other is PatchRegex); //!!!raise error ArrayList rgrgOther = SplitRight((PatchRegex)other, overlap); ArrayList rgrgMe = SplitLeft(overlap); ArrayList rgOuts = new ArrayList(); foreach (PatchRegex[] rgOther in rgrgOther) { foreach (PatchRegex[] rgMe in rgrgMe) { PatchRegex unification = (PatchRegex)rgMe[0].UnifyOrNull(rgOther[1]); if (unification != null) { PatchPattern patchPattern; if (!unification.AnyFlanking()) { patchPattern = Concatenate(rgOther[0], unification, rgMe[1]); Debug.Assert(patchPattern != null); } else { patchPattern = ReunifyWithFlanking(rgOther[0], unification, rgMe[1]); } if (patchPattern != null) { rgOuts.Add(patchPattern); } if (rgOuts.Count >= DisjunctLimit) { goto SkipOutEarly; } } } } SkipOutEarly :; if (rgOuts.Count == 0) { return(null); } else if (rgOuts.Count == 1) { return((PatchPattern)rgOuts[0]); } else { return(MergePatchPatterns(rgOuts, PatchRegexFactory)); } }
public override PatchPattern GetInstance(string expression) { if (Hashtable.ContainsKey(expression)) { PatchPattern patchPattern = (PatchPattern)Hashtable[expression]; return(patchPattern); } else { PatchPattern patchPattern = PatchRegex.GetInstance(expression, this); SpecialFunctions.CheckCondition(patchPattern.ToString() == expression, "PatchPattern expression is not in standard form."); //!!!raise error Hashtable.Add(expression, patchPattern); return(patchPattern); } }
static private PatchPattern MergePatchPatterns(ArrayList rgOut, PatchRegexFactory patchRegexFactory) { ArrayList rgStringDisjuncts = new ArrayList(); foreach (PatchRegex aPatchRegex in rgOut) { Debug.Assert(aPatchRegex != null); // real assert foreach (Disjunct aDisjunct in aPatchRegex.DisjunctCollection) { rgStringDisjuncts.Add(aDisjunct.FullAsAASetSequence.ToString()); } } PatchRegex newRegex = PatchRegex.GetInstance(rgStringDisjuncts, patchRegexFactory); return(newRegex); }
public override PatchPattern GetInstance(ICollection disjunctStringCollection) { PatchRegex patchRegex = PatchRegex.GetInstance(disjunctStringCollection, this); if (Hashtable.ContainsKey(patchRegex.ToString())) { PatchPattern patchPattern = (PatchPattern)Hashtable[patchRegex.ToString()]; return(patchPattern); } else { PatchPattern patchPattern = patchRegex; Hashtable.Add(patchPattern.ToString(), patchPattern); return(patchPattern); } }
static private PatchRegex Concatenate(params PatchRegex[] patchRegexParams) { SpecialFunctions.CheckCondition(patchRegexParams.Length > 0); //!!!raise error PatchRegexFactory patchRegexFactory = null; //ArrayList rgSegment = new ArrayList(); double combinations = 1; AASetSequence sbLuckyOne = AASetSequence.GetInstance(); foreach (PatchRegex patchRegex in patchRegexParams) { //rgSegment.AddRange(patchRegex.SegmentCollection); combinations *= patchRegex.DisjunctCollection.Length; if (combinations == 1) { sbLuckyOne.Append(patchRegex.DisjunctCollection[0].FullAsAASetSequence); } if (patchRegexFactory == null) { patchRegexFactory = patchRegex.PatchRegexFactory; } else { Debug.Assert(patchRegexFactory == patchRegex.PatchRegexFactory); //this says that all PatchRegex's must come from the same Hashtable } } if (combinations == 1) { Debug.Assert(sbLuckyOne[0] != AASet.OptionalAny && sbLuckyOne[sbLuckyOne.Count - 1] != AASet.OptionalAny); // real assert - disjuncts can't start or end with AASet.OptionalAny string[] rgDisjuncts = new string[] { sbLuckyOne.ToString() }; PatchRegex patchRegex = PatchRegex.GetInstance(rgDisjuncts, patchRegexFactory); return(patchRegex); } else { Debug.Fail("how may combinations?"); Debug.WriteLine("place of interest"); return(null); } // else if (rgSegment.Count > 1 && combinations == 2) // { // Segment segment = TurnToOneSegment(rgSegment, patchRegexFactory); // rgSegment.Clear(); // rgSegment.Add(segment); // } }
static public PatchRegex GetInstance(ICollection disjunctStringCollection, PatchRegexFactory patchRegexFactory) { Debug.Assert(!(disjunctStringCollection is AASetSequence)); // real assert // e.g. "ABC123", "ABC123|1BCD23" SortedList rgDisjunct = new SortedList(); foreach (string disjunctAsString in disjunctStringCollection) { Disjunct disjunct = Disjunct.GetInstance(disjunctAsString, patchRegexFactory); rgDisjunct[disjunct.FullAsString] = disjunct; //Remove idenitical disjuncts } PatchRegex patchRegex = FinishUp(rgDisjunct, patchRegexFactory); return(patchRegex); }
//Return an ArrayList of PatchRegex[] pairs private ArrayList SplitLeft(int overlap) { SpecialFunctions.CheckCondition(0 < overlap && overlap < CoreLength); //!!!raise error ArrayList rgrg = new ArrayList(); Disjunct[][] rgrgDisjuncts = SplitDisjuncts(overlap); foreach (Disjunct[] splitDisjunct in rgrgDisjuncts) { PatchRegex patchRegexLeft = PatchRegex.GetInstance(splitDisjunct[0].FullAsAASetSequence.ToString(), PatchRegexFactory); PatchRegex patchRegexRight = PatchRegex.GetInstance(splitDisjunct[1].FullAsAASetSequence.ToString(), PatchRegexFactory); rgrg.Add(new PatchRegex[] { patchRegexLeft, patchRegexRight }); } Debug.Assert(rgrg.Count == rgrgDisjuncts.Length); // real assert return(rgrg); }
private static PatchRegex FinishUp(SortedList rgDisjunct1, PatchRegexFactory patchRegexFactory) { SortedList rgDisjunct2 = RemoveSubsumedDisjuncts(rgDisjunct1); PatchRegex patchRegex = new PatchRegex(); patchRegex.DisjunctCollection = new Disjunct[rgDisjunct2.Count]; rgDisjunct2.Values.CopyTo(patchRegex.DisjunctCollection, 0); SpecialFunctions.CheckCondition(rgDisjunct2.Count > 0); //!!!raise error - just have at least one disjunct patchRegex.SetStringFromDisjunctCollection(); patchRegex.SetEverythingElse(patchRegexFactory); if (patchRegexFactory.Hashtable.ContainsKey(patchRegex.ToString())) { return((PatchRegex)patchRegexFactory.Hashtable[patchRegex.ToString()]); } else { return(patchRegex); } }