How to insert values into drl
by anjana.ackroyd
Hi,
I am new to drools and trying to figure out how to get this simple example
working.
I have intellij IDE .
I am trying to compare two numbers using drools 6.x and dont know how to
insert numberone and numbertwo after I get my drl file into my file system
Here is my code
RuleRunner.java
package com.cambiahealth.enterprise.service.droolsrule;
import org.kie.api.KieServices;
import org.kie.api.builder.KieBuilder;
import org.kie.api.builder.KieFileSystem;
import org.kie.api.builder.KieRepository;
import org.kie.api.builder.Message.Level;
import org.kie.api.io.KieResources;
import org.kie.api.io.Resource;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
public class RuleRunner
{
private KieServices kieServices;
private KieContainer kieContainer;
private KieSession kieSession;
private KieResources kieResources;
private KieFileSystem kieFileSystem;
private KieRepository kieRepository;
public RuleRunner()
{
this.kieServices = KieServices.Factory.get();
this.kieResources = kieServices.getResources();
this.kieFileSystem = kieServices.newKieFileSystem();
this.kieRepository = kieServices.getRepository();
}
public void addRuleFile(String packagename, String rulefile)
{
Resource resource = kieResources.newClassPathResource(rulefile);
packagename = packagename.replace(".","/");
String resourcepath =
"src/main/resources/"+packagename+"/"+rulefile;
kieFileSystem.write(resourcepath, resource);
}
public KieSession buildKnowledgeSession()
{
KieBuilder kb = kieServices.newKieBuilder(kieFileSystem);
kb.buildAll();
if (kb.getResults().hasMessages(Level.ERROR))
{
throw new RuntimeException("Build Errors:\n" +
kb.getResults().toString());
}
kieContainer =
kieServices.newKieContainer(kieRepository.getDefaultReleaseId());
kieSession = this.kieContainer.newKieSession();
return kieSession;
}
public KieSession fireAllRules()
{
kieSession.fireAllRules();
return kieSession;
}
public void dispose()
{
this.kieSession.dispose();
}
}
My numbercompare.drl
declare NumberCompare
message: String
numberone: Integer
numbertwo: Integer
end
rule "numberone is equals to numbertwo"
when
n: NumberCompare(numberone == numbertwo);
then
n.setMessage("numberone is equals to numbertwo");
end
rule "numberone is greater than numbertwo"
when
n: NumberCompare(numberone > numbertwo);
then
n.setMessage("numberone is greater than numbertwo");
end
rule "numberone is less than numbertwo"
when
n: NumberCompare(numberone < numbertwo);
then
n.setMessage("numberone is less than numbertwo");
end
My DroolsService.java where I want to pass in values 1 and 2 to the drl file
runner.addRuleFile("drools", "compareNumberRule.drl");
KieSession kieSession = runner.buildKnowledgeSession();
//How do I pass 1 as numberone and 2 as numbertwo values ????? before
firerules
runner.fireAllRules();
runner.dispose();
return number.getMessage(); //To change body of implemented methods
use File | Settings | File Templates.
}
}
--
View this message in context: http://drools.46999.n3.nabble.com/How-to-insert-values-into-drl-tp4027151...
Sent from the Drools: User forum mailing list archive at Nabble.com.
12 years, 3 months
CEP accumulate unclear behavior
by ters
Hi all. Respected experts of drools, please help me to understand how pattern
+ accumulate behave.
I believe that an example is the best way to explain the problem, maybe it
will look bulky, but suppose be enough.
I use CEP and EventProcessingOption.Stream.
Event class:
/public class ServicePerformanceEvent {
private String serviceName;
private Integer duration;
public ServicePerformanceEvent(String name, Integer duration) {
serviceName = name;
this.duration = duration;
}
}/
There are 2 tests, 1st - with fireAllRules invocation, 2nd - with
firwUntilHalt invocation.
/ @Test
public void *test1_FireAllRules() *throws RuleGeneratorException {
System.out.println("-= 1 =-");
ksession.insert(new ServicePerformanceEvent("MyService", 2));
ksession.fireAllRules();
System.out.println("-= 2 =-");
ksession.insert(new ServicePerformanceEvent("AnotherService", 10));
ksession.fireAllRules();
System.out.println("-= 3 =-");
ksession.insert(new ServicePerformanceEvent("MyService", 12));
ksession.fireAllRules();
System.out.println("---test1 end---");
}
@Test
public void *test2_FireUntilHalt()* throws RuleGeneratorException,
InterruptedException {
System.out.println("-= 1 =-");
ksession.insert(new ServicePerformanceEvent("MyService", 2));
runFireUntilHaltThread();
Thread.sleep(1000);
System.out.println("-= 2 =-");
ksession.insert(new ServicePerformanceEvent("AnotherService", 10));
Thread.sleep(1000);
System.out.println("-= 3 =-");
ksession.insert(new ServicePerformanceEvent("MyService", 12));
Thread.sleep(1000);
System.out.println("---test2 end---");
}/
There are 3 simple rules for which I performing tests:
*////First Rule:*
declare com.test.event.ServicePerformanceEvent
@role( event )
end
rule "ServicePerformanceEvent test rule1"
dialect "mvel"
when
$event : ServicePerformanceEvent(serviceName == "MyService");
then
System.out.println("$event.duration = " + $event.duration);
System.out.println("----------FIRED----------");
end
Invocation results of the test1_FireAllRules() and test2_FireUntilHalt() the
same:
-= 1 =-
$event.duration = 2
----------FIRED----------
-= 2 =-
-= 3 =-
$event.duration = 12
----------FIRED----------
---test end---
Actual: As expected rule fires for each new inserted event with desired
serviceName "MyService" (- behavior is clear).
*////Second rule:*
rule "ServicePerformanceEvent test rule2"
dialect "mvel"
when
accumulate(ServicePerformanceEvent(serviceName == "MyService",
$thisDuration : duration); $avg : average($thisDuration));
then
System.out.println("$events avg duration = " + $avg);
System.out.println("----------FIRED----------");
end
Invocation results of the test1_FireAllRules() and the test2_FireUntilHalt()
the same again:
-= 1 =-
$events avg duration = 2.0
----------FIRED----------
-= 2 =-
-= 3 =-
$events avg duration = 7.0
----------FIRED----------
---test end---
Actual: Now rule accumulates average only for events from working memory
with desired serviceName (-this is clear) but fires only for inserted events
with expected serviceName "MyService" (- not clear)
*My expectation*: rule must fire for each currently inserted event and
average must be calculated for all events from working memory with
serviceName=="MyService".
*////Third rule:*
/rule "ServicePerformanceEvent test rule3"
dialect "mvel"
when
$event : ServicePerformanceEvent(serviceName == "MyService");
accumulate(ServicePerformanceEvent(serviceName ==
"MyService", $thisDuration : duration);
$avg : average($thisDuration));
then
System.out.println("$event.duration = " + $event.duration);
System.out.println("$events avg duration = " + $avg);
System.out.println("----------FIRED----------");
end/
Invocation results of the test1_FireAllRules():
-= 1 =-
$events avg duration = 2.0
----------FIRED----------
-= 2 =-
-= 3 =-
$events avg duration = 7.0
----------FIRED----------
$events avg duration = 7.0
----------FIRED----------
---test1 end---
Actual: In case -=3=- rule fires for each event into working memory during
average calculation (- not clear)
*My expectation*: firing rule only for current/last inserted event and
calculation average for all events into working memory considering
sevriceName=="MyService".
Invocation results of the test2_FireUntilHalt():
-= 1 =-
$events avg duration = 2.0
----------FIRED----------
-= 2 =-
-= 3 =-
$events avg duration = 2.0
----------FIRED----------
$events avg duration = 7.0
----------FIRED----------
$events avg duration = 7.0
----------FIRED----------
---test2 end---
OR (with another sleep time)
-= 1 =-
$events avg duration = 2.0
----------FIRED----------
-= 2 =-
-= 3 =-
$events avg duration = 7.0
----------FIRED----------
$events avg duration = 7.0
----------FIRED----------
$events avg duration = 7.0
----------FIRED----------
---test2 end---
This results I can't explain and understand.
Could anyone help me to understand this pattern + accumulate behavior.
I spent a lot of time on my actual project task but with no result .
*The actual task is*:
Rule must fire for last inserted event if its serviceName equals to
"MyService" and
its duration greater than average duration of all events with same
serviceName from the working memory.
On the project we use fireUntilHalt strategy.
I really hope and appreciate any help. Thanks in advance.
--
View this message in context: http://drools.46999.n3.nabble.com/CEP-accumulate-unclear-behavior-tp40270...
Sent from the Drools: User forum mailing list archive at Nabble.com.
12 years, 3 months
'org.jboss.dashboard-builder:dashboard-builder-bom' not found in repository:
by anjana.ackroyd
When I try to build my drools project I get
Reason: POM 'org.jboss.dashboard-builder:dashboard-builder-bom' not found in
repository: Unable to download the artifact from any repository
org.jboss.dashboard-builder:dashboard-builder-bom:pom:6.0.0.Final
My pom has
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-compiler</artifactId>
<version>6.0.0.Final</version>
</dependency>
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-core</artifactId>
<version>6.0.0.Final</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.5</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.5</version>
</dependency>
--
View this message in context: http://drools.46999.n3.nabble.com/org-jboss-dashboard-builder-dashboard-b...
Sent from the Drools: User forum mailing list archive at Nabble.com.
12 years, 3 months
Re: [rules-users] How to call Drools as a remote rules executor
by milen igrachev
Big thanks on the answers, that was all I needed.
The XML solution seems to be the best for me.
Thanks for the help again. Best Regards
Milen
-------- Оригинално писмо --------
От: Wolfgang Laun wolfgang.laun(a)gmail.com
Относно: Re: [rules-users] How to call Drools as a remote rules executor
До: Rules Users List
Изпратено на: Понеделник, 2013, Декември 9 17:11:53 EET
On 09/12/2013, milen igrachev igrachev(a)abv.bg > wrote:
> Hello again,
>
> I am sorry that i duplicated the question!.
> I have one last question and I will close my questions both in stackoverflow
> and here.
>
> Is my understanging correct? I have two options:
> 1. To have my business logic classes in my java Drools project.
Certainly.
> 2. Find a way to generate these classes in my java Drools project.
> But in any case available I do need the java classes to create and feed
> facts to the Drools? There is no way to feed Drools with any type of
> structured data that defines the fact without creating the instance? (It
> will be my responsibility to find the way of generation.)
Well, there's the DRL declare, which is one way of defining fact types
to Drools.
But you'd still have to create objects from these fact types and to inject the
data (in the constructor). Which means that you'll have to have a way
of creating these declares, and a way of getting the data into the
Java app encapsulating the Drools session, and a way of instantiating
the objects.
As I said on stackoverflow, using XML as a gofer might be easier: it's
simple on the Java side, but I don't know the other end.
-W
>
> Once again Big thanks for your time, and sorry for the duplication of the
> question!
>
> Regards,
> Milen
>
> PS: About the language that I am using its really uncommon one, I seriously
> doubt that the name will help you. Offcourse here it is - GraphTalk.
>
> From: Wolfgang Laun wolfgang.laun(a)gmail.com
>
> About: Re: [rules-users] How to call Drools as a remote rules executor
>
> To: Rules Users List
>
> sent at: Monday, 2013, December 9 13:27:22 CET
>
>
> I've provided one possible approach on stackoverflow.
>
>
>
> [Not knowing what "non-Java" is doesn't help.]
>
>
>
> -W
>
>
>
>
>
> On 09/12/2013, milen igrachev igrachev(a)abv.bg > wrote:
>
>> Hello ,
>
>> What I want to do is use java and Drools for its nice rule engine
>
>> capabilities. Currently I am not using java for my project. I have some
>
>> implementation business classes and implemented logics that works fine
>> for
>
>> me, but I want to externalize the rules in BRMS. I gave Drools a try and
>> I
>
>> like it a lot. However I encounter one obsticle that I want to ask if I
>> can
>
>> skip. I read most of the Drools documentation and across the net in all
>> the
>
>> examples that are given we actually need the implementation business
>> classes
>
>> in order to instantiate facts that we want to feed to the Drools
>
>> KieSession . Well what I want is not to duplicate my class model in both
>> my
>
>> (non java) project and my java Drools project. All I want to keep in my
>
>> Drools project is the Rules themselves. So is that possible, or I will
>> need
>
>> to duplicate the models? If anyone knows where I can read some info on
>> that
>
>> topic links are appriciated! Technical details: I am using Drools 6.0.0
>> in
>
>> Eclipse Juno. Big thanks for the support! Best Regards!
>
>> Milen
>
> _______________________________________________
>
> rules-users mailing list
>
> rules-users(a)lists.jboss.org
>
> https://lists.jboss.org/mailman/listinfo/rules-users
>
>
_______________________________________________
rules-users mailing list
rules-users(a)lists.jboss.org
https://lists.jboss.org/mailman/listinfo/rules-users
12 years, 3 months
Re: [rules-users] How to call Drools as a remote rules executor
by milen igrachev
Hello again,
I am sorry that i duplicated the question!.
I have one last question and I will close my questions both in stackoverflow and here.
Is my understanging correct? I have two options:
1. To have my business logic classes in my java Drools project.
2. Find a way to generate these classes in my java Drools project.
But in any case available I do need the java classes to create and feed facts to the Drools? There is no way to feed Drools with any type of structured data that defines the fact without creating the instance? (It will be my responsibility to find the way of generation.)
Once again Big thanks for your time, and sorry for the duplication of the question!
Regards,
Milen
PS: About the language that I am using its really uncommon one, I seriously doubt that the name will help you. Offcourse here it is - GraphTalk.
From: Wolfgang Laun wolfgang.laun(a)gmail.com
About: Re: [rules-users] How to call Drools as a remote rules executor
To: Rules Users List
sent at: Monday, 2013, December 9 13:27:22 CET
I've provided one possible approach on stackoverflow.
[Not knowing what "non-Java" is doesn't help.]
-W
On 09/12/2013, milen igrachev igrachev(a)abv.bg > wrote:
> Hello ,
> What I want to do is use java and Drools for its nice rule engine
> capabilities. Currently I am not using java for my project. I have some
> implementation business classes and implemented logics that works fine for
> me, but I want to externalize the rules in BRMS. I gave Drools a try and I
> like it a lot. However I encounter one obsticle that I want to ask if I can
> skip. I read most of the Drools documentation and across the net in all the
> examples that are given we actually need the implementation business classes
> in order to instantiate facts that we want to feed to the Drools
> KieSession . Well what I want is not to duplicate my class model in both my
> (non java) project and my java Drools project. All I want to keep in my
> Drools project is the Rules themselves. So is that possible, or I will need
> to duplicate the models? If anyone knows where I can read some info on that
> topic links are appriciated! Technical details: I am using Drools 6.0.0 in
> Eclipse Juno. Big thanks for the support! Best Regards!
> Milen
_______________________________________________
rules-users mailing list
rules-users(a)lists.jboss.org
https://lists.jboss.org/mailman/listinfo/rules-users
12 years, 3 months
Problem with time sliding windows and pseudo clock in Drools Fusion 5.5.0
by martinzp
Hi, i am new in the forum, and a beginner user of Drools so forgive me if my
question isn't quite good.
I want to test the "over window:time" operand.
In order to do that i use the pseudo clock, but i have noticed that it
doesn't behave as i would expect.
The behaviour i think i observed, is that the rule is using the real clock
instead of the pseudo clock.
*Here is my drl file:*
rule "Fire Detected"
when
c : Number(intValue >= 3) from accumulate (
$tr : TemperatureRead(read > 40)
over window:time ( 1s )
from entry-point entryone ,
count ($tr)
)
then
System.out.println("Fire Detected!");
end
*and here is the portion of my main file that inserts the events:*
TemperatureRead temp1 = new TemperatureRead(33.2);
TemperatureRead temp2 = new TemperatureRead(38.7);
TemperatureRead temp3 = new TemperatureRead(39.5);
TemperatureRead temp4 = new TemperatureRead(42.5);
TemperatureRead temp5 = new TemperatureRead(43.1);
TemperatureRead temp6 = new TemperatureRead(47.8);
SessionPseudoClock clock = ksession.getSessionClock();
entryPoint1.insert(temp1);
clock.advanceTime((new Date()).getTime(), TimeUnit.MILLISECONDS);
fireRules();
entryPoint1.insert(temp2);
clock.advanceTime(1, TimeUnit.SECONDS);
fireRules();
entryPoint1.insert(temp3);
clock.advanceTime(1, TimeUnit.SECONDS);
fireRules();
entryPoint1.insert(temp4);
clock.advanceTime(1, TimeUnit.SECONDS);
fireRules();
entryPoint1.insert(temp5);
clock.advanceTime(1, TimeUnit.SECONDS);
fireRules();
entryPoint1.insert(temp6);
fireRules();
In my opinion, the rule shouldn't be fired, because as i am (supposedly)
using the pseudo clock, the events shouldn't fall into the same sliding
window (1 second.)
In general, the rule should fire when it detects 3 or more temperature reads
with a temperature over 40.
Any idea about what i might be doing wrong?
Thanks in advance!
--
View this message in context: http://drools.46999.n3.nabble.com/Problem-with-time-sliding-windows-and-p...
Sent from the Drools: User forum mailing list archive at Nabble.com.
12 years, 3 months
How to call Drools as a remote rules executor
by milen igrachev
Hello ,
What I want to do is use java and Drools for its nice rule engine capabilities. Currently I am not using java for my project. I have some implementation business classes and implemented logics that works fine for me, but I want to externalize the rules in BRMS. I gave Drools a try and I like it a lot. However I encounter one obsticle that I want to ask if I can skip. I read most of the Drools documentation and across the net in all the examples that are given we actually need the implementation business classes in order to instantiate facts that we want to feed to the Drools
KieSession . Well what I want is not to duplicate my class model in both my (non java) project and my java Drools project. All I want to keep in my Drools project is the Rules themselves. So is that possible, or I will need to duplicate the models? If anyone knows where I can read some info on that topic links are appriciated! Technical details: I am using Drools 6.0.0 in Eclipse Juno. Big thanks for the support! Best Regards!
Milen
12 years, 3 months