2

我有一个要求,我只想在工作日触发规则。我有一些规则,如烟雾、温度、运动。你能建议我如何根据我的要求制定规则。请给我一些例子。

除了cron之外,还有什么更好的方法可以根据时间触发规则吗?

4

1 回答 1

1

您可以在工作日或周末触发规则,我遇到了同样的要求,找到了一些解决方案。你只是休闲步骤:

DRL 文件:

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代码:

 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();

注意:在 isExlude 方法中,我提到 000000 表示规则将触发所有天(000000 分别表示 S、M、T、W、T、F、Sat),0 表示开启,1 表示关闭

于 2016-12-13T06:43:31.290 回答