Drools flow is we insert Java objects into KieSession and fire rules on them. But sometimes while firing the rules, we insert new Java objects into KieSession.
There are 2 ways to retrieve the inserted facts in Java code -
1) Using kieSession.getObjects() method. Using this method, we get all the inserted objects. To use the specific object, we need to typecast that object to particular classes.
2) Using QueryResults. This method requires a change in DRL file. This requires a query need to be added in DRL file as shown below
query "getObjectsOfUser" $insertedUserObjectInDRLFile: User() end
Java object is fetched using below code after firing rules
QueryResults results = kieSession.getQueryResults("getObjectsOfUser"); for (QueryResultsRow row : results) { User user = (User) row.get("$insertedUserObjectInDRLFile"); }
Complete Source Code-
User.java - Java Pojo Object
package com.pojo; public class User { private String name; private String address; private Integer age; private boolean adult; @Override public String toString() { return "Name - " + this.name + "\nAddress -" + this.address + "\nAge" + this.age + "\nAdult - " + this.adult; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public boolean isAdult() { return adult; } public void setAdult(boolean adult) { this.adult = adult; } }
DRL File
package com.drools.user import com.pojo.User; rule "Check If User Object Exists" salience 160 when not User() then System.out.println(drools.getRule().getName() + " rule fired"); System.out.println("User Object does not exists in KieSession. Insert new Object in DRL File"); User user = new User(); user.setName("Andy"); user.setAge(32); user.setAddress("New York"); insert(user); drools.halt(); end query "getObjectsOfUser" $insertedUserObjectInDRLFile: User() end
UserDroolsClient.java - Client File in which Inserted Facts are retrieved after firing Rules
package com.drools; import org.kie.api.KieServices; import org.kie.api.runtime.KieContainer; import org.kie.api.runtime.KieSession; import org.kie.api.runtime.rule.QueryResults; import org.kie.api.runtime.rule.QueryResultsRow; import com.pojo.User; public class UserDroolsClient { public static final void main(final String[] args) throws InterruptedException { KieSession kieSession = new UserDroolsClient().execute(); kieSession.fireAllRules(); System.out.println("\n"); // Method 1 Using getObjects() method kieSession.getObjects().forEach(object -> { if (object instanceof User) { User user = (User) object; System.out.println("User Object Inserted in DRL File fetched from KieSession using getObjects() After Firing Rules - \n" + user.toString()); } }); System.out.println("\n"); // Method 2 Using QueryResults QueryResults results = kieSession.getQueryResults("getObjectsOfUser"); for (QueryResultsRow row : results) { User user = (User) row.get("$insertedUserObjectInDRLFile"); System.out.println("User Object Inserted in DRL File fetched from KieSession using QueryResult Object After Firing Rules - \n" + user.toString()); } kieSession.dispose(); } public KieSession execute() { KieServices ks = KieServices.Factory.get(); KieContainer kc = ks.getKieClasspathContainer(); KieSession kieSession = kc.newKieSession("UserKIESession"); return kieSession; } }
No comments:
Post a Comment