setGlobal issue in StatefulSession
by tellkb
Hi there,
I want to set setGlobal in working memory at run time based on derived
class, e.g. DerivedOne or DerivedTwo. But it throws following error:
Illegal class for global. Expected [org.comp.app.bean.Base], found
[org.comp.app.bean.DerivedOne].
It should automatically resolve the derived class. Please let me know if I'm
doing something wrong. I'd appreciate your help.
Thanks,
-KB
Here are the classed I'm using in my test project:
class abstract Base () {
public void init( StatefulSession workingMemory ) {
this.workingMemory = workingMemory;
workingMemory.setGlobal("app", this);
}
public void logMe() {
System.out.println("From Derived BASE...");
}
}
class DerivedOne() extends Base{
public void runRules( Object obj) {
ObjOne obj1 = (ObjOne)obj
workingMemory.insert(obj1);
workingMemory.fireAllRules();
workingMemory.dispose();
}
public void logMe() {
System.out.println("From Derived ONE...");
}
}
class DerivedTwo() extends Base{
public void runRules( Object obj) {
ObjTwo obj2 = (ObjTwo)obj
workingMemory.insert(obj2);
workingMemory.fireAllRules();
workingMemory.dispose();
}
public void logMe() {
System.out.println("From Derived ONE...");
}
}
class Caller() {
RuleBase ruleBase = ruleBaseFactory.open(docRules);
workingMemory = ruleBase.newStatefulSession();
if (sub==1) {
der1 = new DerivedOne();
der1.init(workingMemory);
der1.runRules();
}
else if (sub==1) {
der2 = new DerivedTwo();
der2.init(workingMemory);
der2.runRules();
}
}
//********DRL FILE*****
package com.sample
import com.sample.app.RuleCaller.Message;
global com.sample.app.Base app;
rule "using a static function"
when
eval( true )
then
System.out.println( "Message from Rule." );
app.logMe();
end
//********DRL FILE*****
--
View this message in context: http://www.nabble.com/setGlobal-issue-in-StatefulSession-tp23373008p23373...
Sent from the drools - user mailing list archive at Nabble.com.
16 years, 7 months
Rule problem
by Femke De Backere
Hi!
I want to make en BMI rule application, so I need to test the BMI
values. But in rule "Overgewicht" and "Normaal", I need to use an AND,
but it doesn't seem to work in any way I tried.
Does anybody see the problem? The rest of the application is based on
de sample project (created when making a new Drools project).
Thx,
Femke
package bmi
import bmi.DroolsTest.BMIClass;
rule "Overgewicht"
when
bmiClass : BMIClass(bmi >= 25)
bmiClass : BMIClass(bmi < 30)
then
System.out.println("U heeft overgewicht");
end
rule "Obesitas"
when
bmiClass : BMIClass($bmi : bmi >= 30)
then
System.out.println("U heeft obesitas");
end
rule "Ondergewicht"
when
bmiClass : BMIClass($bmi : bmi < 18.5)
then
System.out.println("U heeft ondergewicht");
end
rule "Normaal"
when
bmiClass : BMIClass(bmi >= 18.5)
bmiClass : BMIClass(bmi < 25)
then
System.out.println("U heeft een normaal gewicht");
end
16 years, 7 months
lock() on a RulesBase object
by Mike Merrill
As I understand the lock() method, the rules base and all stateful sessions
are 'locked' while the application does what it needs to do to
update/remove/add packages to the rules base. Yet, I find that I need to
additionally synchronize on the RuleBase object to achieve this behavior. Am I
misunderstanding what the lock() method is supposed to do?
16 years, 7 months
How to avoid re-propagating entire object?
by Tom.E.Murphy@wellsfargo.com
Apologies if this question has been addressed before.
We generate our Drools rules automatically from a business rules repository we've had here for years.
Currently, our code generator is doing something I'd rather not have to do - it inspects assignments on the RHS and inserts "anti-recursion" logic into the LHS.
This leads to rules like the following:
rule "RS6631.1.2"
no-loop true
dialect "mvel"
when
$policySet : PolicySet
(
marketType == MarketTypeEnum.FHA
# anti-recursion logic derived from assignments in action section
, isSetSelectedDecisionIndicator == false
|| selectedDecisionIndicator != true
# end anti-recursion logic
)
then
modify($policySet)
{
selectedDecisionIndicator = true
};
end
Notice the two lines between the comments - the code generator has analyzed the assignment on the RHS and has inserted this defensive logic in order to prevent (possibly infinite) re-firings of this rule anytime other rules also update the PolicySet object. This approach does not work in all scenarios, however, and there is an occasional struggle by our rule analysts to find ways to suppress the loops caused by rule "A" updating an object versus rule "B" updating the same object, even when the attributes changed by the two rules are different.
The root issue, I believe, is that the modify and update require fact handles that refer to whole objects, not to individual attributes of those objects. When an attribute value is updated, my intuition suggests that only the new value of that attribute should be propagated through the network, not the whole object. Re-propagating the entire object causes all rules that have already fired to be reactivated, since their conditions are still true. All we really want is that the rules whose conditions NEWLY match both the unchanged attributes and the changed attributes should be activated, not rules that matched previously on the unchanged attributes and whose CE's or constraints have not been affected by the change.
I've seen several conversations on this list about how to prevent infinite loops. "No-loop" only seems to work within the context of the current rule. Two rules that update an object, even if their changes are to non-overlapping attributes, cause each other to be reactivated in the absence of some defensive logic like that shown above. "Lock-on-active" doesn't appear to achieve the goal either, because it suppresses firings that might in fact be desired.
Feel free to educate me if my limited understanding is showing here.
But does anyone know of a way to tell the Drools engine to only propagate the attributes that have changed, and not the entire object?
Tom Murphy
Wells Fargo HCFG - CORE Deal Decisioning Platform
16 years, 7 months
Rules flow execution.
by tati-2
Hi,
I have a list of objects in the Stateless session and use salience to
control the order of rules execution .
My expectation is that all objects are going to go through rule_1 then
rule_2 and so on.
However it looks like when I have more then one object in the session the
order is unpredictable. What do I miss?
Thank you for the answer.
Tati
--
View this message in context: http://www.nabble.com/Rules-flow-execution.-tp23391947p23391947.html
Sent from the drools - user mailing list archive at Nabble.com.
16 years, 7 months
How to get workItem name?
by brian.xiang@thomsonreuters.com
Hi,
For example if I have a workItem defined like this:
<nodes>
<workItem id="2" name="Bob" x="63" y="136" width="117" height="40" >
<work name="Email" >
<parameter name="inputType" >
</parameter>
</work>
<mapping type="in" from="content" to="content" />
</workItem>
</nodes>
How do I get the work item name - Bob? WorkItem.getName() returns
"Email" which is to me kind of the type of the WorkItem, not the name of
the WorkItem instance.
Thanks in advance,
Brian
16 years, 7 months
Problems with custom functions and types in one drl file
by Rainer Schulz
Hi,
i have some problems with custom functions and types.
I m using Drools 5.0 and Eclipse 3.4.2 (have to use these eclipse
version, so my decision was to use the drools beta).
I declared a type in my rule file like :
declare VarHolder
var1 : String
var2 : Long
var3 : String
end
also i specified a custom function in the same file, which i have to use
in my rules (it's a example function) :
function String hello(String name) {
return "Hello "+name+"!";
}
.
The declared Type will be used in the java code :
// handle the type as necessary:
// create instances:
Object vars = varHolderTyp.newInstance();
// set attributes values
varHolderTyp.setFromMap(vars,varMap);
// insert fact into a session
StatefulKnowledgeSession ksession = ...
ksession.insert( vars );
When it comes to the "setFromMap", then I get a ClassCastException for
the VarHolderTyp. But when I remove the declaration of the function in
the rule file, then everything works fine (all variables will be filled
correctly). It seems, that the reflection mechanism will not work
correctly, when drools want to create the Pojo for VarHolder and the
Static Method for the function. But it s very difficult to debug,
because the exception fires in the drools engine.
Maybe I've make a mistake so anyone knows a solution for this problem
or can tell me, where my mistake is. It will be nice, when I can declare
a type and a function together in one rule file. At the moment, I
created a java class for VarHolder, so the exception does not appear,
but I m losing a lot of dynamic.
Please give me a hint
16 years, 7 months
Help with MatchesEvaluatorsDefinition that caches compiled regular expression patterns
by Jared Davis
I need some help placing a cache of compiled regular expressions using the
MatchesEvaluatorsDefinition as a starting point.
Just for a test I placed a static map in the class:
static Map<String,Pattern> uglyHack = new HashMap<String,Pattern>();
Then in each of the evaluators I have different versions of:
Pattern p = uglyHack.get(value2);
if (p == null ) {
p = Pattern.compile(value2);
uglyHack.put(value2, p);
}
Matcher m = p.matcher(value1);
return m.matches();
instead of
return value1.matches( value2 );
The time elapsed to inserting objects into the workspace has been cut in half.
.4 seconds -> .2
Has someone already done this "optimization?" Where should the cache be stored?
16 years, 7 months