public MainViewModel(IFileService fileService, IGraphVizService graphVizService, IDfaService dfaService, ILanguageCheckService languageCheckService, IRegularExpressionParserService regularExpressionParserService) { _fileService = fileService; _graphVizService = graphVizService; _dfaService = dfaService; _languageCheckService = languageCheckService; _regularExpressionParserService = regularExpressionParserService; _words = new ObservableCollection <string>(); _file = new GraphVizFileModel(); OpenFileCommand = new RelayCommand(OpenFile, () => true); ParseFileCommand = new RelayCommand(ParseFile, ParseFileCanExecute); ShowAutomatonCommand = new RelayCommand(ShowAutomaton, ShowAutomatonCanExecute); VerifyStringCommmand = new RelayCommand(VerifyString, VerifyStringCanExecute); ParseRegularExpressionCommand = new RelayCommand(ParseRegularExpression, ParseRegularExpressionCanExecute); CopyRELinesCommand = new RelayCommand(CopyRELines, () => true); ShowAllWordsCommand = new RelayCommand(ShowAllWords, ShowAutomatonCanExecute); ConvertToDfaCommand = new RelayCommand(ConvertToDfa, ConvertToDfaCanExecute); CopyConvertLinesCommand = new RelayCommand(CopyConvertLines, () => true); RegularExpressionInput = "|(a,*b)"; //|(.(a,*(b)),*(c)) //|(*(a),.(b,c)) //"*(|(*(.(a,b)),c))" //|(a,*b) }
public GraphVizFileModel ConvertAutomatonToGenericFile(AutomatonModel automaton) { var graphVizFile = new GraphVizFileModel(); //ALPHABET string alphabetLine = ""; foreach (var letter in automaton.Alphabet) { alphabetLine += letter; } graphVizFile.Lines.Add(alphabet + " " + alphabetLine); //STATES string statesLine = ""; for (int index = 0; index < automaton.States.Count; index++) { var automatonState = automaton.States[index]; if (index == automaton.States.Count - 1) { statesLine += automatonState.Name; } else { statesLine += automatonState.Name + ","; } } graphVizFile.Lines.Add(states + " " + statesLine); //FINAL var finalStates = automaton.States.Where(x => x.IsFinal); var finalLine = ""; foreach (var finalState in finalStates) { finalLine += finalState.Name; } graphVizFile.Lines.Add(final + " " + finalLine); //TRANSITIONS graphVizFile.Lines.Add("transitions:"); foreach (var automatonTransition in automaton.Transitions) { var transitionLine = $"{automatonTransition.BeginState.Name},{automatonTransition.Value} --> {automatonTransition.EndState.Name}"; graphVizFile.Lines.Add(transitionLine); } //END. graphVizFile.Lines.Add("end."); return(graphVizFile); }
private Dictionary <string, bool> ReadWords(GraphVizFileModel graphVizFileModel, int lower, int upper) { var words = new Dictionary <string, bool>(); for (int i = lower; i < upper; i++) { var line = graphVizFileModel.Lines[i]; var chars = SplitOnComma(line); var isAccepted = chars[1] == "y"; words.Add(chars[0], isAccepted); } return(words); }
public GraphVizFileModel ConvertExpressionModelToGraphVizFile(ExpressionModel expressionModel) { var file = new GraphVizFileModel(); file.Lines.Add("graph logic {"); file.Lines.Add("node[fontname = \"Arial\"]"); var currentNode = expressionModel.TreeNodes.FirstOrDefault(); RecursivelyAddLines(file, currentNode); file.Lines.Add("}"); return(file); }
private void RecursivelyAddLines(GraphVizFileModel file, NodeModel currentNode) { //Add node to graph file.Lines.Add($"node{currentNode.Id} [ label = \"{currentNode.Value}\" ]"); //Add connection to childs if (currentNode.LeftChild != null) { file.Lines.Add($"node{currentNode.Id} -- node{currentNode.LeftChild.Id}"); RecursivelyAddLines(file, currentNode.LeftChild); } if (currentNode.RightChild != null) { file.Lines.Add($"node{currentNode.Id} -- node{currentNode.RightChild.Id}"); RecursivelyAddLines(file, currentNode.RightChild); } }
private List <TransitionModel> ReadRegularAutomatonTransitions(GraphVizFileModel graphVizFileModel, int lower, int upper, AutomatonModel automaton) { var transitions = new List <TransitionModel>(); for (int i = lower; i < upper; i++) { var transition = new TransitionModel(); var transitionString = graphVizFileModel.Lines[i]; var beginStateString = transitionString.Substring(0, transitionString.IndexOf(',')).Trim(); var valueString = transitionString.Substring(transitionString.IndexOf(',') + 1, transitionString.IndexOf('-') - 2) .Trim(); var endStateString = transitionString.Substring(transitionString.IndexOf('>') + 1).Trim(); foreach (var state in automaton.States) { if (state.Name.Equals(beginStateString) && i == lower) { transition.BeginState = state; state.IsInitial = true; } else if (state.Name.Equals(beginStateString)) { transition.BeginState = state; } if (state.Name.Equals(endStateString)) { transition.EndState = state; } if (transition.BeginState != null && transition.EndState != null) { break; } } valueString = valueString.ToLower(); if (valueString.Equals("_")) { valueString = "ε"; } transition.Value = valueString; transitions.Add(transition); } return(transitions); }
public GraphVizFileModel ConvertToGraphVizFile(AutomatonModel automaton) { var file = new GraphVizFileModel(); file.Lines.Add("digraph myAutomaton {"); file.Lines.Add("rankdir=LR;"); file.Lines.Add("\"\"[shape = none]"); //Add initials foreach (var automatonState in automaton.States) { if (automatonState.IsFinal && automatonState.IsInitial) { file.Lines.Add($"\"{automatonState.Name}\" [shape=doublecircle]"); file.Lines.Add($"\"\" -> \"{automatonState.Name}\""); } else if (automatonState.IsFinal) { file.Lines.Add($"\"{automatonState.Name}\" [shape=doublecircle]"); } else if (automatonState.IsInitial) { file.Lines.Add($"\"{automatonState.Name}\" [shape=circle]"); file.Lines.Add($"\"\" -> \"{automatonState.Name}\""); } else { file.Lines.Add($"\"{automatonState.Name}\" [shape=circle]"); } } //add transitions foreach (var transition in automaton.Transitions) { file.Lines.Add($"\"{transition.BeginState.Name}\" -> \"{transition.EndState.Name}\" [label=\"{transition.Value}\"]"); } file.Lines.Add("}"); return(file); }
public GraphVizFileModel ReadFile() { GraphVizFileModel fileModel = new GraphVizFileModel(); // Create OpenFileDialog Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog(); // Set filter for fileModel extension and default fileModel extension dlg.DefaultExt = ".txt"; dlg.Filter = "Text documents (.txt)|*.txt"; // Display OpenFileDialog by calling ShowDialog method var result = dlg.ShowDialog(); // Get the selected fileModel name and display in a TextBox if (result == true) { // Open document string filename = dlg.FileName; fileModel.FilePath = filename; fileModel.Lines = System.IO.File.ReadLines(filename).ToList(); } return(fileModel); }
private void ParsePrefix() { //Conversion to Infix and Distinct variables of expression Infix = _fixConversionService.ParsePrefix(_expressionModel); _fixConversionService.GetDistinctVariables(_expressionModel); //Display Graph: create GraphVizFileModel, write to dot-file, create and open png-file of graph _graphVizFileModel = _graphVizService.ConvertExpressionModelToGraphVizFile(_expressionModel); _fileService.WriteGraphVizFileToDotFile(_graphVizFileModel.Lines, "infix"); _graphVizService.DisplayGraph("infix"); //Create string for expressionModel.Binary with distinct values string distinctVariables = null; foreach (var c in _expressionModel.DistinctVariables) { distinctVariables += c + " "; } DistinctVariables = distinctVariables; //Build truth table TruthTable = new ObservableCollection <string>(_truthTableService.GetTruthTable(_expressionModel)); Hash = _truthTableService.CalculateHash(_expressionModel); //Simplification of truth table SimplifiedTruthTable = new ObservableCollection <string>(_truthTableService.SimplifyTruthTable(_expressionModel)); //Disjuntive normal form of input DisjunctiveNormalForm = _truthTableService.GetDisjunctiveNormalForm(_expressionModel); _expressionModelDisjunctiveNormalForm.Prefix = DisjunctiveNormalForm; _expressionModelDisjunctiveNormalForm.DistinctVariables = _expressionModel.DistinctVariables; //Calculate truth table and parse infix of DNF _fixConversionService.ParsePrefix(_expressionModelDisjunctiveNormalForm); _truthTableService.GetTruthTable(_expressionModelDisjunctiveNormalForm); //Get hash of DNF HashDisjunctiveNormalForm = _truthTableService.CalculateHash(_expressionModelDisjunctiveNormalForm); _graphVizFileModel = _graphVizService.ConvertExpressionModelToGraphVizFile(_expressionModelDisjunctiveNormalForm); _fileService.WriteGraphVizFileToDotFile(_graphVizFileModel.Lines, "dnf"); _graphVizService.DisplayGraph("dnf"); //Simplified Disjunctive normal form of input SimplifiedDisjunctiveNormalForm = _truthTableService.GetSimplifiedDisjunctiveNormalForm(_expressionModel); _expressionModelSimplifiedDisjunctiveNormalForm.Prefix = SimplifiedDisjunctiveNormalForm; _expressionModelSimplifiedDisjunctiveNormalForm.DistinctVariables = _expressionModel.DistinctVariables; _expressionModelSimplifiedDisjunctiveNormalForm.TruthTable.Rows = _expressionModel.TruthTable.Rows; _expressionModelSimplifiedDisjunctiveNormalForm.TruthTable.RowsSimplified = _expressionModel.TruthTable.RowsSimplified; //Calculate truth table and parse infix of Simpl. DNF _fixConversionService.ParsePrefix(_expressionModelSimplifiedDisjunctiveNormalForm); _truthTableService.RevertTruthTableSimplification(_expressionModelSimplifiedDisjunctiveNormalForm, _expressionModel.TruthTable.Binary); //Get hash of Simpl. DNF HashDisjunctiveNormalFormSimplified = _truthTableService.CalculateHash(_expressionModelSimplifiedDisjunctiveNormalForm); _graphVizFileModel = _graphVizService.ConvertExpressionModelToGraphVizFile(_expressionModelSimplifiedDisjunctiveNormalForm); _fileService.WriteGraphVizFileToDotFile(_graphVizFileModel.Lines, "sdnf"); _graphVizService.DisplayGraph("sdnf"); //Nand Nand = _fixConversionService.GetNandForm(_expressionModel); _expressionModelNand.Prefix = Nand; _expressionModelNand.DistinctVariables = _expressionModel.DistinctVariables; //Calculate truth table and parse infix of Nand _fixConversionService.ParsePrefix(_expressionModelNand); _truthTableService.GetTruthTable(_expressionModelNand); //Get has of Nand HashNand = _truthTableService.CalculateHash(_expressionModelNand); _graphVizFileModel = _graphVizService.ConvertExpressionModelToGraphVizFile(_expressionModelNand); _fileService.WriteGraphVizFileToDotFile(_graphVizFileModel.Lines, "nand"); _graphVizService.DisplayGraph("nand"); }
public AutomatonModel ParseGraphVizFile(GraphVizFileModel graphVizFileModel) { string states = "states:"; string final = "final:"; string alphabet = "alphabet:"; string stack = "stack:"; var automaton = new AutomatonModel(); foreach (var line in graphVizFileModel.Lines) { if (line.Contains(alphabet)) { var chars = GetSubstring(line, alphabet); foreach (var c in chars) { automaton.Alphabet.Add(char.ToLower(c)); } } else if (line.Contains(stack)) { var chars = GetSubstring(line, stack); foreach (var c in chars) { automaton.AccecptedStack.Add(char.ToLower(c).ToString()); } automaton.IsPda = true; } else if (line.Contains(states)) { var sub = GetSubstring(line, states); var chars = SplitOnComma(sub); foreach (var c in chars) { var state = new StateModel() { Name = c.ToUpper() }; automaton.States.Add(state); } } else if (line.Contains(final)) { var sub = GetSubstring(line, final); var chars = SplitOnComma(sub); foreach (var c in chars) { foreach (var state in automaton.States) { if (c.Equals(state.Name)) { state.IsFinal = true; } } } } else if (line.Contains("transitions")) { var lower = graphVizFileModel.Lines.IndexOf(line) + 1; //todo: change upper to next "end." var upper = FindUpperLimit(graphVizFileModel.Lines.IndexOf(line), graphVizFileModel.Lines); var transitions = new List <TransitionModel>(); transitions = !automaton.IsPda ? ReadRegularAutomatonTransitions(graphVizFileModel, lower, upper, automaton) : ReadPdaTransitions(graphVizFileModel, lower, upper, automaton); automaton.Transitions = transitions; //break; } else if (line.Contains("words")) { var lower = graphVizFileModel.Lines.IndexOf(line) + 1; var upper = FindUpperLimit(graphVizFileModel.Lines.IndexOf(line), graphVizFileModel.Lines); automaton.Words = ReadWords(graphVizFileModel, lower, upper); } else if (line.Contains("dfa")) { var last = line[line.Length - 1]; automaton.IsDfaInFile = last == 'y'; } else if (line.Contains("finite")) { var last = line[line.Length - 1]; automaton.IsFiniteInFile = last == 'y'; } } return(automaton); }
private List <TransitionModel> ReadPdaTransitions(GraphVizFileModel graphVizFileModel, int lower, int upper, AutomatonModel automaton) { var transitions = new List <TransitionModel>(); for (int i = lower; i < upper; i++) { var transition = new TransitionModel(); var transitionString = graphVizFileModel.Lines[i]; var beginStateString = transitionString.Substring(0, transitionString.IndexOf(',')).Trim(); string valueString = "", leftStackString = "", rightStackString = ""; if (transitionString.Contains("[")) { valueString = transitionString.Substring(transitionString.IndexOf(',') + 1, transitionString.IndexOf('[') - 2) .Trim(); leftStackString = transitionString.Substring(transitionString.IndexOf('[') + 1, transitionString.IndexOf(',')).ToLower(); rightStackString = transitionString.Substring(transitionString.IndexOf(',', transitionString.IndexOf(',') + 1) + 1, 1).ToLower(); } else { valueString = transitionString.Substring(transitionString.IndexOf(',') + 1, transitionString.IndexOf(',') + 3) .Trim(); leftStackString = "_"; rightStackString = "_"; } var endStateString = transitionString.Substring(transitionString.IndexOf('>') + 1).Trim(); foreach (var state in automaton.States) { if (state.Name.Equals(beginStateString) && i == lower) { transition.BeginState = state; state.IsInitial = true; } else if (state.Name.Equals(beginStateString)) { transition.BeginState = state; } if (state.Name.Equals(endStateString)) { transition.EndState = state; } if (transition.BeginState != null && transition.EndState != null) { break; } } valueString = valueString.ToLower(); if (valueString.Equals("_")) { valueString = "ε"; } if (leftStackString.Equals("_")) { leftStackString = "ε"; } if (rightStackString.Equals("_")) { rightStackString = "ε"; } transition.PushStack = rightStackString; transition.PopStack = leftStackString; transition.Value = valueString; transitions.Add(transition); } return(transitions); }