Rule extension is an extremely useful feature for authoring technical
rules, and it deserves a high level of attention, which it hasn't really
been given up to now.
But, for the scenario that's been presented here, I'm inclined to think
that an approach without extends would be much better:
- The RuleConstant object doesn't have to be bound to $CONSTANT -
apparently fields such as SEDAN and COUPE are static fields and could be
used simply as RuleConstant.SEDAN, etc.
- Keeping objects in a single container fact ("Vehicles") and extracting
them by using "from" in each rule is not good for performance. Simply
inserting all individual Vehicle facts is better, and it avoids the
inevitable loops.
rule "Custom|2"
enabled true
when
$vehicle:Vehicle(modelYear > 2000, type = RuleConstant.COUPE)
then
modify( $vehicle ){
setValue(100)
}
end
-W
On 5 April 2013 19:21, Arul Prashanth <arul.prashanth(a)gmail.com> wrote:
I have a Rule implementation as below which works as expected, here
Vehicles() and RuleConstant are facts.
/rule "Rule|1"
no-loop true
when
$CONSTANT : RuleConstant();
$vehicles : Vehicles();
$vehicle:Vehicle(modelYear > 2000, type = $CONSTANT.SEDAN) from
$vehicles.Vehicle;
then
$vehicle.setValue(100);
update($vehicles);
end /
But am planning to abstract the rules in the below fashion so that we can
reuse the LHS conditions which doesn't work and throws the below said Null
Pointer Exception
*BaseRule.drl*
/package com.xyz.base
rule "Base|1"
no-loop true
enabled false
when
$CONSTANT : RuleConstant();
$vehicles : Vehicles();
then
end /
*CustomRule.drl*
/package com.xyz.custom
rule "Custom|1" extends "Base|1"
no-loop true
enabled true
when
$vehicle:Vehicle(modelYear > 2000, type = $CONSTANT.SEDAN) from
$vehicles.Vehicle;
then
$vehicle.setValue(100);
update($vehicles);
end
rule "Custom|2" extends "Base|1"
no-loop true
enabled true
when
$vehicle:Vehicle(modelYear > 2000, type = $CONSTANT.COUPE) from
$vehicles.Vehicle;
then
$vehicle.setValue(100);
update($vehicles);
end/
*Exception:*
Caused by: [Error: drools.update($vehicles): null]
[Line: 1, Column: 1]
at
org.mvel2.optimizers.impl.refl.ReflectiveAccessorOptimizer.compileGetChain(ReflectiveAccessorOptimizer.java:416)
at
org.mvel2.optimizers.impl.refl.ReflectiveAccessorOptimizer.optimizeAccessor(ReflectiveAccessorOptimizer.java:143)
at org.mvel2.ast.ASTNode.optimize(ASTNode.java:159)
at
org.mvel2.ast.ASTNode.getReducedValueAccelerated(ASTNode.java:115)
at org.mvel2.MVELRuntime.execute(MVELRuntime.java:85)
at
org.mvel2.compiler.CompiledExpression.getValue(CompiledExpression.java:123)
at
org.mvel2.compiler.CompiledExpression.getValue(CompiledExpression.java:116)
at org.mvel2.MVEL.executeExpression(MVEL.java:930)
at
org.drools.base.mvel.MVELConsequence.evaluate(MVELConsequence.java:101)
at
org.drools.common.DefaultAgenda.fireActivation(DefaultAgenda.java:1273)
... 33 more
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at
org.mvel2.optimizers.impl.refl.ReflectiveAccessorOptimizer.getMethod(ReflectiveAccessorOptimizer.java:1105)
at
org.mvel2.optimizers.impl.refl.ReflectiveAccessorOptimizer.getMethod(ReflectiveAccessorOptimizer.java:987)
at
org.mvel2.optimizers.impl.refl.ReflectiveAccessorOptimizer.compileGetChain(ReflectiveAccessorOptimizer.java:377)
... 42 more
Caused by: java.lang.NullPointerException
at
org.drools.common.DefaultAgenda.createActivation(DefaultAgenda.java:569)
at
org.drools.reteoo.RuleTerminalNode.modifyLeftTuple(RuleTerminalNode.java:297)
at
org.drools.reteoo.SingleLeftTupleSinkAdapter.propagateModifyChildLeftTuple(SingleLeftTupleSinkAdapter.java:246)
at org.drools.reteoo.JoinNode.modifyLeftTuple(JoinNode.java:363)
at
org.drools.reteoo.SingleLeftTupleSinkAdapter.propagateModifyChildLeftTuple(SingleLeftTupleSinkAdapter.java:272)
at
org.drools.reteoo.EvalConditionNode.modifyLeftTuple(EvalConditionNode.java:213)
at
org.drools.reteoo.SingleLeftTupleSinkAdapter.propagateModifyChildLeftTuple(SingleLeftTupleSinkAdapter.java:259)
at
org.drools.reteoo.FromNode.checkConstraintsAndPropagate(FromNode.java:337)
at org.drools.reteoo.FromNode.modifyLeftTuple(FromNode.java:281)
at
org.drools.reteoo.CompositeLeftTupleSinkAdapter.propagateModifyChildLeftTuple(CompositeLeftTupleSinkAdapter.java:316)
at org.drools.reteoo.JoinNode.modifyRightTuple(JoinNode.java:273)
at org.drools.reteoo.BetaNode.modifyObject(BetaNode.java:531)
at org.drools.reteoo.JoinNode.modifyObject(JoinNode.java:446)
at
org.drools.reteoo.CompositeObjectSinkAdapter.doPropagateModifyObject(CompositeObjectSinkAdapter.java:507)
at
org.drools.reteoo.CompositeObjectSinkAdapter.propagateModifyObject(CompositeObjectSinkAdapter.java:432)
at
org.drools.reteoo.ObjectTypeNode.modifyObject(ObjectTypeNode.java:314)
at
org.drools.reteoo.EntryPointNode.modifyObject(EntryPointNode.java:265)
at
org.drools.common.NamedEntryPoint.update(NamedEntryPoint.java:470)
at
org.drools.common.NamedEntryPoint.update(NamedEntryPoint.java:370)
at
org.drools.base.DefaultKnowledgeHelper.update(DefaultKnowledgeHelper.java:317)
at
org.drools.base.DefaultKnowledgeHelper.update(DefaultKnowledgeHelper.java:329)
at
org.drools.base.DefaultKnowledgeHelper.update(DefaultKnowledgeHelper.java:325)
... 49 more
-----
- Prashanth
--
View this message in context:
http://drools.46999.n3.nabble.com/Abstracting-Rules-using-extends-tp40232...
Sent from the Drools: User forum mailing list archive at
Nabble.com.
_______________________________________________
rules-users mailing list
rules-users(a)lists.jboss.org
https://lists.jboss.org/mailman/listinfo/rules-users