Drools cannot resolve a global variable - drools

i'm trying to add String items to a global variable $items. But i get an error "$items cannot be resolved"
Below is snippet of my drl rule. What am i missing?
code snippet

you can try this
First: in drl file
global java.util.HashSet items
rule "rule 1"
when
then
items.add("Item 1");
System.err.println(items);
end
Second: in java file
KieSession kieSession = ...
kieSession.setGlobal("items",new HashSet<>());

Related

JBoss Drools - how to get data (facts) from java to DRL

How can I get fact definied by user in GUI and insert it to DRL?
For example: The user has chosen black car in GUI (JavaFX), and now I want to use that fact in DRL code. How to send that info about black car to DRL? Should i use POJO?
If you want to execute rules that you have written in DRL file you have to create a POJO and using KieSession you can execute your rules. For example,
val pojo = new POJO('POJO arguments')
val kieServices = KieServices.Factory.get()
val kieContainer = kieServices.newKieClasspathContainer()
val kieSession = kContainer.newKieSession()
kieSession.insert(pojo)
kieSession.fireAllRules()
Read this documentation. You can get all the drool-API examples here

Get KieSession and KieServices in Optaplanner

I want to create an Audit logger for the rules i have in my OptaPlanner project
and i need to get access to KieSession and KieServices from Drools to do that.
The problem here is i don't have access to them.
In the documentation in section 5.3.4.2.3. A ksessionName in a Kjar from a Maven repository, i can define a KieSessionName from the solverConfig and create the SolverFactory using the createFromKieContainerXmlResource. The problem here is that i can't find the META-INF/kmodule.xml file. Even if i find the folder, can i use the KieSessionName to get a KieSession.
I'm asking this because with the current way of creating the SolverFactory, using createFromXmlResource, i can get to the KieSessionName with using:
SolverConfig solverConfig = solverFactory.getSolverConfig();
ScoreDirectorFactoryConfig scoreDirectorFactoryConfig = solverConfig.getScoreDirectorFactoryConfig();
String kieSessionName = scoreDirectorFactoryConfig.getKsessionName();
After getting the session name i can't find a way to get the KieSession.
So my questions here are:
Where can i locate META-INF/kmodule.xml?
How can i use kSessionName to get use as a KieSession object?
Is there a way to access the KieSession object without chaging the core files?

How to create xPath for java code in PMD rules in Eclipse

I want to create an xPath to add PMD rule to avoid the below code:
String variable = null;
variable.equals("String");
which throws a null pointer exception.

Drools Rule Template - ResultSet Generator does not display complete Rule file generated

I am trying to create a rule template file with decision table. I am populating the template file from DB values. Using ResultSetGenerator to complile and generate DRL. However I am getting just the package and import line in the generated DRL. Please help.
ResultSetGenerator resultSetGenerator = new ResultSetGenerator();
/* Build drl from DB data */
final String drl = resultSetGenerator.compile(result, template);
The Template looks like this
template header
col1
col2
package rules;
import vo.TestVO;
template "template1"
rule "Rule #{row.rowNumber} "
dialect "mvel"
when
o: TestVO(field == "#{col1}")
then
o.addField(#{col2})
end
end template
DRL generated output on console
package rules;
import vo.TestVO;

drools - Unable to Analyse Expression

I have defined some rules in DRL file, and its my first program of creating a drl file. I am getting the error "unable to analyse expression".Here is my code:
package rules
import com.sample.Applicant.appli;
rule "Is of valid age"
when
$a : appli ( age < 18 ) // appli is my class name
// age is a variable in that class
then
$a.setValid( false ); // setValid is a method of appli
end
and getting the error:
Unable to Analyse Expression age < 18:
[Error: unable to resolve method using strict-mode: com.sample.Applicant$appli.age()]
[Near : {... age < 18 ....}]
^
[Line: 16, Column: 4] : [Rule name='Is of valid age']
Make sure in the class appli, age is either public or has a public getAge() method.
Even I was getting similar error 'Unable to Analyse Expression....' while validating a DRL file in Drools Workbench 6.4.0 Final. I checked Data Object, its fields, access specifier of the setters and getters. All seemed OK. Then I saved my data object and came back to DRL file and did validating. Suddenly the above error was gone and I saw message 'Successfully Validated'. My mistake was though I had created the Data Object in Drools Workbench, I had forgotten to save it by clicking 'Save' button.
So I would suggest you, if you are 100% sure that your Data Object and DRL files are correct, save these first and then validate DRL file again.