问题
I have one requirement that i want to fire rule only for weekdays.i have some rule like smoke, temperature, motion.can you suggest me how i can make rule as per my requirement.please provide me some example.
Is there any better way to fire rules based on time other then cron?
回答1:
You can fire rule on weekdays or weekends , same requirement i have faced , found some solution. you just fallow steps:
DRL File:
package com.javacodegeeks.drools;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;
global String topicLevel
rule "Drools Introduction"
when
$droolsIntro : DroolsIntroduction( topic == "Drools" )
then
System.out.println($droolsIntro.introduceYourself() + ", topic level is " + getDefaultIfNull(topicLevel));
end
rule "Drools Introduction2"
when
$droolsIntro : DroolsIntroduction( isTimeBetweenTwoTime("10:12:12","13:13:13","11:12:12") && isExcludeDay("000000","2016-08-08 21:5:21") )
then
System.out.println("time valid and sunday");
end
function String getDefaultIfNull(String topicLevel) {
return topicLevel == null ? "Moderate" : topicLevel;
}
function Boolean isTimeBetweenTwoTime(String initialTime, String finalTime, String currentTime) {
Boolean valid = false;
System.out.println("time check started");
try {
if (currentTime == null) {
currentTime = new SimpleDateFormat("HH:mm:ss").format(Calendar.getInstance().getTime());
System.out.println("check current time "+currentTime);
}
System.out.println(initialTime+" -- "+finalTime+" -- -- "+currentTime);
String reg = "^([0-1][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$";
if (initialTime.matches(reg) && finalTime.matches(reg) && currentTime.matches(reg)) {
valid = false;
// Start Time
java.util.Date inTime = new SimpleDateFormat("HH:mm:ss").parse(initialTime);
Calendar initialTimecalendar1 = Calendar.getInstance();
initialTimecalendar1.setTime(inTime);
// Current Time
java.util.Date checkTime = new SimpleDateFormat("HH:mm:ss").parse(currentTime);
Calendar currentTimecalendar3 = Calendar.getInstance();
currentTimecalendar3.setTime(checkTime);
// End Time
java.util.Date finTime = new SimpleDateFormat("HH:mm:ss").parse(finalTime);
Calendar finalTimecalendar2 = Calendar.getInstance();
finalTimecalendar2.setTime(finTime);
if (finalTime.compareTo(initialTime) < 0) {
finalTimecalendar2.add(Calendar.DATE, 1);
currentTimecalendar3.add(Calendar.DATE, 1);
}
java.util.Date actualTime = currentTimecalendar3.getTime();
if ((actualTime.after(initialTimecalendar1.getTime())
|| actualTime.compareTo(initialTimecalendar1.getTime()) == 0)
&& actualTime.before(finalTimecalendar2.getTime())) {
System.out.println("condition matched --- ");
valid = true;
}
System.out.println("returning "+valid);
return valid;
} else {
System.out.println("else false "+valid);
return valid;
// throw new IllegalArgumentException("not a valid time,
// expecting HH:MM:SS format");
}
} catch (Exception e) {
System.out.println("exception "+valid);
valid=false;
}
System.out.println("finale "+valid);
return valid;
}
function Boolean isExcludeDay(String validdays,String timeStamp) {
try {
System.out.println("validdays"+validdays);
System.out.println("timeStamp"+timeStamp);
DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH);
Date date = format.parse(timeStamp);
Calendar currentTimecalendar3 = Calendar.getInstance();
currentTimecalendar3.setTime(date);
int dayOfWeek = currentTimecalendar3.get(Calendar.DAY_OF_WEEK)-1;
Character a_char = validdays.charAt(dayOfWeek);
System.out.println( a_char );
if(a_char.compareTo('1')==0){
return false;
}else{
return true;
}
} catch (ParseException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
return false;
}
//return true;
//return true;
}
Java code for firing rule:
KieServices ks = KieServices.Factory.get();
KieContainer kContainer = ks.getKieClasspathContainer();
KieSession kSession = kContainer.newKieSession("ksession-rules");
DroolsIntroduction droolsIntroduction = new DroolsIntroduction("Drools");
kSession.insert(droolsIntroduction);
kSession.insert(new DroolsIntroduction("All"));
kSession.insert(new DroolsIntroduction("Specific"));
kSession.fireAllRules();
/* try {
String string1 = "11:06:13";
Date time1 = new SimpleDateFormat("HH:mm:ss").parse(string1);
Calendar calendar1 = Calendar.getInstance();
calendar1.setTime(time1);
String string2 = "10:49:00";
Date time2 = new SimpleDateFormat("HH:mm:ss").parse(string2);
Calendar calendar2 = Calendar.getInstance();
calendar2.setTime(time2);
calendar2.add(Calendar.DATE, 1);
String someRandomTime = "01:00:00";
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(Calendar.getInstance().getTime());
String currenttime = new SimpleDateFormat("HH:mm:ss").format(Calendar.getInstance().getTime());
Date d = new SimpleDateFormat("HH:mm:ss").parse(currenttime);
Calendar calendar3 = Calendar.getInstance();
calendar3.setTime(d);
calendar3.add(Calendar.DATE, 1);
Date x = calendar3.getTime();
if (x.after(calendar1.getTime()) && x.before(calendar2.getTime())) {
//checkes whether the current time is between 14:49:00 and 20:11:13.
System.out.println("checkes whether the current time is between 14:49:00 and 20:11:13");
System.out.println(true);
}
} catch (Exception e) {
e.printStackTrace();
}
String ccurrenttime = new SimpleDateFormat("HH:mm:ss").format(Calendar.getInstance().getTime());
System.out.println(ccurrenttime);
isBetweenValidTime("09:33:00","09:38:00",ccurrenttime);*/
kSession.setGlobal("topicLevel", "Beginner");
kSession.insert(new DroolsIntroduction("All"));
kSession.fireAllRules();
Note:In isExlude method i am mentioning 000000 means that rule will fire all days(000000 means S,M,T,W,T,F,Sat respectivly) and 0 means on and 1 means off
来源:https://stackoverflow.com/questions/41041426/drool-rules-using-cron-expression