如何结合导致流口水

时间:2014-03-21 06:42:56

标签: java union drools accumulate

我想只在drools中得到一个结果。

我尝试使用“from”“来自累积”,但它没有用。

Main.java //主类

    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
    kbuilder.add(ResourceFactory.newClassPathResource("Rules_Schedule.drl", getClass()), ResourceType.DRL);

    if (kbuilder.hasErrors()) {
        System.err.println(kbuilder.getErrors().toString());
    }

    kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
    try {
        String[] Sss = new String[2];
        Sss[0] = "RLC";
        Sss[1] = "DCK";
        int i=0;
        rs_sch_exam = stm_sch_exam.executeQuery("SELECT *,DATE_FORMAT(sc_date,'%a') AS day FROM schedule_exam");
        while(rs_sch_exam.next()){

            String date = rs_sch_exam.getString("sc_date"),
                    day = rs_sch_exam.getString("day"),
                    start = rs_sch_exam.getString("sc_star"),
                    end = rs_sch_exam.getString("sc_end"),
                    subj = rs_sch_exam.getString("sc_subj"),
                    depa = rs_sch_exam.getString("sc_depa"),
                    sec = rs_sch_exam.getString("sc_sec"),
                    room = rs_sch_exam.getString("sc_room");
            Schedule schedule = new Schedule(date,day,subj,sec,depa,room,start,end);
            schedule.setControlExam1(Sss[i++]); //Test add data,Get value from SQL 2 Recode not had Control Exam1
            ksession.insert(schedule);
        }

        rs_peo = stm_peo.executeQuery("SELECT * FROM control_exam");
        while(rs_peo.next()){

            String code = rs_peo.getString("ce_code"),
                    name = rs_peo.getString("ce_name"),
                    depart = rs_peo.getString("ce_department"),
                    room = rs_peo.getString("ce_room"),
                    sun = rs_peo.getString("ce_sunday"),
                    mon = rs_peo.getString("ce_monday"),
                    tue = rs_peo.getString("ce_tuesday"),
                    wed = rs_peo.getString("ce_wednesday"),
                    thu = rs_peo.getString("ce_thursday"),
                    fri = rs_peo.getString("ce_friday"),
                    sat = rs_peo.getString("ce_saturday");
            Control_Exam controlExam = new Control_Exam(code,name,depart,room,
                    Integer.parseInt(sun),Integer.parseInt(mon),Integer.parseInt(tue),
                    Integer.parseInt(wed),Integer.parseInt(thu),Integer.parseInt(fri),
                    Integer.parseInt(sat));
            ksession.insert(controlExam);
        }
    } catch (SQLException ex) {}       
    ksession.fireAllRules();

Schedule.java //在我的包裹中; control1,control2,control3存储来自 Control_Exam

的代码
public class Schedule {
    private String date, day, subject, sec, depart, room, realstarttime, realendtime,
            control1 = null, control2 = null, control3 = null;
    private int starttime,endtime;
    private String[] timeA;

    public Schedule(String date,String day,String subject,String sec,
            String depart,String room,String t_start,String t_end){  
        this.date = date;
        this.day = day;
        this.subject = subject;
        this.sec = sec;
        this.depart = depart;
        this.room = room;
        this.control1 = "";
        this.control2 = "";
        this.control3 = "";

        this.timeA = t_start.split(":");
        try{
            realstarttime = this.timeA[0].trim() + this.timeA[1].trim();
            this.starttime = Integer.parseInt(realstarttime);
            realstarttime = this.timeA[0].trim() + ":" + this.timeA[1].trim();
        } catch(ArrayIndexOutOfBoundsException e){ }

        this.timeA = t_end.split(":");
        try{
            realendtime = this.timeA[0].trim() + this.timeA[1].trim();
            this.endtime = Integer.parseInt(realendtime);
            realendtime = this.timeA[0].trim() + ":" + this.timeA[1].trim();
        } catch(ArrayIndexOutOfBoundsException e){ }
    }

    public String getDate() {
        return date;
    }

    public String getDay() {
        return day;
    }

    public String getSubject() {
        return subject;
    }

    public String getSec() {
        return sec;
    }

    public String getDepart() {
        return depart;
    }

    public String getRoom() {
        return room;
    }

    public int getStarttime() {
        return starttime;
    }

    public int getEndtime() {
        return endtime;
    }

    public String getRealStartTime() {
        return realstarttime;
    }

    public String getRealEndTime() {
        return realendtime;
    }

    public boolean getStateCE1() {
        return control1.equals("");
    }

    public boolean getStateCE2() {
        return control2.equals("");
    }

    public boolean getStateCE3() {
        return control3.equals("");
    }

    public String getControl1() {
        return control1;
    }

    public String getControl2() {
        return control2;
    }

    public String getControl3() {
        return control3;
    }

    public void setControlExam1(String ce_code) {
        this.control1 = ce_code;
    }

    public void setControlExam2(String ce_code) {
        this.control2 = ce_code;
    }

    public void setControlExam3(String ce_code) {
        this.control3 = ce_code;
    }
}

Control_Exam.java //在我的包中

public class Control_Exam {
    private String code,name,department,room,build;
    private int sunday,monday,tuesday,wednesday,thursday,friday,saturday;

