I'm using drools
for alert generation and whenever I meet some condition I'm able to fire a rule and generate an alert.
我正在使用drools来生成警报,每当我遇到某种情况时,我就可以触发规则并生成警报。
My requirement further is for motion and smoke alert, we need to fire a rule on weekdays (i.e., except Saturday and Sunday).
我的要求是进一步提出运动和烟雾警报,我们需要在工作日发布规则(即星期六和星期日除外)。
How is it possible?
这怎么可能?
1 个解决方案
#1
1
You can generate the alert based betwwen times and you can generate the alert by exluding the days, in this case i have done some r&d. for this you can fallow below 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:
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();
#1
1
You can generate the alert based betwwen times and you can generate the alert by exluding the days, in this case i have done some r&d. for this you can fallow below 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:
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();