Best practice for 0..1 relations
by Christopher Dolan
What's the best way to encode a fact that's needed in the RHS but is not important in the LHS?
Consider a contrived example of computing total household income for single or married persons. I can think of two ways to encode this rule, but I don't like either of them:
Style 1: one rule for each scenario
rule "household income, single"
when
$p1 : Person()
not Relation(person1 == $p1, type == "spouse")
then
insertLogical(new Income($p1.getIncome()));
end
rule "household income, married"
when
$p1 : Person()
Relation(person1 == $p1, type == "spouse", $p2: person2)
then
insertLogical(new Income($p1.getIncome() + $p2.getIncome()));
end
Style 2: a single rule with a collection
rule "household income "
when
$p1 : Person()
$rels : List() from collect(Relation($p1 == person1, type == "spouse"))
then
insertLogical(new Income($p1.getIncome() + ($rels.size() == 0 ? 0 : $rels.get(0).getPerson2().getIncome()));
end
(please ignore the bug that the income may get inserted twice because people are spouses of each other)
Style 1 is more verbose, but more straightforward: it's how I think of the problem intuitively. Style 2 is much more compact, and is more maintainable if I need to add more predicates or a more complicated RHS. But the idea of needing a List when I know there will be exactly 0 or 1 related facts just seems wrong.
I've searched for some LHS syntax that assigns a variable without participating in boolean evaluation, but I've failed to find anything.
Chris
13 years, 11 months
Problem with logging using Drools 5.3.0
by FlyingEagle
Hello,
I am debugging my drools application with Eclipse. Using Drools runtime
5.1.0 I've got automatically log entries from Drools on the console window.
Now I am using the 5.3.0 runtime and no log entries are available.
I have defined a log4j.properties and put it in the classpath. As trace
level I have defined ALL.
Own log entries are produced and shown on the console, but no entries from
Drools are shown.
Has anybody an idea, what I can do to get the log entries?
Thx
--
View this message in context: http://drools.46999.n3.nabble.com/Problem-with-logging-using-Drools-5-3-0...
Sent from the Drools: User forum mailing list archive at Nabble.com.
13 years, 11 months
2 Drools Rules in one DRL file and second Rule is not firing.
by Joe Donnelly
Hi All,
I'm new to drools and using 5.2.0
I have 2 test cases I want to run on my test xml files
1. Is the xml node there
2. Does it match XSD data type pattern
The XML file contents is loaded into a DTO(which has sub DTOs) and the DTO
is passed to the RuleService
I created one rule file called F1.drl with 2 rules in it.
rule "F1 - Exists"
when
fact : basicFact()
ojb1 : ObjectDTO() from fact.Objects
not(String() from ojb1.node1())
then
System.out.println("Missing Node Data");
end
rule "F1- Pattern Match"
when
fact : basicFact()
ObjectDTO( node1 not matches
"^[\\S]{1,100}$") from fact.Objects
then
System.out.println("Bad Node value");
end
test.java has 2 junit test cases
@test for TestFailExists.XML
@Test for TestFailPattern.XML
Regardless of which rule is first the first is only one that fires on the
file that it would apply to.
I tried commenting out 1 rule and the other works fine and vice versa. I
even tried separating the
Into separate DRL files which worked as expected.
Hopefully someone can shed some light on this for me
Thank you for your time,
Joe
13 years, 11 months
Drools variables
by skatta1986
Hi,
Could you please answer my queries:
1. Can we create int variable inside LHS (when)? If yes please give me an
example
2. I have a gobal variable which I want to use in timer(int : <variable> s)
and also in after[0s, <variable> s)
timer is accepting global variable but after is giving error.
[74,200]: [ERR 102] Line 74:200 mismatched input 'after' in rule
"EVENT_SUCCESS"
[0,0]: Parser returned a null Package
Please help out me in this. I would like to use a global variable which can
be changed at runtime.
--
View this message in context: http://drools.46999.n3.nabble.com/Drools-variables-tp3935404p3935404.html
Sent from the Drools: User forum mailing list archive at Nabble.com.
13 years, 11 months
[Drools.5.0.1] insertLogical abnormalities
by Stathis Rouvas
Hi List.
I am using Drools.5.0.1.
I am experiencing abnormalities using insertLogical(),
namely that in the following code
RaResultCode and RegistryUsed are found inserted (using the relevant queries), while
InternalResultCode is not found (using the query "get ircQuery")
and I cannot explain why this is hapenning.
The code in question is:
query "get RaCode"
rrc : RaResultCode()
end
query "get RaRegUsed"
regUsed : RegistryUsed()
end
query "get ircQuery"
irc : InternalResultCode()
end
rule "r5"
no-loop true
dialect "mvel"
when
gnId : GoodNumbers()
not ( logg : InternalResultCode( ruleId=="5", goodNo==gnId.goodNo) )
LogicalValue : HeaderMessage( gnId.goodNo==goodNo , totalItemNo == 3 )
v261 : t2206( )
then
InternalResultCode irrc = new InternalResultCode();
irrc.setRuleId("5");
irrc.setGoodNo(gnId.goodNo);
insertLogical(irrc);
RaResultCode rrc = new RaResultCode();
rrc.setResultRuleId("5");
rrc.setResultGoodNo(gnId.goodNo);
insertLogical(rrc);
RegistryUsed ruv261 = new RegistryUsed();
ruv261.setResultGoodNo(gnId.goodNo);
ruv261.setRegRowId(v261.regRowId);
ruv261.setVersionId(v261.versionId);
ruv261.setRuleId("5");
insertLogical(ruv261);
end
Using a statefull session,
the KnowledgeBase is created,
then 26 facts for "t2206" and one fact for "HeaderMessage" are inserted and finally
fireAllRules() is called.
Afterwards the defined queries are used to gather the results, but as noted in the begininning
results for "InternalResultCode", are missing.
There should be one for each of the "InternalResultCode", "RaResultCode" and "RegistryUsed" entities.
Can anyone shed some light?
Thank you for your time,
-Stathis
13 years, 11 months
please provide a rule for events timeout cases
by skatta1986
Hi,
I would like to write a rule for events timeout case.
Consider the events EventRecord(type=EventRequest, id=1) and
EventRecord(type=EventResponse, id=1).
I have written rules (in .drl file) for correlting the event request with
event response.
Now I would like to write timeout case. Suppose
EventRecord(type=EventRequest, id=2) is not followed by its response. If we
doesn't recieve response within 10 seconds, then it is considered as timeout
case.
I tried to use timers but it doen't work as it is not allowing me write
inside when (LHS) pattern.
Is there any other way to find timeout cases?
--
View this message in context: http://drools.46999.n3.nabble.com/please-provide-a-rule-for-events-timeou...
Sent from the Drools: User forum mailing list archive at Nabble.com.
13 years, 11 months