I am completely new to Drools and just exploring. So far I had been using a single rule and withing which we use if conditions just like java.
Now I had to use complex rules where I need to use multiple if and else chain but to end it when one in the series of conditions satisfies.
I call the drools from the Apache Camel. The rules should reply back to Camel.
Now How do I do to break the chain of rules and then send response back to the caller which is Camel code when one of if and else condition fails.
1.Can I use multiple end statements to respond back?
2.I can use functions and I know is to write functions in java and import them.
3.Is there any possibility to create functions in Drools and use just like in java?
I am not using the Drools in the way it should be used but so far the rules had not been so complex as the one we are using them now . Any help is is useful for me.
Here is an example which I would like to use please suggest if the below would work or some other alternative like the below.
rule "my rule"
when
#some condition
then
if(){
end
}else if(){
#do something
}
if(){
#do some other logic
}
end
Sample after My second comment
When
object:SomeObject(); // This helps for my camel code to trigger this rule and this rule only
then
if(){
}
else if()
{
return;
}else if() {
}
if(){
}else if(){
return;
}
if(){
}
end
I don't know what you mean by "breaking a chain of rules". Evaluating rules ends all by iteself, when there aren't any more rule activations to be executed,
Answers to 1. - 3.:
No. - There is no such thing I can associate with the term "respond back", but there aren't "multiple end statements [for one rule]".
Yes, you can use static Java function: import the class, call it, as in Java.
Yes. this is explained in the Drools manual, in a section with the title "Function". The example is from the manual:
function String hello(String name) {
return "Hello "+name+"!";
}
Comment on the rule added later to the question
You cannot use end between then and the actual end of the rule. If you want to terminate the consequence part of a rule, use Java's return statement (without an expression, of course).
Reconsider using complex conditional statements in the consequence. Logic decisions should be expressed in the condition parts of rules.
Much later Workaround due to possible Guvnor bug - does not accept return;
boolean skip = false;
if(){
} else
if() {
skip = true; // return;
} else
if() {
}
if( ! skip ){
if(){
} else
if(){
skip = true; // return;
}
}
if( ! skip ){
if (){
}
}
end
Related
There are two types of if function codes:
condition1 ? function1 : condition2 ? function2 : function3;
if (condition1) {
function1
} else {
if (condition2) {
function2
} else {
function3
}
}
I don't know what is the correct way to use the if else function code, I hope everyone can tell me.
Feel free to leave a comment if you need more information.
Which if else code should I use, single line or multiple lines? I would appreciate any help. Thank you in advance!
Those two are not "different styles of if".
The first one is the ternary operator. As the term operator suggests, it is used to produce a value.
The second one is the flow control statement if. It does not produce any value, it changes your program's flow.
So which one should you use? The one that fits your goal best. Do you need a value? The operator. Do you need to change your program flow? The flow control statement.
Try to reproduce this if statement with a ternary operator:
if (trafficlight.current == red) {
stopVehicle();
}
You cannot. Not without adding pointless waste. Because this is flow control.
On the other hand, this:
var newSpeed = (trafficlight.current == red) ? 0 : this.MaxSpeed;
Would be very convoluted to write as an if statement. Because it is generating a value.
So pick what is best for your program. It's not a "style" to follow blindly. It is a decision you should make for every one of those instances.
I'm working on a CDCL SAT-Solver. I don't know how to implement non-chronological backtracking. Is this even possible with recursion or is it only possible in a iterative approach.
Actually what i have done jet is implemented a DPLL Solver which works with recursion. The great differnece from DPLL and CDCL ist that the backracking in the tree is not chronological. Is it even possible to implement something like this with recursion. In my opionion i have two choices in the node of the binary-decision-tree if one of to path leads i a conlict:
I try the other path -> but then it would be the same like the DPLL, means a chronological backtracking
I return: But then i will never come back to this node.
So am i missing here something. Could it be that the only option is to implement it iterativly?
Non-chronological backtracking (or backjumping as it is usually called) can be implemented in solvers that use recursion for the variable assignments. In languages that support non-local gotos, you would typically use that method. For example in the C language you would use setjmp() to record a point in the stack and longjmp() to backjump to that point. C# has try-catch blocks, Lispy languages might have catch-throw, and so on.
If the language doesn't support non-local goto, then you can implement a substitute in your code. Instead of dpll() returning FALSE, have it return a tuple containing FALSE and the number of levels that need to be backtracked. Upstream callers decrement the counter in the tuple and return it until zero is returned.
You can modify this to get backjumping.
private Assignment recursiveBackJumpingSearch(CSP csp, Assignment assignment) {
Assignment result = null;
if (assignment.isComplete(csp.getVariables())) {
result = assignment;
}
else {
Variable var= selectUnassignedVariable(assignment, csp);
for (Object value : orderDomainValues(var, assignment, csp)) {
assignment.setAssignment(var, value);
fireStateChanged(assignment, csp);
if (assignment.isConsistent(csp.getConstraints(var))) {
result=recursiveBackJumpingSearch(csp, assignment);
if (result != null) {
break;
}
if (result == null)
numberOfBacktrack++;
}
assignment.removeAssignment(var);
}
}
return result;
}
During OptaPlanner solving phrase, I wish to update a global variable. The changes of global variable will modify my next rules validation.
Result.java
package com.domain;
public static boolean status;
...
...
Sample.drl
import org.optaplanner.core.api.score.buildin.hardsoft.HardSoftScoreHolder;
import com.domain.Result;
global HardSoftScoreHolder scoreHolder;
rule "Sample Rule"
when
$PlanningEntity:PlanningEntity()
then
somelogic($PlanningEntity);
if(Result.status){
scoreHolder.addHardConstraintMatch(kcontext, -500);
}
end
function void somelogic(PlanningEntity planningEntity){
if(condition 1...){
Result.status = true;
}else if(condition 2...){
Result.status = false;
}else{
//Do Nothing
}
}
My Question:
How can I declare the static global variable per solving session. (To avoid multiple users doing the OptaPlanner solver at the same timing.)
Currently you can't reliably use a global variable because OptaPlanner might spawn multiple drools sessions (needed for future features such as multi-threaded solving, population based heuristics, ...).
The workaround is simple: simply add a singleton problem fact and use that. See the *Parametrization classes in the examples.
I'm having an object as below:
class License{
private field1;
private field2;
private boolean active;
private String activeMessage;
private boolean processed = false;
//Getter and setter methods
}
What I'm trying to do is, based on the values of field1, and field2, I need to set the isActive flag and a corresponding message. However, if either the rule for field1 or field2 is fired, I need to stop the rules processing. That is, I need to execute only 1 successful rule.
I read on a post that doing ksession.fireAllRules(1) will solve this. But the fireAllRules() method is not available in Drools 6. I also tried putting a return; statement at the end of each rule. That didn't help me either.
Finally, I ended up adding an additional field to my object called processed. So whenever I execute any rule, I set the processed flag to true. And if the flag is already set, then I do not execute any rule. This is my rules file:
rule "Check field1"
when
$obj : License(getField1() == "abc" && isProcessed() == false)
then
System.out.println("isProcessed >>>>>> "+$obj.isProcessed());
$obj.setActive(true);
$order.setActiveMessage("...");
$order.setProcessed(true);
end
rule "Check field2"
when
$obj : License(getField2() == "def" && isProcessed() == false)
then
System.out.println("isProcessed >>>>>> "+$obj.isProcessed());
$obj.setActive(true);
$order.setActiveMessage("...");
$order.setProcessed(true);
end
However, I see that even now both my rules are being fired. When I try to print the value of isProcessed(), it says true, even though I enter the rule only if isProcessed() is false.
This is how I'm calling the drools engine:
kieService = KieServices.Factory.get();
kContainer = kieService.getKieClasspathContainer();
kSession = kContainer.newStatelessKieSession();
kSession.execute(licenseObj);
It is not just 2 rules, I have a lot of rules, so controlling the rules execution by changing the order of the rules in the drl file is not an option. What is happening here? How can I solve this problem? I am sort of new to Drools, so I might be missing something here.
Thanks.
Your question contains a number of errors.
It is definitely not true that fireAllRules has disappeared in Drools 6. You might have looked at the javadoc index, to find four (4!) overloaded versions of this method in package org.kie.api.runtime.rule in the interface StatefulRuleSession.
You might easily avoid the problem of firing just one out of two rules by combining the triggering constraint:
rule "Check field1 and field2"
when
$lic: License(getField1() == "abc" || getField2() == "def" )
//...
then
$lic.setXxx(...);
end
You complain that both of your rules fire in spite of setting the processed flag in the fact. Here you are missing a fundamental point (which is covered in the Drools reference manual), i.e., the necessity of notifying the Engine whenever you change fact data. You should have used modify on the right hand side of your rules.
But even that would not have been good enough. Whenever an update is made due to some properties, a constraint should be added to avoid running the update over and over again. You might have written:
rule "Check field1 and field2"
when
$lic: License(getField1() == "abc" || getField2() == "def",
! active )
//...
then
modify( $lic ){ setActive( true ) }
end
You might even write this in two distinct rules, one for each field, and only one of these rules will fire...
I would like to know the advantage and disadvantage of the following operation
shall i better set the datareader to null than calling the close method. If this is good what are the advantages, else what is the problem in using so?.
You should use the using statement instead:
using (var reader = sqlCommand.ExecuteReader())
{
// do stuff
}
That way, you are sure that the reader is closed (disposed), even if an exception was raised in the "do stuff" block.
For a complete example, see this MSDN page.
Update (regarding your comment):
The using statement is in fact nothing else than a try-finally block to ensure that the reader is disposed (closed) in every case. E.g. the above code is equivalent to this:
SqlDataReader reader = null;
try
{
reader = sqlCommand.ExecuteReader();
}
finally
{
reader.Dispose(); // closes the reader
}