Example code to test an object that contains a collection of other objects?
by lamenramen
Hi,
I have read around for an entire day but still can not figure out how to
write the following in DRL syntax:
"When a person has a an advanced degree, print 'person is educated'"
A person has many degrees. So, I have a Person POJO and a Degree POJO.
A Person object has a field callled "degrees", which is nothing more than an
ArrayList<Degree>.
A degree has nothing but a name field, which is a String.
In regular java, it would be:
for (Degree d : person.getDegrees() {
if (d == "phd" || d == "ms" || d == "jd" || d == "md") {
System.out.println("person is highly educated");
}
}
I can not do this in the Guvnor either.
I've tried using a contains keyword but I just can't my rule to fire.
Simple rules otherwise fire just fine ,so my plumbing is all wired up
correctly.
--
View this message in context: http://drools.46999.n3.nabble.com/Example-code-to-test-an-object-that-con...
Sent from the Drools: User forum mailing list archive at Nabble.com.
12 years, 12 months
Only simple types?
by Wolfgang Laun
Here I have lots of classes with Boolean methods such as
Boolean isSomething()
and a large number of rules where
X(... something == true,...)
used to work fine not too long ago. But 5.5.0 and 5.4.0 claim that
this isn't correct:
Unable to Analyse Expression adult == true:
[Error: unable to resolve method using strict-mode: express.Person.adult()]
[Near : {... adult == true ....}]
This used to work in 5.3.0.
Trying to avoid the Boolean/bool discrepancy, I replaced true with the object:
X(... something == Boolean.TRUE,...)
but being as strict as possible doesn't work, too, neither in 5.4.0nor 5.5.0.
This used to work in 5.3.0, and so did the very simple
X(... something,...)
but not any more.
The fact that all of the following work in 5.5.0
X(... isSomething(),... )
X(... isSomething() == true,... )
X(... isSomething() == Boolean.TRUE,... )
is certainly nice, but:
Since I'm going to have to change a ton of rules: Which of these
versions should I use to have best chances for future compatibility?
Thanks
-W
12 years, 12 months
WorkingMemoryEntryPoints
by Per Sterner
Hello,
We are upgrading our project from Drools 5.3 to Drools 5.5.0 and got
some problems. We tried to locate the problem and it seems there is a
problem with the WorkingMemoryEntryPoints?
In our project everything is in WorkingMemoryEntryPoints. In this
example all facts are in "test_me". We removed the dependencies to the
memoryentrypoint and then it works.
The problem is with the WorkingMemoryEntryPoints that the Rule "TestRule
- set state_2" is triggered more then two times.
The output is:
SETTING STATE 2
STATE 2
STATE 1
STATE 1
SETTING STATE 2
STATE 2
SETTING STATE 2
STATE 2
SETTING STATE 2
STATE 2
[...]
If we remove the entrypoints the output is:
SETTING STATE 2
STATE 2
SETTING STATE 2
STATE 2
[And that's it]
Is there something with the usage from the WorkingMemoryEntryPoints or
is this a bug?
Regards,
Per Sterner
TestObject.java
---------------------------------------------------------------------------
public class TestObject {
private String stateAsString;
public void setStateAsString(final String stateAsString) {
this.stateAsString = stateAsString;
}
public String getStateAsString() {
return stateAsString;
}
}
---------------------------------------------------------------------------
MyTicker.java
---------------------------------------------------------------------------
public class MyTicker {
private final long timeStart;
private long time;
public MyTicker() {
timeStart = System.currentTimeMillis();
}
public long getTime() {
return time;
}
public void setTime(final long time) {
this.time = time;
}
public long getTimeStart() {
return timeStart;
}
}
---------------------------------------------------------------------------
DroolsTest.java
---------------------------------------------------------------------------
public class DroolsTest {
public static void main(final String[] args) {
KnowledgeBuilder kbuilder =
KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newClassPathResource("TestDrools.drl",
Init.class), ResourceType.DRL);
if (kbuilder.hasErrors()) {
System.out.println(kbuilder.getErrors().toString());
throw new RuntimeException("Unable to compile
\"HelloWorld.drl\".");
}
final Collection<KnowledgePackage> pkgs =
kbuilder.getKnowledgePackages();
final KnowledgeBase kbase =
KnowledgeBaseFactory.newKnowledgeBase();
kbase.addKnowledgePackages(pkgs);
final StatefulKnowledgeSession ksession =
kbase.newStatefulKnowledgeSession();
TestObject testObject1 = new TestObject();
testObject1.setStateAsString("STATE_1");
TestObject testObject2 = new TestObject();
testObject2.setStateAsString("STATE_1");
TestCache testCache = new TestCache();
MyTicker ticker = new MyTicker();
ticker.setTime(System.currentTimeMillis());
WorkingMemoryEntryPoint e =
ksession.getWorkingMemoryEntryPoint("test_me");
e.insert(testObject1);
e.insert(testObject2);
e.insert(testCache);
FactHandle tickerHandle = e.insert(ticker);
DroolsTickerThread droolsTickerThread = new
DroolsTickerThread("droolstickerthread", ticker, ksession, tickerHandle);
droolsTickerThread.start();
}
public static class DroolsTickerThread extends Thread {
private final DroolsTicker ticker;
private final StatefulKnowledgeSession ksession;
private final FactHandle tickerHandle;
public DroolsTickerThread(final String name, final DroolsTicker
ticker, final StatefulKnowledgeSession ksession,
final FactHandle tickerHandle) {
super(name);
this.ticker = ticker;
this.ksession = ksession;
this.tickerHandle = tickerHandle;
}
@Override
public void run() {
WorkingMemoryEntryPoint entry =
ksession.getWorkingMemoryEntryPoint("test_me");
while (!isInterrupted()) {
try {
ticker.setTime(System.currentTimeMillis());
entry.update(tickerHandle, ticker);
ksession.fireAllRules();
Thread.sleep(1000);
} catch (InterruptedException e) {
break;
}
}
};
}
}
---------------------------------------------------------------------------
TestCache.java
---------------------------------------------------------------------------
public class TestCache {
private long lastCheckTimestamp = 0;
public void setLastCheckTimestamp(final long lastCheckTimestamp) {
this.lastCheckTimestamp = lastCheckTimestamp;
}
public long getLastCheckTimestamp() {
return lastCheckTimestamp;
}
}
---------------------------------------------------------------------------
TestDrools.drl
---------------------------------------------------------------------------
package de.drools.test
import de.drools.test.*;
rule "TestRule - set state_2"
dialect "mvel"
when
$obj: TestObject (
stateAsString == "STATE_1"
) from entry-point "test_me"
$cache: TestCache ( ) from entry-point "test_me"
DroolsTicker(
$time : time,
(time - $cache.lastCheckTimestamp) > 10000
) from entry-point "test_me"
then
System.out.println("SETTING STATE 2");
modify ($obj) {stateAsString = "STATE_2"};
modify ($cache) {lastCheckTimestamp = $time };
end
rule "TestRule - sysout state_2"
when
TestObject (
stateAsString == "STATE_2"
) from entry-point "test_me"
then
System.out.println("STATE 2");
end
rule "TestRule - sysout state_1"
when
TestObject (
stateAsString == "STATE_1"
) from entry-point "test_me"
then
System.out.println("STATE 1");
end
---------------------------------------------------------------------------
12 years, 12 months
Comparing KnowledgePackages for Equality
by gqmulligan
If I wanted to compare two knowledge packages to find out if they are the
"same" how would I do so?
More specifically if I deserialize the same knowledge package twice how can
I compare the two in a way that will return true signifying they are the
same?
Using the equals method will not work since that simply does identity
comparisons. This is causing big problems for me right now because
hibernate always thinks packages are dirty and is constantly updating them
to the database when they do not need to be.
Any suggestions are much appreciated as this has become an urgent issue.
Thanks.
--
View this message in context: http://drools.46999.n3.nabble.com/Comparing-KnowledgePackages-for-Equalit...
Sent from the Drools: User forum mailing list archive at Nabble.com.
12 years, 12 months
Decision table convertation issue
by mikhal
Hi,
I'm trying to play with decision tables as with a possible rule description
way and have faced
strange behavior as for me. When I upload a XLS spreadsheet got from Drools
examples
ExamplePolicyPricing.xls it uploads and converts to the inner web format
correctly.
But there is a difference between rule sources (use View Sources from
drop-down menu)
before and after convertation:
Before:
/11. | // rule values at C11, header at C5
12. | rule "Pricing bracket_11"
13. | when
14. | Driver(age >= 18, age <= 24, locationRiskProfile == "MED")
15. | policy: Policy(type == "FIRE_THEFT")
16. | then
17. | policy.setBasePrice(200);
18. | System.out.println("Priors not relevant");
19. | end/
After:
/12. | //from row number: 2
13. | //Created from row 11
14. | rule "Row 2 Pricing bracket (converted on 21-Mar-2013 16:10:31)"
15. | dialect "mvel"
16. | when
17. | policy: Policy(type == "FIRE_THEFT")
18. | then
19. | policy.setBasePrice(200);
20. | System.out.println("Priors not relevant");
21. | end/
A result of the convertation is loosing conditions
/Driver(age >= 18, age <= 24, locationRiskProfile == "MED")./
Looks like a bug. Still maybe it is known issue? Could anyone explain
please.
Thanks.
Denis
--
View this message in context: http://drools.46999.n3.nabble.com/Decision-table-convertation-issue-tp402...
Sent from the Drools: User forum mailing list archive at Nabble.com.
12 years, 12 months
Drools Planner renames to OptaPlanner: Announcing www.optaplanner.org
by Geoffrey De Smet
We’re proud to announce the rename Drools Planner to OptaPlanner
starting with version 6.0.0.Beta1. We’re also happy to unveil its new
website: www.optaplanner.org <http://www.optaplanner.org>.
OptaPlanner optimizes business resource usage. Every organization faces
planning problems: provide products or services with a limited set of
constrainedresources (employees, assets, time and money). OptaPlanner
optimizes such planning to do more business with less resources. Typical
use cases include vehicle routing, employee rostering and equipment
scheduling.
OptaPlanner is a lightweight, embeddable planning engine written in
Java™. It helps normal Java™ programmers solve constraint satisfaction
problems efficiently. Under the hood, it combines optimization
heuristics and metaheuristics with very efficient score calculation.
OptaPlanner is open source software, released under the Apache Software
License <http://www.jboss.org/optaplanner/code/license.html>. It is 100%
pure Java™, runs on the JVM and is available in the Maven Central
Repository <http://www.jboss.org/optaplanner/download/download.html>too.
For more information, visit the new website:
http://www.optaplanner.org
Why change the name?
OptaPlanner is the new name for Drools Planner. OptaPlanner is now
standalone, but can still be optionally combined with the Drools rule
engine for a powerful declarative approach to planning optimization.
*
OptaPlanner has graduated from the Drools
<http://www.jboss.org/drools>project to become a top-level JBoss
Community <http://www.jboss.org/>project.
o
OptaPlanner is not a fork of Drools Planner. We simply renamed it.
o
OptaPlanner (the planning engine) joins its siblings Drools (the
rule engine) and jBPM (the workflow engine) in the KIE group.
*
Our commitment to Drools hasn't changed.
o
The efficient Drools rule engine is still the recommended way to
do score calculation.
o
Alternative score calculation options, such as pure Java
calculation (no Drools), also remain fully supported.
How will this affect your business?
From a business point of view, there's little or no change:
*
The mission remains unchanged:
o
We're still 100% committed to put business resource
optimizationin the hands of normal Java developers.
*
The license remains unchanged (Apache Software License 2.0). It's
still the same open source license.
*
The release lifecycle remains unchanged: OptaPlanner is still
released at the same time as Drools and jBPM.
*
Red Hat is considering support subscription offerings for
OptaPlanner as part of its BRMS
<http://www.redhat.com/products/jbossenterprisemiddleware/business-rules/>and
BPM platforms.
o
A Tech Previewof OptaPlanner is targeted for BRMS 6.0.
What has changed?
*
The website has changed tohttp://www.optaplanner.org
*
The distributions artifacts have changed name:
o
Jar names changes:
+
drools-planner-core-*.jar is now optaplanner-core-*.jar
+
drools-planner-benchmark-*.jar is now
optaplanner-benchmark-*.jar
o
Maven identification groupId's and artifactId's changes:
+
groupId org.drools.planner is now org.optaplanner
+
artifactId drools-planner-core is now optaplanner-core
+
artifactId drools-planner-benchmark is now optaplanner-benchmark
o
As usual, for more information see the Upgrade Recipe in the
download zip.
*
The API's namespace has changed. As usual, see the upgrade recipe
<https://github.com/droolsjbpm/optaplanner/blob/master/optaplanner-distrib...>on
how to deal with this efficiently.
o
Starting from 6.1.0.Final, OptaPlanner will have a 100%
backwards compatible API.
*
OptaPlanner gets its own IRC channels on Freenode
<http://freenode.net/>: #optaplanner and #optaplanner-dev
12 years, 12 months
Re: [rules-users] Structuring rules in Drools
by laune
"To figure out a hierarchical structure among rules" isn't a very clear
definition...
Rules (like many other logical entities in some programming environment) are
written in compilation units - DRL file, which imposes one "hierarchical
structure". Then, they are subordinate to "packages" (much like Java
classes), which creates another "hierarchical structure". A rule attribute
such as "agenda-group" establishes another structure, completely independent
from the aforementioned ones, which is effective at runtime, when rules of
one group are firing exclusively.
Another notion of "hierarchical structure" is introduced by the rule feature
"extends", where you can continue the condition of one rule in several other
rules.
As for interaction: there are many ways you can create "interaction", but
typically it will have to be based on facts inserted by one rule and
recognized by other rules. As people might interact without direct
communication, just by sticking chits on a notice board...
--
View this message in context: http://drools.46999.n3.nabble.com/Structuring-rules-in-Drools-tp4022859p4...
Sent from the Drools: User forum mailing list archive at Nabble.com.
12 years, 12 months