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); } }
static private void BeforeOrAfterIsLessGeneralThan(AASetSequence flanking1, AASetSequence flanking2, out bool falseForSure, ref bool bAtLeastOneCharOfOtherIsMoreGeneral) { if (flanking1.Count > flanking2.Count) { falseForSure = true; return; } if (flanking2.Count > flanking1.Count) { bAtLeastOneCharOfOtherIsMoreGeneral = true; } for (int i = 0; i < flanking1.Count; ++i) { AASet c1 = flanking1[i]; AASet c2 = flanking2[i]; if (BeforeOrAfterIsMoreGeneralThan(c1, c2)) { falseForSure = true; return; } else if (BeforeOrAfterIsMoreGeneralThan(c2, c1)) { bAtLeastOneCharOfOtherIsMoreGeneral = true; } } falseForSure = false; return; }
public void EndDisjunct() { SetAndCheckCoreLength(); DisjunctAASetSequenceCollection[sbCurrentDisjunct.ToString()] = null; sbCurrentDisjunct = AASetSequence.GetInstance(); CoreLengthSoFar = 0; }
private bool AfterUnificationAtPosition(Disjunct other, int possiblePos, ref AASetSequence aAASetSequence) { //This creates the new after string. It will be the unification of other's after string //and any of This's after that sticks out beyond the core of other int iHowMuchDoesThisStickOut = Math.Max(0, possiblePos + Core.Count + After.Count - other.Core.Count); int iHowMuchInCommon = Math.Min(iHowMuchDoesThisStickOut, other.After.Count); for (int iCommon = 0; iCommon < iHowMuchInCommon; ++iCommon) { AASet chThis = After[After.Count - iHowMuchDoesThisStickOut + iCommon]; AASet chOther = other.After[iCommon]; AASet chUnifyOrOptionalEmpty = FlankingUnifyOrOptionalEmpty(chThis, chOther); if (chUnifyOrOptionalEmpty == AASet.OptionalEmpty) //!!!const { return(false); } aAASetSequence.Append(chUnifyOrOptionalEmpty); } int iHowMuchLongerDoesOtherStickOutComparedToThis = Math.Max(0, other.After.Count - iHowMuchDoesThisStickOut); aAASetSequence.AppendSubsequence(other.After, iHowMuchInCommon, iHowMuchLongerDoesOtherStickOutComparedToThis); int iHowMuchLongerDoesThisStickOutComparedToOther = Math.Max(0, iHowMuchDoesThisStickOut - other.After.Count); aAASetSequence.AppendSubsequence(After, After.Count - iHowMuchLongerDoesThisStickOutComparedToOther, iHowMuchLongerDoesThisStickOutComparedToOther); return(true); }
private AASetSequence UnifyWithAfterOrNull(int coreLength, AASetSequence disjunctRightFull, AASetSequence disjunctUnificationAfter) { AASetSequence sb; if (disjunctRightFull.Count >= disjunctUnificationAfter.Count) { sb = UnifyWithAfterOrNullLongerShorter(disjunctRightFull, disjunctUnificationAfter); } else { sb = UnifyWithAfterOrNullLongerShorter(disjunctUnificationAfter, disjunctRightFull); } if (sb == null) { return(null); } //make everything after the core region, lower case to shows that it is a flanking region for (int i = (int)coreLength + 1; i < sb.Count; ++i) { sb[i] = AASetSequence.ToOptional(sb[i]); } return(sb); }
static private AASet FlankingUnifyOrOptionalEmpty(AASet c1, AASet c2) { Debug.Assert(AASetSequence.IsOptional(c1) && AASetSequence.IsOptional(c2)); // real assert AASet result = c1 & c2; return(result); }
public void AppendSubsequence(AASetSequence from, int start, int length) { for (int i = start; i < start + length; ++i) { Append(from[i]); } }
static public AASet UnifyOrEmpty(AASet c1, AASet c2) { SpecialFunctions.CheckCondition(!AASetSequence.IsOptional(c1) && !AASetSequence.IsOptional(c2)); //!!!raise error AASet result = c1 & c2; return(result); }
public Disjunct Right(int length) { SpecialFunctions.CheckCondition(length > 0); //!!!raise error AASetSequence expression = FullAsAASetSequence.Subsequence(Before.Count + Core.Count - (int)length); Disjunct disjunct = Disjunct.GetInstance(expression.ToString(), PatchRegexFactory); return(disjunct); }
// private AASetSequence(int size) : base(size) // { // for(int i = 0; i < size; ++i) // { // Add(null); // } // } public static AASetSequence Concatenate(params AASetSequence[] aaSetSequenceParams) { AASetSequence result = AASetSequence.GetInstance(); foreach (AASetSequence aAASetSequence in aaSetSequenceParams) { result.Append(aAASetSequence); } return(result); }
static public AASetSequence GetInstance(AASetSequence aAASetSequence) { AASetSequence r = AASetSequence.GetInstance(); foreach (AASet aaSet in aAASetSequence) { r.Add(aaSet); } return(r); }
public AASetSequence Subsequence(int start) { AASetSequence aAASetSequence = AASetSequence.GetInstance(); for (int i = start; i < Count; ++i) { aAASetSequence.Append(this[i]); } return(aAASetSequence); }
public AASetSequence ReverseClone() { AASetSequence rc = AASetSequence.GetInstance(); for (int i = this.Count - 1; i >= 0; --i) { rc.Add(this[i]); } return(rc); }
public AASetSequence Subsequence(int start, int length) { AASetSequence aAASetSequence = AASetSequence.GetInstance(); for (int i = start; i < start + length; ++i) { aAASetSequence.Append(this[i]); } return(aAASetSequence); }
private AASet FlankingAndCoreUnifyOrEmpty(AASet flanking, AASet core) { if (flanking == AASet.OptionalAny) { return(core); } Debug.Assert(AASetSequence.IsOptional(flanking)); // real assert AASet chUnifyOrEmpty = AASetSequence.UnifyOrEmpty(AASetSequence.ToRequired(flanking), core); return(chUnifyOrEmpty); }
static private AASetSequence Reverse(AASetSequence s) { if (s == null) { return(null); } else { AASetSequence r = s.ReverseClone(); return(r); } }
/// 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> public void AppendUnifications(Disjunct other, ref ArrayList unifiedDisjunctStringList) { SpecialFunctions.CheckCondition(Core.Count <= other.Core.Count); //!!!raise error for (int iPossiblePos = 0; iPossiblePos <= other.Core.Count - Core.Count; ++iPossiblePos) { AASetSequence unifiedDisjunctOrNull = UnificationAtPositionOrNull(other, iPossiblePos); if (unifiedDisjunctOrNull != null) { unifiedDisjunctStringList.Add(unifiedDisjunctOrNull.ToString()); } } }
private bool CoreUnificationAtPosition(Disjunct other, int possiblePos, ref AASetSequence aAASetSequence) { for (int iThisPos = 0; iThisPos < Core.Count; ++iThisPos) { AASet chThis = Core[iThisPos]; int iOtherPos = possiblePos + iThisPos; AASet chOther = other.Core[iOtherPos]; AASet chUnifyOrEmpty = AASetSequence.UnifyOrEmpty(chThis, chOther); if (chUnifyOrEmpty == AASet.Empty) //!!!const { return(false); } aAASetSequence.Append(chUnifyOrEmpty); } return(true); }
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); // } }
private AASetSequence UnifyWithAfterOrNullLongerShorter(AASetSequence longer, AASetSequence shorter) { Debug.Assert(longer.Count >= shorter.Count); // real assert AASetSequence sb = AASetSequence.GetInstance(longer); for (int i = 0; i < shorter.Count; ++i) { AASet chShorter = shorter[i]; AASet chLonger = longer[i]; AASet chUnifyOrEmpty = UnifyOrBoundWithFlanking(chShorter, chLonger); if (chUnifyOrEmpty == AASet.Empty) { return(null); } sb[i] = chUnifyOrEmpty; } return(sb); }
private AASetSequence UnificationAtPositionOrNull(Disjunct other, int possiblePos) { AASetSequence aAASetSequence = AASetSequence.GetInstance(); bool b = BeforeUnificationAtPosition(other, possiblePos, ref aAASetSequence) && BeforeAndCoreUnificationAtPosition(other, possiblePos, ref aAASetSequence) && CoreUnificationAtPosition(other, possiblePos, ref aAASetSequence) && AfterAndCoreUnificationAtPosition(other, possiblePos, ref aAASetSequence) && AfterUnificationAtPosition(other, possiblePos, ref aAASetSequence); if (b) { Debug.Assert(aAASetSequence[0] != AASet.OptionalAny && aAASetSequence[aAASetSequence.Count - 1] != AASet.OptionalAny); // real assert - can't start or end with AASet.OptionalAny } return(b ? aAASetSequence : null); }
private bool AfterAndCoreUnificationAtPosition(Disjunct other, int possiblePos, ref AASetSequence aAASetSequence) { int iHowMuchDoesThisStickIn = Math.Max(other.Core.Count - (possiblePos + Core.Count), 0); for (int iCommon = 0; iCommon < iHowMuchDoesThisStickIn; ++iCommon) { AASet chOther = other.Core[other.Core.Count - iHowMuchDoesThisStickIn + iCommon]; if (iCommon < After.Count) { AASet chThis = After[(int)iCommon]; AASet chUnifyOrEmpty = FlankingAndCoreUnifyOrEmpty(chThis, chOther); if (chUnifyOrEmpty == AASet.Empty) //!!!const { return(false); } aAASetSequence.Append(chUnifyOrEmpty); } else { aAASetSequence.Append(chOther); } } return(true); }
private bool BeforeAndCoreUnificationAtPosition(Disjunct other, int possiblePos, ref AASetSequence aAASetSequence) { for (int iCommon = 0; iCommon < possiblePos; ++iCommon) { AASet chOther = other.Core[iCommon]; int iInBefore = Before.Count - possiblePos + iCommon; if (iInBefore >= 0) { AASet chThis = Before[iInBefore]; AASet chUnifyOrEmpty = FlankingAndCoreUnifyOrEmpty(chThis, chOther); if (chUnifyOrEmpty == AASet.Empty) //!!!const { return(false); } aAASetSequence.Append(chUnifyOrEmpty); } else { aAASetSequence.Append(chOther); } } return(true); }
private AASet UnifyOrBoundWithFlanking(AASet c1, AASet c2) { AASet result = AASetSequence.ToRequired(c1 & c2); return(result); }
private void SetLeftCoreRightRealizationAbstractionAndRegex() { Before = AASetSequence.GetInstance(); Core = AASetSequence.GetInstance(); After = AASetSequence.GetInstance(); StringBuilder sbCoreRealization = new StringBuilder(); StringBuilder sbFullRealization = new StringBuilder(); StringBuilder sbRegex = new StringBuilder(); CharEnumerator charEnumerator = FullAsString.GetEnumerator(); // Read 1st char bool bOK = charEnumerator.MoveNext(); char ch = char.MinValue; if (bOK) { ch = charEnumerator.Current; } // Read before part while (bOK) { if (!(ch == '.' || (char.IsLetter(ch) && !char.IsUpper(ch)))) { break; } AASet aaSet = Before.AppendGroundOrEdge(ch); sbRegex.Append(AASetSequence.ToRegexString(aaSet)); char fullRealizationChar = AASetSequence.ToFlankingRealizationChar(aaSet); sbFullRealization.Append(fullRealizationChar); bOK = charEnumerator.MoveNext(); if (bOK) { ch = charEnumerator.Current; } } // Read main part while (bOK) { if (ch == '.' || (char.IsLetter(ch) && !char.IsUpper(ch))) { break; } if (char.IsLetter(ch) && char.IsUpper(ch)) { AASet aaSet = Core.AppendGround(ch); char coreRealizationChar = AASetSequence.ToCoreRealizationChar(aaSet); Debug.Assert(ch == coreRealizationChar); //!!!real assert sbCoreRealization.Append(coreRealizationChar); sbFullRealization.Append(coreRealizationChar); sbRegex.Append(AASetSequence.ToRegexString(aaSet)); } else { SpecialFunctions.CheckCondition(ch == '['); //!!!raise error // Read [xyz] StringBuilder sbCharSet = new StringBuilder(); bOK = charEnumerator.MoveNext(); SpecialFunctions.CheckCondition(bOK); //!!!raise error ch = charEnumerator.Current; while (ch != ']') { sbCharSet.Append(ch); bOK = charEnumerator.MoveNext(); SpecialFunctions.CheckCondition(bOK); //!!!raise error ch = charEnumerator.Current; } SpecialFunctions.CheckCondition(sbCharSet.Length > 0); //!!!raies error AASet aaSet = Core.AppendGroundSet(sbCharSet.ToString()); char coreRealizationChar = AASetSequence.ToCoreRealizationChar(aaSet); sbCoreRealization.Append(coreRealizationChar); sbFullRealization.Append(coreRealizationChar); sbRegex.Append(AASetSequence.ToRegexString(aaSet)); } bOK = charEnumerator.MoveNext(); if (bOK) { ch = charEnumerator.Current; } } // Read after part while (bOK) { if (!(ch == '.' || (char.IsLetter(ch) && !char.IsUpper(ch)))) { break; } AASet aaSet = After.AppendGroundOrEdge(ch); sbRegex.Append(AASetSequence.ToRegexString(aaSet)); char fullRealizationChar = AASetSequence.ToFlankingRealizationChar(aaSet); sbFullRealization.Append(fullRealizationChar); bOK = charEnumerator.MoveNext(); if (bOK) { ch = charEnumerator.Current; } } FullAsAASetSequence = AASetSequence.Concatenate(Before, Core, After); CoreRealization = sbCoreRealization.ToString(); FullRealization = sbFullRealization.ToString(); RegexString = sbRegex.ToString(); }
static private bool IsCore(AASet c) { bool b = AASetSequence.IsRequired(c); return(b); }
// private void Concatenate(string disjunctLeftString, Disjunct disjunctMiddle, PatchRegex patchRegexRight, ref ArrayList rgStringDisjuncts) // { // Debug.Assert(disjunctMiddle.After == ""); // real assert // Debug.Assert(disjunctMiddle.Before != ""); // real assert // // Debug.Assert(patchRegexRight.SegmentCollection.Length == 1); //!!!need code for other cases // Segment segmentRight = patchRegexRight.SegmentCollection[0]; // foreach(Disjunct disjunctRight in segmentRight.DisjunctCollection) // { // Debug.Assert(disjunctRight.Before == ""); // real asert // string newDisjunctString = string.Format("{0}{1}{2}", disjunctLeftString, disjunctMiddle.Core, disjunctRight.Full); // rgStringDisjuncts.Add(newDisjunctString); // } // } // // private void Concatenate(PatchRegex patchRegexLeft, Disjunct disjunctMiddle, string disjunctRightString, ref ArrayList rgStringDisjuncts) // { // Debug.Assert(disjunctMiddle.Before == ""); // real assert // Debug.Assert(disjunctMiddle.After != ""); // real assert // // Debug.Assert(patchRegexLeft.SegmentCollection.Length == 1); //!!!need code for other cases // Segment segmentLeft = patchRegexLeft.SegmentCollection[0]; // foreach(Disjunct disjunctLeft in segmentLeft.DisjunctCollection) // { // Debug.Assert(disjunctLeft.After == ""); // real asert // string newDisjunctString = string.Format("{0}{1}{2}", disjunctLeft.Full, disjunctMiddle.Core, disjunctRightString); // rgStringDisjuncts.Add(newDisjunctString); // } // } private AASetSequence UnifyWithBeforeOrNull(int coreLength, AASetSequence disjunctRightFull, AASetSequence disjunctUnificationAfter) { AASetSequence s = Reverse(UnifyWithAfterOrNull(coreLength, Reverse(disjunctRightFull), Reverse(disjunctUnificationAfter))); return(s); }
private void Concatenate(AASetSequence disjunctLeftString, Disjunct disjunctMiddle, AASetSequence disjunctRightString, ref ArrayList rgStringDisjuncts) { string newDisjunctString = string.Format("{0}{1}{2}", disjunctLeftString, disjunctMiddle.Core, disjunctRightString); rgStringDisjuncts.Add(newDisjunctString); }
public void Append(AASetSequence aAASetSequence) { AddRange(aAASetSequence); }