Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replaced magic numbers and strings. #187

Merged
merged 1 commit into from
Mar 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class CreatePortViewModelElementAction extends Action {
private final GeckoViewModel geckoViewModel;
private final SystemViewModel systemViewModel;
private PortViewModel createdPortViewModel;
private static final int MARGIN = 2;

CreatePortViewModelElementAction(GeckoViewModel geckoViewModel, SystemViewModel parentSystem) {
this.geckoViewModel = geckoViewModel;
Expand All @@ -26,7 +27,7 @@ public class CreatePortViewModelElementAction extends Action {
boolean run() throws GeckoException {
createdPortViewModel = geckoViewModel.getViewModelFactory().createPortViewModelIn(systemViewModel);
double offset = createdPortViewModel.getSize().getY() * (systemViewModel.getPorts().size() - 1);
createdPortViewModel.setPosition(new Point2D(2, 2 + offset));
createdPortViewModel.setPosition(new Point2D(MARGIN, MARGIN + offset));
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ public class MoveSystemConnectionViewModelElementAction extends Action {
private boolean isVariableBlock = false;
private boolean wasVariableBlock = false;

private static final int PLUS = 1;
private static final int MINUS = -1;
private static final double HALF = 0.5;

MoveSystemConnectionViewModelElementAction(
GeckoViewModel geckoViewModel, SystemConnectionViewModel systemConnectionViewModel,
ElementScalerBlock elementScalerBlock, Point2D delta) {
Expand Down Expand Up @@ -172,9 +176,9 @@ private PortViewModel getPortViewModelAt(Point2D point) {
}

private Point2D calculateEndPortPosition(Point2D position, Point2D size, Visibility visibility, boolean isPort) {
int sign = isPort ? 1 : -1;
return position.add(size.multiply(0.5))
.subtract((visibility == Visibility.INPUT ? 1 : -1) * sign * size.getX() / 2, 0);
int sign = isPort ? PLUS : MINUS;
return position.add(size.multiply(HALF))
.subtract((visibility == Visibility.INPUT ? PLUS : MINUS) * sign * size.getX() / 2, 0);
}

private boolean isSource() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
public class PastePositionableViewModelElementAction extends Action {
private final GeckoViewModel geckoViewModel;
private final Set<PositionableViewModelElement<?>> pastedElements;
private final Point2D pasteOffset = new Point2D(50, 50);
private static final Point2D PASTE_OFFSET = new Point2D(50, 50);
private CopyPositionableViewModelElementVisitor copyVisitor;

PastePositionableViewModelElementAction(GeckoViewModel geckoViewModel) {
Expand Down Expand Up @@ -57,7 +57,7 @@ private boolean pasteSystem() {
geckoViewModel.getCurrentEditor().getCurrentSystem().getTarget().addVariable(copy);
copyVisitor.getCopiedPorts().put(v, copy);
PortViewModel portViewModel = geckoViewModel.getViewModelFactory().createPortViewModelFrom(copy);
portViewModel.setPosition(copyVisitor.getCopiedPosAndSize().get(v).getKey().add(pasteOffset));
portViewModel.setPosition(copyVisitor.getCopiedPosAndSize().get(v).getKey().add(PASTE_OFFSET));
pastedElements.add(portViewModel);
}
for (System s : copyVisitor.getCopiedSystems().keySet()) {
Expand All @@ -72,7 +72,7 @@ private boolean pasteSystem() {
}
copyVisitor.getCopiedSystems().put(s, copy);
SystemViewModel systemViewModel = geckoViewModel.getViewModelFactory().createSystemViewModelFrom(copy);
systemViewModel.setPosition(copyVisitor.getCopiedPosAndSize().get(s).getKey().add(pasteOffset));
systemViewModel.setPosition(copyVisitor.getCopiedPosAndSize().get(s).getKey().add(PASTE_OFFSET));
pastedElements.add(systemViewModel);
}
return false;
Expand All @@ -91,7 +91,7 @@ private boolean pasteAutomaton() throws MissingViewModelElementException {
}
copyVisitor.getCopiedStates().put(state, copy);
StateViewModel stateViewModel = geckoViewModel.getViewModelFactory().createStateViewModelFrom(copy);
stateViewModel.setPosition(copyVisitor.getCopiedPosAndSize().get(state).getKey().add(pasteOffset));
stateViewModel.setPosition(copyVisitor.getCopiedPosAndSize().get(state).getKey().add(PASTE_OFFSET));
stateViewModel.setSize(copyVisitor.getCopiedPosAndSize().get(state).getValue());
pastedElements.add(stateViewModel);
}
Expand All @@ -100,7 +100,7 @@ private boolean pasteAutomaton() throws MissingViewModelElementException {
geckoViewModel.getGeckoModel().getModelFactory().copyRegion(copyVisitor.getCopiedRegions().get(region));
geckoViewModel.getCurrentEditor().getCurrentSystem().getTarget().getAutomaton().addRegion(copy);
RegionViewModel regionViewModel = geckoViewModel.getViewModelFactory().createRegionViewModelFrom(copy);
regionViewModel.setPosition(copyVisitor.getCopiedPosAndSize().get(region).getKey().add(pasteOffset));
regionViewModel.setPosition(copyVisitor.getCopiedPosAndSize().get(region).getKey().add(PASTE_OFFSET));
regionViewModel.setSize(copyVisitor.getCopiedPosAndSize().get(region).getValue());
pastedElements.add(regionViewModel);
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/gecko/application/GeckoIOManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ public File getSaveFileChooser(FileTypes fileType) {
private FileChooser getNewFileChooser(FileTypes fileType) {
FileChooser fileChooser = new FileChooser();
fileChooser.getExtensionFilters()
.addAll(new FileChooser.ExtensionFilter(fileType.getFileDescription(), fileType.getFileNameRegex()));
.addAll(new FileChooser.ExtensionFilter(fileType.getFileDescription(), fileType.getFileNameGlob()));
return fileChooser;
}

Expand Down
5 changes: 4 additions & 1 deletion src/main/java/org/gecko/application/GeckoManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,16 @@ public class GeckoManager {
private Gecko gecko;
private final Stage stage;

private static final int SCENE_WIDTH = 1024;
private static final int SCENE_HEIGHT = 768;

public GeckoManager(Stage stage) throws ModelException {
this.stage = stage;
setGecko(new Gecko());
}

public void setGecko(Gecko gecko) {
this.gecko = gecko;
stage.setScene(new Scene(gecko.getView().getMainPane(), 1024, 768));
stage.setScene(new Scene(gecko.getView().getMainPane(), SCENE_WIDTH, SCENE_HEIGHT));
}
}
50 changes: 32 additions & 18 deletions src/main/java/org/gecko/io/AutomatonFileSerializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,23 @@
* to Gecko, such as regions, kinds and priorities, to be compatible with the sys file format.
*/
public class AutomatonFileSerializer implements FileSerializer {

private final GeckoModel model;

private static final String INDENT = " ";
private static final String SERIALIZED_CONTRACT_NAME_REGEX = "contract %s";
private static final String AUTOMATON_SERIALIZATION_AS_SYSTEM_CONTRACT_REGEX
= SERIALIZED_CONTRACT_NAME_REGEX + " {";
private static final String SERIALIZED_CONTRACT_REGEX = SERIALIZED_CONTRACT_NAME_REGEX + " := %s ==> %s";
private static final String SERIALIZED_TRANSITION_REGEX = "%s -> %s :: %s";
private static final String SERIALIZED_SYSTEM_REGEX = "reactor %s {";
private static final String SERIALIZED_CONNECTION_REGEX = "%s.%s -> %s.%s";
private static final String SERIALIZED_STATE_REGEX = "state %s: %s";
private static final String VARIABLE_ATTRIBUTES_REGEX = " %s: %s";
private static final String TRUE_CONDITION = "TRUE";
private static final String SERIALIZED_INPUT = "input";
private static final String SERIALIZED_OUTPUT = "output";
private static final String SERIALIZED_STATE = "state";
private static final int HIGHEST_PRIORITY = 0;

public AutomatonFileSerializer(GeckoModel model) {
this.model = model;
Expand Down Expand Up @@ -70,7 +84,7 @@ private String serializeSystems(GeckoModel model) {
private String serializeAutomaton(System system) {
Automaton automaton = system.getAutomaton();
StringJoiner joiner = new StringJoiner(java.lang.System.lineSeparator());
joiner.add("contract %s {".formatted(system.getName()));
joiner.add(AUTOMATON_SERIALIZATION_AS_SYSTEM_CONTRACT_REGEX.formatted(system.getName()));
if (!system.getVariables().isEmpty()) {
joiner.add(serializeIo(system));
joiner.add("");
Expand Down Expand Up @@ -127,10 +141,10 @@ private String serializeStateContracts(State state, Automaton automaton) {
}

//applying priorites
int prioIndex = 0;
int prioIndex = HIGHEST_PRIORITY;
for (List<Edge> edgeGroup : groupedEdges) {
for (Edge edge : edgeGroup) {
if (prioIndex == 0) {
if (prioIndex == HIGHEST_PRIORITY) {
continue; //Highest prio doesn't need to be altered
}
Contract contractWithPrio = newContracts.get(edge.getContract());
Expand All @@ -147,7 +161,7 @@ private void applyKindToContract(Contract contract, Kind kind) throws ModelExcep
switch (kind) {
case MISS -> {
contract.setPreCondition(contract.getPreCondition().not());
contract.setPostCondition(Condition.trueCondition());
contract.setPostCondition(new Condition(TRUE_CONDITION));
}
case FAIL -> {
contract.setPostCondition(contract.getPostCondition().not());
Expand Down Expand Up @@ -196,18 +210,18 @@ private List<Condition> andConditions(List<Region> regions) {
}

private String serializeContract(Contract contract) {
return INDENT + "contract %s := %s ==> %s".formatted(contract.getName(), contract.getPreCondition(),
return INDENT + SERIALIZED_CONTRACT_REGEX.formatted(contract.getName(), contract.getPreCondition(),
contract.getPostCondition());
}

private String serializeTransition(Edge edge) {
return INDENT + "%s -> %s :: %s".formatted(edge.getSource().getName(), edge.getDestination().getName(),
edge.getContract().getName());
return INDENT + SERIALIZED_TRANSITION_REGEX
.formatted(edge.getSource().getName(), edge.getDestination().getName(), edge.getContract().getName());
}

private String serializeSystem(System system) {
StringJoiner joiner = new StringJoiner(java.lang.System.lineSeparator());
joiner.add("reactor %s {".formatted(system.getName()));
joiner.add(SERIALIZED_SYSTEM_REGEX.formatted(system.getName()));
if (!system.getVariables().isEmpty()) {
joiner.add(serializeIo(system));
joiner.add("");
Expand All @@ -217,7 +231,7 @@ private String serializeSystem(System system) {
joiner.add("");
}
if (system.getAutomaton() != null && !system.getAutomaton().isEmpty()) {
joiner.add(INDENT + "contract %s".formatted(system.getName()));
joiner.add(INDENT + SERIALIZED_CONTRACT_NAME_REGEX.formatted(system.getName()));
joiner.add("");
}
if (!system.getConnections().isEmpty()) {
Expand All @@ -241,12 +255,12 @@ private String serializeConnection(SystemConnection connection, System parent) {
String startPort = connection.getSource().getName();
String endSystem = serializeSystemReference(parent, connection.getDestination());
String endPort = connection.getDestination().getName();
return INDENT + "%s.%s -> %s.%s".formatted(startSystem, startPort, endSystem, endPort);
return INDENT + SERIALIZED_CONNECTION_REGEX.formatted(startSystem, startPort, endSystem, endPort);
}

private String serializeSystemReference(System parent, Variable var) {
if (parent.getVariables().contains(var)) {
return "self";
return AutomatonFileVisitor.SELF_REFERENCE_TOKEN;
} else {
return parent.getChildSystemWithVariable(var).getName();
}
Expand All @@ -264,24 +278,24 @@ private String serializeChildren(System system) {
}

private String serializeChild(System system) {
return INDENT + "state %s: %s".formatted(system.getName(), system.getName());
return INDENT + SERIALIZED_STATE_REGEX.formatted(system.getName(), system.getName());
}

private String serializeVariable(Variable variable) {
String output = "";
output += INDENT + switch (variable.getVisibility()) {
case INPUT -> "input";
case OUTPUT -> "output";
case STATE -> "state";
case INPUT -> SERIALIZED_INPUT;
case OUTPUT -> SERIALIZED_OUTPUT;
case STATE -> SERIALIZED_STATE;
default -> throw new IllegalArgumentException("Unknown visibility: " + variable.getVisibility());
};
output += " %s: %s".formatted(variable.getName(), variable.getType());
output += VARIABLE_ATTRIBUTES_REGEX.formatted(variable.getName(), variable.getType());
//TODO append variable value
return output;
}

private String serializeCode(String code) {
return INDENT + "{=" + code + "=}";
return INDENT + AutomatonFileVisitor.CODE_BEGIN + code + AutomatonFileVisitor.CODE_END;
}

private <T> String serializeCollectionWithMapping(Collection<T> collection, Function<T, String> mapping) {
Expand Down
14 changes: 8 additions & 6 deletions src/main/java/org/gecko/io/AutomatonFileVisitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,12 @@ public class AutomatonFileVisitor extends SystemDefBaseVisitor<Void> {
private AutomatonFileScout scout;
private int elementsCreated;


private static final String START_STATE_REGEX = "[a-z].*";
private static final String SELF_REFERENCE_TOKEN = "self";
private static final int USER_SYSTEM_CHOICE_VBOX_SPACING = 10;
private static final int USER_SYSTEM_CHOICE_VBOX_PADDING = 20;
protected static final String SELF_REFERENCE_TOKEN = "self";
protected static final String CODE_BEGIN = "{=";
protected static final String CODE_END = "=}";

public AutomatonFileVisitor() throws ModelException {
this.model = new GeckoModel();
Expand Down Expand Up @@ -384,8 +387,7 @@ private void applySubstitution(Condition condition, String toReplace, String toR
}

private String cleanCode(String code) {
//length("{=") = 2
return code.substring(2, code.length() - 2);
return code.substring(CODE_BEGIN.length(), code.length() - CODE_BEGIN.length());
}

private System parseSystemReference(String name) {
Expand Down Expand Up @@ -421,8 +423,8 @@ private String makeUserChooseSystem(List<String> systemNames) {
comboBox.getItems().addAll(systemNames);
comboBox.setPromptText("Choose a system");

VBox vBox = new VBox(10);
vBox.setPadding(new Insets(20));
VBox vBox = new VBox(USER_SYSTEM_CHOICE_VBOX_SPACING);
vBox.setPadding(new Insets(USER_SYSTEM_CHOICE_VBOX_PADDING));
vBox.getChildren().add(comboBox);

Alert alert = new Alert(Alert.AlertType.WARNING);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/gecko/io/FileTypes.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public enum FileTypes {
this.fileExtension = fileExtension;
}

public String getFileNameRegex() {
public String getFileNameGlob() {
return "*." + fileExtension;
}
}
7 changes: 5 additions & 2 deletions src/main/java/org/gecko/model/Condition.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
public class Condition {
private String condition;

private static final String AND_CONDITION_REGEX = "(%s) & (%s)";
private static final String NOT_CONDITION_REGEX = "! (%s)";

@JsonCreator
public Condition(@JsonProperty("condition") String condition) throws ModelException {
setCondition(condition);
Expand All @@ -30,7 +33,7 @@ public void setCondition(@NonNull String condition) throws ModelException {
public Condition and(Condition other) {
try {
// This and other are always valid
return new Condition("(" + condition + ") & (" + other.condition + ")");
return new Condition(AND_CONDITION_REGEX.formatted(condition, other.condition));
} catch (ModelException e) {
return null;
}
Expand All @@ -40,7 +43,7 @@ public Condition and(Condition other) {
public Condition not() {
try {
// This is always valid
return new Condition("! (" + condition + ")");
return new Condition(NOT_CONDITION_REGEX.formatted(condition));
} catch (ModelException e) {
return null;
}
Expand Down
6 changes: 2 additions & 4 deletions src/main/java/org/gecko/tools/CursorTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,17 @@
* the {@link Point2D starting position} and the afferent {@link javafx.scene.shape.Rectangle}.
*/
public class CursorTool extends Tool {

private static final double DRAG_THRESHOLD = 4;

private boolean isDragging = false;
private final SelectionManager selectionManager;
private final EditorViewModel editorViewModel;
private Point2D startDragPosition;
private Point2D previousDragPosition;
private Point2D oldPosition;
private Point2D oldSize;

private ViewElementPane viewPane;

private static final double DRAG_THRESHOLD = 4;

public CursorTool(ActionManager actionManager, SelectionManager selectionManager, EditorViewModel editorViewModel) {
super(actionManager, ToolType.CURSOR, false);
this.selectionManager = selectionManager;
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/org/gecko/tools/MarqueeTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
public class MarqueeTool extends AreaTool {
private final EditorViewModel editorViewModel;

private static final double STROKE_WIDTH = 1;
private static final double STROKE_DASH = 5d;

public MarqueeTool(ActionManager actionManager, EditorViewModel editorViewModel) {
super(actionManager, ToolType.MARQUEE_TOOL, true);
this.editorViewModel = editorViewModel;
Expand All @@ -27,8 +30,8 @@ Rectangle createNewArea() {
Rectangle marquee = new Rectangle();
marquee.setFill(null);
marquee.setStroke(Color.BLUE);
marquee.setStrokeWidth(1);
marquee.getStrokeDashArray().addAll(5d, 5d);
marquee.setStrokeWidth(STROKE_WIDTH);
marquee.getStrokeDashArray().addAll(STROKE_DASH, STROKE_DASH);
return marquee;
}

Expand Down
4 changes: 3 additions & 1 deletion src/main/java/org/gecko/tools/RegionCreatorTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
public class RegionCreatorTool extends AreaTool {
private Color color;

private static final double MEDIUM_OPACITY = 0.5;

public RegionCreatorTool(ActionManager actionManager) {
super(actionManager, ToolType.REGION_CREATOR, false);
}
Expand All @@ -25,7 +27,7 @@ Rectangle createNewArea() {
Rectangle region = new Rectangle();
color = Color.color(Math.random(), Math.random(), Math.random());
region.setFill(color);
region.setOpacity(0.5);
region.setOpacity(MEDIUM_OPACITY);
return region;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public class Graphlayouter {
private final GeckoViewModel viewModel;
private final ELKGraphCreator elkGraphCreator;

private static final double THIRD_OF_SYSTEM_WIDTH_DIVIDER = 3;

public Graphlayouter(GeckoViewModel viewModel) {
this.viewModel = viewModel;
elkGraphCreator = new ELKGraphCreator(viewModel);
Expand Down Expand Up @@ -53,7 +55,7 @@ private void layoutGraph(ElkNode root, LayoutAlgorithms layoutAlgorithm) {
return;
}
root.setProperty(CoreOptions.ALGORITHM, layoutAlgorithm.toString());
double spacing = root.getChildren().getFirst().getWidth() / 3;
double spacing = root.getChildren().getFirst().getWidth() / THIRD_OF_SYSTEM_WIDTH_DIVIDER;
root.setProperty(CoreOptions.SPACING_NODE_NODE, spacing);
if (layoutAlgorithm == LayoutAlgorithms.LAYERED) {
root.setProperty(LayeredOptions.SPACING_NODE_NODE_BETWEEN_LAYERS, spacing);
Expand Down
Loading
Loading