使用cron表达式的口水规则?

编程入门 行业动态 更新时间:2024-10-28 10:28:27
本文介绍了使用cron表达式的口水规则?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个要求我仅在工作日内执行规则.我有诸如烟,温度,运动之类的规则.您能建议我如何按照我的要求制定规则吗?请提供一些示例.

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.

除了cron之外,还有没有其他更好的方法可以基于时间触发规则?

Is there any better way to fire rules based on time other then cron?

推荐答案

您可以在工作日或周末触发规则,我遇到过同样的要求,找到了解决方案.您只是放松步骤:

You can fire rule on weekdays or weekends , same requirement i have faced , found some solution. you just fallow steps:

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 (finalTimepareTo(initialTime) < 0) { finalTimecalendar2.add(Calendar.DATE, 1); currentTimecalendar3.add(Calendar.DATE, 1); } java.util.Date actualTime = currentTimecalendar3.getTime(); if ((actualTime.after(initialTimecalendar1.getTime()) || actualTimepareTo(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_charpareTo('1')==0){ return false; }else{ return true; } } catch (ParseException e) { // TODO Auto-generated catch block //e.printStackTrace(); return false; } //return true; //return true; }

触发规则的Java代码:

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

注意:在isExlude方法中,我提到000000表示规则将全天执行(000000分别表示S,M,T,W,T,F,Sat),0表示打开,1表示关闭

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

更多推荐

使用cron表达式的口水规则?

本文发布于:2023-11-24 08:38:08,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1624501.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:表达式   口水   规则   cron

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!