Commit 5096d194 authored by Robert Zenz's avatar Robert Zenz

#48 Fixed detection of compound units in combination with variables.

parent b16aad00
......@@ -229,7 +229,7 @@ public class Evaluator {
Matcher expressionUnitSeparatorMatcher = EXPRESSION_UNIT_SEPARATOR.matcher(processedExpression);
if (expressionUnitSeparatorMatcher.find()) {
if (findExpressionUnitSeparatorPosition(processedExpression, expressionUnitSeparatorMatcher)) {
String expressionPart = processedExpression.substring(0, expressionUnitSeparatorMatcher.start() + 1);
String unitsPart = processedExpression.substring(expressionUnitSeparatorMatcher.start() + 1);
......@@ -238,7 +238,7 @@ public class Evaluator {
if (unitParts[0] != null && unitParts[0].isEmpty()) {
unitFrom = unitConverter.getCompoundUnit(expressionPart.trim());
if (unitFrom != null) {
if (unitFrom != null && !isKnown(unitFrom)) {
expressionPart = "1";
} else {
unitFrom = unitConverter.getCompoundUnit(unitParts[1]);
......@@ -345,6 +345,54 @@ public class Evaluator {
return expression;
}
private boolean findExpressionUnitSeparatorPosition(String expression, Matcher expressionUnitSeparatorMatcher) {
int searchIndex = 0;
while (expressionUnitSeparatorMatcher.find(searchIndex)) {
if (expression.substring(
expressionUnitSeparatorMatcher.start() + 1,
expressionUnitSeparatorMatcher.end() - 1).equals(" ")) {
return true;
}
String foundExpression = "";
int index = 0;
index = expressionUnitSeparatorMatcher.start();
while (index >= 0 && Character.isLetterOrDigit(expression.charAt(index))) {
index--;
}
foundExpression = expression.substring(
index + 1,
Math.min(expression.length(), expressionUnitSeparatorMatcher.end()));
if (!foundExpression.isEmpty() && Character.isDigit(foundExpression.charAt(0))) {
return true;
}
if (expressionUnitSeparatorMatcher.end() < expression.length()) {
index = expressionUnitSeparatorMatcher.end();
while (index < expression.length() && Character.isLetterOrDigit(expression.charAt(index))) {
index++;
}
foundExpression = expression.substring(expressionUnitSeparatorMatcher.end(), index)
+ foundExpression;
}
if (!isKnown(foundExpression)) {
return true;
}
searchIndex = expressionUnitSeparatorMatcher.start() + 1;
}
return false;
}
private String getNextId() {
expressionCounter = expressionCounter + 1;
......
......@@ -64,6 +64,38 @@ public class TestEvaluator extends AbstractExpressionTest {
assertResult("2", "1 + /* Nested // */ 1", new Evaluator());
}
@Test
public void testCompoundUnitConversions() throws InvalidExpressionException {
Evaluator evaluator = new Evaluator();
// Load the defaults
ResourceLoader.processResource("units/iec.prefixes", evaluator.getUnitConverter()::loadPrefix);
ResourceLoader.processResource("units/si.prefixes", evaluator.getUnitConverter()::loadPrefix);
ResourceLoader.processResource("units/default.units", evaluator.getUnitConverter()::loadUnit);
ResourceLoader.processResource("units/default.conversions", evaluator.getUnitConverter()::loadConversion);
ResourceLoader.processResource("other/default.aliases", evaluator::loadAlias);
ResourceLoader.processResource("other/default.context", evaluator::loadContextExpression);
// Basic support
assertResult("2236.9362920544022906227630637079", "1km/sec to ml/h", evaluator);
assertResult("2236.9362920544022906227630637079", "1km/sec in ml/h", evaluator);
assertResult("2236.9362920544022906227630637079", "1km/sec as ml/h", evaluator);
assertResult("2236.9362920544022906227630637079", "1km/sec ml/h", evaluator);
// Variables
evaluator.evaluate("a1=5");
assertResult("5", "a1", evaluator);
assertResult("61", "a1+7 * 8", evaluator);
assertResult("20", "a1 + a1 + a1 + a1", evaluator);
assertResult("20", "a1 + a1 + a1 + a1 1", evaluator);
assertResult("20", "a1 + a1 + a1 + a1 to 1", evaluator);
assertResult("20000", "a1 + a1 + a1 + a1 km to m", evaluator);
assertResult("20000", "a1 + a1 + a1 + a1 km m", evaluator);
// No value
assertResult("43166.4685056", "l/hour/sqm l/minute/sqml", evaluator);
}
@Test
public void testContextExpressions() throws InvalidExpressionException {
Evaluator evaluator = new Evaluator();
......@@ -161,6 +193,7 @@ public class TestEvaluator extends AbstractExpressionTest {
assertResult("2.54", "1inch to centimeter", evaluator);
assertResult("2.54", "1in to cm", evaluator);
assertResult("2.54", "1in in cm", evaluator);
assertResult("2.54", "1in as cm", evaluator);
assertResult("2.54", "1in cm", evaluator);
assertResult("1", "1 m to m", evaluator);
......
Markdown is supported
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