title: How to union result in drools
body: <p>I want to get one result only in drools.</p>
  
  <p>I tried to use <strong>"from"</strong> and <strong>"from accumulate"</strong> but It did not work.</p>
  
  <p><strong>Main.java</strong> // Main class</p>
  
  <pre><code>    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();
  </code></pre>
  
  <p><strong>Schedule.java</strong> // in my package; control1,control2,control3 store code from <strong>Control_Exam</strong></p>
  
  <pre><code>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;
      }
  }
  </code></pre>
  
  <p><strong>Control_Exam.java</strong> // in my package</p>
  
  <pre><code>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;
      }
  }
  </code></pre>
  
  <p><strong>Rules_Schedule.drl</strong></p>
  
  <pre><code>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
  </code></pre>
  
  <p>Look this had 2 result.</p>
  
  <p>First result don't have "RLC".</p>
  
  <p>Second result don't have "DCK".</p>
  
  <p>But I want to <strong>Control_Exam</strong> put together (Sum / Union / Something like that...).</p>
  
  <p>Union results from <strong>Schedule</strong> of <strong>Control_Exam</strong> first and find result not in <strong>Control_Exam</strong> agian.</p>
  
  <p>** Result : **</p>
  
  <pre><code> 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
  </code></pre>
  
  <p>Sample : <a href="http://i.stack.imgur.com/83GJ1.png" rel="nofollow">http://i.stack.imgur.com/83GJ1.png</a></p>