Have you tried making your classes beans?
Eg adding getters and setters. (As a style guide in java you should generally avoid public
fields and class names should start with a capital letter).
This would give you the following:
public class Message {
private Numbers[] numbersArray;
protected Numbers[] getNumbersArray() {
return numbersArray;
}
protected void setNumbersArray(Numbers[] numbersArray) {
this.numbersArray = numbersArray;
}
}
public class Numbers {
private int first;
private int last;
protected int getFirst() {
return first;
}
protected void setFirst(int first) {
this.first = first;
}
protected int getLast() {
return last;
}
protected void setLast(int last) {
this.last = last;
}
}
Thomas
From: rules-users-bounces(a)lists.jboss.org [mailto:rules-users-bounces@lists.jboss.org] On
Behalf Of stefano corna
Sent: 14 December 2009 11:00
To: rules-users(a)lists.jboss.org
Subject: [rules-users] rule on Array of custom Object
hello,
im using drools 5.0 on eclipse and i have a problem. i want to use an array of object in
my rule condition.
now i try to explain:
i have my java class with the array attribute
-------------------------------------------------
public static class Message {
public numbers[] numbersArray;
}
--------------------------------------------
and the numbers class that rapresent the objects contained in numbersArray
----------------------------------------------
public static class numbers
{
public int first;
public int last;
}
--------------------------------------------------
my goal is make a rule that check if Message.numbersArray[0].first="value"
i use
-------------------------------------------------
rule "rule_001"
dialect "mvel"
when
$msg : Message(numbersArray[0].first=="1")
then
System.out.println("something....");
end
-------------------------------
and on runtime it give me this error:
---------------------------------------
org.drools.RuntimeDroolsException: Exception executing predicate
numbersArray[0].first=="1"
at org.drools.rule.PredicateConstraint.isAllowed(PredicateConstraint.java:279)
at org.drools.reteoo.AlphaNode.assertObject(AlphaNode.java:143)
at
org.drools.reteoo.CompositeObjectSinkAdapter.doPropagateAssertObject(CompositeObjectSinkAdapter.java:360)
at
org.drools.reteoo.CompositeObjectSinkAdapter.propagateAssertObject(CompositeObjectSinkAdapter.java:344)
at org.drools.reteoo.ObjectTypeNode.assertObject(ObjectTypeNode.java:185)
at org.drools.reteoo.EntryPointNode.assertObject(EntryPointNode.java:146)
at
org.drools.common.AbstractWorkingMemory.insert(AbstractWorkingMemory.java:1046)
at
org.drools.common.AbstractWorkingMemory.insert(AbstractWorkingMemory.java:1001)
at org.drools.common.AbstractWorkingMemory.insert(AbstractWorkingMemory.java:788)
at
org.drools.impl.StatefulKnowledgeSessionImpl.insert(StatefulKnowledgeSessionImpl.java:216)
at com.sample.DroolsTest.main(DroolsTest.java:47)
Caused by: [Error: unable to access property (null parent): first]
[Near : {... Unknown ....}]
^
[Line: 1, Column: 0]
at
org.mvel2.optimizers.impl.refl.ReflectiveAccessorOptimizer.getMethod(ReflectiveAccessorOptimizer.java:861)
at
org.mvel2.optimizers.impl.refl.ReflectiveAccessorOptimizer.getBeanProperty(ReflectiveAccessorOptimizer.java:585)
at
org.mvel2.optimizers.impl.refl.ReflectiveAccessorOptimizer.compileGetChain(ReflectiveAccessorOptimizer.java:313)
at
org.mvel2.optimizers.impl.refl.ReflectiveAccessorOptimizer.optimizeAccessor(ReflectiveAccessorOptimizer.java:138)
at org.mvel2.ast.ASTNode.getReducedValueAccelerated(ASTNode.java:133)
at
org.mvel2.ast.BinaryOperation.getReducedValueAccelerated(BinaryOperation.java:102)
at org.mvel2.MVELRuntime.execute(MVELRuntime.java:85)
at org.mvel2.compiler.CompiledExpression.getValue(CompiledExpression.java:107)
at org.mvel2.MVEL.executeExpression(MVEL.java:978)
at
org.drools.base.mvel.MVELPredicateExpression.evaluate(MVELPredicateExpression.java:75)
at org.drools.rule.PredicateConstraint.isAllowed(PredicateConstraint.java:272)
... 10 more
-------------------------------------------------------
moreover if i try to print the "first" value:
.
.
.
then
System.out.println(numbersArray[0].first);
end
appear this new error and i cant compile:
"BuildError: Unable to build expression for 'consequence': Failed to compile:
1 compilation error(s):
- (1,12) unqualified type in strict mode for: numbersArray'
System.out.println(numbersArray[0].first);
'"
someone can kindly help me?
________________________________
**************************************************************************************
This message is confidential and intended only for the addressee. If you have received
this message in error, please immediately notify the postmaster(a)nds.com and delete it from
your system as well as any copies. The content of e-mails as well as traffic data may be
monitored by NDS for employment and security purposes. To protect the environment please
do not print this e-mail unless necessary.
NDS Limited. Registered Office: One London Road, Staines, Middlesex, TW18 4EX, United
Kingdom. A company registered in England and Wales. Registered no. 3080780. VAT no. GB 603
8808 40-00
**************************************************************************************
________________________________
This message is confidential and intended only for the addressee. If you have received
this message in error, please immediately notify the postmaster(a)nds.com and delete it from
your system as well as any copies. The content of e-mails as well as traffic data may be
monitored by NDS for employment and security purposes.
To protect the environment please do not print this e-mail unless necessary.
An NDS Group Limited company.
www.nds.com