use of com.graphhopper.routing.ev.StringEncodedValue in project graphhopper by graphhopper.
the class ExpressionVisitorTest method before.
@BeforeEach
public void before() {
StringEncodedValue sev = new StringEncodedValue("country", 10);
lookup = new GraphBuilder(new EncodingManager.Builder().add(sev).build()).create().getEncodingManager();
sev.setString(false, new IntsRef(1), "DEU");
}
use of com.graphhopper.routing.ev.StringEncodedValue in project graphhopper by graphhopper.
the class ExpressionVisitor method visitRvalue.
@Override
public Boolean visitRvalue(Java.Rvalue rv) throws Exception {
if (rv instanceof Java.AmbiguousName) {
Java.AmbiguousName n = (Java.AmbiguousName) rv;
if (n.identifiers.length == 1) {
String arg = n.identifiers[0];
if (arg.startsWith(IN_AREA_PREFIX)) {
int start = rv.getLocation().getColumnNumber() - 1;
replacements.put(start, new Replacement(start, arg.length(), CustomWeightingHelper.class.getSimpleName() + ".in(this." + arg + ", edge)"));
result.guessedVariables.add(arg);
return true;
} else {
// e.g. like road_class
if (isValidIdentifier(arg))
return true;
try {
factory.create(arg);
invalidMessage = "encoded value '" + arg + "' not available";
return false;
} catch (Exception ex) {
}
}
}
invalidMessage = "identifier " + n + " invalid";
return false;
}
if (rv instanceof Java.Literal) {
return true;
} else if (rv instanceof Java.UnaryOperation) {
Java.UnaryOperation uo = (Java.UnaryOperation) rv;
if (uo.operator.equals("!"))
return uo.operand.accept(this);
return false;
} else if (rv instanceof Java.MethodInvocation) {
Java.MethodInvocation mi = (Java.MethodInvocation) rv;
if (allowedMethods.contains(mi.methodName)) {
// skip methods like this.in() for now
if (mi.target != null) {
// edge.getDistance, Math.sqrt => check target name (edge or Math)
Java.AmbiguousName n = (Java.AmbiguousName) mi.target.toRvalue();
if (n.identifiers.length == 2 && isValidIdentifier(n.identifiers[0]))
return true;
}
}
invalidMessage = mi.methodName + " is illegal method";
return false;
} else if (rv instanceof Java.ParenthesizedExpression) {
return ((Java.ParenthesizedExpression) rv).value.accept(this);
} else if (rv instanceof Java.BinaryOperation) {
Java.BinaryOperation binOp = (Java.BinaryOperation) rv;
int startRH = binOp.rhs.getLocation().getColumnNumber() - 1;
if (binOp.lhs instanceof Java.AmbiguousName && ((Java.AmbiguousName) binOp.lhs).identifiers.length == 1) {
String lhVarAsString = ((Java.AmbiguousName) binOp.lhs).identifiers[0];
boolean eqOps = binOp.operator.equals("==") || binOp.operator.equals("!=");
if (binOp.rhs instanceof Java.StringLiteral) {
// replace String with its index for faster comparison (?) and skipping the Map<String, Integer> lookup at runtime
if (lookup.hasEncodedValue(lhVarAsString)) {
if (!eqOps)
throw new IllegalArgumentException("Operator " + binOp.operator + " not allowed for String");
StringEncodedValue ev = lookup.getStringEncodedValue(lhVarAsString);
String str = ((Java.StringLiteral) binOp.rhs).value;
int integ = ev.indexOf(str.substring(1, str.length() - 1));
// 0 means not found and this should always trigger inequality
if (integ == 0)
integ = -1;
replacements.put(startRH, new Replacement(startRH, str.length(), "" + integ));
}
} else if (binOp.rhs instanceof Java.AmbiguousName && ((Java.AmbiguousName) binOp.rhs).identifiers.length == 1) {
// Make enum explicit as NO or OTHER can occur in other enums so convert "toll == NO" to "toll == Toll.NO"
String rhValueAsString = ((Java.AmbiguousName) binOp.rhs).identifiers[0];
if (nameValidator.isValid(lhVarAsString) && Helper.toUpperCase(rhValueAsString).equals(rhValueAsString)) {
if (!eqOps)
throw new IllegalArgumentException("Operator " + binOp.operator + " not allowed for enum");
String value = toEncodedValueClassName(binOp.lhs.toString());
replacements.put(startRH, new Replacement(startRH, rhValueAsString.length(), value + "." + rhValueAsString));
}
}
}
return binOp.lhs.accept(this) && binOp.rhs.accept(this);
}
return false;
}
Aggregations