//****************************************************************** #region [PopulateTree() Method] //****************************************************************** /// <summary> /// Displays the replace-pattern tree specified by the /// ReplacePatternRoot property of the given TreeTransfer object. /// </summary> public void PopulateTree(TreeTransfer oTreeTransfer) { //************************************************************** // The viewer must be set up to display replace-pattern trees. Debug.Assert(moParseTreeViewer.DisplayReplacePattern); Debug.Assert(moParseTreeViewer.ShowFeatures); Debug.Assert(moParseTreeViewer.ShowMorphology); //************************************************************** // Display the replace-pattern tree. moParseTreeViewer.PopulateTree(oTreeTransfer); }
//****************************************************************** // CONTROL EVENTS //****************************************************************** #region [Timer Tick Event Handler] //****************************************************************** /// <summary> /// The timer is fired once after the form is loaded, and this event /// handler performs the tree-transfer processing. It uses the /// collection of transfer rules to process the parse trees in the /// input file, and it writes the modified parse trees to the output /// file. /// </summary> private void moTimer_Tick(object oSender, EventArgs oArgs) { ParseReader oParseReader = null; ParseWriter oParseWriter = null; try { //********************************************************** // Stop the timer (so it only fires once after the form is // loaded). moTimer.Stop(); //********************************************************** // Initialize the information displayed on the form. Text = "Processing Parse File..."; moInputFileTextBox.Text = InputFileName; moOutputFileTextBox.Text = OutputFileName; moRuleCountTextBox.Text = TransferRules.Count.ToString(); moParseCountTextBox.Text = "0"; moInputFileTextBox.Select( moInputFileTextBox.Text.Length, 0); moOutputFileTextBox.Select( moOutputFileTextBox.Text.Length, 0); moRuleCountTextBox.Select( moRuleCountTextBox.Text.Length, 0); moParseCountTextBox.Select( moParseCountTextBox.Text.Length, 0); moParseCountTextBox.Focus(); moOKButton.Enabled = false; moCancelButton.Enabled = true; //********************************************************** // Make sure the form is visible. Show(); Application.DoEvents(); //********************************************************** // Open the input and output files. StreamReader oStreamReader = new StreamReader(InputFileName); oParseReader = new ParseReader(oStreamReader); StreamWriter oStreamWriter = new StreamWriter(OutputFileName); oParseWriter = new ParseWriter(oStreamWriter); //********************************************************** // Read and process each parse tree from the input file. int iCount = 0; ProcessingWasCanceled = false; while ((oParseReader.Read()) && (!ProcessingWasCanceled)) { //****************************************************** // Create a TreeTransfer object and set its // ParseTreeRoot to the parse tree that was read from // the input file. TreeTransfer oTransfer = new TreeTransfer(); oTransfer.ParseTreeRoot = oParseReader.ParseTreeRoot; //****************************************************** // Apply each rule in the TransferRules collection. foreach (TransferRule oRule in TransferRules) { oTransfer.FindPatternRoot = oRule.FindPatternRoot; oTransfer.ReplacePatternRoot = oRule.ReplacePatternRoot; //************************************************** // Apply the rule to each matching branch. oTransfer.CurrentParseTreeNode = null; while (oTransfer.FindNextMatchingBranch()) { oTransfer.ReplaceCurrentMatchingBranch(); } } //****************************************************** // Write the modified parse tree to the output file. oParseWriter.ParseTreeRoot = oTransfer.ParseTreeRoot; oParseWriter.Write(); //****************************************************** // Update the information displayed on the form. ++iCount; moParseCountTextBox.Text = iCount.ToString(); moParseCountTextBox.Select( moParseCountTextBox.Text.Length, 0); //****************************************************** // Call DoEvents() so the form can update the display // and check if the user clicked the Cancel button. Application.DoEvents(); } //********************************************************** // Close the input and output files. oParseReader.Close(); oParseReader = null; oParseWriter.Close(); oParseWriter = null; //********************************************************** // Update the final information displayed on the form. if (ProcessingWasCanceled) { Text = "Processing Parse File... Canceled"; } else { Text = "Processing Parse File... Finished"; } moOKButton.Enabled = true; moCancelButton.Enabled = false; moOKButton.Focus(); //********************************************************** // If the Cancel button was clicked, set DialogResult to // DialogResult.Cancel, which will close the form. // // If the Cancel button was not clicked, but the // CloseWhenFinished property is true, set DialogResult to // DialogResult.OK, which will close the form. if (ProcessingWasCanceled) { DialogResult = DialogResult.Cancel; } else { if (CloseWhenFinished) { DialogResult = DialogResult.OK; } } } catch (Exception oException) { ShowException(oException); ProcessingWasCanceled = true; } try { //********************************************************** // Make sure the output file is closed (even if an exception // was thrown). if (oParseWriter != null) { oParseWriter.Close(); oParseWriter = null; } } catch (Exception oException) { ShowException(oException); ProcessingWasCanceled = true; } try { //********************************************************** // Make sure the OK button is enabled (even if an exception // was thrown). moOKButton.Enabled = true; moCancelButton.Enabled = false; moOKButton.Focus(); } catch (Exception oException) { ShowException(oException); ProcessingWasCanceled = true; } try { //********************************************************** // If the Cancel button was clicked (or an exception was // thrown), make sure DialogResult is set to // DialogResult.Cancel, which will close the form. if (ProcessingWasCanceled) { DialogResult = DialogResult.Cancel; } } catch (Exception oException) { ShowException(oException); } }