Duplicate Rule Invocations
by mark.nowicki@cubrc.org
Hi, I'm getting duplicate rule invocations when a rule is added to the
KnowledgeBase after the facts have been inserted in the
StatefulKnowledgeSession. Very simple example, seems what I'm doing is
consistent with 5.3.0.Final Drools expert documentation. Thanks for the
assistance,
--Mark
*Running code below outputs the following (firing twice for Person A,
Relationship A-C):*
RelationshipRule Fired!
person: Person( id=A, type=parent )
relationship: Relationship( parentId=A, childId=C )
RelationshipRule Fired!
person: Person( id=A, type=parent )
relationship: Relationship( parentId=A, childId=B )
RelationshipRule Fired!
person: Person( id=A, type=parent )
relationship: Relationship( parentId=A, childId=C )
*resources/factTypes.drl:*
package testing.facttypes;
declare Person
id : String
type : String
end
declare Relationship
parentId : String
childId : String
end
*resources/rules.drl:*
package testing.rules;
import java.util.ArrayList;
import testing.facttypes.Person;
import testing.facttypes.Relationship;
rule "RelationshipRule"
no-loop true
when
person : Person(type == "parent")
relationship : Relationship(parentId == person.id)
then
System.out.println("RelationshipRule Fired!");
System.out.println("person: " + person);
System.out.println("relationship: " + relationship + "\n");
end
*public static void main snippet:*
KnowledgeBase knowledgeBase =
KnowledgeBaseFactory.newKnowledgeBase();
StatefulKnowledgeSession statefulKnowledgeSession =
knowledgeBase.newStatefulKnowledgeSession();
KnowledgeBuilder knowledgeBuilder =
KnowledgeBuilderFactory.newKnowledgeBuilder(knowledgeBase);
Resource factTypes =
ResourceFactory.newClassPathResource("resources/factTypes.drl");
knowledgeBuilder.add(factTypes, ResourceType.DRL);
knowledgeBase.addKnowledgePackages(knowledgeBuilder.getKnowledgePackages());
FactType factType =
knowledgeBase.getFactType("testing.facttypes", "Person");
Object personA = factType.newInstance();
factType.set(personA, "id", "A");
factType.set(personA, "type", "parent");
statefulKnowledgeSession.insert(personA);
factType = knowledgeBase.getFactType("testing.facttypes",
"Person");
Object personB = factType.newInstance();
factType.set(personB, "id", "B");
factType.set(personB, "type", "child");
statefulKnowledgeSession.insert(personB);
factType = knowledgeBase.getFactType("testing.facttypes",
"Relationship");
Object relationshipAB = factType.newInstance();
factType.set(relationshipAB, "parentId", "A");
factType.set(relationshipAB, "childId", "B");
statefulKnowledgeSession.insert(relationshipAB);
factType = knowledgeBase.getFactType("testing.facttypes",
"Person");
Object personC = factType.newInstance();
factType.set(personC, "id", "C");
factType.set(personC, "type", "child");
statefulKnowledgeSession.insert(personC);
factType = knowledgeBase.getFactType("testing.facttypes",
"Relationship");
Object relationshipAC = factType.newInstance();
factType.set(relationshipAC, "parentId", "A");
factType.set(relationshipAC, "childId", "C");
statefulKnowledgeSession.insert(relationshipAC);
knowledgeBuilder =
KnowledgeBuilderFactory.newKnowledgeBuilder(knowledgeBase);
Resource rules =
ResourceFactory.newClassPathResource("resources/rules.drl");
knowledgeBuilder.add(rules, ResourceType.DRL);
knowledgeBase.addKnowledgePackages(knowledgeBuilder.getKnowledgePackages());
statefulKnowledgeSession.fireAllRules();
--
View this message in context: http://drools.46999.n3.nabble.com/Duplicate-Rule-Invocations-tp4018717.html
Sent from the Drools: User forum mailing list archive at Nabble.com.
12 years, 7 months
Setting and extracting different values for the same object using rules
by abhinay_agarwal
suppose i have an object Customer and i insert the object of customer in the
session...
class Customer
{
custID;
//getter and setter
}
Customer customer = new Customer()
ksession.insert(customer)
now i fire a couple of rules on this object..
and within the rules, the custID is set with three different values. Ex -
001,002,003
is there a way by which i can get all the three values ??
if I use a factHandle like
FactHandle fh = ksession.insert(customer)
syso(((Customer)ksession.getObject(fh)).getCustID)
//output is only 003..
i need all the three values..what can be done !!??
--
View this message in context: http://drools.46999.n3.nabble.com/Setting-and-extracting-different-values...
Sent from the Drools: User forum mailing list archive at Nabble.com.
12 years, 7 months
Average over window
by Machiel Groeneveld
Dear Drools users,
I'm new to Drools but loving it so far. I have a question about accumulate
and window:lengt:
What I want: "Display the average order value considering the last 200
orders for one type of product. I want have at least 10 orders for one
product before drawing any conclusions"
How I solved it now, it's not working as I want it:
I now insert product objects to be able to group the orders per product.
The first problem is that the list of products is not unique but every
insert creates a new product object (even though the product object has
already been inserted for that productId), so the second rule is triggered
way too often. I don't actually want to insert this product object, but I
don't know how else to group on product otherwise. Any help on this part is
greatly appreciated.
The second problem is that the window doesn't seem to use all the orders
matching the product, it just limits the rule to use the last 200 orders,
not the last 200 orders for that product. Is there a way to make this
smarter?
(I can't share our source code, so this might not compile)
declare Product
productId : String;
end
delcare Order
@role(event)
productId: String;
price: int;
end
rule "Turn purchase into order"
when
p : Purchase()
then
insert ( new Product(p.productId) )
insert ( new Order(p.productId, p.price)
rule "Count average price"
when
$product : Product ()
accumulate(
Order(productId == $product.productId, $pr : price ) over
window:length(200);
$avg : average( $pr ),
$count: count($pr);
$count > 10, $avg < 10)
then
"The average price for product $product.productId is $avg"
12 years, 7 months
Planner - IntConstraintOccurrence not being retracted
by Garf
I've been building a prototype similar to CloudBalancing.
I've got IntConstraintOccurrence objects being inserted via insertLogical()
through my rules.
For some reason, I don't think these are being retracted as expected -- the
count() of accumulated IntConstraintOccurrence's ramps up to 10x what I
expected to be (and the sum of weights is similarly 10x what I expect).
Also -- I have enabled Drools debugging, but I don't see the Audit Logs.
any ideas?
Jon
--
View this message in context: http://drools.46999.n3.nabble.com/Planner-IntConstraintOccurrence-not-bei...
Sent from the Drools: User forum mailing list archive at Nabble.com.
12 years, 7 months
Simple Global Error !!
by abhinay_agarwal
global java.lang.Integer count
rule "rule1"
agenda-group "1"
when
//smething
then
count=0;
System.out.println(count); // output is 0
count = count +1;
System.out.println(count); // output is 1
drools.setFocus("2");
end
rule "rule2"
agenda-group "2"
when
//smething
then
System.out.println(count); // output is 0
end
in the rule2, the value of the global variable "count" must be 1, why m i
getting it as zero ?
--
View this message in context: http://drools.46999.n3.nabble.com/Simple-Global-Error-tp4018697.html
Sent from the Drools: User forum mailing list archive at Nabble.com.
12 years, 7 months
How to Access Rule RHS (THEN Part) from JAVA?
by Ravikiran
Hi Drools Gurus,
One more newbie here for Drools Guvnor. I have chosen
"guvnor-distribution-5.3.0.Final" (I could see version as SNAPSHOT 5.4.0
within Guvnor-->Administration-->About) to start with. After reading through
the complete user manual, I was able to setup and deploy Drools Guvnor war
in JBoss AS 7.
My intension is to access all the rules that i have created using Guvnor
web. I have written a java test case to access my Rules which are resided in
a drools Package using REST API. I was successful to access them using
standard Authentication mechanism with charset*.xml. Even though i was able
to call fireAllRules with specific rule using "AgendaFilter" provided. But i
was not able to access the values at RHS after a specific rule got fired.
For Example, i want to access "percent value as 2.5" after the below sample
rule is successfully executed,
I have uploaded my model jar to Guvnor containing Person & LoanFormula
classes.
---Rule 1 (please do not see the syntax errors)
WHEN
Person age > 25
----
THEN
LoanFormula percent = 2.5
....
....
JAVA Code follows
---------------------
KnowledgeBase kbase = ...
FactType personType = kbase.getFactType( "org.drools.examples","Person" );
Object bob = personType.newInstance();
personType.set( bob, "name","Bob" );
personType.set( bob,"age",42 );
StatefulKnowledgeSession ksession = ...
ksession.insert( bob );
AgendaFilter filter = new ......("Rule1");
int count = ksession.fireAllRules(filter);
String name = personType.get( bob, "name" );
int age = personType.get( bob, "age" );
It would be great help if some one can show me the sample code for accessing
RHS part of the Rule. Please let me know if you need more details.
thanks
Kiru
--
View this message in context: http://drools.46999.n3.nabble.com/How-to-Access-Rule-RHS-THEN-Part-from-J...
Sent from the Drools: User forum mailing list archive at Nabble.com.
12 years, 7 months
Drools Guvnor - question on "mortgages" package in "guvnor-distribution-5.4.0.Final"
by Ravikiran
Hi,
I have deployed drools-guvnor.war version 5.4.0 in Jboss AS 7. When i launch
the guvnor for the first time, it also loads the defalult packages, one
package is called "mortgages" is also loaded. Mortgages package contans
totally 7 diff. rules, but when i go see the model of the mortgages i do not
see any classes displayed over there. But, i was able to build & create
deployment SNAPSHOT successfully. Even the rule DSL source contains all
classes like LoanApplication etc.,.
How is it possible? Do you mean, some kind of related drl file is uploaded
as a Model? please clarify?
--
View this message in context: http://drools.46999.n3.nabble.com/Drools-Guvnor-question-on-mortgages-pac...
Sent from the Drools: User forum mailing list archive at Nabble.com.
12 years, 7 months