mysql - How to insert a lot of facts in StatefulKnowledgeSession (Drools) -
i want insert lot of facts in statefulknowledgesession.
but not reach fireallrules() part
my facts have 10k facts. in mysql.
http://i.stack.imgur.com/xbnoh.png
http://i.stack.imgur.com/7tjeu.png
and stale
my code :
public void rundrools() { knowledgebuilder kbuilder = knowledgebuilderfactory.newknowledgebuilder(); knowledgebase kbase = knowledgebasefactory.newknowledgebase(); starttime = new date(); if (personalcondition1.isselected() && convenienceday_jcheckbox.isselected()) { kbuilder.add(resourcefactory.newclasspathresource("rules1_conv_schedule.drl", getclass()), resourcetype.drl); } else if (personalcondition1.isselected() && !convenienceday_jcheckbox.isselected()) { kbuilder.add(resourcefactory.newclasspathresource("rules1_non-conv_schedule.drl", getclass()), resourcetype.drl); } else if (personalcondition2.isselected() && convenienceday_jcheckbox.isselected()) { kbuilder.add(resourcefactory.newclasspathresource("rules2_conv_schedule.drl", getclass()), resourcetype.drl); } else if (personalcondition2.isselected() && !convenienceday_jcheckbox.isselected()) { kbuilder.add(resourcefactory.newclasspathresource("rules2_non-conv_schedule.drl", getclass()), resourcetype.drl); } if (kbuilder.haserrors()) { system.err.println(kbuilder.geterrors().tostring()); } kbase.addknowledgepackages(kbuilder.getknowledgepackages()); statefulknowledgesession ksession = kbase.newstatefulknowledgesession(); boolean inbuild, nearbybuild, outbuild; if (nearbybuild1.isselected()) { inbuild = false; nearbybuild = true; outbuild = true; } else if (nearbybuild2.isselected()) { inbuild = true; nearbybuild = true; outbuild = true; } else { inbuild = false; nearbybuild = false; outbuild = true; } if (control3_jcheckbox.isselected()) { amount_test = (integer) amount_jspinner.getvalue(); } else { amount_test = 0; } check check = new check(amount_test, inbuild, nearbybuild, outbuild, status_jlabel); ksession.insert(check); data_list list = new data_list(); ksession.insert(list); if (control3_jcheckbox.isselected()) { amount_test = (integer) amount_jspinner.getvalue(); } else { amount_test = 2; } try { rs_ro = stm_ro.executequery("select * room"); while (rs_ro.next()) { string code = rs_ro.getstring("ro_code"), capa = rs_ro.getstring("ro_capacity"); room room = new room(code, integer.parseint(capa)); ksession.insert(room); } rs_sch_exam = stm_sch_exam.executequery("select *,date_format(sch_date,'%a') day schedule_exam order sch_date asc"); while (rs_sch_exam.next()) { string date = rs_sch_exam.getstring("sch_date"), day = rs_sch_exam.getstring("day"), start = rs_sch_exam.getstring("sch_start"), end = rs_sch_exam.getstring("sch_end"), subj = rs_sch_exam.getstring("sch_subject"), depa = rs_sch_exam.getstring("sch_depart"), sec = rs_sch_exam.getstring("sch_sec"), room = rs_sch_exam.getstring("sch_room"); schedule schedule = new schedule(date, day, subj, sec, depa, room, start, end); schedule.setconnect(gethost(), getuser(), getpass()); ksession.insert(schedule); } random rand = new random(); rs_peo = stm_peo.executequery("select * control_exam"); while (rs_peo.next()) { string code = rs_peo.getstring("ce_code"), 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"), max = rs_peo.getstring("ce_maxcontrol"); if (max.equals("")) { max = maxdays_jspinner.getvalue() + ""; } control_exam controlexam = new control_exam(code, depart, room, integer.parseint(sun), integer.parseint(mon), integer.parseint(tue), integer.parseint(wed), integer.parseint(thu), integer.parseint(fri), integer.parseint(sat), integer.parseint(max)); controlexam.setconnect(gethost(), getuser(), getpass()); ksession.insert(controlexam); } rs_sub = stm_sub.executequery("select * subject"); while (rs_sub.next()) { string code = rs_sub.getstring("su_code"), name = rs_sub.getstring("su_name"), educator = rs_sub.getstring("su_educator"); subject subject = new subject(code, name, educator); system.out.println(" subject:" + subject.getcode()); ksession.insert(subject); } rs_nea = stm_nea.executequery("select * nearby"); while (rs_nea.next()) { string build = rs_nea.getstring("nb_build"), near; if (nearbybuild1.isselected()) { near = rs_nea.getstring("nb_nearby") + "," + build; } else { near = rs_nea.getstring("nb_nearby"); } nearby nearby = new nearby(build, near); system.out.println(" nearby:" + nearby.getbuild()); ksession.insert(nearby); } databasetime = new date(); if ((((databasetime.gettime() - starttime.gettime()) / 1000) / 60) >= 1) { databasetime = mtime.format(databasetime.gettime() - starttime.gettime()) + " นาที"; } else { databasetime = mstime.format(databasetime.gettime() - starttime.gettime()) + " วินาที"; } ksession.fireallrules(); ksession.dispose(); } catch (exception e) { amount_test = -1; manage_schedule.this.dispose(); } }
simple rule (rules1_non-conv_schedule.drl) :
import schedule.schedule import schedule.nearby import schedule.room import schedule.subject import schedule.control_exam import schedule.check import schedule.data_list rule "add control exam first in build" salience 14 when $check : check( ) eval( $check.getinbuildstate() ) $schedulecheck : schedule( ) $controlexamcondition : control_exam( max > 0 ) $room : room( build == $controlexamcondition.getbuild() ) $subject : subject( educator1 not in( $controlexamcondition.getcode() ), educator2 not in( $controlexamcondition.getcode() ), educator3 not in( $controlexamcondition.getcode() ), educator4 not in( $controlexamcondition.getcode() ), educator5 not in( $controlexamcondition.getcode() ) ) $schedule : schedule( room == $room.getcode() , subject == $subject.getcode() , date == $schedulecheck.getdate() && starttime <= $schedulecheck.getstarttime() && endtime > $schedulecheck.getstarttime() ) $controlexamlist : data_list( ) accumulate( $schedulececheck : schedule( date == $schedulecheck.getdate() , starttime == $schedulecheck.getstarttime() || endtime == $schedulecheck.getstarttime() ) , init( data_list datalist = new data_list(); ), action( datalist.adddata($schedulececheck.getcontrol1()); datalist.adddata($schedulececheck.getcontrol2()); datalist.adddata($schedulececheck.getcontrol3()); ), result( datalist ) ) $controlexam : control_exam( code not memberof ( $controlexamlist.getarraycode() ) ) $controlexamcondition eval($schedule.getstateemptyce1()) modify( $schedule ) { setcontrolexam1( $controlexam.getcode() ) }; modify( $controlexam ) { checkmax( $schedule.getdate() ) }; $check.settextstate("เพิ่มผู้คุมสอบที่ 1: "+$controlexam.getcode() + " ในห้องสอบ: " + $schedule.getroom() + " วันที่: " + $schedule.getdate() + " เวลา: " + $schedule.getrealstarttime() + " - " + $schedule.getrealendtime()); end rule "add control exam first nearby build" salience 11 when $check : check( ) eval( $check.getnearbybuildstate() ) $schedulecheck : schedule( ) $near : nearby( ) $room : room( build == $near.getbuild() ) $controlexamcondition : control_exam( build memberof ( $near.getarraynearby() ) , max > 0 ) $subject : subject( educator1 not in( $controlexamcondition.getcode() ), educator2 not in( $controlexamcondition.getcode() ), educator3 not in( $controlexamcondition.getcode() ), educator4 not in( $controlexamcondition.getcode() ), educator5 not in( $controlexamcondition.getcode() ) ) $schedule : schedule( room == $room.getcode() , subject == $subject.getcode() , date == $schedulecheck.getdate() && starttime <= $schedulecheck.getstarttime() && endtime > $schedulecheck.getstarttime() ) $controlexamlist : data_list( ) accumulate( $schedulececheck : schedule( date == $schedulecheck.getdate() , starttime == $schedulecheck.getstarttime() || endtime == $schedulecheck.getstarttime() ) , init( data_list datalist = new data_list(); ), action( datalist.adddata($schedulececheck.getcontrol1()); datalist.adddata($schedulececheck.getcontrol2()); datalist.adddata($schedulececheck.getcontrol3()); ), result( datalist ) ) $controlexam : control_exam( code not memberof ( $controlexamlist.getarraycode() ) ) $controlexamcondition eval($schedule.getstateemptyce1()) modify( $schedule ) { setcontrolexam1( $controlexam.getcode() ) }; modify( $controlexam ) { checkmax( $schedule.getdate() ) }; $check.settextstate("เพิ่มผู้คุมสอบที่ 1: "+$controlexam.getcode() + " ในห้องสอบ: " + $schedule.getroom() + " วันที่: " + $schedule.getdate() + " เวลา: " + $schedule.getrealstarttime() + " - " + $schedule.getrealendtime()); end rule "add control exam first out build" salience 0 when $check : check( ) eval( $check.getoutbuildstate() ) $schedulecheck : schedule( ) $controlexamcondition : control_exam( max > 0 ) $subject : subject( educator1 not in( $controlexamcondition.getcode() ), educator2 not in( $controlexamcondition.getcode() ), educator3 not in( $controlexamcondition.getcode() ), educator4 not in( $controlexamcondition.getcode() ), educator5 not in( $controlexamcondition.getcode() ) ) $schedule : schedule( subject == $subject.getcode() , date == $schedulecheck.getdate() && starttime <= $schedulecheck.getstarttime() && endtime > $schedulecheck.getstarttime() ) $controlexamlist : data_list( ) accumulate( $schedulececheck : schedule( date == $schedulecheck.getdate() , starttime == $schedulecheck.getstarttime() || endtime == $schedulecheck.getstarttime() ) , init( data_list datalist = new data_list(); ), action( datalist.adddata($schedulececheck.getcontrol1()); datalist.adddata($schedulececheck.getcontrol2()); datalist.adddata($schedulececheck.getcontrol3()); ), result( datalist ) ) $controlexam : control_exam( code not memberof ( $controlexamlist.getarraycode() ) ) $controlexamcondition eval($schedule.getstateemptyce1()) modify( $schedule ) { setcontrolexam1( $controlexam.getcode() ) }; modify( $controlexam ) { checkmax( $schedule.getdate() ) }; $check.settextstate("เพิ่มผู้คุมสอบที่ 1: "+$controlexam.getcode() + " ในห้องสอบ: " + $schedule.getroom() + " วันที่: " + $schedule.getdate() + " เวลา: " + $schedule.getrealstarttime() + " - " + $schedule.getrealendtime()); end rule "add control exam second in build" salience 14 when $check : check( ) eval( $check.getinbuildstate() ) $schedulecheck : schedule( ) $controlexamcondition : control_exam( max > 0 ) $room : room( build == $controlexamcondition.getbuild() ) $subject : subject( educator1 not in( $controlexamcondition.getcode() ), educator2 not in( $controlexamcondition.getcode() ), educator3 not in( $controlexamcondition.getcode() ), educator4 not in( $controlexamcondition.getcode() ), educator5 not in( $controlexamcondition.getcode() ) ) $schedule : schedule( room == $room.getcode() , subject == $subject.getcode() , date == $schedulecheck.getdate() && starttime <= $schedulecheck.getstarttime() && endtime > $schedulecheck.getstarttime() ) $controlexamlist : data_list( ) accumulate( $schedulececheck : schedule( date == $schedulecheck.getdate() , starttime == $schedulecheck.getstarttime() || endtime == $schedulecheck.getstarttime() ) , init( data_list datalist = new data_list(); ), action( datalist.adddata($schedulececheck.getcontrol1()); datalist.adddata($schedulececheck.getcontrol2()); datalist.adddata($schedulececheck.getcontrol3()); ), result( datalist ) ) $controlexam : control_exam( code not memberof ( $controlexamlist.getarraycode() ) ) $controlexamcondition eval( !$schedule.getstateemptyce1() && $schedule.getstateemptyce2() ) modify( $schedule ) { setcontrolexam2( $controlexam.getcode() ) }; modify( $controlexam ) { checkmax( $schedule.getdate() ) }; $check.settextstate("เพิ่มผู้คุมสอบที่ 2: "+$controlexam.getcode() + " ในห้องสอบ: " + $schedule.getroom() + " วันที่: " + $schedule.getdate() + " เวลา: " + $schedule.getrealstarttime() + " - " + $schedule.getrealendtime()); end rule "add control exam second nearby build" salience 11 when $check : check( ) eval( $check.getnearbybuildstate() ) $schedulecheck : schedule( ) $near : nearby( ) $room : room( build == $near.getbuild() ) $controlexamcondition : control_exam( build memberof ( $near.getarraynearby() ) , max > 0 ) $subject : subject( educator1 not in( $controlexamcondition.getcode() ), educator2 not in( $controlexamcondition.getcode() ), educator3 not in( $controlexamcondition.getcode() ), educator4 not in( $controlexamcondition.getcode() ), educator5 not in( $controlexamcondition.getcode() ) ) $schedule : schedule( room == $room.getcode() , subject == $subject.getcode() , date == $schedulecheck.getdate() && starttime <= $schedulecheck.getstarttime() && endtime > $schedulecheck.getstarttime() ) $controlexamlist : data_list( ) accumulate( $schedulececheck : schedule( date == $schedulecheck.getdate() , starttime == $schedulecheck.getstarttime() || endtime == $schedulecheck.getstarttime() ) , init( data_list datalist = new data_list(); ), action( datalist.adddata($schedulececheck.getcontrol1()); datalist.adddata($schedulececheck.getcontrol2()); datalist.adddata($schedulececheck.getcontrol3()); ), result( datalist ) ) $controlexam : control_exam( code not memberof ( $controlexamlist.getarraycode() ) ) $controlexamcondition eval( !$schedule.getstateemptyce1() && $schedule.getstateemptyce2() ) modify( $schedule ) { setcontrolexam2( $controlexam.getcode() ) }; modify( $controlexam ) { checkmax( $schedule.getdate() ) }; $check.settextstate("เพิ่มผู้คุมสอบที่ 2: "+$controlexam.getcode() + " ในห้องสอบ: " + $schedule.getroom() + " วันที่: " + $schedule.getdate() + " เวลา: " + $schedule.getrealstarttime() + " - " + $schedule.getrealendtime()); end rule "add control exam second out build" salience 0 when $check : check( ) eval( $check.getoutbuildstate() ) $schedulecheck : schedule( ) $controlexamcondition : control_exam( max > 0 ) $subject : subject( educator1 not in( $controlexamcondition.getcode() ), educator2 not in( $controlexamcondition.getcode() ), educator3 not in( $controlexamcondition.getcode() ), educator4 not in( $controlexamcondition.getcode() ), educator5 not in( $controlexamcondition.getcode() ) ) $schedule : schedule( subject == $subject.getcode() , date == $schedulecheck.getdate() && starttime <= $schedulecheck.getstarttime() && endtime > $schedulecheck.getstarttime() ) $controlexamlist : data_list( ) accumulate( $schedulececheck : schedule( ) $schedulecheck , init( data_list datalist = new data_list(); ), action( datalist.adddata($schedulececheck.getcontrol1()); datalist.adddata($schedulececheck.getcontrol2()); datalist.adddata($schedulececheck.getcontrol3()); ), result( datalist ) ) $controlexam : control_exam( code not memberof ( $controlexamlist.getarraycode() ) ) $controlexamcondition eval( !$schedule.getstateemptyce1() && $schedule.getstateemptyce2() ) modify( $schedule ) { setcontrolexam2( $controlexam.getcode() ) }; modify( $controlexam ) { checkmax( $schedule.getdate() ) }; $check.settextstate("เพิ่มผู้คุมสอบที่ 2: "+$controlexam.getcode() + " ในห้องสอบ: " + $schedule.getroom() + " วันที่: " + $schedule.getdate() + " เวลา: " + $schedule.getrealstarttime() + " - " + $schedule.getrealendtime()); end rule "add control exam third in build" salience 14 when $check : check( ) eval( $check.getinbuildstate() && $check.getthirdcontrolstate() ) $schedulecheck : schedule( ) $controlexamcondition : control_exam( max > 0 ) $room : room( build == $controlexamcondition.getbuild() ) eval($room.getcapacity() > $check.getamountthirdcontrol()) $subject : subject( educator1 not in( $controlexamcondition.getcode() ), educator2 not in( $controlexamcondition.getcode() ), educator3 not in( $controlexamcondition.getcode() ), educator4 not in( $controlexamcondition.getcode() ), educator5 not in( $controlexamcondition.getcode() ) ) $schedule : schedule( room == $room.getcode() , subject == $subject.getcode() , date == $schedulecheck.getdate() && starttime <= $schedulecheck.getstarttime() && endtime > $schedulecheck.getstarttime() ) $controlexamlist : data_list( ) accumulate( $schedulececheck : schedule( date == $schedulecheck.getdate() , starttime == $schedulecheck.getstarttime() || endtime == $schedulecheck.getstarttime() ) , init( data_list datalist = new data_list(); ), action( datalist.adddata($schedulececheck.getcontrol1()); datalist.adddata($schedulececheck.getcontrol2()); datalist.adddata($schedulececheck.getcontrol3()); ), result( datalist ) ) $controlexam : control_exam( code not memberof ( $controlexamlist.getarraycode() ) ) $controlexamcondition eval( !$schedule.getstateemptyce1() && !$schedule.getstateemptyce2() && $schedule.getstateemptyce3() ) modify( $schedule ) { setcontrolexam3( $controlexam.getcode() ) }; modify( $controlexam ) { checkmax( $schedule.getdate() ) }; $check.settextstate("เพิ่มผู้คุมสอบที่ 3: "+$controlexam.getcode() + " ในห้องสอบ: " + $schedule.getroom() + " วันที่: " + $schedule.getdate() + " เวลา: " + $schedule.getrealstarttime() + " - " + $schedule.getrealendtime()); end rule "add control exam third nearby build" salience 11 when $check : check( ) eval( $check.getnearbybuildstate() && $check.getthirdcontrolstate() ) $schedulecheck : schedule( ) $near : nearby( ) $room : room( build == $near.getbuild() ) eval($room.getcapacity() > $check.getamountthirdcontrol()) $controlexamcondition : control_exam( build memberof ( $near.getarraynearby() ) , max > 0 ) $subject : subject( educator1 not in( $controlexamcondition.getcode() ), educator2 not in( $controlexamcondition.getcode() ), educator3 not in( $controlexamcondition.getcode() ), educator4 not in( $controlexamcondition.getcode() ), educator5 not in( $controlexamcondition.getcode() ) ) $schedule : schedule( room == $room.getcode() , subject == $subject.getcode() , date == $schedulecheck.getdate() && starttime <= $schedulecheck.getstarttime() && endtime > $schedulecheck.getstarttime() ) $controlexamlist : data_list( ) accumulate( $schedulececheck : schedule( date == $schedulecheck.getdate() , starttime == $schedulecheck.getstarttime() || endtime == $schedulecheck.getstarttime() ) , init( data_list datalist = new data_list(); ), action( datalist.adddata($schedulececheck.getcontrol1()); datalist.adddata($schedulececheck.getcontrol2()); datalist.adddata($schedulececheck.getcontrol3()); ), result( datalist ) ) $controlexam : control_exam( code not memberof ( $controlexamlist.getarraycode() ) ) $controlexamcondition eval( !$schedule.getstateemptyce1() && !$schedule.getstateemptyce2() && $schedule.getstateemptyce3() ) modify( $schedule ) { setcontrolexam3( $controlexam.getcode() ) }; modify( $controlexam ) { checkmax( $schedule.getdate() ) }; $check.settextstate("เพิ่มผู้คุมสอบที่ 3: "+$controlexam.getcode() + " ในห้องสอบ: " + $schedule.getroom() + " วันที่: " + $schedule.getdate() + " เวลา: " + $schedule.getrealstarttime() + " - " + $schedule.getrealendtime()); end rule "add control exam third out build" salience 0 when $check : check( ) eval( $check.getoutbuildstate() && $check.getthirdcontrolstate() ) $schedulecheck : schedule( ) $controlexamcondition : control_exam( max > 0 ) $room : room( ) eval($room.getcapacity() > $check.getamountthirdcontrol()) $subject : subject( educator1 not in( $controlexamcondition.getcode() ), educator2 not in( $controlexamcondition.getcode() ), educator3 not in( $controlexamcondition.getcode() ), educator4 not in( $controlexamcondition.getcode() ), educator5 not in( $controlexamcondition.getcode() ) ) $schedule : schedule( room == $room.getcode() , subject == $subject.getcode() , date == $schedulecheck.getdate() && starttime <= $schedulecheck.getstarttime() && endtime > $schedulecheck.getstarttime() ) $controlexamlist : data_list( ) accumulate( $schedulececheck : schedule( date == $schedulecheck.getdate() , starttime == $schedulecheck.getstarttime() || endtime == $schedulecheck.getstarttime() ) , init( data_list datalist = new data_list(); ), action( datalist.adddata($schedulececheck.getcontrol1()); datalist.adddata($schedulececheck.getcontrol2()); datalist.adddata($schedulececheck.getcontrol3()); ), result( datalist ) ) $controlexam : control_exam( code not memberof ( $controlexamlist.getarraycode() ) ) $controlexamcondition eval( !$schedule.getstateemptyce1() && !$schedule.getstateemptyce2() && $schedule.getstateemptyce3() ) modify( $schedule ) { setcontrolexam3( $controlexam.getcode() ) }; modify( $controlexam ) { checkmax( $schedule.getdate() ) }; $check.settextstate("เพิ่มผู้คุมสอบที่ 3: "+$controlexam.getcode() + " ในห้องสอบ: " + $schedule.getroom() + " วันที่: " + $schedule.getdate() + " เวลา: " + $schedule.getrealstarttime() + " - " + $schedule.getrealendtime()); end
sorry english bad.
for me looks not drools problem.
you running out of memory because have huge number of objects in project. in case have on 10k facts, create more 10k objects because system.out.println(new date().tolocalestring().....
logs. remember string not primitiv datatyp.
you have tweak code , increase maximum heap size of jvm (default around 64mb).
Comments
Post a Comment