object creation
by thomas kukofka
Hello,
I have a rule which creates an object in the consequence part and updates
this object each time teh rule is executed. So the object has to be created
only the first time.
So I always need two rules, one init rule which creates the object if it
doesn't exist yet and one if it already exists and it should only be
updated:
rule "init rule"
when
"condition"
AND
not OutputObject()
then
OutputObject oo = new OutputObject();
insert(oo);
rule "update rule"
when
"condition"
AND
oo: OutputObject()
then
#do something with the object
update(oo);
But is it really neccessary to write an extra rule only to create the
output-fact initially? Is it possible/good style to make this in one single
rule?
Appreciate your help.
Regards
Thomas
14 years, 9 months
Unable to create restriction '[QualifiedIndentifierRestr: != $nissan.pos ]' for field 'pos' , in the rule 'Racing order'
by Marcin Krol
Hello everyone,
I managed to produce my first example of Drools rules. However, I am
having problems with some field restrictions:
Racing order:Unable to create restriction '[QualifiedIndentifierRestr:
!= $nissan.pos ]' for field 'pos' in the rule 'Racing order'
The problem is only SOME field restrictions produce this error. Other
field restrictions don't!
If anybody has a hint or knows why this is not working, please let me
know, I would greatly appreciate it.
The rules are:
#created on: 2008-08-20
package com.sample
#list any import classes here.
#declare any global variables here
dialect "mvel"
import com.sample.Car;
rule "Racing order"
when
// Porsche was not red
$porsche : Car( manufacturer == "Porsche",
pos != $nissan.pos,
pos != $subaru.pos,
color != "red"
//color != "blue",
//color != $nissan.color,
//color != $subaru.color
)
// First car was Nissan
$nissan : Car( manufacturer == "Nissan",
pos == 1,
color != $subaru.color,
color != $porsche.color )
// Subaru was blue
$subaru : Car( manufacturer == "Subaru",
//pos == 2,
pos != $nissan.pos,
pos != $porsche.pos,
color == "blue" )
// 2nd car was silver
Car( pos == 2,
color == "silver",
this in ( $nissan, $subaru, $porsche ) )
then
System.out.println( "Nissan " + $nissan.getPos() + " " +
$nissan.getColor() );
System.out.println( "Subaru " + $subaru.getPos() + " " +
$subaru.getColor() );
System.out.println( "Porsche " + $porsche.getPos() + " " +
$porsche.getColor() );
end
The Car.java file:
package com.sample;
public class Car {
private String manufacturer;
private String color;
private int pos;
public Car() {
}
public Car(String manufacturer, String color, int pos) {
super();
this.color = color;
this.pos = pos;
this.manufacturer = manufacturer;
}
public String getManufacturer() {
return this.manufacturer;
}
public float getPos() {
return this.pos;
}
public String getColor() {
return this.color;
}
}
The CarExample.java file, which is the main app:
package com.sample;
import java.io.InputStreamReader;
import org.drools.RuleBase;
import org.drools.RuleBaseFactory;
import org.drools.StatefulSession;
import org.drools.compiler.PackageBuilder;
import com.sample.Car;
public class CarExample {
public static void main(String[] args) throws Exception {
final PackageBuilder builder = new PackageBuilder();
builder.addPackageFromDrl( new InputStreamReader(
CarExample.class.getResourceAsStream( "CarExample.drl" ) ) );
final RuleBase ruleBase = RuleBaseFactory.newRuleBase();
ruleBase.addPackage( builder.getPackage() );
final StatefulSession session = ruleBase.newStatefulSession();
String[] names = new String[] { "Nissan", "Subaru", "Porsche"};
String[] colors = new String[] { "red", "blue", "silver"};
int[] positions = new int[] { 1, 2, 3 };
for ( int n = 0; n < names.length; n++ ) {
for ( int c = 0; c < colors.length; c++ ) {
for ( int p = 0; p < positions.length; p++ ) {
session.insert( new Car( names[n], colors[c],
positions[p]) );
}
}
}
session.fireAllRules();
session.dispose();
}
}
14 years, 9 months
constraints and shadow facts
by Benoit VILLAUMIE
Hello,
I am using drools 4.0.7 with eclipse 3.3 plugin, and JVM 1.6.0_04-b12
I am a drools beginner and I first try to implement basics rules.
I have faced a very strange behaviour I don't understand. Below the use
case & rules :
----------------------------------------
Rules
----------------------------------------
package poc
import poc.FlashFact;
*rule "always"*
when
$f : FlashFact()
then
System.out.println("*** always ... "+$f);
end
*rule "one (eval)"*
when
$f : FlashFact()
eval($f.getHomepage().equals("one"))
then
System.out.println("*** one (eval) ... "+$f);
end
*rule "one (==)"*
when
$f : FlashFact(homepage == "one")
then
System.out.println("*** one (==) ... "+$f);
end
----------------------------------------
Use Cases
----------------------------------------
I insert 2 FlashFact instances (one with homepage == "one", another with
"dummy")
when I lauch the rules (without shadows proxy), the output is :
*** one (eval) ... poc.FlashFact[one]
*** always ... poc.FlashFact[one]
*** always ... poc.FlashFact[dummy]
/*=> I don't understand why rule one(==) is not fired ?*/
when I lauch the rules (with shadow proxy), the output is :
*** one (==) ... poc.FlashFact[one]
*** one (eval) ... poc.FlashFact[one]
*** always ... poc.FlashFact[one]
*** always ... poc.FlashFact[dummy]
The == and eval does not behave the same way with/without shadow proxy.
Moreover, I have run the State example provided with Drools in my
workspace and it worked fine (== without shadow proxy).
Can someone has a deep understanding of this difference of behaviour ?
Thanks for your reply.
14 years, 9 months
Problem using RuleFlows .
by Harsh Jetly
Hi ,
The version of Drools that I have used is 4.0.4
I have used ruleflow groups to create my rule file.
It has a split node (XOR) in which I specify a constraint on which a
ruleflow-group is to be selected .
Insert arraylists as facts in the working memory one by one.
The problem I face is that when I insert these arraylists in the same
session , it doesn't evaluate every constraint in the split node .
The work around I am using , it that I create a new session for every
arraylist , this works just fine . But creating a new session always is an
overhead .
Can you please assist me with this situation .
Thank You
Harsh Jetly
Larsen & Toubro Infotech Ltd.
www.Lntinfotech.com
This Document is classified as:
|------|
| [X] |
|------|L&T Infotech Proprietary |------|
| [ ] |
|------|L&T Infotech Confidential |------|
| [ ] |
|------|
L&T Infotech Internal Use Only |------|
| [ ] |
|------|L&T Infotech General Business
This Email may contain confidential or privileged information for the
intended recipient (s) If you are not the intended recipient, please do not
use or disseminate the information, notify the sender and delete it from
your system.
______________________________________________________________________
14 years, 9 months
Cause(LHS) of rule an query brings different results.
by Tomasz Krzywicki
I have query and rule which have identical LHS or "cause"
for some unknown for me reason my query returns only one Item
while rule is fired multiple times showing multiple occurences of my
desired facts.
query "my query"
i: Item( x == "A", q > 0 ) or
Item( x == "B", q > 1 ) or
$x: Ref( name=="Exp" ) and
Item( x == "A", quantity > 0 ) from $x.items
end
rule "my query test"
when
i: Item( x == "A", q > 0 ) or
Item( x == "B", q > 1 ) or
$x: Ref( name=="Exp" ) and
Item( x == "A", quantity > 0 ) from $x.items
then
System.out.println ("FOUND: "+i);
end
There must be something fundamentally wrong in the approach I am
taking ;)
I am using drools 4.0.7
Thanks in advance
tk
14 years, 9 months
ClassCastException using RuleAgent
by Vinicius Carvalho
Hello there! I'm getting an classcastexception when I try to insert facts on
my RuleAgent:
Properties props = new Properties();
props.put("url", "
http://localhost:8080/drools-jbrms/org.drools.brms.JBRMS/package/com.acme...
");
props.put("newInstance", "true");
props.put("name","testagent");
RuleAgent agent = RuleAgent.newRuleAgent(props);
StatefulSession session = agent.getRuleBase().newStatefulSession();
session.insert(fact);
The error happens on the session.insert method. It also happens inside my
Seam application When I try to insert the facts on the working memory. My
drools package contains the model that I've extracted from my seam
application.
If I run it using a local drl file, it works fine. This problem only happens
with the rule agent
Could someone give me a hand on this?
Regards
--
"In a world without fences and walls, who needs Gates and Windows?"
14 years, 9 months
strategy for rulebase updates in a cluster?
by Marina
Hello,
I'm integrating DRools into a J2EE application and need to understand how it behaves in a multi-threaded distributed environment. I have many questions about thread safety but for now I'll ask about clustering only and post other questions separately.
So, as I understand, Drools per se is not cluster-aware. In my current design I have a singleton class, RuleEngineMAnager, that holds a reference to a RuleBase. Hence, there will be one instance of a rulebase per JVM per machine in a cluster. Rules can be added/modified from any node in the cluster, and the modifications have to me made visible in all rulebases.
The way I handle it now is by using a DB as the shared unit and setting up a flag when modifications are made in any JVM. Next time a request comes in for the rule engine to run all rule , the flag is checked and the RuleBase is updated.
So, my question is about how to handle the rulebase updates most efficiently.
I see two possibilities:
1. ditch the rulebase completely, build a new Package of rules , create a new rulebase and add the package to it
2. Use the same rulebase - just remove the old package of rules and load in the new one.
The issues here are:
-- what if there are operations going on in the rule engine, invoked from other threads, at the time I want to do either 1 or 2?
Say, in the scenario #2, the rule engine is running all rules in one thread. At the same time I'm asking it to drop the rule package from another thread and add the new one. Is it going to finish running all rules with the old package, return results, and only then do the update of the package?
And in the scenario #1, if the rule engine is running the rules, and I try to destroy the old rule base - are those operations sequential?
Any suggestions as to how to design this functionality in the most efficient and safe way?
thank you!
Marina Popova
14 years, 9 months
Unable to create restriction '[QualifiedIndentifierRestr: != $nissan.pos ]' for field 'pos' , in the rule 'Racing order'
by Marcin Krol
Hello everyone,
The same problem regards the "golfing example" from examples project.
Adding nearly any restrictions, e.g. this one:
position != $tom.position,
in rules for Bob:
// Bob is wearing plaid pants
$bob : Golfer( name == "Bob",
position != $fred.position,
position != $joe.position,
position != $tom.position,
color == "plaid",
color != $fred.color,
color != $joe.color )
produces an error:
find solution:Unable to create restriction '[QualifiedIndentifierRestr:
!= $tom.position ]' for field 'position' in the rule 'find solution'
How to resolve this?
14 years, 9 months
Where is WorkingMemoryFactoryBean?
by Ray Pendergraph
I am trying create my RuleBase and WorkingMemory via a Spring configuration. This older post http://forum.springframework.org/archive/index.php/t-16289.html sort of looks like what I am trying to do with WorkingMemoryFactoryBean. It seems the org.drools.spring. package does not seem to be a part of recent Drools implementations. I could probably implement the Spring widgets myself but I am wondering why these Spring artifacts were removed. Is there a more preferred or better way of doing this now?
thanks,
ray
14 years, 9 months