Accessing items in a list
by mkhan
Hi,
I am new to doorls so please can you help me.
I am trying to access an ietm from a list in a rule like ...
$data: List(this != null, size > 9) from accumulate(
$obj1 : FwVehicle(
notchCodingWire1 != null,
lineVoltage != null
)
over window:time(10s)
from entry-point "DataStream",
collectList($obj1)
)
$first : $data.get(0)
but i get an error ...
[ERR 102] Line 164:19 mismatched input '0' expecting ')' in rule "test1" in
pattern $data.get
Does anyone have any ideas what I am doing wrong please?
--
View this message in context: http://drools.46999.n3.nabble.com/Accessing-items-in-a-list-tp4027194.html
Sent from the Drools: User forum mailing list archive at Nabble.com.
12 years
Prevent re-evaluation of events in stream mode
by sumantp
Hi,
I've written a simple rule to illustrate the issue I'm running into.
Transaction events representing amounts deposited into a bank account are
inserted sequentially. The goal is to detect a transaction that is higher
than the average from a rolling window. What I'm expecting is that only the
most recent insertion is compared with the average from the window. However,
I'm observing that all previous events in the window are re-evaluated each
time a new event is inserted. So events that previously were below the
average (at that point in time) may now be above the average since
subsequent events brought the average down. I don't want those transactions
to be re-evaluated.
Here's my rule:
declare Transaction
@role(event)
end
rule "Higher than average amount"
when
$transaction : Transaction($account : getAccount(), $amount : getAmount())
Number($avg : doubleValue, $amount > $avg) from
accumulate(Transaction(getAccount() == $account, $amt : getAmount()) over
window:length(100), average($amt))
then
System.out.println("\t***ALERT***: Higher than average transaction amount
(Average = " + $avg + "): " + $transaction.toString());
end
Here's a snippet from my Java class:
public static final void main(String[] args) {
try {
// Load the knowledge base
KieServices ks = KieServices.Factory.get();
KieContainer kContainer = ks.getKieClasspathContainer();
KieSession kSession = kContainer.newKieSession("ksession-rules");
// Insert events
List<Transaction> list = getEvents();
for (Transaction event : list) {
printEvent(event);
kSession.insert(event);
kSession.fireAllRules();
}
} catch (Throwable t) {
t.printStackTrace();
}
}
private static List<Transaction> getEvents() {
List<Transaction> list = new Vector<Transaction>();
list.add(new Transaction(1, 600));
list.add(new Transaction(1, 600));
list.add(new Transaction(1, 800)); // This should trigger an alert
list.add(new Transaction(1, 100)); // This should NOT re-evaluate previous
transactions
return list;
}
This is my kmodule.xml:
<?xml version="1.0" encoding="UTF-8"?>
<kmodule xmlns="http://jboss.org/kie/6.0.0/kmodule">
<kbase name="rules" eventProcessingMode="stream" packages="rules">
<ksession name="ksession-rules"/>
</kbase>
</kmodule>
I'm using 6.0.0.Final.
Any help is appreciated!
--
View this message in context: http://drools.46999.n3.nabble.com/Prevent-re-evaluation-of-events-in-stre...
Sent from the Drools: User forum mailing list archive at Nabble.com.
12 years
Fwd: Gradle/SBT example for Drools6
by Diego Alvarez Zuluaga
Hi
Anyone has a working gradle/sbt configuration for Drools6?
I tried a lot to convert the "bom" unsuccessfully :(
This is not supported by gradle or SBT:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-bom</artifactId>
<type>pom</type>
<version>6.0.0.Final</version>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
--
Diego Alvarez Zuluaga
http://d1egoaz.com/
12 years
Optaplanner (V6.0.0) / Vehicle Routing / Speed of a Vehicle
by Suleyman Demirel
Hello everyone,
I have just found out about optaplanner and have been testing the vehicle
routing problem on the examples provided. I am not able to see how we can
change the speed of a vehicle using the data files provided. If my
calculations are not wrong, the current assumption looks like a vehicle's
speed is 30 kmh (or miles per hour, whichever is assumed to be used).
Does anyone know how that can be modified? Thanks.
--
Suleyman Demirel
12 years
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
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