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; } }
private void btnMinimizeDfa_Click(object sender, RoutedEventArgs e) { if (diagram.isErroneous()) { return; } DfaTransitionDiagram diagTemp = ((DfaTransitionDiagram)diagram).minimizeStates(); if (diagTemp != null)//is null on errors { diagram = diagTemp; setInterfaceOnMachineLoad(); fileStatus.isEdited = true; } }
public DfaTransitionDiagram minimizeStates() { List <List <State> > pi = new List <List <State> >(); //initialize pi pi.Add(this.finalStates); List <State> temp = new List <State>(); //find non final states foreach (State s in this.states) { if (s.type == stateType.intermediate || s.type == stateType.initial) { temp.Add(s); } } if (temp.Count != 0) { pi.Add(temp); } //now pi has two sets: finals and non-finals if (pi.Count == 1) // i.e. pi has only one set because all the states are either final or both (initial & final) { MessageBox.Show("The diagram is already minimized"); return(this); } DfaTransitionDiagram newDiagram = new DfaTransitionDiagram(); newDiagram.inputs = this.inputs; newDiagram.noOfInputs = this.noOfInputs; newDiagram.setCanvas(canvasMain); List <List <State> > newPi = pi; // for the initial condition List <List <State> > set; while (true) { pi = newPi; set = pi; List <List <State> > piTemp = null; foreach (String input in inputs) { piTemp = new List <List <State> >(); foreach (List <State> ls in set) { if (ls.Count == 1) { //beacuse if a set contains only a single item then it will remain single piTemp.Add(new List <State>()); piTemp.Last().Add(ls[0]); continue; } List <int> abc = new List <int>(); foreach (State s in ls) { State stt = table[s.index, getIndexOfInput(input)]; if (stt == null) { MessageBox.Show("To minimize a DFA, For each State you must define transitions for all input symbols"); return(null); } else { abc.Add(piTemp.Count + getSetIndex(pi, stt)); } } //pitemp.count is added because before that it was problematic for the iteration //with the next set on same input symbol due to same 0 1 etc in abc for (int i = 0; i < abc.Count; i++) { piTemp.Add(new List <State>()); } while (piTemp.Count <= abc.Max()) { piTemp.Add(new List <State>()); } int j = 0; foreach (int i in abc) { piTemp[i].Add(ls[j]); j++; } // remove empty lists List <List <State> > t = new List <List <State> >(); // to bypass the collection modified error foreach (List <State> ls2 in piTemp) { if (ls2.Count == 0) { t.Add(ls2); } } foreach (List <State> ls2 in t) { piTemp.Remove(ls2); } } set = piTemp; } newPi = piTemp; if (isPiEqualsNewPi(pi, newPi)) { break; } } canvasMain.Children.Clear(); // now create a new diagram by taking states from pi foreach (List <State> ls in pi) { State s = newDiagram.addState(State.getNewStatePosition()); s.figure.ToolTip = getToolTip(ls); if (containsInitialState(ls)) { s.addStartingArrow(canvasMain); newDiagram.initialStates.Add(s); s.type = stateType.initial; if (containsFinalState(ls)) { s.type = stateType.both; newDiagram.finalStates.Add(s); s.figure.Stroke = Brushes.Black; s.figure.StrokeThickness = 2; } } else if (containsFinalState(ls)) { s.type = stateType.final; newDiagram.finalStates.Add(s); s.figure.Stroke = Brushes.Black; s.figure.StrokeThickness = 2; } } foreach (List <State> ls in pi) { State source = ls[0]; foreach (string input in newDiagram.inputs) { State dest = table[ls[0].index, getIndexOfInput(input)]; if (dest != null) { source = getStateByToolTip(newDiagram, getToolTip(ls)); foreach (List <State> ls2 in pi) { if (ls2.Contains(dest)) { dest = getStateByToolTip(newDiagram, getToolTip(ls2)); break; } } if (source != null && dest != null) { Transition tr = newDiagram.addTransition(source, dest); if (tr != null) { ((ComboBox)((StackPanel)tr.figure.Tag).Children[0]).SelectedItem = input; } } } } } //code inserted later to patch the flaws in algorithm bool isDeletedSomeState = true; while (isDeletedSomeState) { isDeletedSomeState = false; bool isOrphanState; List <State> tmp = new List <State>(newDiagram.states); foreach (State s in tmp) { isOrphanState = true; bool hasSource = false, hasDest = false; foreach (Transition t in s.transitions) { if (s.type == stateType.initial || s.type == stateType.final || s.type == stateType.both) { isOrphanState = false; break; } if (t.destState.index == s.index) { hasDest = true; } if (t.sourceState.index == s.index) { hasSource = true; } if (hasSource && hasDest) { isOrphanState = false; break; } } if (isOrphanState) { newDiagram.selectedState = s; newDiagram.deleteState(); isDeletedSomeState = true; } } } return(newDiagram); }
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 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 }