public static void invert(WriteGraph graph, Variable base, String expression, Object value) throws DatabaseException {
InvertBasicExpressionVisitor visitor = new InvertBasicExpressionVisitor();
Expressions.evaluate(replaced(expression), visitor);
- Object pair = visitor.getResult();
- if(pair == null) return;
- if(pair instanceof Triple) {
+ Object result = visitor.getResult();
+ if(result == null) return;
+ if(result instanceof Triple) {
@SuppressWarnings("unchecked")
- Triple<Double, Double, String> data = (Triple<Double, Double, String>)pair;
+ Triple<Double, Double, String> data = (Triple<Double, Double, String>)result;
String key = data.third.replace(MAGIC, ".");
String path = getVariablePath(graph, base, key);
Variable targetVariable = base.browse(graph, path);
if(value instanceof Number) {
if(Math.abs(data.first) > 1e-9) {
- Double inverted = (((Number)value).doubleValue() - data.second) / data.first;
- targetVariable.setValue(graph, inverted);
+ double inverted = (double) (((Number)value).doubleValue() - data.second) / data.first;
+ Object invertedValue = numericValueInType(inverted, value.getClass());
+ targetVariable.setValue(graph, invertedValue);
}
} else if (value instanceof Boolean) {
// TODO: support 'not'
}
}
-
+
+ private static Object numericValueInType(double value, Class<?> type) {
+ if (type == Integer.class) {
+ return (int) value;
+ } else if (type == Long.class) {
+ return (long) value;
+ } else if (type == Byte.class) {
+ return (byte) value;
+ } else if (type == Float.class) {
+ return (float) value;
+ }
+ return value;
+ }
+
private static String getVariablePath(ReadGraph graph, Variable base, String key) throws DatabaseException {
StructuralResource2 STR = StructuralResource2.getInstance(graph);
Resource type = base.getPossibleType(graph);
}
- public static Variable possibleInvertibleExpressionReferencedProperty(ReadGraph graph, Variable base, String expression) throws DatabaseException {
+ @SuppressWarnings("unchecked")
+ private static Triple<Double, Double, String> possibleInvertibleExpression(ReadGraph graph, Variable base, String expression) throws DatabaseException {
if (base == null || expression == null || expression.isEmpty())
return null;
InvertBasicExpressionVisitor visitor = new InvertBasicExpressionVisitor();
//System.out.println("invert : " + expression + " -> " + replaced(expression) + " for " + base.getURI(graph));
Expressions.evaluate(replaced(expression), visitor);
- Object pair = visitor.getResult();
- if(pair == null)
- return null;
- if(pair instanceof Triple) {
- @SuppressWarnings("unchecked")
- Triple<Double, Double, String> data = (Triple<Double, Double, String>)pair;
- String key = data.third.replace(MAGIC,".");
- String path = getVariablePath(graph, base, key);
- if (path == null)
- return null;
- Variable targetVariable = base.browsePossible(graph, path);
- return targetVariable;
- }
+ Object result = visitor.getResult();
+ if (result instanceof Triple)
+ return (Triple<Double, Double, String>) result;
return null;
}
+ public static Variable possibleInvertibleExpressionReferencedProperty(ReadGraph graph, Variable base, String expression) throws DatabaseException {
+ Triple<Double, Double, String> data = possibleInvertibleExpression(graph, base, expression);
+ if (data == null)
+ return null;
+ String path = getVariablePath(graph, base, data.third.replace(MAGIC, "."));
+ return path != null ? base.browsePossible(graph, path) : null;
+ }
+
+ public static Triple<Double, Double, Variable> possibleInvertibleExpressionReferencedTransformedProperty(ReadGraph graph, Variable base, String expression) throws DatabaseException {
+ Triple<Double, Double, String> data = possibleInvertibleExpression(graph, base, expression);
+ if (data == null)
+ return null;
+ String path = getVariablePath(graph, base, data.third.replace(MAGIC, "."));
+ if (path == null)
+ return null;
+ Variable targetVariable = base.browsePossible(graph, path);
+ return targetVariable != null ? Triple.make(data.first, data.second, targetVariable) : null;
+ }
+
}