Commit 1bb03156 authored by Benedikt Zoennchen's avatar Benedikt Zoennchen
Browse files

fix #147: spaceing problem.

parent 930bc017
Pipeline #71182 passed with stages
in 63 minutes and 58 seconds
package org.vadere.gui.postvisualization.control;
import com.fasterxml.jackson.databind.JsonNode;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.jetbrains.annotations.NotNull;
import org.vadere.gui.postvisualization.model.PedestrianColorTableModel;
import org.vadere.gui.postvisualization.model.PostvisualizationModel;
import org.vadere.util.io.parser.JsonLogicParser;
import org.vadere.util.io.parser.VPredicate;
import java.text.ParseException;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
/**
* @author Benedikt Zoennchen
*/
public class TableListenerLogicExpression implements TableModelListener {
private static final Logger logger = LogManager.getLogger(TableListenerLogicExpression.class);
private PostvisualizationModel model;
private PedestrianColorTableModel pedestrianColorTableModel;
public TableListenerLogicExpression(@NotNull final PostvisualizationModel model, @NotNull final PedestrianColorTableModel pedestrianColorTableModel) {
this.model = model;
this.pedestrianColorTableModel = pedestrianColorTableModel;
}
@Override
public void tableChanged(final TableModelEvent e) {
for (int row = e.getFirstRow(); row <= e.getLastRow(); row++) {
if (row >= 0 && e.getColumn() == PedestrianColorTableModel.CIRTERIA_COLUMN) {
try {
String expression = pedestrianColorTableModel.getValueAt(row, e.getColumn()).toString();
VPredicate<JsonNode> evaluator = new JsonLogicParser(expression).parse();
model.putExpression(row, evaluator);
} catch (ParseException e1) {
model.removeExpression(row);
pedestrianColorTableModel.setValueAt("", e.getColumn(), row);
logger.warn(e1.getLocalizedMessage());
}
}
}
}
}
......@@ -19,6 +19,7 @@ import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.jetbrains.annotations.NotNull;
import org.vadere.gui.components.model.SimulationModel;
import org.vadere.gui.postvisualization.control.TableListenerLogicExpression;
import org.vadere.gui.postvisualization.utils.PotentialFieldContainer;
import org.vadere.simulator.projects.Scenario;
import org.vadere.state.scenario.Agent;
......@@ -83,23 +84,18 @@ public class PostvisualizationModel extends SimulationModel<PostvisualizationCon
}
}*/
this.pedestrianColorTableModel.addTableModelListener(
e -> {
for (int row = e.getFirstRow(); row <= e.getLastRow(); row++) {
if (row >= 0 && e.getColumn() == PedestrianColorTableModel.CIRTERIA_COLUMN) {
try {
VPredicate<JsonNode> evaluator = new JsonLogicParser(
pedestrianColorTableModel.getValueAt(row, e.getColumn()).toString()).parse();
colorEvalFunctions.put(row, evaluator);
} catch (ParseException e1) {
colorEvalFunctions.remove(row);
logger.warn(e1.getLocalizedMessage());
}
}
}
});
this.pedestrianColorTableModel.addTableModelListener(new TableListenerLogicExpression(this, pedestrianColorTableModel));
}
public void putExpression(final int row, @NotNull final VPredicate<JsonNode> predicate) {
colorEvalFunctions.put(row, predicate);
}
public void removeExpression(final int row) {
colorEvalFunctions.remove(row);
}
/**
* Initialize the {@link PostvisualizationModel}.
*
......
......@@ -78,17 +78,24 @@ public abstract class LogicalParser<T> {
}
private String reformatText(final String text) {
// remove all spaces
String reformatText = text.replaceAll("\\s+", "");
String reformatText = text.replaceAll("\\(", " ( ");
// space for brackets and logic relations
reformatText = reformatText.replaceAll("\\(", " ( ");
reformatText = reformatText.replaceAll("\\)", " ) ");
reformatText = reformatText.replaceAll("\\s==\\s", "==");
reformatText = reformatText.replaceAll("&&", " && ");
reformatText = reformatText.replaceAll("\\|\\|", " \\|\\| ");
/*reformatText = reformatText.replaceAll("\\s==\\s", "==");
reformatText = reformatText.replaceAll("\\s<\\s", "<");
reformatText = reformatText.replaceAll("\\s<=\\s", "<=");
reformatText = reformatText.replaceAll("\\s>=\\s", ">=");
reformatText = reformatText.replaceAll("\\s>\\s", ">");
reformatText = reformatText.replaceAll("\\s<\\s", "<");
reformatText = reformatText.replaceAll("\\s!=\\s", "!=");
reformatText = reformatText.replaceAll("\\s:\\s", ":");
reformatText = reformatText.replaceAll("\\s:\\s", ":");*/
if (!reformatText.startsWith("(") || !reformatText.endsWith(")")) {
reformatText = "( " + reformatText + " )";
}
......
......@@ -48,7 +48,13 @@ public class TestLogicParser {
@Test
public void testSubSet() throws ParseException {
// spacing test
assertTrue("id:{5,6,7}", new JsonLogicParser("id:{5,6,7}").parse().test(jsonObject));
assertTrue("id: {5,6,7}", new JsonLogicParser("id: {5,6,7}").parse().test(jsonObject));
assertTrue("id : {5,6,7}", new JsonLogicParser("id : {5,6,7}").parse().test(jsonObject));
assertTrue("id :{5,6,7}", new JsonLogicParser("id :{5,6,7}").parse().test(jsonObject));
// spacing test end
assertTrue("shape.x:{0.5,0.7,0.1}", new JsonLogicParser("shape.x:{0.5,0.7,0.1}").parse().test(jsonObject));
assertTrue("targetIds:{2,3,4,5.3}", new JsonLogicParser("targetIds:{2,3,4,5.3}").parse().test(jsonObject));
assertTrue("targetIds:{1,2,3,4,5}", new JsonLogicParser("targetIds:{1,2,3,4,5}").parse().test(jsonObject));
......@@ -62,7 +68,13 @@ public class TestLogicParser {
@Test
public void testSuperSet() throws ParseException {
// spacing test
assertTrue("{6}: id", new JsonLogicParser("{6}: id").parse().test(jsonObject));
assertTrue("{6} :id", new JsonLogicParser("{6} :id").parse().test(jsonObject));
assertTrue("{6} : id", new JsonLogicParser("{6} : id").parse().test(jsonObject));
assertTrue("{6}:id", new JsonLogicParser("{6}:id").parse().test(jsonObject));
// spacing test end
assertTrue("{0.5}:shape.x", new JsonLogicParser("{0.5}:shape.x").parse().test(jsonObject));
assertTrue("{2,3,4}:targetIds", new JsonLogicParser("{2,3,4}:targetIds").parse().test(jsonObject));
assertTrue("{2,3.0,4}:targetIds", new JsonLogicParser("{2,3.0,4}:targetIds").parse().test(jsonObject));
......@@ -75,7 +87,14 @@ public class TestLogicParser {
@Test
public void testEquals() throws ParseException {
// spacing test
assertTrue("id ==6", new JsonLogicParser("id ==6").parse().test(jsonObject));
assertTrue("id== 6", new JsonLogicParser("id== 6").parse().test(jsonObject));
assertTrue("id == 6", new JsonLogicParser("id == 6").parse().test(jsonObject));
assertTrue("id= =6", new JsonLogicParser("id= =6").parse().test(jsonObject));
assertTrue("id==6", new JsonLogicParser("id==6").parse().test(jsonObject));
// spacing end
assertTrue("shape.x==0.5", new JsonLogicParser("shape.x==0.5").parse().test(jsonObject));
assertTrue("shape.type==RECTANGLE", new JsonLogicParser("shape.type==RECTANGLE").parse().test(jsonObject));
......@@ -86,7 +105,15 @@ public class TestLogicParser {
@Test
public void testNotEquals() throws ParseException {
// spacing test
assertFalse("id!= 6", new JsonLogicParser("id!= 6").parse().test(jsonObject));
assertFalse("id !=6", new JsonLogicParser("id !=6").parse().test(jsonObject));
assertFalse("id! =6", new JsonLogicParser("id! =6").parse().test(jsonObject));
assertFalse("id != 6", new JsonLogicParser("id != 6").parse().test(jsonObject));
assertFalse("id ! = 6", new JsonLogicParser("id ! = 6").parse().test(jsonObject));
assertFalse("id!=6", new JsonLogicParser("id!=6").parse().test(jsonObject));
// spacing end
assertFalse("shape.x!=0.5", new JsonLogicParser("shape.x!=0.5").parse().test(jsonObject));
assertFalse("shape.type!=RECTANGLE", new JsonLogicParser("shape.type!=RECTANGLE").parse().test(jsonObject));
......@@ -97,7 +124,12 @@ public class TestLogicParser {
@Test
public void testNot() throws ParseException {
// spacing test
assertFalse("!(id==6)", new JsonLogicParser("!(id==6)").parse().test(jsonObject));
assertFalse("! (id==6)", new JsonLogicParser("! (id==6)").parse().test(jsonObject));
assertFalse("! (id= =6)", new JsonLogicParser("! (id= =6)").parse().test(jsonObject));
// spacing end
assertFalse("!(shape.x==0.5)", new JsonLogicParser("!(shape.x==0.5)").parse().test(jsonObject));
assertFalse("!(shape.type==RECTANGLE)",
new JsonLogicParser("!(shape.type==RECTANGLE)").parse().test(jsonObject));
......@@ -109,7 +141,13 @@ public class TestLogicParser {
@Test
public void testGreaterThan() throws ParseException {
// spacing test
assertTrue("id> 5", new JsonLogicParser("id> 5").parse().test(jsonObject));
assertTrue("id >5", new JsonLogicParser("id >5").parse().test(jsonObject));
assertTrue("id > 5", new JsonLogicParser("id > 5").parse().test(jsonObject));
assertTrue("id>5", new JsonLogicParser("id>5").parse().test(jsonObject));
// spacing end
assertTrue("shape.x>0.4", new JsonLogicParser("shape.x>0.4").parse().test(jsonObject));
assertFalse("id>6", new JsonLogicParser("id>6").parse().test(jsonObject));
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment