Re: [rules-users] Question about Drools Spring, StatelessKnowledgeSession and globals
by Patrick van Kann
Hello fellow Droolers,
A while ago I asked why the <drools:batch> element seemed to work
differently in the case of a StatelessKnowledgeSession and a
StatefulKnowedgeSession (see below).
I just wanted to update those interested with the answer and ask some
further questions, probably aimed at any developers who happen to be
reading.
I checked out the 5.1.1.34858 tag and had a look at how the Spring
integration works. I found that the reason that the global is not set in the
case of a StatelessKnowledgeSession is because the <spring:batch> element is
effectively ignored in the StatelessKnowledgeSessionBeanFactory, which lacks
the following code present in the StatefulKnowledgeSessionBeanFactory (in
the internalAfterPropertiesSet() method):
if ( getBatch() != null && !getBatch().isEmpty()) {
for ( Command<?> cmd : getBatch() ) {
ksession.execute( cmd );
}
}
So, either by accident or design the <spring:batch> element has no effect
when using a StatelessKnowledgeSession (but is perfectly allowable in the
XSD/Spring namespace).
So my questions are:
1) Is this the intended behaviour or a bug? On reflection, I am now not sure
the idea of a "batch/script" makes sense for a StatelessKnowledgeSession
since execute() is a one-shot method and any globals set this way would not
be available to later executions, which is what I was looking for.
2) If so, should the XSD be changed to disallow the batch element within a
stateless session (difficult, given that this is determined via the "type"
attribute) or should the documentation simply warn people that the
<spring:batch> element doesn't do anything if you set the type attribute to
stateless (somewhat confusing, I suppose)
3) If not, should the code above be added to the
StatelessKnowledgeSessionBeanFactory.internalAfterPropertiesSet() method?
4) Should there be another way to declare a global that isn't through the
batch element (one that would cause globals to be set via the
setGlobal(String, Object) method rather than using the SetGlobalCommand via
the execute() method.
I am happy to try and contribute something along these lines if I can get
some guidance as to what was actually intended.
Hope this helps and thanks again for Drools.
Patrick
=====================================
Hello,
I've been trying out the Spring integration package in Drools 5.1.1 and it
works really well, but I have run into one issue I can't figure out.
I've defined 2 knowledge sessions from the same knowledge base in the app
context - one stateless, one stateful but otherwise identical. They both
refer to a collaborator defined as a bean in the app context which is to be
used as a global in my rules. This is just an excerpt of my full Spring
context, the kbase definition itself is not an issue.
<bean id="applicantDao" class="com.acme.app.dao.impl.ApplicantDaoImpl" />
<drools:ksession id="statelessKSession" type="stateless"
name="statelessKSession" kbase="kbase">
<drools:batch>
<drools:set-global identifier="applicantDao" ref="applicantDao" />
</drools:batch>
</drools:ksession>
<drools:ksession id="statefulKSession" type="stateful"
name="statefulKSession" kbase="kbase">
<drools:batch>
<drools:set-global identifier="applicantDao" ref="applicantDao" />
</drools:batch>
</drools:ksession>
The issue is that this configuration works for the stateful but not the
stateless session, in the sense that the stateful session appears to have a
valid reference to the applicantDao object in the Globals object but the
stateless session doesn't.
<at> Test
public void testStatelessGlobal() {
Globals globals = statelessKSession.getGlobals();
Object global = globals.get("applicantDao");
Assert.assertNotNull(global);
}
<at> Test
public void testStatefulGlobal() {
Globals globals = statefulKSession.getGlobals();
Object global = globals.get("applicantDao");
Assert.assertNotNull(global);
}
The first test fails (the global variable is null) but the second passes. No
errors are thrown by Drools during the setup of the Spring container.
What am I doing wrong? Should I be able to define globals in this way for
stateless sessions? The XSD seems to indicate this is a valid configuration,
but it just doesn't work.
Is anyone else working with the Spring integration that can point out my
error here?
Many thanks,
Patrick
11 years, 10 months
Drools as a Webservice
by abhinavgupta
Hi,
I am new to the Drools , i wanted to expose rules as a webservice using
Axis2.
what i have done is that
1. I have created a drools Project (working fine on eclispe) export all
files in jar
2. Placed the jar file in C:\Program Files\Apache Software Foundation\Tomcat
7.0\webapps\axis2\WEB-INF\lib directory
3. Created a new Dynamic java project with axis2 facet.created a class with
**
package in.abhi.ws;
import java.math.BigDecimal;
import org.drools.KnowledgeBase;
import org.drools.KnowledgeBaseFactory;
import org.drools.builder.KnowledgeBuilder;
import org.drools.builder.KnowledgeBuilderError;
import org.drools.builder.KnowledgeBuilderErrors;
import org.drools.builder.KnowledgeBuilderFactory;
import org.drools.builder.ResourceType;
import org.drools.io.ResourceFactory;
import org.drools.runtime.StatefulKnowledgeSession;
import com.sample.SaleItem;
public class Test {
private static KnowledgeBase readKnowledgeBase() throws Exception {
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory
.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newClassPathResource("KanasSalesTax.drl"),
ResourceType.DRL);
KnowledgeBuilderErrors errors = kbuilder.getErrors();
if (errors.size() > 0) {
for (KnowledgeBuilderError error : errors) {
System.err.println(error);
}
throw new IllegalArgumentException("Could not parse knowledge.");
}
KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
return kbase;
}
public BigDecimal salesTax(String taxState, String typeItem) {
BigDecimal taxCal = null;
try {
// load up the knowledge base
KnowledgeBase kbase = readKnowledgeBase();
StatefulKnowledgeSession ksession = kbase
.newStatefulKnowledgeSession();
// Non-prescription item
// Expect $1.89 for the tax
SaleItem item1 = new SaleItem();
item1.setPurchaseState(taxState);
item1.setItemType(typeItem);
item1.setSalesPrice(new BigDecimal(29.95));
ksession.insert(item1);
ksession.fireAllRules();
taxCal = item1.getSalesTax();
System.out.println(item1.getPurchaseState().toString() + " "
+ item1.getItemType().toString() + ": "
+ item1.getSalesTax().toString());
} catch (Throwable t) {
t.printStackTrace();
}
return taxCal;
}
}
**
4. Then created a bottom up java bean webservice of it using the same above
class.
5. Wsdl is generated
i am getting the error response on eclispe console while testing by soap-ui
**********
java.lang.NullPointerException
at
org.drools.util.CompositeClassLoader.getResourceAsStream(CompositeClassLoader.java:122)
at
org.drools.commons.jci.compilers.EclipseJavaCompiler$2.findType(EclipseJavaCompiler.java:234)
at
org.drools.commons.jci.compilers.EclipseJavaCompiler$2.findType(EclipseJavaCompiler.java:215)
at
org.eclipse.jdt.internal.compiler.lookup.LookupEnvironment.createPackage(LookupEnvironment.java:748)
at
org.eclipse.jdt.internal.compiler.lookup.CompilationUnitScope.buildTypeBindings(CompilationUnitScope.java:87)
at
org.eclipse.jdt.internal.compiler.lookup.LookupEnvironment.buildTypeBindings(LookupEnvironment.java:167)
at
org.eclipse.jdt.internal.compiler.Compiler.internalBeginToCompile(Compiler.java:721)
at
org.eclipse.jdt.internal.compiler.Compiler.beginToCompile(Compiler.java:381)
at org.eclipse.jdt.internal.compiler.Compiler.compile(Compiler.java:426)
at
org.drools.commons.jci.compilers.EclipseJavaCompiler.compile(EclipseJavaCompiler.java:358)
at
org.drools.commons.jci.compilers.AbstractJavaCompiler.compile(AbstractJavaCompiler.java:49)
at
org.drools.rule.builder.dialect.java.JavaDialect.compileAll(JavaDialect.java:369)
at
org.drools.compiler.DialectCompiletimeRegistry.compileAll(DialectCompiletimeRegistry.java:53)
at org.drools.compiler.PackageRegistry.compileAll(PackageRegistry.java:71)
at org.drools.compiler.PackageBuilder.compileAll(PackageBuilder.java:869)
at org.drools.compiler.PackageBuilder.addPackage(PackageBuilder.java:826)
at
org.drools.compiler.PackageBuilder.addPackageFromDrl(PackageBuilder.java:404)
at
org.drools.compiler.PackageBuilder.addKnowledgeResource(PackageBuilder.java:586)
at
org.drools.builder.impl.KnowledgeBuilderImpl.add(KnowledgeBuilderImpl.java:37)
at in.abhi.ws.Test.readKnowledgeBase(Test.java:31)
at in.abhi.ws.Test.salesTax(Test.java:55)
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.apache.axis2.rpc.receivers.RPCUtil.invokeServiceClass(RPCUtil.java:212)
at
org.apache.axis2.rpc.receivers.RPCMessageReceiver.invokeBusinessLogic(RPCMessageReceiver.java:117)
at
org.apache.axis2.receivers.AbstractInOutMessageReceiver.invokeBusinessLogic(AbstractInOutMessageReceiver.java:40)
at
org.apache.axis2.receivers.AbstractMessageReceiver.receive(AbstractMessageReceiver.java:110)
at org.apache.axis2.engine.AxisEngine.receive(AxisEngine.java:181)
at
org.apache.axis2.transport.http.HTTPTransportUtils.processHTTPPostRequest(HTTPTransportUtils.java:172)
at org.apache.axis2.transport.http.AxisServlet.doPost(AxisServlet.java:146)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)
at
org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
at
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
at
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
at
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
at
org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:987)
at
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:579)
at
org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:307)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
java.lang.NullPointerException
at
org.drools.util.CompositeClassLoader.getResourceAsStream(CompositeClassLoader.java:122)
at
org.drools.commons.jci.compilers.EclipseJavaCompiler$2.findType(EclipseJavaCompiler.java:234)
at
org.drools.commons.jci.compilers.EclipseJavaCompiler$2.findType(EclipseJavaCompiler.java:215)
at
org.eclipse.jdt.internal.compiler.lookup.LookupEnvironment.createPackage(LookupEnvironment.java:748)
at
org.eclipse.jdt.internal.compiler.lookup.CompilationUnitScope.buildTypeBindings(CompilationUnitScope.java:87)
at
org.eclipse.jdt.internal.compiler.lookup.LookupEnvironment.buildTypeBindings(LookupEnvironment.java:167)
at
org.eclipse.jdt.internal.compiler.Compiler.internalBeginToCompile(Compiler.java:721)
at
org.eclipse.jdt.internal.compiler.Compiler.beginToCompile(Compiler.java:381)
at org.eclipse.jdt.internal.compiler.Compiler.compile(Compiler.java:426)
at
org.drools.commons.jci.compilers.EclipseJavaCompiler.compile(EclipseJavaCompiler.java:358)
at
org.drools.commons.jci.compilers.AbstractJavaCompiler.compile(AbstractJavaCompiler.java:49)
at
org.drools.rule.builder.dialect.java.JavaDialect.compileAll(JavaDialect.java:369)
at
org.drools.compiler.DialectCompiletimeRegistry.compileAll(DialectCompiletimeRegistry.java:53)
at org.drools.compiler.PackageRegistry.compileAll(PackageRegistry.java:71)
at org.drools.compiler.PackageBuilder.compileAll(PackageBuilder.java:869)
at org.drools.compiler.PackageBuilder.addPackage(PackageBuilder.java:826)
at
org.drools.compiler.PackageBuilder.addPackageFromDrl(PackageBuilder.java:404)
at
org.drools.compiler.PackageBuilder.addKnowledgeResource(PackageBuilder.java:586)
at
org.drools.builder.impl.KnowledgeBuilderImpl.add(KnowledgeBuilderImpl.java:37)
at in.abhi.ws.Test.readKnowledgeBase(Test.java:31)
at in.abhi.ws.Test.salesTax(Test.java:55)
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.apache.axis2.rpc.receivers.RPCUtil.invokeServiceClass(RPCUtil.java:212)
at
org.apache.axis2.rpc.receivers.RPCMessageReceiver.invokeBusinessLogic(RPCMessageReceiver.java:117)
at
org.apache.axis2.receivers.AbstractInOutMessageReceiver.invokeBusinessLogic(AbstractInOutMessageReceiver.java:40)
at
org.apache.axis2.receivers.AbstractMessageReceiver.receive(AbstractMessageReceiver.java:110)
at org.apache.axis2.engine.AxisEngine.receive(AxisEngine.java:181)
at
org.apache.axis2.transport.http.HTTPTransportUtils.processHTTPPostRequest(HTTPTransportUtils.java:172)
at org.apache.axis2.transport.http.AxisServlet.doPost(AxisServlet.java:146)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)
at
org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
at
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
at
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
at
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
at
org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:987)
at
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:579)
at
org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:307)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
****************
Please let me know how to make it work
--
View this message in context: http://drools.46999.n3.nabble.com/Drools-as-a-Webservice-tp3950198.html
Sent from the Drools: User forum mailing list archive at Nabble.com.
11 years, 10 months
Drools: Using Declared Types in Decision Tables
by David Smith
Hi,
I am trying to use a declared type in a decision table
but get an "Unable to resolve ObjectType" message.
The declared type is in a file types.drl,
I have a spreadsheet test.xls that has a Ruleset with
an import line for the declared type.
Is it possible to reference a declared type from a decision table?
Is it possible to declare a type in a decision table?
Thanks
David
11 years, 10 months
Rules with variable timers
by Shannon Hastings
Is there a way to write a rule in DRL that uses a timer or something that is dynamic. I.E. I want the time that rule waits to fire to be dependent on a variable such as below:
rule "CheckDoseMissed"
no-loop
timer(int: $eventA.sleepTime)
when
$eventA : MyEvent()
………..
----------
Shannon Hastings
Inventrio
shannon.hastings(a)inventrio.com
(614) 389-2795 ext: 101
http://www.inventrio.com
11 years, 11 months
Building our own UI for Drools
by kapokfly
Due to some reasons (for example, build a consistent UI within our own
application, easier to work with our own meta data, don't want SSO etc), we
are considering to build our own UI to generate Drools rule file basing on
the user input via the UI , has anyone tried this before? Is there any
library shipped within Drools Gunvor can be used to be easier to work with
Drools rule syntax?
Thanks,
Ivan
-----
Ivan, your Panda, forever
--
View this message in context: http://drools.46999.n3.nabble.com/Building-our-own-UI-for-Drools-tp350884...
Sent from the Drools: User forum mailing list archive at Nabble.com.
12 years
Drools Guvnor API information?
by Vikas Hazrati
Hi,
I am looking at using Guvnor for our project where users would be creating
rules using our UI. For this i need to add / modify rule or any asset for
that matter using an api.
Unfortunately, i could not find enough documentation to suggest the best way
to use the REST api, which I guess is provided by Guvnor. Could someone let
me know the location of where i can get some information for this api. I
also see that the issue GUVNOR-1080
(https://issues.jboss.org/browse/GUVNOR-1080) is marked resolved so the api
should exist right? or that we cannot access it remotely until we have the
Atom Pub Interface done?
Help appreciated .
--
View this message in context: http://drools-java-rules-engine.46999.n3.nabble.com/Drools-Guvnor-API-inf...
Sent from the Drools - User mailing list archive at Nabble.com.
12 years
Debug rule functions ?
by ipeshev
Hello,
could some give me some tips for debugging *functions* in .drl ?
by now I'm not able to debug *inside the functions* which are called by the
rule sequence.
I follow the
http://docs.jboss.org/tools/3.0.0.GA/en/drools_tools_ref_guide/html_singl...
JBoss Drools Tools Reference Guide but what is show there is how to debug a
very simple *rule sequence*. I have no problem with that - following the
guide everything is ok.
BUT
There is nothing shown about a more complex case when there are functions
(java-like) in the .drl file and the rule calls one or more of these
functions. And one function calls another etc. (just like in normal
programming)
When a put a breakpoint inside functions I cannot debug them.
I can only debug the *rule* ("then" section)
Any help would be appreciated?
Or any official info if this supported at all.
versions etc.
Best Regards
Ivan
--
View this message in context: http://drools.46999.n3.nabble.com/Debug-rule-functions-tp3689774p3689774....
Sent from the Drools: User forum mailing list archive at Nabble.com.
12 years
java.lang.OutOfMemoryError: Java heap space
by vdelbart
Hello,
In my tests of the 4.0.3, I have some : java.lang.OutOfMemoryError.
So, I try a simple test with 10.000 sequential execution of 1 rule with no
context and just a workingmemorylogger and I have the error :
java.lang.OutOfMemoryError: Java heap space
My code is :
public class ExecReglesMemotyTest
{
private static final Reader DRL = new
InputStreamReader(ExecReglesMemotyTest.class
.getResourceAsStream("/rules/exemple.drl"));
private static final String FICHIER_LOG = "session";
/**
* @param args
*/
public static void main(String[] args)
{
try
{
PackageBuilder builder = new PackageBuilder();
builder.addPackageFromDrl(DRL);
RuleBase ruleBase = RuleBaseFactory.newRuleBase();
ruleBase.addPackage(builder.getPackage());
for (int i = 0; i < 10000; i++)
{
StatefulSession session = ruleBase.newStatefulSession();
WorkingMemoryFileLogger logger = new WorkingMemoryFileLogger(session);
logger.setFileName(FICHIER_LOG);
session.fireAllRules();
logger.writeToDisk();
session.dispose();
}
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
with this rule (doesn't matter):
#created on: Fri Nov 09 15:48:45 CET 2007
package initPackage
#list any import classes here.
rule "ERG9"
when
then
end
I try this in 4.0.1, and it's working (no OutOfMemoryError)
What's the problem ?
thanks for any information,
V.
--
View this message in context: http://www.nabble.com/java.lang.OutOfMemoryError%3A-Java-heap-space-tf477...
Sent from the drools - user mailing list archive at Nabble.com.
12 years, 1 month