public void RepeaterTestCase() { TokenSequence sequence = new TokenSequence(); Token t = new Token("NUMBER"); t.Text = "200.9"; sequence.Append(t); t = new Token("ADD"); t.Text = "+"; sequence.Append(t); t = new Token("NUMBER"); t.Text = "14"; sequence.Append(t); t = new Token("ADD"); t.Text = "+"; sequence.Append(t); t = new Token("NUMBER"); t.Text = "28"; sequence.Append(t); SyntacticalRule rule = new SyntacticalRule("formula"); SyntacticalExpression exp = new SyntacticalExpression(); exp.FormatString = "{0}{1}"; ExpressionTokenItem eti = new ExpressionTokenItem(); eti.TokenType = "NUMBER"; exp.Items.Add(eti); ExpressionGroupItem group = new ExpressionGroupItem(); group.Modifier = ExpressionItemModifier.RepeatingNonCompulsory; eti = new ExpressionTokenItem(); eti.TokenType = "ADD"; group.ChildrenItems.Add(eti); eti = new ExpressionTokenItem(); eti.TokenType = "NUMBER"; group.ChildrenItems.Add(eti); group.FormatString = " + {1}"; exp.Items.Add(group); rule.Expressions.Add(exp); string output; bool res = rule.Match(ref sequence, out output); Assert.IsTrue(res, "Matching wasn't succesfull"); Assert.AreEqual("200.9 + 14 + 28", output, "Output isn't correct.'"); }
public void RepeaterTestCase() { TokenSequence sequence= new TokenSequence(); Token t = new Token("NUMBER"); t.Text="200.9"; sequence.Append(t); t = new Token("ADD"); t.Text="+"; sequence.Append(t); t = new Token("NUMBER"); t.Text="14"; sequence.Append(t); t = new Token("ADD"); t.Text="+"; sequence.Append(t); t = new Token("NUMBER"); t.Text="28"; sequence.Append(t); SyntacticalRule rule = new SyntacticalRule("formula"); SyntacticalExpression exp = new SyntacticalExpression(); exp.FormatString="{0}{1}"; ExpressionTokenItem eti = new ExpressionTokenItem(); eti.TokenType = "NUMBER"; exp.Items.Add(eti); ExpressionGroupItem group = new ExpressionGroupItem(); group.Modifier = ExpressionItemModifier.RepeatingNonCompulsory; eti = new ExpressionTokenItem(); eti.TokenType = "ADD"; group.ChildrenItems.Add(eti); eti = new ExpressionTokenItem(); eti.TokenType = "NUMBER"; group.ChildrenItems.Add(eti); group.FormatString=" + {1}"; exp.Items.Add(group); rule.Expressions.Add(exp); string output; bool res = rule.Match(ref sequence, out output); Assert.IsTrue(res, "Matching wasn't succesfull"); Assert.AreEqual("200.9 + 14 + 28", output, "Output isn't correct.'"); }
private Token TestToken(string text, LexicalRule rule) { TokenSequence tokens = new TokenSequence(); foreach (char c in text) { tokens.Append(new Token(c.ToString(), 0,0, new FloatBitmap(2,2))); } Token res; if(!rule.Match(tokens, out res)) return null; return res; }
private Token TestToken(string text, LexicalRule rule) { TokenSequence tokens = new TokenSequence(); foreach (char c in text) { tokens.Append(new Token(c.ToString(), 0, 0, new FloatBitmap(2, 2))); } Token res; if (!rule.Match(tokens, out res)) { return(null); } return(res); }
public void SumAndMultTestCase() { TokenSequence sequence = new TokenSequence(); Token t = new Token("NUMBER"); t.Text = "200.9"; sequence.Append(t); t = new Token("ADD"); t.Text = "+"; sequence.Append(t); t = new Token("NUMBER"); t.Text = "28"; sequence.Append(t); t = new Token("MULT"); t.Text = "x"; sequence.Append(t); t = new Token("NUMBER"); t.Text = "14"; sequence.Append(t); SyntacticalRulesLibrary.Instance.ClearRules(); SyntacticalRule rule = new SyntacticalRule("multiplicacion"); SyntacticalRulesLibrary.Instance.AddRule(rule); SyntacticalExpression exp = new SyntacticalExpression(); exp.FormatString = "{0}{1}"; ExpressionTokenItem eti = new ExpressionTokenItem(); eti.TokenType = "NUMBER"; exp.Items.Add(eti); ExpressionGroupItem group = new ExpressionGroupItem(); group.Modifier = ExpressionItemModifier.RepeatingNonCompulsory; eti = new ExpressionTokenItem(); eti.TokenType = "MULT"; group.ChildrenItems.Add(eti); eti = new ExpressionTokenItem(); eti.TokenType = "NUMBER"; group.ChildrenItems.Add(eti); group.FormatString = " x {1}"; exp.Items.Add(group); rule.Expressions.Add(exp); rule = new SyntacticalRule("formula"); SyntacticalRulesLibrary.Instance.AddRule(rule); exp = new SyntacticalExpression(); exp.FormatString = "{0}{1}"; ExpressionRuleCallItem esi = new ExpressionRuleCallItem(); esi.RuleName = "multiplicacion"; exp.Items.Add(esi); group = new ExpressionGroupItem(); group.Modifier = ExpressionItemModifier.RepeatingNonCompulsory; eti = new ExpressionTokenItem(); eti.TokenType = "ADD"; group.ChildrenItems.Add(eti); esi = new ExpressionRuleCallItem(); esi.RuleName = "multiplicacion"; group.ChildrenItems.Add(esi); group.FormatString = " + {1}"; exp.Items.Add(group); rule.Expressions.Add(exp); string output; bool res = rule.Match(ref sequence, out output); Assert.IsTrue(res, "Matching wasn't succesfull"); Assert.AreEqual("200.9 + 28 x 14", output, "Output isn't correct.'"); }
/// <summary> /// Retrives the related items for a token from the remaining items list. /// </summary> /// <param name="matched"> /// The <see cref="Token"/> the items we are looking for are related to. /// </param> /// <param name="remainingItems"> /// A <see cref="TokenSequence"/> containing the yet to be matched items. /// </param> /// <param name="position"> /// A <see cref="ExpressionItemPosition"/> the position of the related item. /// </param> /// <returns> /// A <see cref="TokenSequence"/> containing the items related to the /// matched item found in the given position. /// </returns> protected TokenSequence GetRelatedItems(Token matched, TokenSequence remainingItems, ExpressionItemPosition position) { TokenSequence sequence = new TokenSequence(); string remainingItemsString = remainingItems.ToString(); int i= 0; while (i < remainingItems.Count) { Token checkedItem = remainingItems[i]; if(CheckTokenInRelatedSequence(matched, checkedItem, position)) { sequence.Append(checkedItem); remainingItems.RemoveAt(i); } else if(!SpecialPosition(matched, checkedItem)) { LogSentInvoker("Encontrado {0}, cancelando la creación de la secuencia de items «{1}» {2}", checkedItem.Text, position, matched.Type); break; } else { i++; } } LogSentInvoker("Extraida la secuencia ({0}) en posicion «{1}» de entre los elementos de ({2})", sequence, position, remainingItemsString); return sequence; }
public void SumAndMultTestCase() { TokenSequence sequence= new TokenSequence(); Token t = new Token("NUMBER"); t.Text="200.9"; sequence.Append(t); t = new Token("ADD"); t.Text="+"; sequence.Append(t); t = new Token("NUMBER"); t.Text="28"; sequence.Append(t); t = new Token("MULT"); t.Text="x"; sequence.Append(t); t = new Token("NUMBER"); t.Text="14"; sequence.Append(t); SyntacticalRulesLibrary.Instance.ClearRules(); SyntacticalRule rule = new SyntacticalRule("multiplicacion"); SyntacticalRulesLibrary.Instance.AddRule(rule); SyntacticalExpression exp = new SyntacticalExpression(); exp.FormatString="{0}{1}"; ExpressionTokenItem eti = new ExpressionTokenItem(); eti.TokenType = "NUMBER"; exp.Items.Add(eti); ExpressionGroupItem group = new ExpressionGroupItem(); group.Modifier = ExpressionItemModifier.RepeatingNonCompulsory; eti = new ExpressionTokenItem(); eti.TokenType = "MULT"; group.ChildrenItems.Add(eti); eti = new ExpressionTokenItem(); eti.TokenType = "NUMBER"; group.ChildrenItems.Add(eti); group.FormatString=" x {1}"; exp.Items.Add(group); rule.Expressions.Add(exp); rule = new SyntacticalRule("formula"); SyntacticalRulesLibrary.Instance.AddRule(rule); exp = new SyntacticalExpression(); exp.FormatString="{0}{1}"; ExpressionRuleCallItem esi = new ExpressionRuleCallItem(); esi.RuleName = "multiplicacion"; exp.Items.Add(esi); group = new ExpressionGroupItem(); group.Modifier = ExpressionItemModifier.RepeatingNonCompulsory; eti = new ExpressionTokenItem(); eti.TokenType = "ADD"; group.ChildrenItems.Add(eti); esi = new ExpressionRuleCallItem(); esi.RuleName = "multiplicacion"; group.ChildrenItems.Add(esi); group.FormatString=" + {1}"; exp.Items.Add(group); rule.Expressions.Add(exp); string output; bool res = rule.Match(ref sequence, out output); Assert.IsTrue(res, "Matching wasn't succesfull"); Assert.AreEqual("200.9 + 28 x 14", output, "Output isn't correct.'"); }
/// <summary> /// Matches a given token sequence into one or more Tokens. /// </summary> /// <param name="sequence"> /// The token sequence (possibly) containing tokens. /// </param> private void MatchTokens(SequenceNode node) { TokenSequence discarded = new TokenSequence(); TokenSequence accepted = new TokenSequence(); SequenceNode acceptedNode = new SequenceNode(accepted, view); SequenceNode discardedNode = new SequenceNode(discarded, view); TokenSequence sequence = node.Sequence; node.AddChildSequence(acceptedNode); foreach (Token t in sequence) { accepted.Append(t); } NodeBeingProcessedInvoker(node); SuspendByStep(); bool found = false; Token foundToken = null; bool discardedNodeAdded = false; MessageLogSentInvoker("===== Tratando la secuencia {0} =====", node.NodeName); while(accepted.Count > 0 && !found) { foreach (LexicalRule rule in lexicalRules) { found = rule.Match(accepted, out foundToken); MessageLogSentInvoker("¿La regla «{0}» acepta la secuencia ({1})?: {2}", rule.Name, accepted.ToString(), found?"Sí":"No"); SequenceBeingMatchedInvoker(foundToken, rule, found); SuspendByStep(); if(found) { // We search no more. break; } Thread.Sleep(50); } // We check if a token was found if(!found) { // We remove the token from the input sequence and add it // at the beggining of the discarded set. int lastIndex = accepted.Count -1; if(!discardedNodeAdded) { // If we haven't done so, we add the discarded sequence. node.AddChildSequence(discardedNode); discardedNodeAdded = true; } this.MatchingFailedInvoker(); MessageLogSentInvoker("Se elimina el último símbolo de la secuencia {0} para seguir probando.", accepted.ToString()); discarded.Prepend(accepted[lastIndex]); accepted.RemoveAt(lastIndex); } else { // We found a token, so we stop searching and add // the token to the result. acceptedNode.FoundToken = foundToken; } } if(found && discarded.Count > 0) { MessageLogSentInvoker("Se tratará la secuencia de símbolos descartados."); // We follow the recursive path. MatchTokens(discardedNode); } else if(found && discarded.Count ==0) { // Only one token was found, we assimilate the acceptedNode // with its parent. node.FoundToken = foundToken; node.RemoveSequenceChildren(); } else { // If nothing was found, we remove the children. node.RemoveSequenceChildren(); MessageLogSentInvoker("No se pudo reconocer la secuencia {0}.", node.Sequence.ToString()); } StepDoneInvoker(); SuspendByNode(); }
/// <summary> /// Matches a given token sequence into one or more Tokens. /// </summary> /// <param name="sequence"> /// The token sequence (possibly) containing tokens. /// </param> private void MatchTokens(SequenceNode node) { TokenSequence discarded = new TokenSequence(); TokenSequence accepted = new TokenSequence(); SequenceNode acceptedNode = new SequenceNode(accepted, view); SequenceNode discardedNode = new SequenceNode(discarded, view); TokenSequence sequence = node.Sequence; node.AddChildSequence(acceptedNode); foreach (Token t in sequence) { accepted.Append(t); } NodeBeingProcessedInvoker(node); SuspendByStep(); bool found = false; Token foundToken = null; bool discardedNodeAdded = false; MessageLogSentInvoker("===== Tratando la secuencia {0} =====", node.NodeName); while (accepted.Count > 0 && !found) { foreach (LexicalRule rule in lexicalRules) { found = rule.Match(accepted, out foundToken); MessageLogSentInvoker("¿La regla «{0}» acepta la secuencia ({1})?: {2}", rule.Name, accepted.ToString(), found?"Sí":"No"); SequenceBeingMatchedInvoker(foundToken, rule, found); SuspendByStep(); if (found) { // We search no more. break; } Thread.Sleep(50); } // We check if a token was found if (!found) { // We remove the token from the input sequence and add it // at the beggining of the discarded set. int lastIndex = accepted.Count - 1; if (!discardedNodeAdded) { // If we haven't done so, we add the discarded sequence. node.AddChildSequence(discardedNode); discardedNodeAdded = true; } this.MatchingFailedInvoker(); MessageLogSentInvoker("Se elimina el último símbolo de la secuencia {0} para seguir probando.", accepted.ToString()); discarded.Prepend(accepted[lastIndex]); accepted.RemoveAt(lastIndex); } else { // We found a token, so we stop searching and add // the token to the result. acceptedNode.FoundToken = foundToken; } } if (found && discarded.Count > 0) { MessageLogSentInvoker("Se tratará la secuencia de símbolos descartados."); // We follow the recursive path. MatchTokens(discardedNode); } else if (found && discarded.Count == 0) { // Only one token was found, we assimilate the acceptedNode // with its parent. node.FoundToken = foundToken; node.RemoveSequenceChildren(); } else { // If nothing was found, we remove the children. node.RemoveSequenceChildren(); MessageLogSentInvoker("No se pudo reconocer la secuencia {0}.", node.Sequence.ToString()); } StepDoneInvoker(); SuspendByNode(); }
/// <summary> /// Handles the start of processing of a new node. /// </summary> /// <param name="sender"> /// A <see cref="System.Object"/> /// </param> /// <param name="args"> /// A <see cref="EventArgs"/> /// </param> private void OnControllerTokenChecked(object sender, TokenCheckedArgs args) { Application.Invoke(sender, args, delegate(object resender, EventArgs _args) { TokenCheckedArgs a = _args as TokenCheckedArgs; if(!sequencingFinished) { currentToken = a.CurrentToken; FloatBitmap sequenceImage; if(a.LastSequence!= null) { TokenSequence joinSeq = new TokenSequence(a.LastSequence); lastToken = joinSeq.Last; joinSeq.Append(currentToken); Token joinedToken =Token.Join(joinSeq, ""); sequenceImage = joinedToken.Image; } else { sequenceImage = currentToken.Image; lastToken = null; } // We add a border to the orginal image. Gdk.Pixbuf sequencePixbuf = sequenceImage.CreatePixbuf(); Gdk.Pixbuf drawnImage = new Gdk.Pixbuf(sequencePixbuf.Colorspace,false, 8, sequencePixbuf.Width+10, sequencePixbuf.Height+10); drawnImage.Fill(0xFFFFFFFF); sequencePixbuf.CopyArea(0, 0, sequencePixbuf.Width, sequencePixbuf.Height, drawnImage, 5,5); if(lastToken!=null) { uint color; if(currentToken.CloseFollows(lastToken)) { color = 0x00FF00; sequencingStepResultLbl.Markup = String.Format("<b>Sí, el símbolo «{0}» se añadirá a la secuencia actual</b>", currentToken.Text); } else { color = 0xFF0000; sequencingStepResultLbl.Markup = String.Format("<b>No, «{0}» no puede ser considerado parte de la secuencia ({1})</b>", currentToken.Text, a.LastSequence.ToString()); } Gdk.Pixbuf markedImage = drawnImage.Copy(); // We paint the image of the color markedImage = markedImage.CompositeColorSimple(markedImage.Width, markedImage.Height, Gdk.InterpType.Nearest, 100, 1, color, color); // We are going to mark the image of the to symbols being considered // with their baselines. int min = int.MaxValue; foreach (Token t in a.LastSequence ) { if(t.Top < min) min =t.Top; } int offset = Math.Min(min, currentToken.Top); int lastBaseline = lastToken.Baseline - offset; int currentBaseline = currentToken.Baseline - offset; markedImage.CopyArea(0, lastBaseline, markedImage.Width, 5, drawnImage, 0,lastBaseline); markedImage.CopyArea(0, currentBaseline, markedImage.Width,5, drawnImage, 0,currentBaseline); } baselineImageArea.Image = drawnImage; } if(controller.StepMode == ControllerStepMode.StepByStep) tokenizingNextButtonsAlign.Sensitive = true; }); }
/// <summary> /// Handles the start of processing of a new node. /// </summary> /// <param name="sender"> /// A <see cref="System.Object"/> /// </param> /// <param name="args"> /// A <see cref="EventArgs"/> /// </param> private void OnControllerTokenChecked(object sender, TokenCheckedArgs args) { Application.Invoke(sender, args, delegate(object resender, EventArgs _args) { TokenCheckedArgs a = _args as TokenCheckedArgs; if (!sequencingFinished) { currentToken = a.CurrentToken; FloatBitmap sequenceImage; if (a.LastSequence != null) { TokenSequence joinSeq = new TokenSequence(a.LastSequence); lastToken = joinSeq.Last; joinSeq.Append(currentToken); Token joinedToken = Token.Join(joinSeq, ""); sequenceImage = joinedToken.Image; } else { sequenceImage = currentToken.Image; lastToken = null; } // We add a border to the orginal image. Gdk.Pixbuf sequencePixbuf = sequenceImage.CreatePixbuf(); Gdk.Pixbuf drawnImage = new Gdk.Pixbuf(sequencePixbuf.Colorspace, false, 8, sequencePixbuf.Width + 10, sequencePixbuf.Height + 10); drawnImage.Fill(0xFFFFFFFF); sequencePixbuf.CopyArea(0, 0, sequencePixbuf.Width, sequencePixbuf.Height, drawnImage, 5, 5); if (lastToken != null) { uint color; if (currentToken.CloseFollows(lastToken)) { color = 0x00FF00; sequencingStepResultLbl.Markup = String.Format("<b>Sí, el símbolo «{0}» se añadirá a la secuencia actual</b>", currentToken.Text); } else { color = 0xFF0000; sequencingStepResultLbl.Markup = String.Format("<b>No, «{0}» no puede ser considerado parte de la secuencia ({1})</b>", currentToken.Text, a.LastSequence.ToString()); } Gdk.Pixbuf markedImage = drawnImage.Copy(); // We paint the image of the color markedImage = markedImage.CompositeColorSimple(markedImage.Width, markedImage.Height, Gdk.InterpType.Nearest, 100, 1, color, color); // We are going to mark the image of the to symbols being considered // with their baselines. int min = int.MaxValue; foreach (Token t in a.LastSequence) { if (t.Top < min) { min = t.Top; } } int offset = Math.Min(min, currentToken.Top); int lastBaseline = lastToken.Baseline - offset; int currentBaseline = currentToken.Baseline - offset; markedImage.CopyArea(0, lastBaseline, markedImage.Width, 5, drawnImage, 0, lastBaseline); markedImage.CopyArea(0, currentBaseline, markedImage.Width, 5, drawnImage, 0, currentBaseline); } baselineImageArea.Image = drawnImage; } if (controller.StepMode == ControllerStepMode.StepByStep) { tokenizingNextButtonsAlign.Sensitive = true; } }); }