Implementing Refraction with Drools
by magaram
I believe we can inject in Refraction capabilities into Drools Rule Engine
using a simple Agenda Filter. I have tested this with test cases against
JRules and Drools and get similar results. Without the Refraction Agenda
Filter it loops infinitely as expected.
Please try it out and tell me what you think. I believe refraction is a much
needed feature of a forward chaining rule engine for commercial purposes.
Any feedback is deeply appreciated.
Here is what the code looks like
---------Agenda Filter---------------------------
package test;
import java.util.ArrayList;
import java.util.List;
import org.drools.runtime.rule.Activation;
import org.drools.runtime.rule.AgendaFilter;
/**
* This custom agenda filter injects refraction behavior into the rule engine
* @author Mukundan Agaram
*
*/
public class RefractionAgendaFilter implements AgendaFilter {
private List encounteredActivations = new ArrayList();
@Override
public boolean accept(Activation act)
{
//Check for a Refraction
if (encounteredActivations.contains(act))
{
//Remove from encountered activations for future firing
encounteredActivations.remove(act);
//return false so the rule is not selected as part of the refraction
return false;
}
//Otherwise add the rule to check for potential refractions in the future
encounteredActivations.add(act);
//select the rule to be part of the agenda
return true;
}
}
-----------------DRL------------------------
//created on: Jan 15, 2013
package test
dialect "mvel"
rule testFooBar1
salience 100
when
$foo : Foo()
$bar : Bar(y > 0)
then
$foo.setX(($foo.getX() + $bar.getY()));
System.out.println("Fired "+drools.getRule().getName())
update($foo);
end
rule testFooBar2
salience 150
when
$foo : Foo(x >= 6)
then
System.out.println("Fired "+drools.getRule().getName())
end
rule testFooBar3
salience 50
when
$foo : Foo(x >= 7)
then
System.out.println("Fired "+drools.getRule().getName())
end
----------------Runtime execution code--------------------------
package test;
import org.drools.KnowledgeBase;
import org.drools.KnowledgeBaseFactory;
import org.drools.builder.KnowledgeBuilder;
import org.drools.builder.KnowledgeBuilderFactory;
import org.drools.builder.ResourceType;
import org.drools.io.ResourceFactory;
import org.drools.runtime.StatefulKnowledgeSession;
public class TestFooBarMain {
private static void test()
{
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add( ResourceFactory.newClassPathResource( "TestFooBar.drl",
TestFooBarMain.class ),
ResourceType.DRL );
if ( kbuilder.hasErrors() ) {
System.out.println( kbuilder.getErrors().toString() );
}
else
{
System.out.println("DRL parsing - success!");
}
KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addKnowledgePackages( kbuilder.getKnowledgePackages() );
StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
Foo foo1 = new Foo();
foo1.setX(6);
Bar bar1 = new Bar();
bar1.setY(3);
ksession.insert(foo1);
ksession.insert(bar1);
ksession.fireAllRules(new RefractionAgendaFilter());
}
/**
* @param args
*/
public static void main(String[] args) {
test();
}
}
----------------Facts-------------------------
package test;
public class Foo {
private int x;
private int y;
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
}
package test;
public class Bar {
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
private int x;
private int y;
}
Thanks,
Mukundan Agaram
--
View this message in context: http://drools.46999.n3.nabble.com/Implementing-Refraction-with-Drools-tp4...
Sent from the Drools: User forum mailing list archive at Nabble.com.
13 years
Question about drools-spring in a multi-treaded container (like tomcat).
by Gurvinder Narula1
Hello all,
I've been reading up about drool-spring integration and have put together a simple project that wires together a KnowledgeBase and KnowledgeSession as follows :
<drools:resource id="GroupUnit" type="DRL"
source="file:/Users/drools/drools-spring-test/src/Rules/drls/GroupUnit.drl" />
<drools:resource id="GradeUnit" type="DRL"
source="file:/Users/drools/drools-spring-test/src/Rules/drls/GradeUnit.drl"" />
<drools:resource id="EvaluateUnit" type="DRL"
source="file:/Users/drools/drools-spring-test/src/Rules/drls/EvaluateUnit.drl"" />
<drools:grid-node id="node1" />
<drools:kbase id="kbase1" node="node1">
<drools:resources>
<drools:resource ref="GroupUnit" />
<drools:resource ref="GradeUnit" />
<drools:resource ref="EvaluateUnit" />
</drools:resources>
</drools:kbase>
<drools:ksession id="ksession" type="stateful" kbase="kbase1"
node="node1" />
Then in my Controller call, I 'AutoWire' in the StatefulKnowledgeSession as follows :
@Controller
@RequestMapping( value = "foo" )
final class FooController{
@Autowired
StatefulKnowledgeSession ksession;
@RequestMapping( method = RequestMethod.GET )
@ResponseBody
public String evaluateUnit() {
Unit unit = new Unit("030", "502", "C", "9484", "45", new String[] {},
null);
if (ksession != null) {
ksession.fireAllRules();
ksession.insert(unit);
ksession.fireAllRules();
return "<UnitCategory>" + unit.getUnitCategory() + "</UnitCategory>";
}
else
return "<message> stateful session is null</message>";
}
The main question that I have is that – is this solution thread-safe ? From what I understand of spring is that beans configured without any additional qualifiers are inherently singletons. And when deployed in a multi-theaded container like tomcat, Spring assumes that the beans being severed up a thread-safe. So from I have read is that KnowledeSessions are inherently not thread safe. So putting the 2 together, I leaning towards the assessment that this above solution is NOT thread safe and that if I do want it to be thread safe I should set the StatefulKnowledgeSession to 'prototype' and not leave it as a singleton.
Please let me know if I'm missing anything in my assessment here !
Thanks in advance,
Gurvinder
This message is intended only for the individual or entity to which it is addressed. It may contain privileged, confidential information which is exempt from disclosure under applicable laws. If you are not the intended recipient, please note that you are strictly prohibited from disseminating or distributing this information (other than to the intended recipient) or copying this information. If you have received this communication in error, please notify us immediately by e-mail or by telephone at the above number. Thank you.
13 years
constraints lacking and in excess
by Michiel Vermandel
Hi,
I have written a custom move implementation.
At first I got the exception:
java.lang.IllegalStateException: The moveClass (class my.planner.solver.MoveGroupToPeriod)'s move (PROJECT Period GroupChange:{T= Inspect 55871.I#0 [4->4] Bart DE BIE IV XI} => (P5) [i1]) probably has a corrupted undoMove (my.planner.solver.util.TaskChangeListMove@35e8df37). Or maybe there are corrupted score rules.
...
I found out that I did not undo all changes that I made in the move.
After fixing this, I now often get Score corruption as below.
I checked and all changes are preceded with director.beforeVariableChanged(...) and followed by director.afterVariableChanged(...);
What could cause this corruption?
java.lang.IllegalStateException: Score corruption: the workingScore (-286hard/-30601soft) is not the uncorruptedScore (-286hard/-30401soft):
The workingMemory has 3 ConstraintOccurrence(s) in excess:
preferedRegion/NEGATIVE_SOFT:[{T= Inspect 115519.I#0 [8->8] Dirk JANSSENS IV V}, Johan LAMOTE, 99, V]=99
taskSpreading/NEGATIVE_HARD:[(P7) , 77]=100
preferedRegion/NEGATIVE_SOFT:[{T= Inspect 115519.I#1 [8->8] Erik VERHOEVEN TL V}, Dirk JANSSENS, 99, V]=99
The workingMemory has 6 ConstraintOccurrence(s) lacking:
preferedRegion/NEGATIVE_SOFT:[{T= Inspect 115519.I#0 [8->8] Dirk JANSSENS IV V}, Dirk JANSSENS, 99, V]=99
taskSpreading/NEGATIVE_HARD:[(P7) , 75]=36
preferedRegion/NEGATIVE_SOFT:[{T= Inspect 115519.I#1 [8->8] Erik VERHOEVEN TL V}, Erik VERHOEVEN, 99, V]=99
preferedRegion/NEGATIVE_SOFT:[{T= Inspect 4747.I#0 [10->10] Johan LAMOTE IV V}, Johan LAMOTE, 99, V]=99
taskSpreading/NEGATIVE_HARD:[(P8) , 74]=16
preferedRegion/NEGATIVE_SOFT:[{T= Inspect 4747.I#1 [10->10] Dirk JANSSENS TL V}, Dirk JANSSENS, 99, V]=99
Check the score rules who created those ConstraintOccurrences. Verify that each ConstraintOccurrence's causes and weight is correct.
at org.drools.planner.core.score.director.AbstractScoreDirector.assertWorkingScore(AbstractScoreDirector.java:249)
at org.drools.planner.core.solver.scope.DefaultSolverScope.assertWorkingScore(DefaultSolverScope.java:96)
at org.drools.planner.core.phase.AbstractSolverPhaseScope.assertWorkingScore(AbstractSolverPhaseScope.java:124)
at org.drools.planner.core.localsearch.DefaultLocalSearchSolverPhase.solve(DefaultLocalSearchSolverPhase.java:86)
at org.drools.planner.core.solver.DefaultSolver.runSolverPhases(DefaultSolver.java:190)
at org.drools.planner.core.solver.DefaultSolver.solve(DefaultSolver.java:155)
at my.planner.app.InspectionSchedule.solve(InspectionSchedule.java:275)
at my.planner.testcore.AbstractPlanningTestClass.solve(AbstractPlanningTestClass.java:444)
at my.planner.testcore.AbstractPlanningTestClass.solve(AbstractPlanningTestClass.java:469)
at my.planner.RealBaoTest.datasetBAO20122013(RealBaoTest.java:226)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Thanks,
Michiel
-----------------
http://www.codessentials.com - Your essential software, for free!
Follow us at http://twitter.com/#!/Codessentials
13 years
add guvnor-webapp-core as a dependency to project
by kooper
Hi,I'm trying to add guvnor-webapp-core as a dependency to my project, but
after when deploying app to JBOSS 7(tried 7.1.1 and 7.0.2), I have following
exception:
11:19:50,138 ERROR [org.jboss.msc.service.fail] (MSC service thread 1-8)
MSC00001: Failed to start service
jboss.deployment.unit."my_app-1.0-SNAPSHOT.war".WeldService:
org.jboss.msc.service.StartException in service
jboss.deployment.unit."my_app-1.0-SNAPSHOT.war".WeldService:
org.jboss.weld.exceptions.DeploymentException: Exception List with 3
exceptions:Exception 0 :org.jboss.weld.exceptions.DeploymentException:
WELD-001408 Unsatisfied dependencies for type [RepositoryStartupService]
with qualifiers [@Default] at injection point [[field] @Inject private
org.drools.guvnor.server.repository.RulesRepositoryManager.repositoryStartupService]
at
org.jboss.weld.bootstrap.Validator.validateInjectionPoint(Validator.java:275)
at
org.jboss.weld.bootstrap.Validator.validateInjectionPoint(Validator.java:244)
at org.jboss.weld.bootstrap.Validator.validateBean(Validator.java:107) at
org.jboss.weld.bootstrap.Validator.validateRIBean(Validator.java:127) at
org.jboss.weld.bootstrap.Validator.validateBeans(Validator.java:346) at
org.jboss.weld.bootstrap.Validator.validateDeployment(Validator.java:331) at
org.jboss.weld.bootstrap.WeldBootstrap.validateBeans(WeldBootstrap.java:366)
at org.jboss.as.weld.WeldContainer.start(WeldContainer.java:83) at
org.jboss.as.weld.services.WeldService.start(WeldService.java:76) at
org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1811)
at
org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1746)
at
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:636)Exception 0
:org.jboss.weld.exceptions.DeploymentException: WELD-001408 Unsatisfied
dependencies for type [RepositoryStartupService] with qualifiers [@Default]
at injection point [[field] @Inject private
org.drools.guvnor.server.repository.MailboxService.repositoryStartupService]
at
org.jboss.weld.bootstrap.Validator.validateInjectionPoint(Validator.java:275)
at
org.jboss.weld.bootstrap.Validator.validateInjectionPoint(Validator.java:244)
at org.jboss.weld.bootstrap.Validator.validateBean(Validator.java:107) at
org.jboss.weld.bootstrap.Validator.validateRIBean(Validator.java:127) at
org.jboss.weld.bootstrap.Validator.validateBeans(Validator.java:346) at
org.jboss.weld.bootstrap.Validator.validateDeployment(Validator.java:331) at
org.jboss.weld.bootstrap.WeldBootstrap.validateBeans(WeldBootstrap.java:366)
at org.jboss.as.weld.WeldContainer.start(WeldContainer.java:83) at
org.jboss.as.weld.services.WeldService.start(WeldService.java:76) at
org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1811)
at
org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1746)
at
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:636)Exception 0
:org.jboss.weld.exceptions.DeploymentException: WELD-001408 Unsatisfied
dependencies for type [ConversionService] with qualifiers [@Default] at
injection point [[field] @Inject private
org.drools.guvnor.server.RepositoryAssetService.conversionService] at
org.jboss.weld.bootstrap.Validator.validateInjectionPoint(Validator.java:275)
at
org.jboss.weld.bootstrap.Validator.validateInjectionPoint(Validator.java:244)
at org.jboss.weld.bootstrap.Validator.validateBean(Validator.java:107) at
org.jboss.weld.bootstrap.Validator.validateRIBean(Validator.java:127) at
org.jboss.weld.bootstrap.Validator.validateBeans(Validator.java:346) at
org.jboss.weld.bootstrap.Validator.validateDeployment(Validator.java:331) at
org.jboss.weld.bootstrap.WeldBootstrap.validateBeans(WeldBootstrap.java:366)
at org.jboss.as.weld.WeldContainer.start(WeldContainer.java:83) at
org.jboss.as.weld.services.WeldService.start(WeldService.java:76) at
org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1811)
at
org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1746)
at
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:636) at
org.jboss.as.weld.services.WeldService.start(WeldService.java:83) at
org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1811)
[jboss-msc-1.0.2.GA.jar:1.0.2.GA] at
org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1746)
[jboss-msc-1.0.2.GA.jar:1.0.2.GA] at
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
[rt.jar:1.6.0_18] at
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
[rt.jar:1.6.0_18] at java.lang.Thread.run(Thread.java:636)
[rt.jar:1.6.0_18]Caused by: org.jboss.weld.exceptions.DeploymentException:
Exception List with 3 exceptions:....
Do I need to add anything to project configuration to be able to add guvnor
as dependency?Thanks.
--
View this message in context: http://drools.46999.n3.nabble.com/add-guvnor-webapp-core-as-a-dependency-...
Sent from the Drools: User forum mailing list archive at Nabble.com.
13 years
Fill dependent dropdown lists based on a web decision table
by peach_wyss
Hi
I'm using Drools and Guvnor in our project. We have the following Data Model
(simplified):
- Object C
-> has a List
-> has a List
I need to build an easy to edit web decision table in guvnor. We need the
table to fill a dropdown list (Object B) in dependency of another dropdown
list (Object A), when for example creating a new Object C on a form. The
table should look something like this:
[Object A] [Object B]
Test A1 Test B1
Test B2
Test B3
Test A2 Test B4
Test A3 Test B5
Test B6
Test A4 Test B7
Test B8
Test B9
Test B10
...so when a User creates a new Object C (Create-Form) and select on for
example "Test A3" in a dropdown list (Object A), Drools should be firing and
result "Test B5" and "Test B6", which then can be filled in the second
dropdown list (Object B).
How can I achieve such a scenario? We have already Drools Guvnor in use and
the requirement is, to have such a table to easy edit the dependencies of
this two dropdown lists.
Some thoughts:
- Is there a way to fill Objects in a List as a Action-Column in a web
decision table?
- Is there a way to add Objects to a List (global variable) in a web
decision table?
- Is there a way to achieve this with rule templates?
- Are there other ways to achieve this?
Thanks for your help.
Kind regards,
Peter
--
View this message in context: http://drools.46999.n3.nabble.com/Fill-dependent-dropdown-lists-based-on-...
Sent from the Drools: User forum mailing list archive at Nabble.com.
13 years
Tree iteration with accumulate: is it possible ?
by mcyrb
Hello.
I have a tree, with each leaf containing a random value.
The value of each node is the sum of its children value.
I would like to write a rule which looks like this:
when
nodeValue == 18 (e.g.)
then
// do something with this node
end
The accumulate conditional element looked adequate for this, so I wrote
something like this
when
n: Node()
accumulate( Leaf( parentNode == n, v : value ); nodeValue : sum( v );
nodeValue == 18 )
then
// do something with n
end
So... the problem is that this works only for nodes where its direct childs
are leafs, and not for upper nodes.
Have you got an idea to solve this ?
Thank you
--
View this message in context: http://drools.46999.n3.nabble.com/Tree-iteration-with-accumulate-is-it-po...
Sent from the Drools: User forum mailing list archive at Nabble.com.
13 years
Drools / Guvnor 6
by Stephen Masters
Hi folks,
Just wondering whether there's a download available yet for Guvnor v6, or whether I need to build from source.
As mentioned previously I'm tinkering with it on WebSphere, and so far (using WAS 8.5 Liberty Profile) I am experiencing zero issues. It just works! I'm using the Tomcat distribution of Guvnor 5.3. So I'm downloading 5.5 as we speak, and I might as well try out 6 while I'm at it. :)
Steve
13 years
How to integrate Declarative Fact created using Guvnor in Java
by IPatel
Hi,
Basic questions on the facts that are created in Guvnor using New
Declaritive Model
I have the facts that are created in Guvnor which i dont have java classes
defined. I am refering to the tutorial provided on Chapter 5 The Fact Model
and in that it shows how to use the declarative facts from Java.
I am having hard time following the steps. So i am going to layout below to
see if my understanding is correct:
Step 1: // get a reference to a knowledge base with a declared type:
KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
Step 2:// get the declared FactType
FactType personType = kbase.getFactType( "PackageName", "FactName" );
Step 3:// handle the type as necessary:
Not sure what this means
Step 4:// create instances:
Object bob = personType.newInstance();
Step5: // set attributes values
personType.set( bob,
"name",
"Bob" );
Step 6: // insert fact into a session
StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
Step 7:ksession.insert( bob );
Step 8:ksession.fireAllRules();
I am able to understand Step 4 thru Step 8. I guess i am having trouble
understanding step 1 and Step 3.
Can someone please help me clarify or have example for me to refer.
--
View this message in context: http://drools.46999.n3.nabble.com/How-to-integrate-Declarative-Fact-creat...
Sent from the Drools: User forum mailing list archive at Nabble.com.
13 years