Skip to content

Commit

Permalink
Fix issues
Browse files Browse the repository at this point in the history
- Add better message when unhandled exception happen
- Detect recursive graphs
  • Loading branch information
Borjianamin98 committed Apr 28, 2020
1 parent 18e62e6 commit 9f17116
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 12 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>ir.ac.sbu</groupId>
<artifactId>PGen</artifactId>
<version>2.2.0</version>
<version>2.2.1</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down
17 changes: 16 additions & 1 deletion src/main/java/ir/ac/sbu/controller/MainController.java
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,9 @@ public void parserCheckGraphs(ActionEvent actionEvent) {
DialogUtility.showSuccessDialog("There is no problem!");
} catch (TableException e) {
DialogUtility.showErrorDialog(e.getMessages());
} catch (Exception e) {
DialogUtility.showErrorDialog(String.format("Unhandled exception happened %s.\nPlease send your " +
"PGen file to us. Thanks.", e.toString()));
}
}

Expand All @@ -247,6 +250,9 @@ private void parserBuildTable(ActionEvent actionEvent) {
}
} catch (TableException e) {
DialogUtility.showErrorDialog(e.getMessages());
} catch (Exception e) {
DialogUtility.showErrorDialog(String.format("Unhandled exception happened %s.\nPlease send your " +
"PGen file to us. Thanks.", e.toString()));
}
}

Expand All @@ -261,6 +267,9 @@ private void parserBuildPrettyTable(ActionEvent actionEvent) {
}
} catch (TableException e) {
DialogUtility.showErrorDialog(e.getMessages());
} catch (Exception e) {
DialogUtility.showErrorDialog(String.format("Unhandled exception happened %s.\nPlease send your " +
"PGen file to us. Thanks.", e.toString()));
}
}

Expand All @@ -275,6 +284,9 @@ private void parserBuildCsvTable(ActionEvent actionEvent) {
}
} catch (TableException e) {
DialogUtility.showErrorDialog(e.getMessages());
} catch (Exception e) {
DialogUtility.showErrorDialog(String.format("Unhandled exception happened %s.\nPlease send your " +
"PGen file to us. Thanks.", e.toString()));
}
}

Expand Down Expand Up @@ -309,7 +321,10 @@ public void parserExportFullParser(ActionEvent actionEvent) {
} catch (TableException e) {
DialogUtility.showErrorDialog(e.getMessages());
} catch (IOException e) {
DialogUtility.showErrorDialog(e.getMessage());
DialogUtility.showErrorDialog("Exception during exporting files", e.getMessage());
} catch (Exception e) {
DialogUtility.showErrorDialog(String.format("Unhandled exception happened %s.\nPlease send your " +
"PGen file to us. Thanks.", e.toString()));
}
}

Expand Down
6 changes: 6 additions & 0 deletions src/main/java/ir/ac/sbu/exception/TableException.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ir.ac.sbu.exception;

import java.util.ArrayList;
import java.util.List;

public class TableException extends Exception {
Expand All @@ -9,6 +10,11 @@ public TableException(List<String> messages) {
this.messages = messages;
}

public TableException(String message) {
this.messages = new ArrayList<>();
messages.add(message);
}

public List<String> getMessages() {
return messages;
}
Expand Down
38 changes: 29 additions & 9 deletions src/main/java/ir/ac/sbu/parser/LLParserGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import ir.ac.sbu.model.EdgeModel;
import ir.ac.sbu.model.GraphModel;
import ir.ac.sbu.model.NodeModel;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
Expand Down Expand Up @@ -404,18 +405,31 @@ private Set<Integer> getNullableNodes() {
return nullableNodes;
}

private Map<Integer, Set<String>> getTotalFirstSets() {
private Map<Integer, Set<String>> getTotalFirstSets() throws TableException {
Map<Integer, Set<String>> firsts = new HashMap<>();
Set<Integer> visited = new HashSet<>();
for (NodeModel nodeModel : allNodes) {
calculateFirstSet(firsts, visited, nodeModel);
try {
for (NodeModel nodeModel : allNodes) {
calculateFirstSet(firsts, visited, nodeModel);
}
return firsts;
} catch (TableException e) {
// First element is message title and others are path in DFS search.
throw new TableException(
e.getMessages().get(0) + e.getMessages().subList(1, e.getMessages().size()));
}
return firsts;
}

private Set<String> calculateFirstSet(Map<Integer, Set<String>> firstSets, Set<Integer> visited, NodeModel nodeModel) {
private Set<String> calculateFirstSet(Map<Integer, Set<String>> firstSets, Set<Integer> visited,
NodeModel nodeModel) throws TableException {
if (visited.contains(nodeModel.getId())) {
return firstSets.get(nodeModel.getId());
Set<String> lastCalculatedFirstSet = firstSets.get(nodeModel.getId());
if (lastCalculatedFirstSet == null) {
// We use DFS to calculate first sets. If in DFS tree, node 'A' was children of itself directly
// or indirectly, it means we can move in 'A' infinitely.
throw new TableException(nodeModel.getGraph() + " graph call itself recursively: ");
}
return lastCalculatedFirstSet;
}
visited.add(nodeModel.getId());
Set<String> firstOfCurrentNode = new HashSet<>();
Expand All @@ -428,9 +442,15 @@ private Set<String> calculateFirstSet(Map<Integer, Set<String>> firstSets, Set<I
*/
for (EdgeModel edgeModel : nodeModel.getAdjacent()) {
if (edgeModel.isGraph()) {
Set<String> firstSetOfGraph = calculateFirstSet(firstSets, visited,
variableGraph.get(edgeModel.getToken()).getStart());
firstOfCurrentNode.addAll(firstSetOfGraph);
try {
Set<String> firstSetOfGraph = calculateFirstSet(firstSets, visited,
variableGraph.get(edgeModel.getToken()).getStart());
firstOfCurrentNode.addAll(firstSetOfGraph);
} catch (TableException e) {
// Add illegal graph to message list
e.getMessages().add(nodeModel.getGraph().toString());
throw e;
}
if (nullableVariables.contains(edgeModel.getToken()) &&
edgeModel.getEnd().getId() != nodeModel.getId()) {
Set<String> firstSetOfRestOfGraph = calculateFirstSet(firstSets, visited,
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/ir/ac/sbu/utility/DialogUtility.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,11 @@ public static void showSuccessDialog(String message) {
}

public static void showErrorDialog(String message) {
Dialog<ButtonType> dialog = CustomDialogWithLabel("Result", "assets/dialog/Error.png", ButtonType.OK);
showErrorDialog("Error", message);
}

public static void showErrorDialog(String title, String message) {
Dialog<ButtonType> dialog = CustomDialogWithLabel(title, "assets/dialog/Error.png", ButtonType.OK);
setContent(dialog, new Label(message));
dialog.showAndWait();
}
Expand Down

0 comments on commit 9f17116

Please sign in to comment.