Permalink
Browse files

[DROOLS-79] fix jitting of constraints with primitive cast

  • Loading branch information...
1 parent e7326f4 commit 1307c923eca9fec7ef5af750fab11bbee8e0c72e @mariofusco mariofusco committed Mar 28, 2013
@@ -1583,4 +1583,20 @@ public void testScrambleProperties() {
assertTrue(list.contains("React"));
assertTrue(list.contains("React2"));
}
+
+ @Test
+ public void testJitCastOfPrimitiveType() {
+ // DROOLS-79
+ String str =
+ "rule R when\n" +
+ " Number(longValue < (Long)7)\n" +
+ "then\n" +
+ "end\n";
+
+ KnowledgeBase kbase = loadKnowledgeBaseFromString(str);
+ StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
+
+ ksession.insert(new Long(6));
+ assertEquals(1, ksession.fireAllRules());
+ }
}
@@ -678,8 +678,9 @@ protected final void cast(Class<?> from, Class<?> to) {
if (to.isPrimitive()) {
castPrimitiveToPrimitive(from, to);
} else {
- castPrimitiveToPrimitive(from, convertToPrimitiveType(to));
- castFromPrimitive(from);
+ Class toPrimitive = convertToPrimitiveType(to);
+ castPrimitiveToPrimitive(convertToPrimitiveType(from), toPrimitive);
+ castFromPrimitive(toPrimitive);
}
} else {
if (to.isPrimitive()) {
@@ -564,7 +564,7 @@ private void castOrCoercePrimitive(int regNr, Class<?> fromType, Class<?> toType
private Class<?> jitCastExpression(CastExpression exp) {
jitExpression(exp.expression, Object.class);
- cast(exp.getType());
+ cast(exp.expression instanceof FixedExpression ? ((FixedExpression)exp.expression).getTypeAsPrimitive() : exp.expression.getType(), exp.getType());
return exp.getType();
}
@@ -184,8 +184,14 @@ private Expression analyzeNode(ASTNode node) {
}
if (node instanceof TypeCast) {
- ExecutableAccessor accessor = (ExecutableAccessor) ((TypeCast)node).getStatement();
- return new CastExpression(node.getEgressType(), analyzeNode(accessor.getNode()));
+ ExecutableStatement statement = ((TypeCast)node).getStatement();
+ if (statement instanceof ExecutableAccessor) {
+ ExecutableAccessor accessor = (ExecutableAccessor) statement;
+ return new CastExpression(node.getEgressType(), analyzeNode(accessor.getNode()));
+ } else {
+ ExecutableLiteral literal = (ExecutableLiteral) statement;
+ return new CastExpression(node.getEgressType(), new FixedExpression(literal.getLiteral()));
+ }
}
if (node instanceof Union) {
@@ -631,6 +637,10 @@ public static boolean isFixed(Expression expression) {
private final Class<?> type;
private final Object value;
+ FixedExpression(Object value) {
+ this(value.getClass(), value);
+ }
+
FixedExpression(Class<?> type, Object value) {
this.type = type == CharSequence.class ? String.class : type;
this.value = value;
@@ -648,6 +658,18 @@ public boolean canBeNull() {
return type;
}
+ public Class<?> getTypeAsPrimitive() {
+ if (type == Integer.class) return int.class;
+ if (type == Long.class) return long.class;
+ if (type == Float.class) return float.class;
+ if (type == Double.class) return double.class;
+ if (type == Short.class) return short.class;
+ if (type == Byte.class) return byte.class;
+ if (type == Character.class) return char.class;
+ if (type == Boolean.class) return boolean.class;
+ return type;
+ }
+
public Object getValue() {
return value;
}

0 comments on commit 1307c92

Please sign in to comment.