Skip to content

Commit

Permalink
Merge pull request #189 from DecwLK/magicNumbers
Browse files Browse the repository at this point in the history
restore localization and tooltips
  • Loading branch information
crissNb authored Mar 10, 2024
2 parents fbcaa77 + 87d9426 commit 6497ec8
Show file tree
Hide file tree
Showing 45 changed files with 177 additions and 289 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ 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 @@ -176,9 +172,9 @@ private PortViewModel getPortViewModelAt(Point2D point) {
}

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

private boolean isSource() {
Expand Down
47 changes: 22 additions & 25 deletions src/main/java/org/gecko/io/AutomatonFileSerializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,17 @@ 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_CONTRACT_NAME = "contract %s";
private static final String AUTOMATON_SERIALIZATION_AS_SYSTEM_CONTRACT = SERIALIZED_CONTRACT_NAME + " {";
private static final String SERIALIZED_CONTRACT = SERIALIZED_CONTRACT_NAME + " := %s ==> %s";
private static final String SERIALIZED_TRANSITION = "%s -> %s :: %s";
private static final String SERIALIZED_SYSTEM = "reactor %s {";
private static final String SERIALIZED_CONNECTION = "%s.%s -> %s.%s";
private static final String SERIALIZED_STATE = "state %s: %s";
private static final String VARIABLE_ATTRIBUTES = " %s: %s";
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;
private static final String SERIALIZED_STATE_VISIBILITY = "state";

public AutomatonFileSerializer(GeckoModel model) {
this.model = model;
Expand Down Expand Up @@ -84,7 +81,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(AUTOMATON_SERIALIZATION_AS_SYSTEM_CONTRACT_REGEX.formatted(system.getName()));
joiner.add(AUTOMATON_SERIALIZATION_AS_SYSTEM_CONTRACT.formatted(system.getName()));
if (!system.getVariables().isEmpty()) {
joiner.add(serializeIo(system));
joiner.add("");
Expand Down Expand Up @@ -141,10 +138,10 @@ private String serializeStateContracts(State state, Automaton automaton) {
}

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

private String serializeContract(Contract contract) {
return INDENT + SERIALIZED_CONTRACT_REGEX.formatted(contract.getName(), contract.getPreCondition(),
return INDENT + SERIALIZED_CONTRACT.formatted(contract.getName(), contract.getPreCondition(),
contract.getPostCondition());
}

private String serializeTransition(Edge edge) {
return INDENT + SERIALIZED_TRANSITION_REGEX
.formatted(edge.getSource().getName(), edge.getDestination().getName(), edge.getContract().getName());
return INDENT + SERIALIZED_TRANSITION.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(SERIALIZED_SYSTEM_REGEX.formatted(system.getName()));
joiner.add(SERIALIZED_SYSTEM.formatted(system.getName()));
if (!system.getVariables().isEmpty()) {
joiner.add(serializeIo(system));
joiner.add("");
Expand All @@ -231,7 +228,7 @@ private String serializeSystem(System system) {
joiner.add("");
}
if (system.getAutomaton() != null && !system.getAutomaton().isEmpty()) {
joiner.add(INDENT + SERIALIZED_CONTRACT_NAME_REGEX.formatted(system.getName()));
joiner.add(INDENT + SERIALIZED_CONTRACT_NAME.formatted(system.getName()));
joiner.add("");
}
if (!system.getConnections().isEmpty()) {
Expand All @@ -255,7 +252,7 @@ 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 + SERIALIZED_CONNECTION_REGEX.formatted(startSystem, startPort, endSystem, endPort);
return INDENT + SERIALIZED_CONNECTION.formatted(startSystem, startPort, endSystem, endPort);
}

private String serializeSystemReference(System parent, Variable var) {
Expand All @@ -278,18 +275,18 @@ private String serializeChildren(System system) {
}

private String serializeChild(System system) {
return INDENT + SERIALIZED_STATE_REGEX.formatted(system.getName(), system.getName());
return INDENT + SERIALIZED_STATE.formatted(system.getName(), system.getName());
}

private String serializeVariable(Variable variable) {
String output = "";
output += INDENT + switch (variable.getVisibility()) {
case INPUT -> SERIALIZED_INPUT;
case OUTPUT -> SERIALIZED_OUTPUT;
case STATE -> SERIALIZED_STATE;
case STATE -> SERIALIZED_STATE_VISIBILITY;
default -> throw new IllegalArgumentException("Unknown visibility: " + variable.getVisibility());
};
output += VARIABLE_ATTRIBUTES_REGEX.formatted(variable.getName(), variable.getType());
output += VARIABLE_ATTRIBUTES.formatted(variable.getName(), variable.getType());
//TODO append variable value
return output;
}
Expand Down
11 changes: 6 additions & 5 deletions src/main/java/org/gecko/model/Condition.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +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)";
private static final String AND_CONDITIONS = "(%s) & (%s)";
private static final String NOT_CONDITION = "! (%s)";
private static final String TRUE_CONDITION = "true";

@JsonCreator
public Condition(@JsonProperty("condition") String condition) throws ModelException {
Expand All @@ -33,7 +34,7 @@ public void setCondition(@NonNull String condition) throws ModelException {
public Condition and(Condition other) {
try {
// This and other are always valid
return new Condition(AND_CONDITION_REGEX.formatted(condition, other.condition));
return new Condition(AND_CONDITIONS.formatted(condition, other.condition));
} catch (ModelException e) {
return null;
}
Expand All @@ -43,7 +44,7 @@ public Condition and(Condition other) {
public Condition not() {
try {
// This is always valid
return new Condition(NOT_CONDITION_REGEX.formatted(condition));
return new Condition(NOT_CONDITION.formatted(condition));
} catch (ModelException e) {
return null;
}
Expand All @@ -58,7 +59,7 @@ public String toString() {
public static Condition trueCondition() {
//Name has to be this because true is reserved
try {
return new Condition("true");
return new Condition(TRUE_CONDITION);
} catch (ModelException e) {
return null;
}
Expand Down
4 changes: 1 addition & 3 deletions src/main/java/org/gecko/tools/RegionCreatorTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
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 @@ -27,7 +25,7 @@ Rectangle createNewArea() {
Rectangle region = new Rectangle();
color = Color.color(Math.random(), Math.random(), Math.random());
region.setFill(color);
region.setOpacity(MEDIUM_OPACITY);
region.setOpacity(0.5);
return region;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ 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 @@ -55,7 +53,7 @@ private void layoutGraph(ElkNode root, LayoutAlgorithms layoutAlgorithm) {
return;
}
root.setProperty(CoreOptions.ALGORITHM, layoutAlgorithm.toString());
double spacing = root.getChildren().getFirst().getWidth() / THIRD_OF_SYSTEM_WIDTH_DIVIDER;
double spacing = root.getChildren().getFirst().getWidth() / 3;
root.setProperty(CoreOptions.SPACING_NODE_NODE, spacing);
if (layoutAlgorithm == LayoutAlgorithms.LAYERED) {
root.setProperty(LayeredOptions.SPACING_NODE_NODE_BETWEEN_LAYERS, spacing);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import javafx.scene.control.SeparatorMenuItem;
import org.gecko.actions.ActionManager;
import org.gecko.model.Kind;
import org.gecko.view.ResourceHandler;
import org.gecko.viewmodel.EdgeViewModel;

/**
Expand All @@ -17,11 +18,6 @@ public class EdgeViewElementContextMenuBuilder extends ViewContextMenuBuilder {

private final EdgeViewModel edgeViewModel;

private static final String CHANGE_KIND_MENU_ITEM = "Change Kind";
private static final String HIT_MENU_ITEM = "HIT";
private static final String MISS_KIND_MENU_ITEM = "MISS";
private static final String FAIL_KIND_MENU_ITEM = "FAIL";

public EdgeViewElementContextMenuBuilder(
ActionManager actionManager, EdgeViewModel edgeViewModel) {
super(actionManager);
Expand All @@ -36,23 +32,23 @@ public ContextMenu build() {
SeparatorMenuItem dataTransferToEdgeEditingSeparator = new SeparatorMenuItem();

// Edge editing commands:
Menu changeKindMenu = new Menu(CHANGE_KIND_MENU_ITEM); // TODO: Synchronize fields showed in inspector.
Menu changeKindMenu = new Menu(ResourceHandler.getString("Buttons", "change_kind"));

MenuItem hitMenuItem = new MenuItem(HIT_MENU_ITEM);
MenuItem hitMenuItem = new MenuItem(Kind.HIT.name());
hitMenuItem.setOnAction(
e -> actionManager.run(actionManager.getActionFactory().createChangeKindAction(edgeViewModel, Kind.HIT)));

MenuItem missMenuItem = new MenuItem(MISS_KIND_MENU_ITEM);
MenuItem missMenuItem = new MenuItem(Kind.MISS.name());
missMenuItem.setOnAction(
e -> actionManager.run(actionManager.getActionFactory().createChangeKindAction(edgeViewModel, Kind.MISS)));

MenuItem failMenuItem = new MenuItem(FAIL_KIND_MENU_ITEM);
MenuItem failMenuItem = new MenuItem(Kind.FAIL.name());
failMenuItem.setOnAction(
e -> actionManager.run(actionManager.getActionFactory().createChangeKindAction(edgeViewModel, Kind.FAIL)));

changeKindMenu.getItems().addAll(hitMenuItem, missMenuItem, failMenuItem);

MenuItem deleteMenuItem = new MenuItem(DELETE_MENU_ITEM);
MenuItem deleteMenuItem = new MenuItem(ResourceHandler.getString("Buttons", "delete"));
deleteMenuItem.setOnAction(e -> actionManager.run(
actionManager.getActionFactory().createDeletePositionableViewModelElementAction(edgeViewModel)));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import javafx.scene.control.MenuItem;
import javafx.scene.control.SeparatorMenuItem;
import org.gecko.actions.ActionManager;
import org.gecko.view.ResourceHandler;
import org.gecko.viewmodel.RegionViewModel;

/**
Expand All @@ -29,7 +30,7 @@ public ContextMenu build() {
SeparatorMenuItem dataTransferToRegionEditingSeparator = new SeparatorMenuItem();

// Region editing commands:
MenuItem deleteMenuItem = new MenuItem(DELETE_MENU_ITEM);
MenuItem deleteMenuItem = new MenuItem(ResourceHandler.getString("Buttons", "delete"));
deleteMenuItem.setOnAction(e -> actionManager.run(
actionManager.getActionFactory().createDeletePositionableViewModelElementAction(regionViewModel)));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import javafx.scene.control.MenuItem;
import javafx.scene.control.SeparatorMenuItem;
import org.gecko.actions.ActionManager;
import org.gecko.view.ResourceHandler;
import org.gecko.viewmodel.StateViewModel;

/**
Expand All @@ -15,8 +16,6 @@ public class StateViewElementContextMenuBuilder extends ViewContextMenuBuilder {

private final StateViewModel stateViewModel;

private static final String START_STATE_MENU_ITEM = "Start State";

public StateViewElementContextMenuBuilder(
ActionManager actionManager, StateViewModel stateViewModel) {
super(actionManager);
Expand All @@ -30,12 +29,12 @@ public ContextMenu build() {
SeparatorMenuItem dataTransferToStateEditingSeparator = new SeparatorMenuItem();

// State editing commands:
MenuItem startStateMenuItem = new MenuItem(START_STATE_MENU_ITEM);
MenuItem startStateMenuItem = new MenuItem(ResourceHandler.getString("Buttons", "set_start_state"));
startStateMenuItem.setDisable(stateViewModel.getIsStartState());
startStateMenuItem.setOnAction(e -> actionManager.run(
actionManager.getActionFactory().createSetStartStateViewModelElementAction(stateViewModel)));

MenuItem deleteMenuItem = new MenuItem(DELETE_MENU_ITEM);
MenuItem deleteMenuItem = new MenuItem(ResourceHandler.getString("Buttons", "delete"));
deleteMenuItem.setOnAction(e -> actionManager.run(
actionManager.getActionFactory().createDeletePositionableViewModelElementAction(stateViewModel)));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import javafx.scene.control.MenuItem;
import javafx.scene.control.SeparatorMenuItem;
import org.gecko.actions.ActionManager;
import org.gecko.view.ResourceHandler;
import org.gecko.viewmodel.SystemConnectionViewModel;

/**
Expand All @@ -29,7 +30,7 @@ public ContextMenu build() {
SeparatorMenuItem dataTransferToEdgeEditingSeparator = new SeparatorMenuItem();

// SystemConnection editing commands:
MenuItem deleteMenuItem = new MenuItem(DELETE_MENU_ITEM);
MenuItem deleteMenuItem = new MenuItem(ResourceHandler.getString("Buttons", "delete"));
deleteMenuItem.setOnAction(e -> actionManager.run(actionManager.getActionFactory()
.createDeletePositionableViewModelElementAction(systemConnectionViewModel)));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import javafx.scene.control.MenuItem;
import javafx.scene.control.SeparatorMenuItem;
import org.gecko.actions.ActionManager;
import org.gecko.view.ResourceHandler;
import org.gecko.view.views.shortcuts.Shortcuts;
import org.gecko.viewmodel.SystemViewModel;

Expand All @@ -16,8 +17,6 @@ public class SystemViewElementContextMenuBuilder extends ViewContextMenuBuilder

private final SystemViewModel systemViewModel;

private static final String OPEN_SYSTEM_MENU_ITEM = "Open System";

public SystemViewElementContextMenuBuilder(
ActionManager actionManager, SystemViewModel systemViewModel) {
super(actionManager);
Expand All @@ -32,12 +31,12 @@ public ContextMenu build() {
SeparatorMenuItem dataTransferToSystemAccessSeparator = new SeparatorMenuItem();

// Access system commands:
MenuItem openSystemMenuItem = new MenuItem(OPEN_SYSTEM_MENU_ITEM);
MenuItem openSystemMenuItem = new MenuItem(ResourceHandler.getString("Buttons", "open_system"));
openSystemMenuItem.setOnAction(
e -> actionManager.run(actionManager.getActionFactory().createViewSwitchAction(systemViewModel, false)));
openSystemMenuItem.setAccelerator(Shortcuts.OPEN_CHILD_SYSTEM_EDITOR.get());

MenuItem deleteMenuItem = new MenuItem(DELETE_MENU_ITEM);
MenuItem deleteMenuItem = new MenuItem(ResourceHandler.getString("Buttons", "delete"));
deleteMenuItem.setOnAction(e -> actionManager.run(
actionManager.getActionFactory().createDeletePositionableViewModelElementAction(systemViewModel)));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import javafx.scene.control.MenuItem;
import javafx.scene.control.SeparatorMenuItem;
import org.gecko.actions.ActionManager;
import org.gecko.view.ResourceHandler;
import org.gecko.viewmodel.PortViewModel;

/**
Expand All @@ -28,7 +29,7 @@ public ContextMenu build() {
SeparatorMenuItem dataTransferToVariableBlockEditingSeparator = new SeparatorMenuItem();

// Variable Block editing commands:
MenuItem deleteMenuItem = new MenuItem(DELETE_MENU_ITEM);
MenuItem deleteMenuItem = new MenuItem(ResourceHandler.getString("Buttons", "delete"));
deleteMenuItem.setOnAction(e -> actionManager.run(
actionManager.getActionFactory().createDeletePositionableViewModelElementAction(portViewModel)));

Expand Down
Loading

0 comments on commit 6497ec8

Please sign in to comment.