public void ConvertToComplexTest_Ok() { Label expectedLabel = new Label(EnumerateHelper.Sequence(new SingleLabel('b'))); Label actualLabel = new Label(new SingleLabel('b')).ConvertToComplex(); Assert.AreEqual(expectedLabel, actualLabel); }
internal override GrammarExpressionTuple GenerateGrammar(GrammarType grammarType, int grammarNumber, ref int index, ref int additionalGrammarNumber, Action <GrammarPostReport> onIterate, params GrammarExpressionWithOriginal[] dependencies) { CheckDependencies(dependencies); NonTerminalSymbol target = new NonTerminalSymbol(new Label(new SingleLabel(Grammar._DefaultNonTerminalSymbol, index++))); GrammarExpressionTuple grammarExpressionTuple = new GrammarExpressionTuple( this, new Grammar( EnumerateHelper.Sequence( new Rule(EnumerateHelper.Sequence(new Chain(Enumerable.Empty <Grammars.Symbol>())), target) ), target ), grammarNumber ); if (onIterate != null) { onIterate(new GrammarPostReport(grammarExpressionTuple, dependencies)); } return(grammarExpressionTuple); }
private GrammarExpressionTuple GenerateLeftGrammar(int grammarNumber, ref int index, ref int additionalGrammarNumber, Action <GrammarPostReport> onIterate, params GrammarExpressionWithOriginal[] dependencies) { Grammar leftExpGrammar = dependencies[0].GrammarExpression.Grammar; Grammar rightExpGrammar = dependencies[1].GrammarExpression.Grammar; IReadOnlySet <Rule> terminalSymbolsOnlyRules; IReadOnlySet <Rule> otherRules; rightExpGrammar.SplitRules(out terminalSymbolsOnlyRules, out otherRules); ISet <Rule> newRules = new HashSet <Rule>(otherRules.Concat(leftExpGrammar.Rules)); foreach (Rule rule in terminalSymbolsOnlyRules) { ISet <Chain> newChains = new HashSet <Chain>( rule.Chains.Select( chain => new Chain( EnumerateHelper.Sequence(leftExpGrammar.Target).Concat(chain) ) ) ); newRules.Add(new Rule(newChains, rule.Target)); } GrammarExpressionTuple grammarExpressionTuple = new GrammarExpressionTuple(this, new Grammar(newRules, rightExpGrammar.Target), grammarNumber); if (onIterate != null) { onIterate(new GrammarPostReport(grammarExpressionTuple, dependencies)); } return(grammarExpressionTuple); }
internal override GrammarExpressionTuple GenerateGrammar(GrammarType grammarType, int grammarNumber, ref int index, ref int additionalGrammarNumber, Action <GrammarPostReport> onIterate, params GrammarExpressionWithOriginal[] dependencies) { CheckDependencies(dependencies); Grammar leftExpGrammar = dependencies[0].GrammarExpression.Grammar; Grammar rightExpGrammar = dependencies[1].GrammarExpression.Grammar; NonTerminalSymbol target = new NonTerminalSymbol(new Label(new SingleLabel(Grammar._DefaultNonTerminalSymbol, index++))); Rule rule = new Rule( EnumerateHelper.Sequence( new Chain(EnumerateHelper.Sequence(leftExpGrammar.Target)), new Chain(EnumerateHelper.Sequence(rightExpGrammar.Target)) ), target); IEnumerable <Rule> newRules = leftExpGrammar.Rules.Concat(rightExpGrammar.Rules).Concat(rule); GrammarExpressionTuple grammarExpressionTuple = new GrammarExpressionTuple(this, new Grammar(newRules, target), grammarNumber); if (onIterate != null) { onIterate(new GrammarPostReport(grammarExpressionTuple, dependencies)); } return(grammarExpressionTuple); }
public void CctorTest_Ok() { IEnumerable<Label> expectedCurrentReachableStates = EnumerateHelper.Sequence(new Label(new SingleLabel('b')), new Label(new SingleLabel('c'))); IEnumerable<Label> expectedNextReachableStates = EnumerateHelper.Sequence(new Label(new SingleLabel('d')), new Label(new SingleLabel('e'))); IEnumerable<Label> expectedCurrentApproachedStates = EnumerateHelper.Sequence(new Label(new SingleLabel('f')), new Label(new SingleLabel('g'))); IEnumerable<Label> expectedNextApproachedStates = EnumerateHelper.Sequence(new Label(new SingleLabel('h')), new Label(new SingleLabel('i'))); int expectedIteration = 45; bool expectedIsLastIteration = true; RemovingUnreachableStatesIterationPostReport actual = new RemovingUnreachableStatesIterationPostReport( expectedCurrentReachableStates, expectedNextReachableStates, expectedCurrentApproachedStates, expectedNextApproachedStates, expectedIteration, expectedIsLastIteration); CollectionAssert.AreEquivalent(expectedCurrentReachableStates,actual.CurrentReachableStates); CollectionAssert.AreEquivalent(expectedNextReachableStates, actual.NextReachableStates); CollectionAssert.AreEquivalent(expectedCurrentApproachedStates, actual.CurrentApproachedStates); CollectionAssert.AreEquivalent(expectedNextApproachedStates, actual.CurrentApproachedMinusCurrentReachableStates); Assert.AreEqual(expectedIteration, actual.Iteration); Assert.AreEqual(expectedIsLastIteration, actual.IsLastIteration); }
private Grammar MakeGrammarLeft() { IDictionary <char, TerminalSymbol> charTerminalMap = Alphabet.ToDictionary(c => c, c => new TerminalSymbol(c)); IDictionary <Label, NonTerminalSymbol> stateNonTerminalMap = States.ToDictionary(s => s, s => new NonTerminalSymbol(s)); NonTerminalSymbol target = Grammar.GetNewNonTerminal(stateNonTerminalMap.Values); ISet <Rule> rules = new HashSet <Rule>(); foreach (Transition transition in Transitions) { Chain chain = new Chain( EnumerateHelper.Sequence <Symbol>( stateNonTerminalMap[transition.CurrentState], charTerminalMap[transition.Symbol] ) ); rules.Add(new Rule(chain.AsSequence(), stateNonTerminalMap[transition.NextState])); } rules.Add(new Rule(Chain.Empty.AsSequence(), stateNonTerminalMap[InitialState])); IEnumerable <Chain> chains = FinalStates.Select(fs => new Chain(stateNonTerminalMap[fs].AsSequence())); rules.Add(new Rule(chains, target)); return(new Grammar(rules, target)); }
public void CctorTest_Ok() { IEnumerable <SetOfEquivalence> setsOfEquivalenceExpected = EnumerateHelper.Sequence( new SetOfEquivalence ( new SortedSet <Label>(EnumerateHelper.Sequence( new Label(new SingleLabel('P')), null, new Label(new SingleLabel('D')) ) ) ), null, new SetOfEquivalence ( new SortedSet <Label>(EnumerateHelper.Sequence( new Label(new SingleLabel('S')), new Label(new SingleLabel('M')) ) ) ) ); SetsOfEquivalence setsOfEquivalence = new SetsOfEquivalence(setsOfEquivalenceExpected); CollectionAssert.AreEquivalent(setsOfEquivalenceExpected, setsOfEquivalence); }
public void ConvertToReadOnlyListAndSortTest_Ok() { IEnumerable <int> expectedList = EnumerateHelper.Sequence(3, 4, 5, 8, 9, 10); IReadOnlyList <int> actualList = EnumerateHelper.Sequence(5, 8, 3, 4, 9, 10).ToHashSet().ConvertToReadOnlyListAndSort(); CollectionAssert.AreEqual(expectedList, actualList); }
public void CctorTest_Ok() { Union union = new Union( EnumerateHelper.Sequence <Entity>( new Symbol('a'), new Symbol('b') ) ); }
private static IEnumerable <Chain> RightChainEnumerator(Chain chain, NonTerminalSymbol target) { return(EnumerateHelper.Sequence( new Chain( chain.Concat(EnumerateHelper.Sequence(target)) ), chain )); }
public void ToStringTest() { IEnumerable <Entity> sequence = EnumerateHelper.Sequence <Entity>(new Symbol('a'), new Symbol('b')); IReadOnlyList <Entity> expectedEntityCollection = new List <Entity>(sequence).AsReadOnly(); Concat concat = new Concat(sequence); Assert.AreEqual("(ab)", concat.ToString()); }
public void CctorTest_Ok() { IEnumerable <Entity> sequence = EnumerateHelper.Sequence <Entity>(new Symbol('a'), new Symbol('b')); IReadOnlyList <Entity> expectedEntityCollection = new List <Entity>(sequence).AsReadOnly(); Concat concat = new Concat(sequence); Assert.AreEqual(expectedEntityCollection, concat.EntityCollection); }
public void CctorTest_SetLessTwoItems_Fail() { Assert.Throws <ArgumentException>(() => new Union( EnumerateHelper.Sequence <Entity>( new Symbol('a') ) ) ); }
public void CctorTest_Ok() { int expectedSetNumber = 2; IEnumerable <char> expectedSymbols = EnumerateHelper.Sequence('a', 'b'); ClassOfEquivalence classOfEquivalence = new ClassOfEquivalence(expectedSetNumber, expectedSymbols); Assert.AreEqual(expectedSetNumber, classOfEquivalence.SetNum); CollectionAssert.AreEquivalent(expectedSymbols, classOfEquivalence.Symbols); }
public void CCtorTest_Simple_Ok() { SingleLabel singleLabel = new SingleLabel('b'); IEnumerable <SingleLabel> expectedLabels = EnumerateHelper.Sequence( singleLabel ); Label label = new Label(singleLabel); CollectionAssert.AreEquivalent(expectedLabels, label.Sublabels); Assert.AreEqual(LabelType.Simple, label.LabelType); }
public void ToStringTest() { Union union = new Union( EnumerateHelper.Sequence <Entity>( new Symbol('a'), new Symbol('b') ) ); Assert.AreEqual("(a,b)", union.ToString()); }
public void CctorTest_AnySingleLabelNull_Fail() { Assert.Throws <ArgumentException>( () => new Label( EnumerateHelper.Sequence( new SingleLabel('S'), null, new SingleLabel('D') ) ) ); }
public void CctorTest_Ok() { IEnumerable <Label> expected = EnumerateHelper.Sequence( new Label(new SingleLabel('A')), new Label(new SingleLabel('C')) ); SetOfEquivalence actual = new SetOfEquivalence(expected); CollectionAssert.AreEquivalent(expected, actual); }
public void CCtorTest_Complex_Ok() { List <SingleLabel> expectedLabels = EnumerateHelper.Sequence( new SingleLabel('c'), new SingleLabel('b') ).ToList(); Label label = new Label(expectedLabels); expectedLabels.Sort(); CollectionAssert.AreEqual(expectedLabels, label.Sublabels); Assert.AreEqual(LabelType.Complex, label.LabelType); }
private IEnumerable <Expression> Enumerate(params Expression[] expressions) { switch (GrammarType) { case GrammarType.Left: return(EnumerateHelper.Sequence(expressions)); case GrammarType.Right: return(EnumerateHelper.ReverseSequence(expressions)); default: throw new InvalidOperationException(Grammar.GrammarIsNotSupportedMessage); } }
private static IEnumerable <T> Enumerate <T>(GrammarType grammarType, params T[] items) { switch (grammarType) { case GrammarType.Left: return(EnumerateHelper.Sequence(items)); case GrammarType.Right: return(EnumerateHelper.ReverseSequence(items)); default: throw new NotSupportedException(string.Format(GrammarIsNotSupportedMessage, grammarType)); } }
public void SequenceTest() { int[] expected = new int[] { 3, 6, 2 }; int actualCount = 0; foreach (int v in EnumerateHelper.Sequence(3, 6, 2)) { Assert.IsTrue(actualCount < expected.Length); Assert.AreEqual(expected[actualCount], v); actualCount++; } Assert.AreEqual(expected.Length, actualCount); }
public void CctorTest_OptionalStatesNull_Fail() { IEnumerable <Label> expectedRequiredStates = EnumerateHelper.Sequence <Label> ( new Label( new SingleLabel( 'a' ) ), new Label( new SingleLabel( 'b' ) ) ); Assert.Throws <ArgumentNullException>(() => new MetaFinalState(expectedRequiredStates, null)); }
public void CctorTest_Ok() { IEnumerable <Label> expectedRequiredStates = EnumerateHelper.Sequence <Label> ( new Label( new SingleLabel( 'a' ) ), new Label( new SingleLabel( 'b' ) ) ); IEnumerable <Label> expectedOptionalStates = EnumerateHelper.Sequence <Label> ( new Label( new SingleLabel( 'c' ) ), new Label( new SingleLabel( 'd' ) ) ); MetaFinalState metaFinalState = new MetaFinalState( expectedRequiredStates, expectedOptionalStates ); CollectionAssert.AreEquivalent(expectedRequiredStates, metaFinalState.RequiredStates); CollectionAssert.AreEquivalent(expectedOptionalStates, metaFinalState.OptionalStates); }
public void ToStringTest() { MetaTransition metaTransition = new MetaTransition( EnumerateHelper.Sequence( new Label(new SingleLabel('e')), new Label(new SingleLabel('r')) ), EnumerateHelper.Sequence( new Label(new SingleLabel('e')), new Label(new SingleLabel('r')) ), 'a', EnumerateHelper.Sequence( new Label(new SingleLabel('e')), new Label(new SingleLabel('r')) ) ); Assert.AreEqual("δ([{e_null} {r_null} q_1 ... q_2], a) -> [ {e_null} {r_null}], q_1 ... q_2 : {e_null} {r_null}", metaTransition.ToString()); }
private bool SimpleIterate(int row, bool isDirect) { bool result = false; int first = isDirect ? 0 : row + 1; int last = isDirect ? row - 1 : _Matrix.GetLength(0) - 1; for (int i = first; i <= last; i++) { if (_Matrix[row, i] != null) { result = true; Expression expression = _Matrix[row, i]; _Matrix[row, i] = null; for (int j = 0; j < _Matrix.GetLength(1); j++) { if (_Matrix[i, j] != null) { Concat concat = new Concat(Enumerate(_Matrix[i, j], expression)); if (_Matrix[row, j] != null) { Union union = new Union(EnumerateHelper.Sequence(_Matrix[row, j], concat)); _Matrix[row, j] = union.Optimize(); } else { _Matrix[row, j] = concat.Optimize(); } } } } } return(result); }
public void GetSequenceHashCodeTest() { Assert.AreEqual(0, ((string[])null).GetSequenceHashCode()); Assert.AreEqual(0, new string[0].GetSequenceHashCode()); IEnumerable <string> sequence1 = EnumerateHelper.Sequence("4j9dneh3", "d9end", "djeheb,gf"); IEnumerable <string> sequence2 = EnumerateHelper.Sequence("4j9dneh3", "d9end", "djeheb,gf"); IEnumerable <string> sequence3 = EnumerateHelper.Sequence("erg34g", "3ghg6", "7g33g"); IEnumerable <string> sequence4 = EnumerateHelper.Sequence("erg34g", "3ghg6", "7g33g"); int hash1 = sequence1.GetSequenceHashCode(); int hash2 = sequence2.GetSequenceHashCode(); int hash3 = sequence3.GetSequenceHashCode(); int hash4 = sequence4.GetSequenceHashCode(); Assert.AreEqual(hash1, hash2); Assert.AreNotEqual(hash1, hash3); Assert.AreNotEqual(hash1, hash4); Assert.AreNotEqual(hash2, hash3); Assert.AreNotEqual(hash2, hash4); Assert.AreEqual(hash3, hash4); }
public void CctorTest_Ok() { IEnumerable <Label> expectedMetaCurrentRequiredStates = EnumerateHelper.Sequence( new Label(new SingleLabel('a')), new Label(new SingleLabel('b')), new Label(new SingleLabel('c')) ); IEnumerable <Label> expectedMetaCurrentOptionalStates = EnumerateHelper.Sequence( new Label(new SingleLabel('d')), new Label(new SingleLabel('e')), new Label(new SingleLabel('f')), new Label(new SingleLabel('j')) ); IEnumerable <Label> expectedMetaNextStates = EnumerateHelper.Sequence( new Label(new SingleLabel('f')), new Label(new SingleLabel('j')) ); char expectedSymbol = 's'; MetaTransition metaTransition = new MetaTransition( expectedMetaCurrentRequiredStates, expectedMetaCurrentOptionalStates, expectedSymbol, expectedMetaNextStates); Assert.AreEqual(expectedSymbol, metaTransition.Symbol); CollectionAssert.AreEquivalent(expectedMetaCurrentRequiredStates, metaTransition.CurrentRequiredStates); CollectionAssert.AreEquivalent(expectedMetaCurrentOptionalStates, metaTransition.CurrentOptionalStates); CollectionAssert.AreEquivalent(expectedMetaNextStates, metaTransition.NextStates); }
public bool RemoveEmptyRules(out Grammar grammar, Action <BeginPostReport <NonTerminalSymbol> > onBegin = null, Action <IterationPostReport <NonTerminalSymbol> > onIterate = null) { int i = 0; ISet <NonTerminalSymbol> newSymbolSet = Rules.Where(r => r.Chains.Any(c => !c.Any())).Select(r => r.Target).ToHashSet(); if (onBegin != null) { onBegin(new BeginPostReport <NonTerminalSymbol>(i, newSymbolSet)); } bool isAddedSomething; do { i++; isAddedSomething = false; ISet <NonTerminalSymbol> nextSymbolSet = newSymbolSet; newSymbolSet = new HashSet <NonTerminalSymbol>(); foreach (Rule rule in Rules) { foreach (Chain chain in rule.Chains) { bool isAllSymbolsInNext = true; foreach (Symbol symbol in chain.Sequence) { NonTerminalSymbol nonTerminal = symbol.As <NonTerminalSymbol>(); if (nonTerminal == null || !nextSymbolSet.Contains(nonTerminal)) { isAllSymbolsInNext = false; break; } } if (isAllSymbolsInNext) { if (!newSymbolSet.Contains(rule.Target) && !nextSymbolSet.Contains(rule.Target)) { newSymbolSet.Add(rule.Target); isAddedSomething = true; } } } } ISet <NonTerminalSymbol> previousSymbolSet = nextSymbolSet.ToHashSet(); nextSymbolSet.UnionWith(newSymbolSet); if (onIterate != null) { onIterate(new IterationPostReport <NonTerminalSymbol>(i, previousSymbolSet, newSymbolSet, nextSymbolSet, !isAddedSomething)); } newSymbolSet = nextSymbolSet; } while (isAddedSomething); ISet <Rule> newRules = new HashSet <Rule>(); foreach (Rule rule in Rules) { newRules.Add(new Rule(rule.Chains.SelectMany(c => ForkByEmpty(c, newSymbolSet).Where(chain => chain != Chain.Empty)), rule.Target)); } IDictionary <NonTerminalSymbol, Rule> targetRuleMap = Rules.ToDictionary(r => r.Target); NonTerminalSymbol newTarget = Target; if (newSymbolSet.Contains(Target)) { Chain targetChain = new Chain(EnumerateHelper.Sequence(Target)); newTarget = GetNewNonTerminal(NonTerminals); Rule rule = new Rule(EnumerateHelper.Sequence(targetChain, Chain.Empty), newTarget); newRules.Add(rule); } newRules = Normalize(newRules); if (!newRules.SetEquals(Rules)) { grammar = new Grammar(newRules, newTarget); return(true); } grammar = this; return(false); }
public void ToRegExp_Ok() { Variable kVariable = new Variable('k', Sign.MoreThanOrEqual, 0); Variable mVariable = new Variable('m', Sign.MoreThan, 0); Degree degree1 = new Degree( new Degree( new Union( EnumerateHelper.Sequence( new Symbol('a'), new Symbol('b') ) ), kVariable ), new Quantity(2) ); Degree degree2 = new Degree( new Degree( new Concat( EnumerateHelper.Sequence( new Symbol('b'), new Symbol('a') ) ), new Quantity(2) ), mVariable ); Concat concat1 = new Concat( EnumerateHelper.Sequence <Entity>( new Degree( new Symbol('c'), kVariable ), new Symbol('d') ) ); Union union1 = new Union( EnumerateHelper.Sequence <Entity>( new Degree( new Symbol('e'), mVariable ), new Symbol('f') ) ); Degree degree3 = new Degree( new Symbol('m'), mVariable ); Degree degree4 = new Degree( new Symbol('z'), kVariable ); Concat concat2 = new Concat( EnumerateHelper.Sequence <Entity>( degree3, degree4 ) ); Symbol symbol1 = new Symbol('y'); Entity entity = new Union( EnumerateHelper.Sequence <Entity>( degree1, degree2, concat1, union1, concat2, symbol1 ) ); Variable variable = new Variable('k', Sign.MoreThanOrEqual, 0); variable.ToRegExp(entity); }