Skip to content

Commit

Permalink
Manual arc expr error - fix 2085838 (#183)
Browse files Browse the repository at this point in the history
Fixes:
https://bugs.launchpad.net/tapaal/+bug/2085838

by automatically trying to conform invalid names to the tapaal gui
format, otherwise it will show a dialog saying: "name x could not be
conformed"
  • Loading branch information
srba authored Nov 6, 2024
2 parents 3f84132 + 1abfd0f commit fdfdaab
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 7 deletions.
16 changes: 10 additions & 6 deletions src/main/java/dk/aau/cs/io/LoadTACPN.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import dk.aau.cs.model.CPN.Expressions.*;
import dk.aau.cs.model.tapn.TimedArcPetriNetNetwork;
import dk.aau.cs.util.FormatException;
import dk.aau.cs.util.NameTransformer;
import dk.aau.cs.util.Require;
import dk.aau.cs.util.Tuple;
import org.w3c.dom.Element;
Expand All @@ -20,6 +21,7 @@ public class LoadTACPN { //the import feature for CPN and load for TACPN share s
private final HashMap<String, ColorExpression> tupleVarExpressions = new HashMap<>();
private final Collection<String> messages = new ArrayList<>(10);
private final Collection<Node> productTypes = new ArrayList<>();
private final NameTransformer nc = new NameTransformer();

public HashMap<String, ColorType> getColortypes() {
return colortypes;
Expand Down Expand Up @@ -83,7 +85,7 @@ public void parseDeclarations(Node node, TimedArcPetriNetNetwork network) throws
var type = skipWS(n.getFirstChild());
String typeTag = type.getNodeName();
if (!typeTag.equals("productsort")) throw new FormatException("Expected productsort type got: " + typeTag);
String name = getAttribute(n, "name").getNodeValue();
String name = nc.transform(getAttribute(n, "name").getNodeValue());
String id = getAttribute(n, "id").getNodeValue();

ProductType pt = new ProductType(name);
Expand All @@ -104,7 +106,7 @@ public void parseDeclarations(Node node, TimedArcPetriNetNetwork network) throws
var variabledecl = declarations.getElementsByTagName("variabledecl");
forEachElementInNodeList(variabledecl, element -> {
String id = getAttribute(element, "id").getNodeValue();
String name = getAttribute(element, "name").getNodeValue();
String name = nc.transform(getAttribute(element, "name").getNodeValue());
ColorType ct = parseUserSort(element);
Variable var = new Variable(name, id, ct);
Require.that(variables.put(id, var) == null, "the id " + id + ", was already used");
Expand All @@ -131,7 +133,8 @@ public void parseDeclarations(Node node, TimedArcPetriNetNetwork network) throws

renameWarnings.append(varName).append(elementSubstring).append(",");

Variable newVar = new Variable(var.getName() + elementSubstring, varName + elementSubstring, colorType);
String name = nc.transform(var.getName() + elementSubstring);
Variable newVar = new Variable(name, varName + elementSubstring, colorType);
Require.that(newVars.put(varName + elementSubstring, newVar) == null, "the id " + varName + elementSubstring + ", was already used");
network.add(newVar);
constituentVarExpressions.addElement(new VariableExpression(newVar));
Expand Down Expand Up @@ -164,7 +167,7 @@ private void parseNamedSort(Node node, TimedArcPetriNetNetwork network) throws F

Node type = skipWS(node.getFirstChild());
String typetag = type.getNodeName();
String name = getAttribute(node, "name").getNodeValue();
String name = nc.transform((getAttribute(node, "name").getNodeValue()));
String id = getAttribute(node, "id").getNodeValue();

if (typetag.equals("productsort")) {
Expand All @@ -185,7 +188,8 @@ private void parseNamedSort(Node node, TimedArcPetriNetNetwork network) throws F
while (typechild != null) {
Node colorId = getAttribute(typechild, "id");
if (colorId != null) {
ct.addColor(colorId.getNodeValue());
String colorName = nc.transform(colorId.getNodeValue());
ct.addColor(colorName);
typechild = skipWS(typechild.getNextSibling());
} else {
throw new FormatException(String.format("No id found on %s\n", typechild.getNodeName()));
Expand Down Expand Up @@ -315,7 +319,7 @@ private ColorExpression parseColorExpression(Node node) throws FormatException {
return tupleVarExpressions.get(varname);
}
} else if (name.equals("useroperator")) {
String colorname = getAttribute(node, "declaration").getNodeValue();
String colorname = nc.transform(getAttribute(node, "declaration").getNodeValue());
Color color = getColor(colorname);
return new UserOperatorExpression(color);
} else if (name.equals("successor")) {
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/dk/aau/cs/io/TapnXmlLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import dk.aau.cs.io.queries.TAPNQueryLoader;
import dk.aau.cs.util.FormatException;
import dk.aau.cs.util.Require;
import dk.aau.cs.util.RequireException;

public class TapnXmlLoader {
private static final String PLACENAME_ERROR_MESSAGE = "The keywords \"true\" and \"false\" are reserved and can not be used as place names.\nPlaces with these names will be renamed to \"_true\" and \"_false\" respectively.\n\n Note that any queries using these places may not be parsed correctly.";
Expand Down Expand Up @@ -84,7 +85,7 @@ public LoadedModel load(InputStream file) throws Exception {
if(doc == null) return null;
try {
return parse(doc);
} catch (FormatException | NullPointerException e) {
} catch (FormatException | RequireException | NullPointerException e) {
throw e;
} catch (Exception e) {
throw new Exception("One or more necessary attributes were not found\n - One or more attribute values have an incorrect type", e);
Expand Down
43 changes: 43 additions & 0 deletions src/main/java/dk/aau/cs/util/NameTransformer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package dk.aau.cs.util;

import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;

import pipe.gui.TAPAALGUI;

public class NameTransformer {
private boolean hasConformed = false;

/**
* Transforms a string to the following regex pattern:
* {@code [a-zA-Z][_a-zA-Z0-9]*}
*/
public String transform(String name) {
String transformedName = name;

transformedName = transformedName.replaceAll("\\s+|-", "_");
transformedName = transformedName.replace("*", "star");
transformedName = transformedName.replace("/", "slash");
transformedName = transformedName.replace("+", "plus");
transformedName = transformedName.replace("-", "minus");

Require.that(transformedName.matches("[a-zA-Z][_a-zA-Z0-9]*"), "Name: " + name + " does not obey the regex pattern [a-zA-Z][_a-zA-Z0-9]*, and could not be transformed automatically");

// Displays warning only once for a instance
if (!hasConformed && !transformedName.equals(name)) {
hasConformed = true;
SwingUtilities.invokeLater(() -> {
StringBuilder message = new StringBuilder();
message.append("Some names were incompatible with TAPAAL, and have\n");
message.append("been transformed to the pattern [a-zA-Z][_a-zA-Z0-9]*");
JOptionPane.showMessageDialog(
TAPAALGUI.getApp(),
message.toString(),
"Warning",
JOptionPane.WARNING_MESSAGE);
});
}

return transformedName;
}
}

0 comments on commit fdfdaab

Please sign in to comment.