Skip to content

BAEL-9161 Added forward chaining example #18745

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions drools/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@

<properties>
<httpcore.version>4.4.16</httpcore.version>
<drools.version>9.44.0.Final</drools.version>
<poi.version>5.2.3</poi.version>
<optaplanner-core.version>8.32.0.Final</optaplanner-core.version>
<drools.version>10.1.0</drools.version>
<poi.version>5.4.1</poi.version>
<optaplanner-core.version>9.44.0.Final</optaplanner-core.version>
</properties>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.baeldung.drools.forward_chaining;

import com.baeldung.drools.config.DroolsBeanFactory;
import com.baeldung.drools.model.Applicant;
import com.baeldung.drools.model.SuggestedRole;
import org.kie.api.runtime.KieSession;

public class ForwardChaining {
public static void main(String[] args) {
ForwardChaining result = new ForwardChaining();
result.forwardChaining();
}

private void forwardChaining() {
KieSession ksession = new DroolsBeanFactory().getKieSession();
Applicant applicant = new Applicant("Daniel", 38, 1_600_000.0, 11);
SuggestedRole suggestedRole = new SuggestedRole();

ksession.setGlobal("suggestedRole", suggestedRole);
ksession.insert(applicant);

int fired = ksession.fireAllRules();
System.out.println("Rules fired: " + fired);
System.out.println("Suggested role: " + suggestedRole.getRole());

ksession.dispose();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ rule "Suggest Manager Role"
suggestedRole.setRole("Manager");
end

rule "Suggest Senior developer Role"
rule "Suggest Senior Developer Role"
when
Applicant(experienceInYears > 5 && experienceInYears <= 10)
Applicant(currentSalary > 500000 && currentSalary <= 1500000)
Expand Down