    public Control_Exam(){
    }

    public Control_Exam(String code,String name,String depart,String room,
            int sun,int mon,int tue,int wed,int thu,int fri,
            int sat){
        this.code = code;
        this.name = name;
        this.department = depart;
        this.room = room;
        this.build = room.substring(0,room.indexOf("-")).trim();
        this.sunday = sun;
        this.monday = mon;
        this.tuesday = tue;
        this.wednesday = wed;
        this.thursday = thu;
        this.friday = fri;
        this.saturday = sat;
    }

    public void addData(Control_Exam Lock){
        this.code = Lock.getCode();
        this.name = Lock.getName();
        this.department = Lock.getDepart();
        this.room = Lock.getRoom();
        this.build = Lock.getBuild();
        this.sunday = Lock.getValueofDay("Sun");
        this.monday = Lock.getValueofDay("Mon");
        this.tuesday = Lock.getValueofDay("Tue");
        this.wednesday = Lock.getValueofDay("Wed");
        this.thursday = Lock.getValueofDay("Thu");
        this.friday = Lock.getValueofDay("Fri");
        this.saturday = Lock.getValueofDay("Sat");
    }

    public String getCode() {
        return code;
    }

    public String getName() {
        return name;
    }

    public String getDepart() {
        return department;
    }

    public String getRoom() {
        return room;
    }

    public String getBuild() {
        return build;
    }

    public int getValueofDay(String day){
        if(day.equals("Sun"))
            return sunday;
        else if(day.equals("Mon"))
            return monday;
        else if(day.equals("Tue"))
            return tuesday;
        else if(day.equals("Wed"))
            return wednesday;
        else if(day.equals("Thu"))
            return thursday;
        else if(day.equals("Fri"))
            return friday;
        else if(day.equals("Sat"))
            return saturday;
        else
            return 0;
    }
}

Rules_Schedule.drl

import schedule.Schedule
import schedule.Control_Exam
rule "Schedule test 01"
    salience 2
    when
        Schedule( $scheduleCheck1 : control1 , $scheduleCheck2 : control2 , $scheduleCheck3 : control3 )
        $controlExamCheck1 : Control_Exam( ) from accumulate( $controlExamCheck2 : Control_Exam( $scheduleCheck1 == code || $scheduleCheck2 == code || $scheduleCheck3 == code ),
                              init( Control_Exam CEL = new Control_Exam(); ),
                              action( CEL.addData($controlExamCheck2); ),
                              result( CEL ) )
        $controlExamCheck3 : Control_Exam( ) from Control_Exam( code != $controlExamCheck1.getCode() ) 
    then
        System.out.println(" Test EC3 : " + $controlExamCheck3.getCode() );
end

看看这有两个结果。

第一个结果没有“RLC”。

第二个结果没有“DCK”。

但我想 Control_Exam 放在一起(Sum / Union /类似的东西......)。

联盟首先来自 Control_Exam 时间表,并找到不在 Control_Exam agian中的结果。

**结果:**

 Test EC3 : PTN
 Test EC3 : PTN
 Test EC3 : RCT
 Test EC3 : RCT
 Test EC3 : TRK
 Test EC3 : TRK
 Test EC3 : PST
 Test EC3 : PST
 Test EC3 : NSV
 Test EC3 : NSV
 Test EC3 : NBD
 Test EC3 : NBD
 Test EC3 : NPY
 Test EC3 : NPY
 Test EC3 : SPJ
 Test EC3 : SPJ
 Test EC3 : PTA
 Test EC3 : PTA
 Test EC3 : OPT
 Test EC3 : OPT
 Test EC3 : SDN
 Test EC3 : SDN
 Test EC3 : SRH
 Test EC3 : SRH
 Test EC3 : SHG
 Test EC3 : SHG
 Test EC3 : RLC
 Test EC3 : KBP
 Test EC3 : KBP
 Test EC3 : SYS
 Test EC3 : SYS
 Test EC3 : CCK
 Test EC3 : CCK
 Test EC3 : SCV
 Test EC3 : SCV
 Test EC3 : CPR
 Test EC3 : CPR
 Test EC3 : DCK
 Test EC3 : PDD
 Test EC3 : PDD
 Test EC3 : SAK
 Test EC3 : SAK

示例:http://i.stack.imgur.com/83GJ1.png

1 个答案:

答案 0 :(得分:0)

我不确定这与你的问题究竟是如何相关的,但我已经能够使用external function support为我的drools规则添加联合函数,例如union。

首先在java类中创建一个set union函数作为静态方法,并将其导入到drools文件中:

在SetFunctions.java中:

@SuppressWarnings({ "rawtypes", "unchecked" })
public static Set setUnion(Set setA, Set setB) {
    Set tmp = new HashSet(setA);
    tmp.addAll(setB);
    return tmp;
}

在.drl文件中:

import function org.test.rules.functions.SetFunctions.setUnion
rule 'Test'
  $factSet1 : java.util.Set() from collect(Fact(cond = test1))
  $factSet2 : java.util.Set() from collect(Fact(cond = test2))
  $combinedSet : java.util.Set() from collect(Fact() from setUnion($factSet1, $factSet2))
...
then
....
end