我很新,学习流口水。 我正在尝试创建基于规则的假日日历。实施了一套规则,根据日期确定假日或营业日
我创建了似乎正在运行的规则文件和类
规则文件
//created on: Dec 30, 2017
//list any import classes here.
import com.intlsys.holidaycalendar.model.CheckDay;
import java.util.*;
//declare any global variables here
rule "Weekend1_saturday"
when
//conditions
$checkday : CheckDay($dayofWeek : dayofWeek == 7)
then
//actions
System.out.println("Weekend1_saturday");
modify($checkday) {
setWeekend1(true)
};
modify($checkday) {
setHoliday(true)
};
end
rule "Weekend2_sunday"
when
//conditions
$checkday : CheckDay($dayofWeek : dayofWeek == 1)
then
//actions
System.out.println("Weekend1_sunday");
modify($checkday) {
setWeekend2(true)
};
end
rule "Holiday_newyear_day"
when
//conditions
$checkday : CheckDay($month : month == 1, $dayofMonth : dayofMonth == 1)
then
System.out.println("Holiday_newyear_day");
//actions
modify($checkday) {
setHoliday(true)
};
end
rule "Holiday_MartinLutherKingBirthday."
when
//conditions
$checkday : CheckDay(month == 1, dayofWeek == 2, dayofWeekinMonth == 3)
then
System.out.println("Holiday_MartinLutherKingBirthday.");
//actions
modify($checkday) {
setHoliday(true)
};
end
rule "Holiday_WashingtonBirthday_PresidentsDay."
when
//conditions
$checkday : CheckDay(month == 2, dayofWeek == 2, dayofWeekinMonth == 3)
then
System.out.println("Holiday_WashingtonBirthday_PresidentsDay.");
//actions
modify($checkday) {
setHoliday(true)
};
end
rule "Holiday_MemorialDay."
when
//conditions
$checkday : CheckDay(month == 5, dayofWeek == 2, dayofMonth > (31 - 7))
then
System.out.println("Holiday_MemorialDay.");
//actions
modify($checkday) {
setHoliday(true)
};
end
rule "Holiday_IndependenceDay."
when
//conditions
$checkday : CheckDay(month == 7, dayofMonth == 4)
then
System.out.println("Holiday_IndependenceDay.");
//actions
modify($checkday) {
setHoliday(true)
};
end
rule "Holiday_LaborDay."
when
//conditions
$checkday : CheckDay(month == 9, dayofWeek == 2, dayofWeekinMonth == 1)
then
System.out.println("Holiday_LaborDay.");
//actions
modify($checkday) {
setHoliday(true)
};
end
rule "Holiday_ColumbusDay."
when
//conditions
$checkday : CheckDay(month == 10, dayofWeek == 2, dayofWeekinMonth == 2)
then
System.out.println("Holiday_ColumbusDay.");
//actions
modify($checkday) {
setHoliday(true)
};
end
rule "Holiday_VeteransDay."
when
//conditions
$checkday : CheckDay(month == 11, dayofMonth == 11)
then
System.out.println("Holiday_VeteransDay.");
//actions
modify($checkday) {
setHoliday(true)
};
end
rule "Holiday_ThanksGivingDay."
when
//conditions
$checkday : CheckDay(month == 11, dayofWeek == 5, dayofWeekinMonth == 4)
then
System.out.println("Holiday_ThanksGivingDay.");
//actions
modify($checkday) {
setHoliday(true)
};
end
rule "Holiday_ChristmasDay."
when
//conditions
$checkday : CheckDay(month == 12, dayofMonth == 25)
then
System.out.println("Holiday_ChristmasDay.");
//actions
modify($checkday) {
setHoliday(true)
};
end
java类文件
package com.intlsys.holidaycalendar.model;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import lombok.Data;
public @Data class CheckDay {
private int dayofWeek;
private int month;
private int dayofMonth;
private int dayofWeekinMonth;
private int year;
public boolean isWeekend1; // Weekend 1 = Saturday
public boolean isWeekend2; // Weekend 1 = Sunday
public boolean isHoliday;
private Date currentday;
private Date nextday;
private Date previousday;
private Calendar cal;
public CheckDay(int year, int month, int dayOfMonth) {
cal = new GregorianCalendar(year, month - 1, dayOfMonth);
this.year = cal.get(Calendar.YEAR);
this.month = cal.get(Calendar.MONTH) + 1;
this.dayofMonth = cal.get(Calendar.DAY_OF_MONTH);
this.dayofWeek = cal.get(Calendar.DAY_OF_WEEK);
this.dayofWeekinMonth = cal.get(Calendar.DAY_OF_WEEK_IN_MONTH);
this.currentday = cal.getTime();
this.setNextday();
this.setPreviousday();
}
public void setPreviousday() {
Calendar c = Calendar.getInstance();
c.setTime(this.currentday);
c.add(Calendar.DATE, -1);
this.previousday = c.getTime();
}
public void setNextday() {
Calendar c = Calendar.getInstance();
c.setTime(this.currentday);
c.add(Calendar.DATE, 1);
this.nextday = c.getTime();
}
}
但现在我想实施2条规则
如果nextdate是假日,则nextdate是weekend1然后isholiday = true
如果previousdate是假期且previousdate是weekend2则isholiday = true
我将次日和前一天的属性添加到CheckDay类,但我无法找到实现这两条规则的方法
我需要流口水专家提供有关如何实施这些规则或如何使用流口水来解决此问题的建议。
注意:我已经知道如何在不使用drools的情况下解决这个问题,所以我不是在寻找答案。我的意图不是解决holidaycalendar问题,但我想了解这种问题可以用流口水来解决吗?
其他课程
package com.intlsys.holidaycalendar;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import com.intlsys.holidaycalendar.model.CheckDay;
import com.intlsys.holidaycalendar.service.HolidayCalculator;
import com.intlsys.holidaycalendar.service.HolidayCalculatorImpl;
@SpringBootApplication
public class HolidaycalendarApplication {
public static void main(String[] args) {
SpringApplication.run(HolidaycalendarApplication.class, args);
ApplicationContext context =
new AnnotationConfigApplicationContext(HolidayCalendarCalculation.class);
HolidayCalculator holidayCalculatorService =
(HolidayCalculator) context.getBean(HolidayCalculatorImpl.class);
CheckDay day = new CheckDay(2018, 12, 25);
boolean isbusinessday = holidayCalculatorService.isbusinessday(day);
System.out.println("isbusinessday: " + isbusinessday);
}
}
和
package com.intlsys.holidaycalendar;
import org.kie.api.KieServices;
import org.kie.api.builder.KieBuilder;
import org.kie.api.builder.KieFileSystem;
import org.kie.api.builder.KieModule;
import org.kie.api.runtime.KieContainer;
import org.kie.internal.io.ResourceFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan("com.intlsys.holidaycalendar.service")
public class HolidayCalendarCalculation {
public static final String drlFile = "HOLIDAY_RULES.drl";
@Bean
public KieContainer kieContainer() {
KieServices kieServices = KieServices.Factory.get();
KieFileSystem kieFileSystem = kieServices.newKieFileSystem();
kieFileSystem.write(ResourceFactory.newClassPathResource(drlFile));
KieBuilder kieBuilder = kieServices.newKieBuilder(kieFileSystem);
kieBuilder.buildAll();
KieModule kieModule = kieBuilder.getKieModule();
return kieServices.newKieContainer(kieModule.getReleaseId());
}
}
服务类
package com.intlsys.holidaycalendar.service;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.intlsys.holidaycalendar.model.CheckDay;
@Service
public class HolidayCalculatorImpl implements HolidayCalculator {
@Autowired
private KieContainer kContainer;
@Override
public boolean isbusinessday(CheckDay day) {
KieSession kieSession = kContainer.newKieSession();
kieSession.insert(day);
kieSession.fireAllRules();
kieSession.dispose();
return day.isHoliday()? false: true;
}
}
答案 0 :(得分:0)
我能够解决问题,但应该有更好的方法
我解决的方式在holidaycalculation服务中我填充setNextdayweekend1Holiday和setPreviousdayWeekend2Holiday 然后我添加了两个规则Weekend1_saturday和Weekend2_sunday
规则文件
//created on: Dec 30, 2017
//list any import classes here.
import com.intlsys.holidaycalendar.model.CheckDay;
import java.util.*;
//declare any global variables here
rule "Weekend1_saturday"
when
//conditions
$checkday : CheckDay($dayofWeek : dayofWeek == 7)
then
//actions
System.out.println("Weekend1_saturday");
modify($checkday) {
setWeekend1(true)
};
modify($checkday) {
setHoliday(true)
};
end
rule "Weekend2_sunday"
when
//conditions
$checkday : CheckDay($dayofWeek : dayofWeek == 1)
then
//actions
System.out.println("Weekend1_sunday");
modify($checkday) {
setWeekend2(true)
};
end
rule "Holiday_newyear_day"
when
//conditions
$checkday : CheckDay($month : month == 1, $dayofMonth : dayofMonth == 1)
then
System.out.println("Holiday_newyear_day");
//actions
modify($checkday) {
setHoliday(true)
};
end
rule "Holiday_MartinLutherKingBirthday."
when
//conditions
$checkday : CheckDay(month == 1, dayofWeek == 2, dayofWeekinMonth == 3)
then
System.out.println("Holiday_MartinLutherKingBirthday.");
//actions
modify($checkday) {
setHoliday(true)
};
end
rule "Holiday_WashingtonBirthday_PresidentsDay."
when
//conditions
$checkday : CheckDay(month == 2, dayofWeek == 2, dayofWeekinMonth == 3)
then
System.out.println("Holiday_WashingtonBirthday_PresidentsDay.");
//actions
modify($checkday) {
setHoliday(true)
};
end
rule "Holiday_MemorialDay."
when
//conditions
$checkday : CheckDay(month == 5, dayofWeek == 2, dayofMonth > (31 - 7))
then
System.out.println("Holiday_MemorialDay.");
//actions
modify($checkday) {
setHoliday(true)
};
end
rule "Holiday_IndependenceDay."
when
//conditions
$checkday : CheckDay(month == 7, dayofMonth == 4)
then
System.out.println("Holiday_IndependenceDay.");
//actions
modify($checkday) {
setHoliday(true)
};
end
rule "Holiday_LaborDay."
when
//conditions
$checkday : CheckDay(month == 9, dayofWeek == 2, dayofWeekinMonth == 1)
then
System.out.println("Holiday_LaborDay.");
//actions
modify($checkday) {
setHoliday(true)
};
end
rule "Holiday_ColumbusDay."
when
//conditions
$checkday : CheckDay(month == 10, dayofWeek == 2, dayofWeekinMonth == 2)
then
System.out.println("Holiday_ColumbusDay.");
//actions
modify($checkday) {
setHoliday(true)
};
end
rule "Holiday_VeteransDay."
when
//conditions
$checkday : CheckDay(month == 11, dayofMonth == 11)
then
System.out.println("Holiday_VeteransDay.");
//actions
modify($checkday) {
setHoliday(true)
};
end
rule "Holiday_ThanksGivingDay."
when
//conditions
$checkday : CheckDay(month == 11, dayofWeek == 5, dayofWeekinMonth == 4)
then
System.out.println("Holiday_ThanksGivingDay.");
//actions
modify($checkday) {
setHoliday(true)
};
end
rule "Holiday_ChristmasDay."
when
//conditions
$checkday : CheckDay(month == 12, dayofMonth == 25)
then
System.out.println("Holiday_ChristmasDay.");
//actions
modify($checkday) {
setHoliday(true)
};
end
rule "Holiday_Nextdayweekend1Holiday."
when
//conditions
$checkday : CheckDay(nextdayweekend1Holiday == true)
then
System.out.println("Holiday_Nextdayweekend1Holiday.");
//actions
modify($checkday) {
setHoliday(true)
};
end
rule "Holiday_Previousdayweekend2Holiday."
when
//conditions
$checkday : CheckDay(previousdayWeekend2Holiday == true)
then
System.out.println("Holiday_Previousdayweekend2Holiday.");
//actions
modify($checkday) {
setHoliday(true)
};
end
班级档案
package com.intlsys.holidaycalendar.service;
import java.util.Calendar;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.intlsys.holidaycalendar.model.CheckDay;
@Service
public class HolidayCalculatorImpl implements HolidayCalculator {
@Autowired private KieContainer kContainer;
@Override
public boolean isbusinessday(CheckDay day) {
KieSession kieSession = kContainer.newKieSession();
Calendar cal1 = Calendar.getInstance();
//check if next day is holiday and saturday
cal1.setTime(day.getNextday());
CheckDay nextday =
new CheckDay(
cal1.get(Calendar.YEAR), cal1.get(Calendar.MONTH) + 1, cal1.get(Calendar.DAY_OF_MONTH));
kieSession.insert(nextday);
kieSession.fireAllRules();
if (nextday.isHoliday == true && nextday.isWeekend1 == true)
day.setNextdayweekend1Holiday(true);
// check if previous day is holiday and sunday
cal1.setTime(day.getPreviousday());
CheckDay previousday =
new CheckDay(
cal1.get(Calendar.YEAR), cal1.get(Calendar.MONTH) + 1, cal1.get(Calendar.DAY_OF_MONTH));
kieSession.insert(previousday);
kieSession.fireAllRules();
if (previousday.isHoliday == true && previousday.isWeekend2 == true)
day.setPreviousdayWeekend2Holiday(true);
kieSession.insert(day);
kieSession.fireAllRules();
kieSession.dispose();
return day.isHoliday() ? false : true;
}
}
服务类
package com.intlsys.holidaycalendar.service;
import java.util.Calendar;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.intlsys.holidaycalendar.model.CheckDay;
@Service
public class HolidayCalculatorImpl implements HolidayCalculator {
@Autowired private KieContainer kContainer;
@Override
public boolean isbusinessday(CheckDay day) {
KieSession kieSession = kContainer.newKieSession();
Calendar cal1 = Calendar.getInstance();
//check if next day is holiday and saturday
cal1.setTime(day.getNextday());
CheckDay nextday =
new CheckDay(
cal1.get(Calendar.YEAR), cal1.get(Calendar.MONTH) + 1, cal1.get(Calendar.DAY_OF_MONTH));
kieSession.insert(nextday);
kieSession.fireAllRules();
if (nextday.isHoliday == true && nextday.isWeekend1 == true)
day.setNextdayweekend1Holiday(true);
// check if previous day is holiday and sunday
cal1.setTime(day.getPreviousday());
CheckDay previousday =
new CheckDay(
cal1.get(Calendar.YEAR), cal1.get(Calendar.MONTH) + 1, cal1.get(Calendar.DAY_OF_MONTH));
kieSession.insert(previousday);
kieSession.fireAllRules();
if (previousday.isHoliday == true && previousday.isWeekend2 == true)
day.setPreviousdayWeekend2Holiday(true);
kieSession.insert(day);
kieSession.fireAllRules();
kieSession.dispose();
return day.isHoliday() ? false : true;
}
}