/// <summary> /// Ask the user for his permissions to perform a new lexical /// analysis on the selected sequence. /// </summary> /// <param name="sender"> /// A <see cref="System.Object"/> /// </param> /// <param name="arg"> /// A <see cref="EventArgs"/> /// </param> private void OnForceTokenizingItemActivate(object sender, EventArgs arg) { if (selectedNode.FoundToken != null || selectedNode.ChildCount > 0) { // We need that the user confirm the operation as, it is desctructive. ResponseType res = ConfirmDialog.Show(this.MainRecognizerWindow.Window, "Se van a perder los items identificados en la secuencia seleccionada (o sus hijos), ¿deseas continuar?"); if (res == ResponseType.No) { return; } } selectedNode.RemoveSequenceChildren(); selectedNode.FoundToken = null; List <LexicalRule> rules = Config.RecognizerConfig.Instance.LexicalRules; controller.SetLexicalRules(rules); ListStore store = new ListStore(typeof(string), typeof(string)); foreach (LexicalRule rule in rules) { store.AppendValues(rule.Name, String.Join(" | ", rule.LexicalExpressions.ToArray())); } this.tokenizingRulesTV.Model = store; // We set again the rules, because the user _should_ have modified them. controller.SetLexicalRules(Config.RecognizerConfig.Instance.LexicalRules); controller.SetSequenceForTokenizing(selectedNode); tokenizingButtonsNB.Page = 1; tokenizingNextButtonsAlign.Sensitive = true; // We lauch the tokenizer again. NextStep(ControllerStepMode.StepByStep); }
/// <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(); }