how to add value to duration attribute in drools - drools

Whenever i write duration(0s), it works but as soon as i change it to duration(1s) or duration(5s), the rule doesn't fire...
This is the rule which i want to fire.
rule "ContainsChecking"
agenda-group "town4"
duration(0s)
when
Town(owner matches "[N || n][a-z]+")
then
System.out.println("Rule Fired ContainsChecking");
end
do we need to import something for duration attribute to work, because i m not getting it anywhere. Thanks in advance.

You need to run the session using
kieSession.fireUntilHalt();
If you just use fireAllRules, the agenda is empty and the call terminates.
This is a CEP feature and need not and should not be used in a simple production rule environment.

Related

Why does Drools re-evaluate and re-trigger rule actions after a persistent session is reloaded?

I have a simple rule like the one below:
package rules
dialect "mvel"
declare MyEvent
#role( event )
#expires( 2d )
id String
value: Double
end
rule "My Rule"
when
MyEvent($value : value)
then
System.out.println("My event with value: " + $value);
end
I create a persistent session and call fireAllRules() on it. Then, I insert a MyEvent fact, and as expected the rule is evaluated, matched and the action is executed. If I call fireAllRules() again the rule is not matched, as expected because it has already matched for the same fact. At this point everything is fine.
Then I kill the JVM and run the app again. At startup the app loads the session like this:
kieSession = kieServices.getStoreServices().loadKieSession(KIE_SESSION_ID, kieBase, kieSessionConfiguration, kieEnvironment);
The session gets loaded successfully, and then fireAllRules() is called again. Since the rule has already matched for the inserted event, I am expecting that it does not match again. However I can see the message in the rule action is printed again. Why does Drools match the rule for the same eventagaian? To me it looks like the session state is not properly saved to database. I mean, the event is saved, but Drools can not recogonize that it has already matched the rule. When I load a persistent session I expect to recover exactly the same state that the session had in the previous running instance. Is my assumption wrong? or Am I doing something wrong for the expected behaviour?
Running:
JavaSE 11
SpringBoot 2.3
Drools 7.53.0

Business rule execute only once on jbpm process

I execute a business rule from a process on Jbpm, the rule is simple:
package com.test.flow;
rule "sample"
ruleflow-group "test"
when
then
System.out.println("Hello World");
end
But, I don't no why, this rule execute only once, for instance, I run a new instance of the process and in the jbpm console print "Hello World", but, when I run a second instance of the process doesn print anymore "Hello World", some one can you help me? or tellme why does this happen?
Screen Jbpm console
For this kind of "hello-world" rule, using ruleflow-group is most likely just causing confusion. Do you control when this group is activated? How? - Omitting this rule attribute is indicated.
A rule with an empty left-hand-side will execute only once in a session (as Esteban pointed out).
If you want a rule that fires once for each inserted fact, use
rule "new fact"
when
Object()
then
System.out.println( "new fact inserted" );
end

Event xforms-model-construct-done behaviour

In my form I would like to call web service after to form is loaded after publishing. I've created custom XBL control for it, where I have :
<xf:group id="component-group">
<xf:action ev:event="xforms-enabled" ev:target="component-group">
<xf:send ev:event="xforms-enabled" submission="my-submission"/>
</xf:action>
</xf:group>
But it doesn't work as expected : my submission is sent everytime when I add new element in FormBuilder or change a name of some other controls. Generally speaking submission is sent when my form is changing in some way.
Now I want submission to be sent ONLY when I publish my form and someone would open it to fill (and of course when I press "Test" in FormBuilder, but I guess it's the same as filling form after publishing).
I was trying something like this :
<xf:group id="component-group">
<xf:action ev:event="xforms-model-construct-done" ev:target="component-group">
<xf:send ev:event="xforms-model-construct-done" submission="my-submission"/>
</xf:action>
</xf:group>
Unfortunately it's not working, this way submission is not sent at all. Any thoughts ?
This is due to the fact that XBL components are live at design time too. So you need a way to test whether the component is running within Form Builder or not.
There should be a function for this, really, but there isn't (I added this to the list of functions we should add to the API here). You can do:
<xf:group id="component-group">
<xf:var name="fr-params" value="xxf:instance('fr-parameters-instance')"/>
<xf:action
event="xforms-enabled"
target="component-group"
if="not($fr-params/app = 'orbeon' and $fr-params/form = 'builder')">
<xf:send submission="my-submission"/>
</xf:action>
</xf:group>
A few minor comments:
you don't need to (in fact shouldn't) place event attributes on nested actions
you don't even need the ev prefix

Eclipse breakpoint condition for objects

I have a problem with the eclipse breakpoint condition pattern.
Regarding this code snippet
public void updateGateway(Gateway pGatewy) {
mGateway = pGateway;
I want to stop the process only if
pGateway.getNumber()==123.
But the condition is without effect and I tested it already successful with this number(but without condition).
What I have to insert in the editor of the Breakpoint properties dialog? The checkbox Conditional is selected.
Thx in advance.
Update your condition
pGateway.getNumber()==123.
to
pGateway.getNumber()==123
Choose Suspend when true option.
Conditional debugging can be run on my machine. You can reference here.

Drools related problem

Hi I want to bring a little change in my site. So i have written a set of rules but I dont know how to fire them in the place that i want them to work i.e. the original code of my site.Can anyone plz help me on it?
A little information is missing (like which Drools version, the environment you're running in, etc.), so I'll try to answer in a general way, and if you need something more specific let me know:
In general in Drools you 'assert' objects into the working memory (using methods like, well, assertObject..), this allows the rule engine to be aware of them, and later, when you execute 'fireAllRules' on that working memory, all the rules are executed (so, you explicitly call the fireAllRules method on your working memory when you want to - I think this is what you were asking).
For more detailed information, in case you haven't looked already, you can check this: http://legacy.drools.codehaus.org/Working+Memory .
thanks
Gadi
when you have your abc object, this will be the fact that you insert into the Working Memory. For example:
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add( ResourceFactory.newFileSystemResource( fileName ), ResourceType.DRL );
if (kbuilder.hasErrors() ) {
System.out.println( kbuilder.getErrors() );
} else {
KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addKnowledgePackages( kbuilder.getKnowledgePackages() );
StatelessKnowledgeSession ksession = kbase.newStatelessKnowledgeSession();
ksession.execute( abc );
}
Then you will have your modified abc object when the execute finishes. Take a look at here
You probably dont't want the rules being build everytime (this is really time consumer), so you can use or KnowledgeAgent or have an static KnowledgeBase and recreate it whenever your rules files change.