single rule fire in drl
by yash kapoor
Hi All,
I have two questions related with drools 5.0
Q1- How can I triggered each rule one by one like
rule 01
when
......
then
......
End
rule 02
when
......
then
......
End
rule 03
when
......
then
......
End
If I write a command
*
final* StatefulKnowledgeSession ksession =
kbase.newStatefulKnowledgeSession();
ksession.fireAllRules();
then all rules will be fired but I want to executed only *rule 02* how can
I able to do this?
Q2-Can I make two objects in when section like
rule 01
when
obj1:Temp1();
obj2:Temp2();
then
System.out.println("Hello World");
End
Here Temp1 and Temp 2 is two different class ,when I am running this DRL
file is not parsed .
Please correct me if I am wrong .
Regards
Yash
15 years, 9 months
Re: Drools solver performance question
by Geoffrey De Smet
Unfortunately, when a constraint rule no longer applies, the score
wouldn't decrement.
So this would work in a stateless working memory, where the entire score
is recalculated for every move of every step.
But drools-solver works in a statefull working memory, where only the
part of the score that is dirty is recalculated (="score delta") for
every move of every step.
Now I haven't experimented with the first option yet, though I am pretty
sure it's exponentially slower.
If anyone wants to try it out, it would be nice to confirm or break my
theory :)
PS: please keep this discussion on the mailing list, so others can
benefit from it too.
With kind regards,
Geoffrey De Smet
Greg Barton schreef:
> What I'm talking about isn't like what you've got in that commented out code. Basically, it would use the same rules, matching on an accumulator object instead of inserting new Constraint objects, and the last rule would match the accumulator and set the score.
>
> rule "multipleQueensHorizontal"
> when
> $a : Accumulator();
> $q1 : Queen($id : id, $y : y);
> $q2 : Queen(id > $id, y == $y);
> then
> $a.increment();
> end
>
> // multipleQueensVertical is obsolete because it is always 0
>
> rule "multipleQueensAscendingDiagonal"
> when
> $a : Accumulator();
> $q1 : Queen($id : id, $ascendingD : ascendingD);
> $q2 : Queen(id > $id, ascendingD == $ascendingD);
> then
> $a.increment();
> end
>
> rule "multipleQueensDescendingDiagonal"
> when
> $a Accumulator();
> $q1 : Queen($id : id, $descendingD : descendingD);
> $q2 : Queen(id > $id, descendingD == $descendingD);
> then
> $a.increment();
> end
>
> rule "hardConstraintsBroken"
> salience -10
> when
> $a : Accumulator();
> then
> scoreCalculator.setScore(- $a.intValue());
> end
>
>
> --- On Fri, 2/13/09, Geoffrey De Smet <ge0ffrey.spam(a)gmail.com> wrote:
>
>
>> From: Geoffrey De Smet <ge0ffrey.spam(a)gmail.com>
>> Subject: Re: Drools solver performance question
>> To:
>> Cc: greg_barton(a)yahoo.com
>> Date: Friday, February 13, 2009, 8:42 AM
>> Hi Greg,
>>
>> Yes, I expected that too, but my experiments proved me
>> wrong (for now).
>> The current implementation of accumulate backwards chains I
>> believe.
>> Backward chaining doesn't do score delta calculation,
>> so it's very bad for scalability.
>>
>> Try it yourself with the nqueens example.
>> Uncomment the code in nqueensScoreRules.drl and see what
>> happens if you go from n=8 to n=16 and more.
>>
>> With kind regards,
>> Geoffrey De Smet
>>
>>
>>
>> Greg Barton schreef:
>>
>>> I answered a performance question a user had about
>>> drools-solver, but I'm afraid I answered with a
>>>
>> touch of
>>
>>> ignorance about the way drools-solver is set up. His
>>>
>> rules
>>
>>> had a new object being inserted into the working
>>>
>> memory each
>>
>>> time a constraint rule was matched. The constraint
>>>
>> objects
>>
>>> were then accumulated by a low priority rule at the
>>>
>> end. After looking at the drools-solver examples it looks
>> like
>>
>>> they're all that way. It strikes me that having a
>>> single accumulator object, and not creating and
>>>
>> inserting a
>>
>>> new object in each constraint rule, would be more
>>>
>> efficient.
>>
>>> Is it possible to set this up in a solver ruleset?
>>> Thanks,
>>> GreG
>>>
>>>
>>>
>>>
>>>
>
>
>
>
>
15 years, 9 months
re:Shadow Facts
by J Michael Dean
Sorry - in previous email forgot to include the following code, as the
NPE occurs when this routine is called at the addPackage call.
private void readDRLintoPackage(String rulesFile) throws
DroolsParserException, IOException, Exception {
Reader source = new
InputStreamReader(RulesEngine.class.getResourceAsStream("/" +
rulesFile));
PackageBuilder builder = new PackageBuilder();
builder.addPackageFromDrl(source);
Package pkg = builder.getPackage();
rules = RuleBaseFactory.newRuleBase();
rules.addPackage(pkg);
}
15 years, 9 months
Re:Shadow Facts
by J Michael Dean
Edson - build2589-rev25264-20090214-0603 is the snapshot that I
downloaded and installed. However, I have done a search on the source
and have not been able to locate
MiscTest.testPropertyChangeSupportNewAPI() in the source at all.
Interestingly, the DRL compilation does not crash if I use
@propertyChangeSupport() or @propertyChangeSupport(true) etc. But
otherwise it crashes. It dies in the following code:
public DecisionEngine() {
super();
try {
rulesEngine = new RulesEngine("GlucoseDroolRules00.drl");
//rulesEngine = new RulesEngine("rules_version1.drl");
//rulesEngine = new RulesEngine();
} catch (RulesEngineException e) {
System.out.println("Error in DecisionEngine constructor");
e.printStackTrace();
}
}
public RulesEngine(String rulesFile) throws RulesEngineException {
super();
try {
readDRLintoPackage(rulesFile);
addSpreadsheetToPackage();
} catch (Exception e) {
System.out.println(e);
throw new RulesEngineException("Could not load/compile rules file:
" + rulesFile, e);
}
}
java.lang.NullPointerException
Error in DecisionEngine constructor
drools.engine.RulesEngineException: Could not load/compile rules file:
GlucoseDroolRules00.drl
at drools.engine.RulesEngine.<init>(RulesEngine.java:33)
at drools.engine.DecisionEngine.<init>(DecisionEngine.java:22)
at glucose.tests.AllDroolsRulesTests.setUp(AllDroolsRulesTests.java:33)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun
.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:
39)
at
sun
.reflect
.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:
25)
at java.lang.reflect.Method.invoke(Method.java:585)
at
org
.junit
.internal
.runners.BeforeAndAfterRunner.invokeMethod(BeforeAndAfterRunner.java:74)
at
org
.junit
.internal
.runners.BeforeAndAfterRunner.runBefores(BeforeAndAfterRunner.java:50)
at
org
.junit
.internal
.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:33)
at
org.junit.internal.runners.TestClassRunner.run(TestClassRunner.java:52)
at
org
.eclipse
.jdt
.internal
.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:45)
at
org
.eclipse
.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at
org
.eclipse
.jdt
.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:
460)
at
org
.eclipse
.jdt
.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:
673)
at
org
.eclipse
.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:
386)
at
org
.eclipse
.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:
196)
Caused by: java.lang.NullPointerException
at
org.drools.common.AbstractRuleBase.addPackages(AbstractRuleBase.java:
429)
at org.drools.reteoo.ReteooRuleBase.addPackage(ReteooRuleBase.java:394)
at drools.engine.RulesEngine.readDRLintoPackage(RulesEngine.java:54)
at drools.engine.RulesEngine.<init>(RulesEngine.java:29)
... 16 more
If I do put () after the annotation, the DRL file does compile, but I
do NOT get dynamic behavior.
Thanks.
- Mike
Can you provide the error messages your are getting?
Make sure you use at least build #2589, that include the changes.
https://hudson.jboss.org/hudson/job/drools/2589/artifact/trunk/target/
There is a test case for this on
MiscTest.testPropertyChangeSupportNewAPI().
[]s
Edson
> Using snapshot from yesterday so presume I have the revision. The
> following
> DRL file dies a horrible death, being unable to be compiled. If I
> comment
> out the tags then it works. In the rules editor, the word "declare"
> is not
> highlighted either. I must be misunderstanding something simple.
>
> package glucose.decision.rules
>
> import glucose.decision.object.GlucoseDecision;
> import glucose.decision.object.GlucoseDecisionState;
>
> declare GlucoseDecision
> @propertyChangeSupport
> end
>
> declare GlucoseDecisionState
> @propertyChangeSupport
> end
15 years, 9 months
Re: [rules-users] Shadow facts
by J Michael Dean
I think I was not clear in my note. I used dynamic facts with Drools
4.07 but am migrating to Drools 5, where there is no longer a second
argument to the insert method. My POJOs do have property change
listeners.
Here is an example rule that activates but never apparently fires:
rule "Detect insulin on"
dialect "java"
when
decision : GlucoseDecision(currentInsulinDripRate > 0)
decisionState : GlucoseDecisionState( insulinOn == false )
then
decisionState.setInsulinOn(true);
end
The relevant method that is called on the RHS:
public void setInsulinOn(boolean insulinOn) {
boolean oldValue = this.isInsulinOn();
this.insulinOn = insulinOn;
firePropertyChange("insulinOn", oldValue, insulinOn);
}
The eventual call is to fireRules which then calls executeRules in a
callback. Previously, I had a Boolean argument to the insert
statements and all the code worked with the old RuleBase, RuleAgent
objects. But since changing to KnowledgeSession, the rules
do not appear to fire. Here are the relevant methods:
public void executeRules(WorkingEnvironmentCallback callback) {
StatefulKnowledgeSession ksession =
kbase.newStatefulKnowledgeSession();
callback.initEnvironment(ksession);
ksession.fireAllRules();
ksession.dispose();
}
public void fireRules(final GlucoseDecision glucoseDecision, final
GlucoseDecisionState decisionState) {
rulesEngine.executeRules(new WorkingEnvironmentCallback() {
public void initEnvironment(StatefulKnowledgeSession workingMemory) {
workingMemory.insert(glucoseDecision);
workingMemory.insert(decisionState);
}
});
}
>
> Message: 2
> Date: Wed, 11 Feb 2009 10:26:06 -0500
> From: Edson Tirelli <tirelli(a)post.com>
> Subject: Re: [rules-users] Shadow facts
> To: Rules Users List <rules-users(a)lists.jboss.org>
> Message-ID:
> <e6dd5ba30902110726t2b09ee9dtdc7e85ed4217f80a(a)mail.gmail.com>
> Content-Type: text/plain; charset="iso-8859-1"
>
> Dean,
>
> There are two unrelated things in here... the boolean flag for
> dynamic
> facts means your fact supports the property change listeners and you
> don't
> need to call modify() when you change an attribute. ShadowFacts are a
> complete different thing that should be mostly transparent to users.
> If you can provide an example, it will be easier to help you.
>
> []s
> Edson
>
> 2009/2/11 J Michael Dean <mdean77(a)comcast.net>
>
>> I am migrating from 4.07 to 5 and my previous DRL file does not
>> function
>> correctly; I previousliy inserted facts with the dynamic Boolean
>> set true.
>> I understand that this is no longer done because shadow facts are
>> "not
>> needed" in Drools 5, but is it necessary to do anything different
>> in the RHS
>> when modifying facts? My fact POJOs have a method for adding text to
>> themselves. I believe my rules are not working properly because
>> nothing is
>> being modified - only the initial activation set fires and then
>> nothing
>> happens.
>>
>> Restated, the documentation talks about how to "avoid" shadow facts
>> in
>> Drools 4.07 - are the restrictions in this part of the docs
>> necessary in
>> Drools 5?
>>
>> Thanks.
>> _______________________________________________
>> rules-users mailing list
>> rules-users(a)lists.jboss.org
>> https://lists.jboss.org/mailman/listinfo/rules-users
>>
15 years, 9 months
Drools-solver performance optimizations?
by Wim Vancroonenburg
Hi,
I'm a student currently evaluating Drools Solver for my dissertation. I am
currently trying to solve an optimization problem with two different solvers
(one of which is Drools Solver) and I am comparing the results with earlier
obtained results from literature. However I am having some troubles with the
performance of Drools Solver, and I was hoping if someone could look at my
rules to see if they could be tuned:
rule "patientsToBeAssignedToRoomsOfAppropriateSex"
when
$n : Night();
$room : Room(sexRestriction == Sex.Dependent && capacity >
1);
$genders : ArrayList(size>1) from collect(
PatientStay(bed.room == $room, night == $n) );
exists PatientStay(bed.room == $room, night == $n, $a :
admission,
eval(((PatientStay)$genders.get(0)).getAdmission().getPatient().getSex() !=
$a.getPatient().getSex()));
then
insertLogical(new
IntConstraintOccurrence("patientsToBeAssignedToRoomsOfAppropriateSex",ConstraintType.NEGATIVE_HARD,50,$room,$n));
end
rule "hasRequiredRoomProperties"
when
$pr : RequiredRoomPropertiesConstraint($a : admission, $r :
room, $w : weight );
$ps : PatientStay(admission == $a, bed.room == $r);
then
insertLogical(new
IntConstraintOccurrence("hasPreferredRoomProperties",ConstraintType.NEGATIVE_SOFT,50*$w,$ps));
end
rule "unplannedTransfers"
when
$ps : PatientStay($a : admission, $b : bed, $n : night);
$ps2 : PatientStay(admission == $a, bed != $b, $n2 :
night,eval($n.getIndex()+1 == $n2.getIndex()));
then
insertLogical(new
IntConstraintOccurrence("unplannedTransfers",ConstraintType.NEGATIVE_SOFT,110,$ps,$ps2));
end
rule "hasPreferredRoomProperties"
when
$pr : PreferredRoomPropertiesConstraint($a : admission, $r :
room, $w : weight );
$ps : PatientStay(admission == $a, bed.room == $r);
then
insertLogical(new
IntConstraintOccurrence("hasPreferredRoomProperties",ConstraintType.NEGATIVE_SOFT,20*$w,$ps));
end
rule "meetsRoomPreference"
when
$mr : MeetsRoomPreferenceConstraint($a : admission, $r :
room);
$ps : PatientStay(admission == $a, bed.room == $r);
then
insertLogical(new
IntConstraintOccurrence("meetsRoomPreference",ConstraintType.NEGATIVE_SOFT,8,$ps));
end
rule "inGoodDepartment"
when
$gd : GoodDepartmentConstraint($a : admission, $d :
department);
$ps : PatientStay(admission == $a, $b : bed,
eval($b.getRoom().getDepartment().equals($d)));
then
insertLogical(new
IntConstraintOccurrence("inGoodDepartment",ConstraintType.NEGATIVE_SOFT,10,$ps));
end
rule "inGoodRoom"
when
$gr : GoodRoomConstraint($a : admission, $r : room, $w :
weight);
$ps : PatientStay(admission == $a, bed.room == $r);
then
insertLogical(new
IntConstraintOccurrence("inGoodRoom",ConstraintType.NEGATIVE_SOFT,10*$w,$ps));
end
rule "calcScore"
salience -10
when
$count : Number() from accumulate(
IntConstraintOccurrence($w : weight) ,
sum($w) );
then
scoreCalculator.setScore(-$count.doubleValue());
end
The classes with **Constraint in it are possible combinations that cause a
constraint to be violated, and are calculated and inserted at initialization
time (and are never changed). I know that the rule
"patientsToBeAssignedToRoomsOfAppropriateSex" is fairly complex, but even
when I remove it, the performance is not fantastic. Is there anything else I
can do to get better performance? I'm already using JDK 1.6 and -server
mode. Furthermore, all classes used here have their default equals and
hashCode methods, so they don't have an impact on performance.
Sincerely,
Wim Vancroonenburg
15 years, 9 months
JSON parser?
by can_man@gmx.de
Hello,
I am new to drools, so please don't burn me ;-)
I have successfully setup drools 5 and have written a couple of rules. In addition, I have tried the web interface of guvner and looked at the code.
What I am after is a parser for JSON to DRL - I have seen that JSON is used in the web server, but there is a huge amount of code
which makes it rather difficult to find the right bit to look at. It would be great if someone could point me towards the part which
is responsible for constructing a rule in JSON and the part where it gets parsed and processed in the rule engine.
Thank you very much.
Phil
--
Jetzt 1 Monat kostenlos! GMX FreeDSL - Telefonanschluss + DSL
für nur 17,95 Euro/mtl.!* http://dsl.gmx.de/?ac=OM.AD.PD003K11308T4569a
15 years, 9 months
Dynamically selecting ruleflow group
by Richard Evans
I have a situation where I need to execute one group of rules in one
circumstance and another in a different circumstance. So frar so good. I
just have 2 rulesets in a flow -each pointing at a different ruleflow group
- and select using a split.
However I expect the number of rule-flow groups to increase over time and
need this to be under business control.
I would like to ...
- start with a business rule which determines which group to
execute next
- allow the business to add ruleflow groups without editing the RF.
Is that possible?
Thanks
Rich
PS. I attempted to post this about 3 hours ago but believe I made a mistake.
Sorry if it comes through as a duplicate
15 years, 9 months