java - Setting a Calendar to a knowledge session dynamically inside the rules file -


i want attach calendar knowledge session inside rules file.

currently, doing writing required code in java file:

classcalendar.java:

package app1;  import org.kie.api.runtime.kiesession; import org.kie.api.time.calendar;  import org.quartz.impl.calendar.weeklycalendar;  public class classcalendar {    public static final void main(string[] args) {     try {       weeklycalendar weekdaycal = new weeklycalendar();       weekdaycal.setdaysexcluded(new boolean[] { false, false, false, false, false, false, false, false, false });       weekdaycal.setdayexcluded(java.util.calendar.tuesday, true);       calendar calendar = new calendarwrapper(weekdaycal);        string rulefilepath = "src/main/resources/rules/rulefile1.drl";       kiesession ksession = ksessionutil.buildksession(rulefilepath);       ksession.getcalendars().set("calendar", calendar);        ksession.insert(new string("hello"));       ksession.fireallrules();        system.out.println("bye");     } catch (throwable t) {       t.printstacktrace();     }   } } 

calendarwrapper.java:

package app1;  import org.kie.api.time.calendar; import org.quartz.impl.calendar.weeklycalendar;  // class act converter quartz calendar drools calendar public class calendarwrapper implements calendar {   private weeklycalendar cal;    public calendarwrapper(weeklycalendar cal) {     this.cal = cal;   }    public boolean istimeincluded(long timestamp) {     return cal.istimeincluded(timestamp);   }    public static void main(string[] args) {   }  } 

ksessionutil.java:

package app1;  import java.io.fileinputstream; import java.io.filenotfoundexception; import java.util.linkedlist; import java.util.list;  import org.kie.api.kiebase; import org.kie.api.kiebaseconfiguration; import org.kie.api.kieservices; import org.kie.api.builder.kiebuilder; import org.kie.api.builder.kiefilesystem; import org.kie.api.builder.message; import org.kie.api.builder.results; import org.kie.api.conf.equalitybehavioroption; import org.kie.api.conf.eventprocessingoption; import org.kie.api.runtime.kiecontainer; import org.kie.api.runtime.kiesession; import org.kie.api.runtime.kiesessionconfiguration; import org.kie.api.runtime.conf.timedruleexectionoption; import org.kie.api.runtime.rule.facthandle;  public class ksessionutil {    public static kiesession buildksession(string rulefilepath) {     list<string> rulefilepathlist = new linkedlist<string>();     rulefilepathlist.add(rulefilepath);     return buildksession(rulefilepathlist);   }    public static kiesession buildksession(list<string> rulefilepathlist) {     final boolean showerrors = true;     final boolean showwarnings = true;     final boolean showinfo = true;     final boolean timedruleoptionenabled = false;      kieservices kieservices = kieservices.factory.get();     kiefilesystem kfs = kieservices.newkiefilesystem();      string virtualrulefilepathformat = "src/main/resources/rulefile%d.drl";     int virtualrulefilepathindex = 0;     (string rulefilepath : rulefilepathlist) {       virtualrulefilepathindex++;       fileinputstream fis = null;       try {         fis = new fileinputstream(rulefilepath);       } catch (filenotfoundexception e) {         e.printstacktrace();         return null;       }       string virtualrulefilepath = string.format(virtualrulefilepathformat, virtualrulefilepathindex);       // system.out.println(virtualrulefilepath);       kfs = kfs.write(virtualrulefilepath, kieservices.getresources().newinputstreamresource(fis));     }      kiebuilder kiebuilder = kieservices.newkiebuilder(kfs).buildall();     results results = kiebuilder.getresults();     if (showerrors && results.hasmessages(message.level.error)) {       system.out.println("~~error~~:");       system.out.println(results.getmessages());       throw new illegalstateexception("### errors ###");     }     if (showwarnings && results.hasmessages(message.level.warning)) {       system.out.println("~~warning~~:");       system.out.println(results.getmessages());     }     if (showinfo && results.hasmessages(message.level.info)) {       system.out.println("~~info~~:");       system.out.println(results.getmessages());     }      kiecontainer kiecontainer = kieservices.newkiecontainer(kieservices.getrepository().getdefaultreleaseid());      // change engine mode 'stream'     kiebaseconfiguration config = kieservices.newkiebaseconfiguration();     config.setoption(eventprocessingoption.stream);     config.setoption(equalitybehavioroption.equality);      kiebase kiebase = kiecontainer.newkiebase(config);      kiesession ksession = null;     if (timedruleoptionenabled) {       // set timedruleexception true       // make fireallrules() asynchronous ( fireuntilhalt() )       // in case of timed rules.       kiesessionconfiguration ksconf = kieservices.factory.get().newkiesessionconfiguration();       ksconf.setoption(timedruleexectionoption.yes);       ksession = kiebase.newkiesession(ksconf, null);     } else {       ksession = kiebase.newkiesession();     }      return ksession;   }    public static boolean updateorinsert(kiesession ksession, object obj) {     facthandle facthandle = ksession.getfacthandle(obj);     if (facthandle == null) {       facthandle = ksession.insert(obj);       return false;     } else {       ksession.update(facthandle, obj);       return true;     }   }    public static void fireallrules(kiesession ksession) {     system.out.println("firing rules ...");     ksession.fireallrules();     system.out.println("rules fired!");   }  } 

rulefile1.drl:

package app1;  rule "rule1" calendars "calendar" when     str: string()   system.out.println("in rule - " + drools.getrule().getname());     system.out.println("string matched " + str); end 

the code required initialize , set calendar has been written in java file classcalendar.java:

weeklycalendar weekdaycal = new weeklycalendar(); weekdaycal.setdaysexcluded(new boolean[] { false, false, false, false, false, false, false, false, false }); weekdaycal.setdayexcluded(java.util.calendar.tuesday, true); calendar calendar = new calendarwrapper(weekdaycal);  string rulefilepath = "src/main/resources/rules/rulefile1.drl"; kiesession ksession = ksessionutil.buildksession(rulefilepath); ksession.getcalendars().set("calendar", calendar); 

is there way such able rule itself, can inside java file:

public static final void main(string[] args) {   try {     string rulefilepath = "src/main/resources/rules/rulefile1.drl";     kiesession ksession = ksessionutil.buildksession(rulefilepath);     ksession.insert(new string("hello"));     ksession.fireallrules();      system.out.println("bye");   } catch (throwable t) {     t.printstacktrace();   } } 

in drools, concept of calendar nothing map of moments in time selected calendar attributes boolean, set property of rule, permitting rule fire. drools can evaluate current date , time , permit or disallow firing of rule, based on such calendar. not have way of changing compiled property of rule.

you can create dynamic logic using calendar fact defining intervals firing can turned on , off ("schedule", e.g. hours of day), insert facts defining schedule , use rules modify these schedule facts. evaluating set of facts, schedule facts need evaluated against current time, have inserted fact.

rule "determine time to..." when     now( $time: time )     schedule( permitsat( $time ) )     str: thisorthat()     //... 

this require amount of work, dynamic behaviour more expensive... ;-)

edit note 1 pair plus schedule can put single rule can extended set of rules should valid according particular schedule.


Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -