Note that there are some explanatory texts on larger screens.

plurals
  1. POC# Undo/Redo with trees and threading
    primarykey
    data
    text
    <p>I've been searching for hours and haven't been able to find anything remotely similar to this question, even though im sure it must have been done before :(</p> <p>The user first types in a combination of numbers and +/- signs. E.g: 5 + (7-(4+1)-3) + 11. I have a class Add and a class Subtract, the program builds a tree of these two classes from the input. So in this example the tree would look something like:</p> <pre><code>Add: 5 Subtract: 7 11 Add: 4 3 1 </code></pre> <p>Now each class has a recursive StepEvaluate method which first prints to the console the entire sum. So for the Subtract subtree of the previous example, that object would print "7 - (4+1) - 3". It would then list each node of the tree and ask a user to click a branch to add/subtract to the running total of initially 0. It would finally Print the answer at the end. So if we take 5 + (7-3) + 11, the output to the console would be:</p> <pre><code>5 + (7-3) + 11 We are going to add to the total with each choice. The current total is 0. Choose from the following: 5 + 7-3 + 11 You chose: 11 The current total is 11. Choose from the following: 5 + 7-3 You chose: 7-3 7 - 3 We are going to subtract from the total with each choice. The current total is 0. Choose from the following: 7 - 3 You chose: -3 The current total is -3. The only remaining choice is 7. The current total is 4. So the answer is 4. The current total is 15. The only remaining choice is 5. The current total is 20. So the answer is 20. </code></pre> <p>Now my console is a textbox. In the form constructor, a new thread, Eval, is made to evaluate and output the above results. Each time the user presses the button, it resumes the thread Eval and pauses the current thread. Eval then works out the next string to output (using the recursive classes above), writes the string to a static variable, resumes execution of the main thread and pauses execution of Eval. The main thread then reads those static variables and outputs it to the textbox (producer-consumer). In between button clicks, the inputs by the user are written to static variables so Eval can read these once resumed.</p> <p>I want to have another button on the form which will go back a line. If this erases the line "You chose A" then the user can change his choice, so the next output can be "You chose B".</p> <p>I have read up on the memento and command patterns, and know about state based and action based undo/redo. However I have no clue how to implement this with the above recursion and producer/consumer threads. All I really need is a way to make the point of execution of Eval go back a line. But I'm pretty sure I can't do that...</p> <p>If you want the code for Add and Subtract, here you go:</p> <pre><code>class Add : MathsOperation { public List&lt;MathsOperation&gt; Children; private int total; public override void StepEvaluate() { MathsOperation currentEvaluation; writeToConsole(this.ToString()); List&lt;MathsOperation&gt; toBeEvaluated = new List&lt;MathsOperation&gt;(Children); writeToConsole("We are going to add to the total with each choice."); while (toBeEvaluated.Count &gt; 1) { writeToConsole("The current total is " + total + "."); writeToConsole("Please choose from the following:"); string choices = ""; foreach (MathsOperation child in toBeEvaluated) { choices += Environment.NewLine + child.ToString(); if (child != Children.Last()) choices += Environment.NewLine + "+"; } writeToConsole(choices); currentEvaluation = read(); if (currentEvaluation is Number) { //add to the total and write output } else { currentEvaluation.StepEvaluate(); //add the static variable Result to the total and write output } toBeEvaluated.Remove(currentEvaluation); } currentEvaluation = toBeEvaluated.First(); writeToConsole("The only remaining choice is " + currentEvaluation.ToString()); if (currentEvaluation is Number) { //add to the total and write output } else { currentEvaluation.StepEvaluate(); //add the static variable Result to the total and write output } toBeEvaluated.Remove(currentEvaluation); writeToConsole("So the answer is " + total.ToString()); //write total to static variable Result } private void writeToConsole(string txt) { //Write to static variable, resume main thread and pause this one } private MathsOperation read() { //read the static variable. I have replaced this with return new for simplicity return new MathsOperation(); } } </code></pre> <p>If you have any ideas, I'll be more than happy to accept them. </p> <p>Thank you in advanced for your help.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload