private void btnClear_Click(object sender, RoutedEventArgs e) { if (canvasMain.Children.Count == 0) { return; } else { if (MessageBox.Show("Are you sure to clear the Page", "Clear?", MessageBoxButton.YesNo) == MessageBoxResult.No) { return; } } grpStateOptions.IsEnabled = false; grpTransOptions.IsEnabled = false; btnInitial.IsChecked = btnFinal.IsChecked = false; TransitionDiagram newDiag = null; switch (machine) { case machines.Dfa: newDiag = new DfaTransitionDiagram(); break; case machines.Nfa: newDiag = new NfaTransitionDiagram(); break; case machines.Moore: newDiag = new MooreTransitionDiagram(); break; case machines.Mealy: newDiag = new MealyTransitionDiagram(); break; case machines.Turing: newDiag = new TuringTransitionDiagram(); break; } if (newDiag != null) { newDiag.inputs = diagram.inputs; newDiag.noOfInputs = diagram.noOfInputs; newDiag.outputs = diagram.outputs; canvasMain.Children.Clear(); newDiag.setCanvas(canvasMain); diagram = newDiag; } }
public DfaTransitionDiagram convertToDfa() { NfaTransitionDiagram diagram = this.removeNullMoves(); DfaTransitionDiagram newDiagram = new DfaTransitionDiagram(); newDiagram.setCanvas(canvasMain); canvasMain.Children.Clear(); newDiagram.inputs = diagram.inputs; newDiagram.noOfInputs = diagram.noOfInputs; State.resetPoints(); // because the some state positions have been consumed by removeNullMoves(), so reset them //add the initial state State iState = newDiagram.addState(State.getNewStatePosition()); iState.addStartingArrow(canvasMain); iState.type = diagram.initialStates[0].type; newDiagram.initialStates.Add(iState); if (iState.type == stateType.both) { newDiagram.finalStates.Add(iState); iState.figure.Stroke = Brushes.Black; iState.figure.StrokeThickness = 2; } iState.figure.ToolTip = diagram.initialStates[0].figure.ToolTip; newDiagram.selectedState = iState; List <List <State> > newStates = new List <List <State> >(); List <State> temp = new List <State>(); temp.Add(iState); newStates.Add(temp); string tooltip; int i = -1; while (i < newStates.Count - 1) { List <State> currentState = newStates[++i]; newDiagram.selectedState = getStateByToolTip(newDiagram, getToolTip(currentState)); foreach (string input in newDiagram.inputs) { List <State> nextState = new List <State>(); foreach (State s in currentState) { if (diagram.table[s.index, diagram.getIndexOfInput(input)] != null) { foreach (State t in diagram.table[s.index, diagram.getIndexOfInput(input)]) { if (!nextState.Contains(t)) { nextState.Add(t); } } } } tooltip = getToolTip(nextState); if (tooltip != "") { State x = getStateByToolTip(newDiagram, tooltip); if (x == null) //nextState do not exists in the newStates or newDiagram.States { // a new state is found State s = newDiagram.addState(State.getNewStatePosition()); s.figure.ToolTip = tooltip; if (containsFinalState(nextState)) { s.type = stateType.final; newDiagram.finalStates.Add(s); s.figure.Stroke = Brushes.Black; s.figure.StrokeThickness = 2; } Point pt1 = new Point(); Random rnd = new Random(); pt1.X = (double)rnd.Next((int)canvasMain.ActualWidth); pt1.X = (double)rnd.Next((int)canvasMain.ActualHeight); Transition tr = newDiagram.addTransition(newDiagram.selectedState, s, pt1.ToString()); ((ComboBox)((StackPanel)tr.figure.Tag).Children[0]).SelectedItem = input; newStates.Add(nextState); } else { Transition tr = newDiagram.addTransition(newDiagram.selectedState, x); if (tr != null) { ((ComboBox)((StackPanel)tr.figure.Tag).Children[0]).SelectedItem = input; } } } } } return(newDiagram); }
public NfaTransitionDiagram removeNullMoves() { if (!this.inputs.Contains("^")) { return(this); } bool conatainNullMoves = false; //if ^ is present in input alphabet but there are no null transitions foreach (Transition t in this.transitions) { if (t.input == "^") { conatainNullMoves = true; break; } } if (!conatainNullMoves) { MessageBox.Show("The diagram contains no ^ Moves"); return(this); } NfaTransitionDiagram newDiagram = new NfaTransitionDiagram(); canvasMain.Children.Clear(); newDiagram.setCanvas(canvasMain); string temp = ""; if (!this.inputs.Contains <string>("^")) { newDiagram.inputs = inputs; } else { foreach (string s in inputs) { if (s != "^") { temp += s + ";"; } } newDiagram.inputs = temp.Trim(';').Split(';'); newDiagram.noOfInputs = temp.Trim(';').Split(';').Length; } List <List <State> > newStates = new List <List <State> >(); List <List <State> > tempp = new List <List <State> >(); foreach (State initialState in initialStates) { tempp.Add(nullClosure(initialState)); } newStates.Add(tempp[0]); for (int j = 1; j < tempp.Count; j++) { List <State> ls = tempp[j]; foreach (State s in ls) { if (!newStates[0].Contains(s)) { newStates[0].Add(s); } } } //add the initial state State iState = newDiagram.addState(State.getNewStatePosition()); iState.addStartingArrow(canvasMain); newDiagram.initialStates.Add(iState); iState.type = stateType.initial; if (containsFinalState(newStates[0])) { newDiagram.finalStates.Add(iState); iState.type = stateType.both; iState.figure.Stroke = Brushes.Black; iState.figure.StrokeThickness = 2; } string tooltip = getToolTip(newStates[0]); iState.figure.ToolTip = tooltip; newDiagram.selectedState = iState; int i = -1; while (i < newStates.Count - 1) { List <State> currentState = newStates[++i]; newDiagram.selectedState = getStateByToolTip(newDiagram, getToolTip(currentState)); foreach (string input in newDiagram.inputs) { List <State> nextState = new List <State>(); List <State> next = new List <State>(); foreach (State s in currentState) { if (table[s.index, getIndexOfInput(input)] != null) { foreach (State t in table[s.index, getIndexOfInput(input)]) { if (!next.Contains(t)) { next.Add(t); } } } } // now next contains the next state but no null closures are found yet if (next.Count == 0) { nextState = next; } else { foreach (State st in next) { foreach (State p in nullClosure(st)) { if (!nextState.Contains(p)) { nextState.Add(p); } } } } tooltip = getToolTip(nextState); if (tooltip != "") { State x = getStateByToolTip(newDiagram, tooltip); if (x == null) //nextState do not exists in the newStates or newDiagram.States { // a new state is found State s = newDiagram.addState(State.getNewStatePosition()); s.figure.ToolTip = tooltip; if (containsFinalState(nextState)) { s.type = stateType.final; newDiagram.finalStates.Add(s); s.figure.Stroke = Brushes.Black; s.figure.StrokeThickness = 2; } Point pt1 = new Point(); Random rnd = new Random(); pt1.X = (double)rnd.Next((int)canvasMain.ActualWidth); pt1.X = (double)rnd.Next((int)canvasMain.ActualHeight); Transition tr = newDiagram.addTransition(newDiagram.selectedState, s, pt1.ToString()); ((ComboBox)((StackPanel)tr.figure.Tag).Children[0]).SelectedItem = input; newStates.Add(nextState); } else { Transition tr = newDiagram.addTransition(newDiagram.selectedState, x); if (tr != null) { ((ComboBox)((StackPanel)tr.figure.Tag).Children[0]).SelectedItem = input; } } } } } return(newDiagram); }
public void newMachine(machines type, TransitionDiagram newDiagram = null) { if (newDiagram != null) // don't clear the canvas, i.e. we are only updating the interface on conversions etc { ; } else { if (saveOnNewOrExit() == MessageBoxResult.Cancel) { return; } fileStatus.isEdited = false; loadedFilePath = ""; grammar = null; pda = null; mode = modes.Machines; GrammarTab.Visibility = pdaTab.Visibility = Visibility.Collapsed; transitionDiagramTab.Visibility = Visibility.Visible; ExecuteTab.Visibility = Visibility.Visible; ribbonMain.Visibility = Visibility.Visible; transitionDiagramTab.IsSelected = true; clearAllTextBoxes(); } //restore original settings btnFinal.IsChecked = btnInitial.IsChecked = false; btnAddState.IsChecked = btnAddTrans.IsChecked = btnSelect.IsChecked = false; btnNull.IsChecked = false; grpSymbols.IsEnabled = false; grpToolbox.IsEnabled = false; grpSelection.IsEnabled = false; btnStatistics.IsEnabled = false; grpStateOptions.IsEnabled = false; btnFinal.IsEnabled = true;// disabled in turing, moore, mealy grpTransOptions.IsEnabled = false; grpStringAcceptance.Visibility = Visibility.Collapsed; grpOutputProducer.Visibility = Visibility.Collapsed; btnNull.IsEnabled = false; //because they are changed in turing machine interface txtInput.Width = txtOutput.Width = 155; lblInput.Content = "Input "; //changed to input tape in turnig lblOutput.Content = "Output"; //changed to output tape in turnig grpDfaOptions.Visibility = grpNfaOptions.Visibility = grpMooreMealyOptions.Visibility = Visibility.Collapsed; switch (type) { case machines.Dfa: machine = machines.Dfa; if (newDiagram == null) { newDiagram = new DfaTransitionDiagram(); } diagram = newDiagram; spInputSymbols.IsEnabled = true; spOutputSymbols.IsEnabled = false; grpDfaOptions.Visibility = Visibility.Visible; grpStringAcceptance.Visibility = Visibility.Visible; Application.Current.MainWindow.Title = "DFA"; break; case machines.Nfa: machine = machines.Nfa; if (newDiagram == null) { newDiagram = new NfaTransitionDiagram(); } diagram = newDiagram; btnNullClosure.IsEnabled = false; // to be enabled on state selection spInputSymbols.IsEnabled = true; spOutputSymbols.IsEnabled = false; btnNull.IsEnabled = true; grpNfaOptions.Visibility = Visibility.Visible; grpStringAcceptance.Visibility = Visibility.Visible; Application.Current.MainWindow.Title = "NFA"; break; case machines.Moore: machine = machines.Moore; if (newDiagram == null) { newDiagram = new MooreTransitionDiagram(); } diagram = newDiagram; spInputSymbols.IsEnabled = true; spOutputSymbols.IsEnabled = true; btnFinal.IsEnabled = false; grpMooreMealyOptions.Visibility = Visibility.Visible; btnConvertToMoore.Visibility = Visibility.Collapsed; btnConvertToMealy.Visibility = Visibility.Visible; grpMooreMealyOptions.Header = "Moore Options"; grpOutputProducer.Visibility = Visibility.Visible; Application.Current.MainWindow.Title = "Moore Machine"; break; case machines.Mealy: machine = machines.Mealy; if (newDiagram == null) { newDiagram = new MealyTransitionDiagram(); } diagram = newDiagram; spInputSymbols.IsEnabled = true; spOutputSymbols.IsEnabled = true; btnFinal.IsEnabled = false; grpMooreMealyOptions.Visibility = Visibility.Visible; btnConvertToMoore.Visibility = Visibility.Visible; btnConvertToMealy.Visibility = Visibility.Collapsed; grpMooreMealyOptions.Header = "Mealy Options"; grpOutputProducer.Visibility = Visibility.Visible; Application.Current.MainWindow.Title = "Mealy Machine"; break; case machines.Turing: machine = machines.Turing; if (newDiagram == null) { newDiagram = new TuringTransitionDiagram(); } diagram = newDiagram; lblInput.Content = "Input Tape "; lblOutput.Content = "Output Tape"; spInputSymbols.IsEnabled = true; spOutputSymbols.IsEnabled = false; btnFinal.IsEnabled = false; grpOutputProducer.Visibility = Visibility.Visible; txtInput.Width = txtOutput.Width = 500; Application.Current.MainWindow.Title = "Turing Machine"; break; case machines.None: return; // deselected above } //enable common options grpSymbols.IsEnabled = true; grpToolbox.IsEnabled = true; grpSelection.IsEnabled = true; btnStatistics.IsEnabled = true; btnAddTrans.IsEnabled = false; State.resetLabels(); btnOk.IsEnabled = true; diagram.setCanvas(canvasMain); btnAddState.IsChecked = true; btnAddState_Click(btnAddState, null); // to set the mode as addState }