-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathTestWrapper.java
2097 lines (1809 loc) · 106 KB
/
TestWrapper.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*current error CREATE TABLE unielwin_target_final_CT.`diff(course0)_CT` like unielwin_target_db.`local_CT`;
Table 'unielwin_target_db.local_CT' doesn't exist*/
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException;
public class TestWrapper {
static Connection con0, con1, conFinal,con_preprocess;
static String databaseName, databaseName1, databaseName2, databaseName3;
// unielwin, unielwin_target, unielwin_target_setup, unielwin_target_final_CT,
static String databaseName4, databaseName5, databaseName6;
// unielwin_target_CT, unielwin_target_final, unielwin_target_db
//databaseName4 is not used
//databaseName6 is used but not created. Used in buildsubct (todo??)
static String dbUsername;
static String dbPassword;
static String dbaddress;
static int groundingCount;
static String functorId;
static boolean processingRNode = false;
static boolean tableNameIsTooLong = false;
static boolean CrossValidation;
// Global variable: true to use nodes in Markov Blanket, false to use only functor's parents and associated RNodes
static boolean useMarkovBlanket = true;
static ArrayList<String> CurrendID_List = new ArrayList<String>();
static String current_id1="";
static String functorId_In_BN="";
public static void main(String[] args) throws Exception
{
long time1=System.currentTimeMillis();
/*
*Target Database Setup (i.e. Testing Database)
* @database@_target_setup, @database@_target
*
*/
MakeTargetSetup.runMakeTargetSetup();
/*
*Read configure file for TestWrapper
*/
setVarsFromConfig();
/*
* drop the `mult` column in @database@_BN.*_cp tables
* remapping the rnid with orig_rnid, e.g. a --> RA(prof0,student0)
* */
pre_process( );
connectDBTargetSetup();
connectDB_preprocess();
/* get the each functor, e.g. @database@_BN.FNodes
* */
ArrayList<String> functors = GetFunctors();
Statement st = con0.createStatement(); // connect to @database@_target_setup
Statement st1 = con_preprocess.createStatement(); // connect to @database@_bn
st.execute( "DROP SCHEMA IF EXISTS " + databaseName3 + ";" ); //@database@_target_final_CT
st.execute( "CREATE SCHEMA " + databaseName3 + ";" );
st.execute( "DROP SCHEMA IF EXISTS " + databaseName5 + ";" ); //@database@_target_final
st.execute( "CREATE SCHEMA " + databaseName5 + ";" );
connectDBTargetFinalCT();
connectDBTargetFinal();
functorId = "";
String node = "";
String table = "";
if ( useMarkovBlanket ) {
node = "TargetMBNode";
table = "TargetMB";
}
else{
node = "TargetParent";
table = "TargetParents";
}
//functorId = "`teachingability(prof0)`";//zqian April 10th
//functorId= "`intelligence(student0)`";
//functorId= "`b`";
//functorId= "`a`";
//functorId = "`rating(course0)`";
//functorId ="`diff(course0)`";
//functorId ="`ranking(student0)`";// no rchain
functorId="";
for ( int i = 0; i < functors.size(); i++ ) //zqian April 10th
{
tableNameIsTooLong=false;
String target_common_select_string = ProcessTargetParent(st,st1,functors,node,table,i);
// Aug. 20, compute the subCTs for target and its children
long ctt2 = System.currentTimeMillis();
ProcessTargetChildren(st,st1,functors,node,table,i,target_common_select_string);
/********* processing target's children**************END*****************/
long ctt3 = System.currentTimeMillis();
// System.out.println( "\n extract the target_parent_ct and target_child_ct, run time is: " + ( ctt3 - ctt2 ) + "ms. \n ******************************** \n\n" );
if (!tableNameIsTooLong)
{
//&&&&&&&&&&&&&&
/* Extend the final_CT tables for each functorId with all possible values */
Extend_Final_CT();
long ctt4 = System.currentTimeMillis();
System.out.println( "\n Extend the final_CT tables with all possible values, run time is: " + ( ctt4 - ctt3 ) + "ms. \n ******************************** \n\n" );
// remove the n/a ??
Update_CT(CurrendID_List,current_id1 );
long ctt14 = System.currentTimeMillis();
System.out.println( "\n Computing the Frequency, run time is: " + ( ctt14 - ctt4 ) + "ms. \n ******************************** \n\n" );
// Create *_final table, target_parent/target_child_final
//createSubFinal_zqian( functorArgs ); //using natural join,
//create the target_sum by summation
//compute the probability only for entries with the highest score and store them in target_score table, April 30
//createFinal_zqian( functorArgs );
// update the null value to 0, May 7th, zqian
// June 30, 2014, some weights are positive? `intelligence(student0)`, double check
createSubFinal2_zqian(functorId_In_BN,current_id1 ); // using natural right join
long ctt5 = System.currentTimeMillis();
System.out.println( "\n create final tables for each final_CT, run time is: " + ( ctt5 - ctt14 ) + "ms. \n ******************************** \n\n" );
//createFinal2_zqian( functorArgs ); // set null to 0
createFinal3_zqian( CurrendID_List ); // set null to -50
long ctt6 = System.currentTimeMillis();
System.out.println( "\n create Score tables , run time is: " + ( ctt6 - ctt5 ) + "ms. \n ******************************** \n\n" );
/* prepare for the performance analysis, July 9th 2014, zqian
* to do:
* 1. extract the primary keys, target node from @database@final_CT.target_CT into new table target_True
* i.e. extract `student_id(student0)`,`intelligence(student0)` from unielwin_traning1_final_CT.`intelligence(student0)_CT`
* to create table unielwin_traning1_final_CT.`intelligence(student0)_True`
* 2. do the natural join
* with @[email protected]_Score ?
* */
/*may Not always be consistent, some could have duplicated value, Sep. 29, 2014 */
//Extract_Target_True_CT_zqian( functorId_In_BN, current_id1 );
}
// Cleanup for next functor
st.execute( "DROP TABLE 1Nodes;" );
st.execute( "DROP TABLE 2Nodes;" );
st.execute( "DROP TABLE RNodes;" );
}
st.close();
disconnectDBTargetFinalCT();
disconnectDB();
disconnectDBFinal();
disconnectDB_preprocess();
long time2=System.currentTimeMillis();
System.out.println( "Total TestWrapper run time: " + ( time2 - time1 ) +"ms." );
}
public static String ProcessTargetParent(Statement st,Statement st1, ArrayList<String> functors,String node,String table, int i ) throws Exception {
/* Create Markov Blanket in @database@_BN for each functor, keep consistency */
MarkovBlanket.runMakeMarkovBlanket();
functorId = functors.get(i);
/* Store functorId in @database@_BN */
/*so why didn't you just pass a functorID? Vidhi June 16 2017 */
functorId_In_BN=functorId;
// Get pvars for functor, e.g. student0_counts */
ArrayList<String> functorArgs = GetFunctorArgs( functorId );
/* Store the associated primary keys for each functor: e.g. course_id(course0) */
//ArrayList<String>
CurrendID_List = new ArrayList<String>();
System.out.println("\n**********\nFunctor: '"+functorId+"'");
/* @database@_target_setup*/
st.execute( "DROP TABLE IF EXISTS 1Nodes,2Nodes,RNodes;" );
// 1Nodes
System.out.println( "setup 1node: \n CREATE TABLE if not exists 1Nodes AS SELECT * FROM " + databaseName + "_BN.1Nodes WHERE 1nid IN (SELECT " +
node + " FROM " + databaseName + "_BN." + table + " WHERE TargetNode = '" + functorId + "') or 1nid='" + functorId + "';" );
st.execute( "CREATE TABLE if not exists 1Nodes AS SELECT * FROM " + databaseName + "_BN.1Nodes WHERE 1nid IN (SELECT " +
node + " FROM " + databaseName + "_BN." + table + " WHERE TargetNode = '" + functorId + "') or 1nid='" + functorId + "';" );
// 2Nodes
System.out.println( "CREATE TABLE if not exists 2Nodes AS SELECT * FROM " + databaseName + "_BN.2Nodes WHERE 2nid IN (SELECT " +
node + " FROM " + databaseName + "_BN." + table + " WHERE TargetNode = '" + functorId + "') or 2nid='" + functorId + "';" );
st.execute( "CREATE TABLE if not exists 2Nodes AS SELECT * FROM " + databaseName + "_BN.2Nodes WHERE 2nid IN (SELECT " +
node + " FROM " + databaseName + "_BN." + table + " WHERE TargetNode = '" + functorId + "') or 2nid='" + functorId + "';" );
// RNodes
System.out.println( "CREATE TABLE if not exists RNodes AS SELECT * FROM " + databaseName + "_BN.RNodes WHERE rnid IN (SELECT " +
node + " FROM " + databaseName + "_BN." + table + " WHERE TargetNode = '" + functorId + "') or rnid " +
"IN (SELECT rnid FROM " + databaseName + "_BN.RNodes_2Nodes WHERE 2nid IN (SELECT 2nid FROM " +
databaseName + "_target_setup.2Nodes)) or rnid='" + functorId + "';" );
st.execute( "CREATE TABLE if not exists RNodes AS SELECT * FROM " + databaseName + "_BN.RNodes WHERE rnid IN (SELECT " +
node + " FROM " + databaseName + "_BN." + table + " WHERE TargetNode = '" + functorId + "') or rnid " +
"IN (SELECT rnid FROM " + databaseName + "_BN.RNodes_2Nodes WHERE 2nid IN (SELECT 2nid FROM " +
databaseName + "_target_setup.2Nodes)) or rnid='" + functorId + "';" );
/* Store functor for fast grounding processing */
st.execute( "DROP TABLE IF EXISTS Test_Node;" );
st.execute( "CREATE TABLE `Test_Node` ( `FID` varchar(199) NOT NULL, PRIMARY KEY (`FID`)) ENGINE=InnoDB DEFAULT CHARSET=latin1;" );
st.execute(" INSERT INTO `Test_Node` (`FID`) VALUES('"+functorId+"') ;");
long ctt1 = System.currentTimeMillis();
System.out.println( "\n\n*****\nEntering BayesBaseCT_SortMerge.buildCTTarget() for target and its parents ");
// int max = BayesBaseCT_SortMerge.buildCTTarget();
// to do : generate local ct based on the associated columns, Aug. 8th, 2014, zqian
int max = BayesBaseCT_SortMerge.buildSubCTTarget(functorId,databaseName+"_BN",databaseName1,databaseName1+"_BN",databaseName6);
/*the main hard work */
long ctt2 = System.currentTimeMillis();
System.out.println( "\nBayesBaseCT_SortMerge.buildCTTarget() for target and its parents run time is: " + ( ctt2 - ctt1 ) + "ms. \n ******************************** \n\n" );
/* replace rnid with orig_rnid, be consistent with ct and cp tables for weight learning, June 24, 2014 zqian */
st1.execute("update `TargetParents`,`RNodes` set `TargetParents`.TargetNode = `RNodes`.orig_rnid where `TargetParents`.TargetNode = `RNodes`.rnid; ");
st1.execute("update `TargetParents`,`RNodes` set `TargetParents`.TargetParent = `RNodes`.orig_rnid where `TargetParents`.TargetParent = `RNodes`.rnid; ");
st1.execute("update `TargetChildren`, `RNodes` set `TargetChildren`.TargetNode = `RNodes`.orig_rnid where `TargetChildren`.TargetNode = `RNodes`.rnid; ");
st1.execute("update `TargetChildren`, `RNodes` set `TargetChildren`.TargetChild = `RNodes`.orig_rnid where `TargetChildren`.TargetChild = `RNodes`.rnid; ");
st1.execute("update `TargetChildrensParents`, `RNodes` set `TargetChildrensParents`.TargetNode = `RNodes`.orig_rnid where `TargetChildrensParents`.TargetNode = `RNodes`.rnid; ");
st1.execute("update `TargetChildrensParents`, `RNodes` set `TargetChildrensParents`.TargetChildParent = `RNodes`.orig_rnid where `TargetChildrensParents`.TargetChildParent = `RNodes`.rnid; ");
st1.execute("update `TargetMB`, `RNodes` set `TargetMB`.TargetNode = `RNodes`.orig_rnid where `TargetMB`.TargetNode = `RNodes`.rnid; ");
st1.execute("update `TargetMB`, `RNodes` set `TargetMB`.TargetMBNode = `RNodes`.orig_rnid where `TargetMB`.TargetMBNode = `RNodes`.rnid; ");
/* find the corresponding ct tables in @database@_target_ct database*/
String rchainQuery = "SELECT name FROM " + databaseName1 + "_BN.lattice_set WHERE length = " + max + ";"; //@database@_target
ResultSet rsRchain = st.executeQuery( rchainQuery );
String rchain = "";
boolean rchainExists = false;
if ( rsRchain.absolute(1)){
rchainExists = true;
rchain = rsRchain.getString(1);
}
rsRchain.close();
// make rnid be consistent between the training and testing/target database
if ( rchainExists ){
System.out.println( "biggest Rchain in testing database : "+rchain +" for Functor: '"+functorId+"'");
ResultSet rNodeName_t = st.executeQuery( "SELECT orig_rnid FROM " + databaseName +"_BN.RNodes WHERE rnid = '" + functorId + "';" ); // `b`
if ( rNodeName_t.absolute( 1 ) ){
functorId = rNodeName_t.getString(1); //`registration(course0,student0)`
}
rNodeName_t.close();
/*copy table from @database@_target_ct to @database@_target_final_ct*/
st.execute("DROP TABLE IF EXISTS "+ databaseName3 +".`"+ functorId.replace("`","")+"_CT` ;"); //@database@_target_final_ct
//System.out.println( "CREATE TABLE " + databaseName3 +".`"+ functorId.replace("`","")+"_CT` as select * from "+ databaseName4+".`"+rchain.replace("`","")+"_CT`;" );
// st.execute( "CREATE TABLE " + databaseName3 +".`"+ functorId.replace("`","")+"_CT` as "
// + " select * from "+ databaseName4+".`"+rchain.replace("`","")+"_CT`;" ); //@database@_target_ct
//also copy the index, July 17,2014, zqian
// System.out.println( "CREATE TABLE " + databaseName3 +".`"+ functorId.replace("`","")+"_CT` like " + databaseName6+".`"+rchain.replace("`","")+"_CT`;" );
// System.out.println( "insert " + databaseName3 +".`"+ functorId.replace("`","")+"_CT` select * from "+ databaseName6+".`"+rchain.replace("`","")+"_CT`;" );
// st.execute( "CREATE TABLE " + databaseName3 +".`"+ functorId.replace("`","")+"_CT` like " + databaseName6+".`"+rchain.replace("`","")+"_CT`;" );
// st.execute( "insert " + databaseName3 +".`"+ functorId.replace("`","")+"_CT` select * from "+ databaseName6+".`"+rchain.replace("`","")+"_CT`;" ); //@database@_target_ct
//
System.out.println( "CREATE TABLE " + databaseName3 +".`"+ functorId.replace("`","")+"_CT` like " + databaseName6+".`local_CT`;" );
System.out.println( "insert " + databaseName3 +".`"+ functorId.replace("`","")+"_CT` select * from "+ databaseName6+".`local_CT`;" );
st.execute( "CREATE TABLE " + databaseName3 +".`"+ functorId.replace("`","")+"_CT` like " + databaseName6+".`local_CT`;" );
st.execute( "insert " + databaseName3 +".`"+ functorId.replace("`","")+"_CT` select * from "+ databaseName6+".`local_CT`;" ); //@database@_target_ct
/* get the mapping of RNodes in @database@_target_BN */
ResultSet rNodes = st.executeQuery( "SELECT orig_rnid, rnid FROM " + databaseName1 + "_BN.RNodes;" ); //@database@_target
ArrayList<String> rnids = new ArrayList<String>(); // `a`
ArrayList<String> origrnids = new ArrayList<String>(); //`RA(pfrof0,student0)`
while ( rNodes.next() ) {
origrnids.add( rNodes.getString(1));
rnids.add( rNodes.getString(2));
}
rNodes.close();
for ( int j = 0; j < rnids.size(); j++ ) {
System.out.println( "SHOW COLUMNS FROM " + databaseName3 + ".`" +functorId.replace("`","")+"_CT` WHERE Field = '"
+rnids.get(j).replace("`", "") + "';" );
ResultSet rsTypes = st.executeQuery( "SHOW COLUMNS FROM " + databaseName3 + ".`" +functorId.replace("`","")+"_CT` WHERE Field = '"
+rnids.get(j).replace("`", "") + "';" );
if ( !rsTypes.absolute(1) ) {//zqian, no rnode in the header, so do not need to replace
System.out.println( "NO Need to do the mapping for table `" +functorId.replace("`","")+"_CT` ;" );
rsTypes.close();
continue;
}
String type = rsTypes.getString(2);
rsTypes.close();
System.out.println( "ALTER TABLE " + databaseName3 + ".`" +functorId.replace("`","")+"_CT` CHANGE COLUMN "
+ rnids.get(j) + " " + origrnids.get(j) + " " + type + ";" );
st.execute( "ALTER TABLE " + databaseName3 + ".`" +functorId.replace("`","")+"_CT` CHANGE COLUMN "
+ rnids.get(j) + " " + origrnids.get(j) + " " + type + ";" );
}
}
else{
System.out.println( "NO Rchain for Functor: '"+functorId+"'");
//copy _counts table from @database@_target_ct to @database@_target_final_ct
st.execute("DROP TABLE IF EXISTS "+ databaseName3 +".`"+ functorId.replace("`","")+"_CT` ;"); //@database@_target_final_ct
// System.out.println( "CREATE TABLE " + databaseName3 +".`"+ functorId.replace("`","")+"_CT` as select * from "
// + databaseName4+".`"+functorArgs.get(0).replace("`","")+"_counts`;" );
// st.execute( "CREATE TABLE " + databaseName3 +".`"+ functorId.replace("`","")+"_CT` as select * from "
// + databaseName4+".`"+functorArgs.get(0).replace("`","")+"_counts`;" );
// System.out.println( "CREATE TABLE " + databaseName3 +".`"+ functorId.replace("`","")+"_CT` like " + databaseName4+".`"+functorArgs.get(0).replace("`","")+"_counts`;" );
// System.out.println(" insert " + databaseName3 +".`"+ functorId.replace("`","")+"_CT` select * from " + databaseName4+".`"+functorArgs.get(0).replace("`","")+"_counts`;" );
// st.execute( "CREATE TABLE " + databaseName3 +".`"+ functorId.replace("`","")+"_CT` like " + databaseName4+".`"+functorArgs.get(0).replace("`","")+"_counts`;" );
// st.execute("insert " + databaseName3 +".`"+ functorId.replace("`","")+"_CT` select * from " + databaseName4+".`"+functorArgs.get(0).replace("`","")+"_counts`;" );
//
System.out.println( "CREATE TABLE " + databaseName3 +".`"+ functorId.replace("`","")+"_CT` like " + databaseName6+".`local_CT`;" );
System.out.println( "insert " + databaseName3 +".`"+ functorId.replace("`","")+"_CT` select * from "+ databaseName6+".`local_CT`;" );
st.execute( "CREATE TABLE " + databaseName3 +".`"+ functorId.replace("`","")+"_CT` like " + databaseName6+".`local_CT`;" );
st.execute( "insert " + databaseName3 +".`"+ functorId.replace("`","")+"_CT` select * from "+ databaseName6+".`local_CT`;" ); //@database@_target_ct
}
// Aug. 20, compute the subCTs for target and its parents
/********* processing target's parents***************Begin****************/
// June 24 2014
String current_id="";
current_id1="";
String target_common_select_string ="";
//should contain, current and its parents
String target_parent_select_string= "";
int target_primaykey_counts=0;
String target_primaykey_counts_query ="select count(distinct 1nid) from "+databaseName1+"_BN.Test_1nid ;"; //functorId in unielwin_target_BN
//System.out.println( "target_primaykey_counts_query : "+ target_primaykey_counts_query );
Statement st_temp = con_preprocess.createStatement();
ResultSet rstarget_primaykey_counts = st_temp.executeQuery( target_primaykey_counts_query );
if ( rstarget_primaykey_counts.absolute( 1 ) ) {
target_primaykey_counts = rstarget_primaykey_counts.getInt(1); // counts
}
rstarget_primaykey_counts.close();
System.out.println( " target_primaykey_counts : "+ target_primaykey_counts +" : " );
String target_primaykey_query ="select distinct 1nid from "+databaseName1+"_BN.Test_1nid ;"; //`course_id(course0)`
//System.out.println( "zqian : "+ target_primaykey_query );
Statement st_te = con_preprocess.createStatement();
ResultSet rstarget_primaykey = st_te.executeQuery( target_primaykey_query );
while ( rstarget_primaykey.next() && target_primaykey_counts !=0 ) {
current_id= rstarget_primaykey.getString(1);
System.out.print(current_id);
CurrendID_List.add(rstarget_primaykey.getString(1));
current_id1 += current_id+ " ,"; // all the primary keys
target_parent_select_string += current_id + " ,";
}
System.out.println();
target_parent_select_string += " mult, "+ functorId + " ,";
target_common_select_string = target_parent_select_string;
//concat each parent of the target
int target_parents_counts=0;
String target_parents_counts_query ="SELECT count(TargetParent) FROM "+databaseName+"_BN.TargetParents where TargetNode ='"+functorId+"' ;"; //functorId in unielwin_BN
//System.out.println( "zqian : "+ target_parents_counts_query );
Statement st_temp1 = con0.createStatement();
ResultSet rstarget_parents_counts = st_temp1.executeQuery( target_parents_counts_query );
if ( rstarget_parents_counts.absolute( 1 ) ){
target_parents_counts = rstarget_parents_counts.getInt(1); // counts
}
rstarget_parents_counts.close();
System.out.println( " target_parents_counts: "+ target_parents_counts );
String target_parents_query ="SELECT TargetParent FROM "+databaseName+"_BN.TargetParents where TargetNode ='"+functorId+"' ;"; //functorId in unielwin_BN
//System.out.println( "zqian : "+ target_parents_query );
Statement st_tem = con0.createStatement();
ResultSet rstarget_parents = st_tem.executeQuery( target_parents_query );
// zqian: concat each parent of the target
while ( rstarget_parents.next() && target_parents_counts !=0 ){
String current= rstarget_parents.getString(1);
//System.out.println("zqian: "+current+";");
target_parent_select_string += current + " ,";
}
// for some target node that does not have any parents
if (target_parents_counts == 0){
System.out.println("zqian: NO parents for target "+ functorId );
//target_parent_select_string = "";
}
String create_string = "create table "+databaseName3+".`"+functorId.replace("`", "")+"_parent_final_CT` as select "
+ target_parent_select_string.substring(0, target_parent_select_string.lastIndexOf(",")-1)
+ " From "+databaseName3+".`"+functorId.replace("`", "")+"_CT` ;" ;
System.out.println (" create_target_parent_final_ct_string: "+ create_string );
st.execute("Drop table if exists "+databaseName3+".`"+functorId.replace("`", "")+"_parent_final_CT` ;" );
st.execute(create_string );
/********* processing target's parents******************END*************/
long ctt3 = System.currentTimeMillis();
System.out.println( "\n extract the target_parent_ct, run time is: " + ( ctt3 - ctt2 ) + "ms. \n ******************************** \n\n" );
return target_common_select_string;
}
public static void ProcessTargetChildren( Statement st,Statement st1, ArrayList<String> functors,String node,String table, int i,String target_common_select_string ) throws Exception {
/* Create Markov Blanket in @database@_BN for each functor, keep consistency */
MarkovBlanket.runMakeMarkovBlanket();
/********* processing target's children*****************Begin**************/
//--if target is rnode then have to rule out the associated 2node --// April 25
String target_children ="SELECT TargetChild FROM "+databaseName+"_BN.TargetChildren where TargetNode ='"+functorId_In_BN+"' "
+ " and (TargetChild) not in ( SELECT 2nid FROM "
+ databaseName+"_BN.RNodes_2Nodes where rnid = '"+functorId_In_BN+"' );"; //functorId in unielwin_BN
System.out.println( "zqian : "+ target_children );
Statement s_temp = con0.createStatement();
ResultSet rstarget_children = s_temp.executeQuery( target_children );
// begin while 1
while ( rstarget_children.next() ){
String current= rstarget_children.getString(1);
String current_In_BN=current;
System.out.println(" Target Child : "+current+" current_In_BN : "+ current_In_BN);
ResultSet rNodeName_tt = st.executeQuery( "SELECT orig_rnid FROM " + databaseName +"_BN.RNodes WHERE rnid = '" + current + "';" ); // `b`
if ( rNodeName_tt.absolute( 1 ) ){
current = rNodeName_tt.getString(1); //`registration(course0,student0)`
}
rNodeName_tt.close();
System.out.println(" Target Child : "+current+" current_In_BN : "+ current_In_BN);
/* Create Markov Blanket in @database@_BN for each functor, keep consistency */
MarkovBlanket.runMakeMarkovBlanket();
functorId = functors.get(i);
/* Store functorId in @database@_BN */
functorId_In_BN=functorId;
// Get pvars for functor, e.g. student0_counts */
ArrayList<String> functorArgs = GetFunctorArgs( functorId );
/* Store the associated primary keys for each functor: e.g. course_id(course0) */
//ArrayList<String> CurrendID_List = new ArrayList<String>();
//CurrendID_List = new ArrayList<String>();
System.out.println("\n**********\nFunctor: '"+functorId+"'");
/* @database@_target_setup*/
st.execute( "DROP TABLE IF EXISTS 1Nodes,2Nodes,RNodes;" );
// 1Nodes
System.out.println( "setup 1node: \n CREATE TABLE if not exists 1Nodes AS SELECT * FROM " + databaseName + "_BN.1Nodes WHERE 1nid IN (SELECT " +
node + " FROM " + databaseName + "_BN." + table + " WHERE TargetNode = '" + functorId + "') or 1nid='" + functorId + "';" );
st.execute( "CREATE TABLE if not exists 1Nodes AS SELECT * FROM " + databaseName + "_BN.1Nodes WHERE 1nid IN (SELECT " +
node + " FROM " + databaseName + "_BN." + table + " WHERE TargetNode = '" + functorId + "') or 1nid='" + functorId + "';" );
// 2Nodes
System.out.println( "CREATE TABLE if not exists 2Nodes AS SELECT * FROM " + databaseName + "_BN.2Nodes WHERE 2nid IN (SELECT " +
node + " FROM " + databaseName + "_BN." + table + " WHERE TargetNode = '" + functorId + "') or 2nid='" + functorId + "';" );
st.execute( "CREATE TABLE if not exists 2Nodes AS SELECT * FROM " + databaseName + "_BN.2Nodes WHERE 2nid IN (SELECT " +
node + " FROM " + databaseName + "_BN." + table + " WHERE TargetNode = '" + functorId + "') or 2nid='" + functorId + "';" );
// RNodes
System.out.println( "CREATE TABLE if not exists RNodes AS SELECT * FROM " + databaseName + "_BN.RNodes WHERE rnid IN (SELECT " +
node + " FROM " + databaseName + "_BN." + table + " WHERE TargetNode = '" + functorId + "') or rnid " +
"IN (SELECT rnid FROM " + databaseName + "_BN.RNodes_2Nodes WHERE 2nid IN (SELECT 2nid FROM " +
databaseName + "_target_setup.2Nodes)) or rnid='" + functorId + "';" );
st.execute( "CREATE TABLE if not exists RNodes AS SELECT * FROM " + databaseName + "_BN.RNodes WHERE rnid IN (SELECT " +
node + " FROM " + databaseName + "_BN." + table + " WHERE TargetNode = '" + functorId + "') or rnid " +
"IN (SELECT rnid FROM " + databaseName + "_BN.RNodes_2Nodes WHERE 2nid IN (SELECT 2nid FROM " +
databaseName + "_target_setup.2Nodes)) or rnid='" + functorId + "';" );
/* Store functor for fast grounding processing */
st.execute( "DROP TABLE IF EXISTS Test_Node;" );
st.execute( "CREATE TABLE `Test_Node` ( `FID` varchar(199) NOT NULL, PRIMARY KEY (`FID`)) ENGINE=InnoDB DEFAULT CHARSET=latin1;" );
st.execute(" INSERT INTO `Test_Node` (`FID`) VALUES('"+functorId+"') ;");
long ctt1 = System.currentTimeMillis();
System.out.println( "\n\n*****\nEntering BayesBaseCT_SortMerge.buildCTTarget() for child ");
// int max = BayesBaseCT_SortMerge.buildCTTarget();
// to do : generate local ct based on the associated columns, Aug. 8th, 2014, zqian
int max = BayesBaseCT_SortMerge.buildSubCTTarget(functorId,databaseName+"_BN",databaseName1,databaseName1+"_BN",databaseName6,current_In_BN,current);
long ctt2 = System.currentTimeMillis();
System.out.println( "\nBayesBaseCT_SortMerge.buildCTTarget() run time is: " + ( ctt2 - ctt1 ) + "ms. \n ******************************** \n\n" );
// replace rnid with orig_rnid, be consistent with ct and cp tables for weight learning, June 24, 2014 zqian
st1.execute("update `TargetParents`,`RNodes` set `TargetParents`.TargetNode = `RNodes`.orig_rnid where `TargetParents`.TargetNode = `RNodes`.rnid; ");
st1.execute("update `TargetParents`,`RNodes` set `TargetParents`.TargetParent = `RNodes`.orig_rnid where `TargetParents`.TargetParent = `RNodes`.rnid; ");
st1.execute("update `TargetChildren`, `RNodes` set `TargetChildren`.TargetNode = `RNodes`.orig_rnid where `TargetChildren`.TargetNode = `RNodes`.rnid; ");
st1.execute("update `TargetChildren`, `RNodes` set `TargetChildren`.TargetChild = `RNodes`.orig_rnid where `TargetChildren`.TargetChild = `RNodes`.rnid; ");
st1.execute("update `TargetChildrensParents`, `RNodes` set `TargetChildrensParents`.TargetNode = `RNodes`.orig_rnid where `TargetChildrensParents`.TargetNode = `RNodes`.rnid; ");
st1.execute("update `TargetChildrensParents`, `RNodes` set `TargetChildrensParents`.TargetChildParent = `RNodes`.orig_rnid where `TargetChildrensParents`.TargetChildParent = `RNodes`.rnid; ");
st1.execute("update `TargetMB`, `RNodes` set `TargetMB`.TargetNode = `RNodes`.orig_rnid where `TargetMB`.TargetNode = `RNodes`.rnid; ");
st1.execute("update `TargetMB`, `RNodes` set `TargetMB`.TargetMBNode = `RNodes`.orig_rnid where `TargetMB`.TargetMBNode = `RNodes`.rnid; ");
/* find the corresponding ct tables in @database@_target_ct database*/
String rchainQuery = "SELECT name FROM " + databaseName1 + "_BN.lattice_set WHERE length = " + max + ";"; //@database@_target
ResultSet rsRchain = st.executeQuery( rchainQuery );
String rchain = "";
boolean rchainExists = false;
if ( rsRchain.absolute(1)){
rchainExists = true;
rchain = rsRchain.getString(1);
}
rsRchain.close();
// make rnid be consistent between the training and testing/target database
if ( rchainExists ){
System.out.println( "biggest Rchain in testing database : "+rchain +" for Functor: '"+functorId+"'");
ResultSet rNodeName_t = st.executeQuery( "SELECT orig_rnid FROM " + databaseName +"_BN.RNodes WHERE rnid = '" + functorId + "';" ); // `b`
if ( rNodeName_t.absolute( 1 ) ){
functorId = rNodeName_t.getString(1); //`registration(course0,student0)`
}
rNodeName_t.close();
/*copy table from @database@_target_ct to @database@_target_final_ct*/
st.execute("DROP TABLE IF EXISTS "+ databaseName3 +".`"+ functorId.replace("`","")+"_CT` ;"); //@database@_target_final_ct
//System.out.println( "CREATE TABLE " + databaseName3 +".`"+ functorId.replace("`","")+"_CT` as select * from "+ databaseName4+".`"+rchain.replace("`","")+"_CT`;" );
// st.execute( "CREATE TABLE " + databaseName3 +".`"+ functorId.replace("`","")+"_CT` as "
// + " select * from "+ databaseName4+".`"+rchain.replace("`","")+"_CT`;" ); //@database@_target_ct
//also copy the index, July 17,2014, zqian
// System.out.println( "CREATE TABLE " + databaseName3 +".`"+ functorId.replace("`","")+"_CT` like " + databaseName6+".`"+rchain.replace("`","")+"_CT`;" );
// System.out.println( "insert " + databaseName3 +".`"+ functorId.replace("`","")+"_CT` select * from "+ databaseName6+".`"+rchain.replace("`","")+"_CT`;" );
// st.execute( "CREATE TABLE " + databaseName3 +".`"+ functorId.replace("`","")+"_CT` like " + databaseName6+".`"+rchain.replace("`","")+"_CT`;" );
// st.execute( "insert " + databaseName3 +".`"+ functorId.replace("`","")+"_CT` select * from "+ databaseName6+".`"+rchain.replace("`","")+"_CT`;" ); //@database@_target_ct
//
System.out.println( "CREATE TABLE " + databaseName3 +".`"+ functorId.replace("`","")+"_CT` like " + databaseName6+".`local_CT`;" );
System.out.println( "insert " + databaseName3 +".`"+ functorId.replace("`","")+"_CT` select * from "+ databaseName6+".`local_CT`;" );
st.execute( "CREATE TABLE " + databaseName3 +".`"+ functorId.replace("`","")+"_CT` like " + databaseName6+".`local_CT`;" );
st.execute( "insert " + databaseName3 +".`"+ functorId.replace("`","")+"_CT` select * from "+ databaseName6+".`local_CT`;" ); //@database@_target_ct
/* get the mapping of RNodes in @database@_target_BN */
ResultSet rNodes = st.executeQuery( "SELECT orig_rnid, rnid FROM " + databaseName1 + "_BN.RNodes;" ); //@database@_target
ArrayList<String> rnids = new ArrayList<String>(); // `a`
ArrayList<String> origrnids = new ArrayList<String>(); //`RA(pfrof0,student0)`
while ( rNodes.next() ) {
origrnids.add( rNodes.getString(1));
rnids.add( rNodes.getString(2));
}
rNodes.close();
for ( int j = 0; j < rnids.size(); j++ ) {
System.out.println( "SHOW COLUMNS FROM " + databaseName3 + ".`" +functorId.replace("`","")+"_CT` WHERE Field = '"
+rnids.get(j).replace("`", "") + "';" );
ResultSet rsTypes = st.executeQuery( "SHOW COLUMNS FROM " + databaseName3 + ".`" +functorId.replace("`","")+"_CT` WHERE Field = '"
+rnids.get(j).replace("`", "") + "';" );
if ( !rsTypes.absolute(1) ) {//zqian, no rnode in the header, so do not need to replace
System.out.println( "NO Need to do the mapping for table `" +functorId.replace("`","")+"_CT` ;" );
rsTypes.close();
continue;
}
String type = rsTypes.getString(2);
rsTypes.close();
System.out.println( "ALTER TABLE " + databaseName3 + ".`" +functorId.replace("`","")+"_CT` CHANGE COLUMN "
+ rnids.get(j) + " " + origrnids.get(j) + " " + type + ";" );
st.execute( "ALTER TABLE " + databaseName3 + ".`" +functorId.replace("`","")+"_CT` CHANGE COLUMN "
+ rnids.get(j) + " " + origrnids.get(j) + " " + type + ";" );
}
System.out.println( "zqian :target_common_select_string "+ target_common_select_string );
String current_parents_select_string = target_common_select_string + current +" ,";
/*String current_parents_query ="SELECT TargetParent FROM "+databaseName+"_BN.TargetParents where TargetNode ='"+current+"' "
+ " and ( TargetParent ) NOT IN (select '"+functorId +"' );";*/
String current_parents_query ="SELECT TargetParent FROM "+databaseName+"_BN.TargetParents where TargetNode ='"+current+"' "
+ " and ( TargetParent ) NOT IN (select '"+functorId +"' );";
System.out.println( "zqian : "+ current_parents_query );
Statement st_t = con0.createStatement();
ResultSet rscurrent_parents = st_t.executeQuery( current_parents_query );
// begin while 2
while (rscurrent_parents.next()){
String sub_current= rscurrent_parents.getString(1);
System.out.println(" parent of TargetChild: "+sub_current+";");
current_parents_select_string += sub_current + " ,";
}// end while 2
// throw the exception Oct. 2nd. 2014
try {
st.execute("Drop table if exists "+databaseName3+".`"+functorId.replace("`", "")+"_"+current.replace("`", "")+"_final_CT` ;" );
String create_string1 = "create table "+databaseName3+".`"+functorId.replace("`", "")+"_"+current.replace("`", "")+"_final_CT` as select "
+ current_parents_select_string.substring(0, current_parents_select_string.lastIndexOf(",")-1)
+ " From "+databaseName3+".`"+functorId.replace("`", "")+"_CT` ;" ;
System.out.println (" create_target_child_final_ct_string: "+create_string1 +"\n");
st.execute(create_string1 );
}
catch(MySQLSyntaxErrorException e) {
tableNameIsTooLong=true;
System.out.println("Oops, the following table name is too long: "+ functorId.replace("`", "")+"_"+current.replace("`", "")+"_final_CT" );
}
}
else{
System.out.println( "NO Rchain for Functor: '"+functorId+"'");
//copy _counts table from @database@_target_ct to @database@_target_final_ct
st.execute("DROP TABLE IF EXISTS "+ databaseName3 +".`"+ functorId.replace("`","")+"_CT` ;"); //@database@_target_final_ct
// System.out.println( "CREATE TABLE " + databaseName3 +".`"+ functorId.replace("`","")+"_CT` as select * from "
// + databaseName4+".`"+functorArgs.get(0).replace("`","")+"_counts`;" );
// st.execute( "CREATE TABLE " + databaseName3 +".`"+ functorId.replace("`","")+"_CT` as select * from "
// + databaseName4+".`"+functorArgs.get(0).replace("`","")+"_counts`;" );
// System.out.println( "CREATE TABLE " + databaseName3 +".`"+ functorId.replace("`","")+"_CT` like " + databaseName4+".`"+functorArgs.get(0).replace("`","")+"_counts`;" );
// System.out.println(" insert " + databaseName3 +".`"+ functorId.replace("`","")+"_CT` select * from " + databaseName4+".`"+functorArgs.get(0).replace("`","")+"_counts`;" );
// st.execute( "CREATE TABLE " + databaseName3 +".`"+ functorId.replace("`","")+"_CT` like " + databaseName4+".`"+functorArgs.get(0).replace("`","")+"_counts`;" );
// st.execute("insert " + databaseName3 +".`"+ functorId.replace("`","")+"_CT` select * from " + databaseName4+".`"+functorArgs.get(0).replace("`","")+"_counts`;" );
//
System.out.println( "CREATE TABLE " + databaseName3 +".`"+ functorId.replace("`","")+"_CT` like " + databaseName6+".`local_CT`;" );
System.out.println( "insert " + databaseName3 +".`"+ functorId.replace("`","")+"_CT` select * from "+ databaseName6+".`local_CT`;" );
st.execute( "CREATE TABLE " + databaseName3 +".`"+ functorId.replace("`","")+"_CT` like " + databaseName6+".`local_CT`;" );
st.execute( "insert " + databaseName3 +".`"+ functorId.replace("`","")+"_CT` select * from "+ databaseName6+".`local_CT`;" ); //@database@_target_ct
}
}
}
public static void setVarsFromConfig()
{
Config conf = new Config();
databaseName = conf.getProperty("dbname");
databaseName1 = databaseName + "_target";
databaseName2 = databaseName1 + "_setup";
databaseName3 = databaseName1 + "_final_CT";
databaseName4 = databaseName1 + "_CT";
databaseName5 = databaseName1 + "_final";
databaseName6 = databaseName1 + "_db";
dbUsername = conf.getProperty("dbusername");
dbPassword = conf.getProperty("dbpassword");
dbaddress = conf.getProperty("dbaddress");
//Sep 11, 2014,zqian
String temp = conf.getProperty("CrossValidation");
if ( temp.equals( "1" ) )
{
CrossValidation = true;
}
else
{
CrossValidation = false;
}
}
public static void connectDBTargetSetup() throws SQLException
{
String CONN_STR2 = "jdbc:" + dbaddress + "/" + databaseName2;
try
{
java.lang.Class.forName( "com.mysql.jdbc.Driver" );
}
catch ( Exception ex )
{
System.err.println( "Unable to load MySQL JDBC driver" );
}
con0 = (Connection) DriverManager.getConnection( CONN_STR2,
dbUsername,
dbPassword );
}
public static void connectDBTargetFinalCT() throws SQLException
{
String CONN_STR2 = "jdbc:" + dbaddress + "/" + databaseName3;
try
{
java.lang.Class.forName( "com.mysql.jdbc.Driver" );
}
catch ( Exception ex )
{
System.err.println( "Unable to load MySQL JDBC driver" );
}
con1 = (Connection) DriverManager.getConnection( CONN_STR2,
dbUsername,
dbPassword );
}
public static void connectDBTargetFinal() throws SQLException
{
String CONN_STR2 = "jdbc:" + dbaddress + "/" + databaseName5;
try
{
java.lang.Class.forName( "com.mysql.jdbc.Driver" );
}
catch ( Exception ex )
{
System.err.println( "Unable to load MySQL JDBC driver" );
}
conFinal = (Connection) DriverManager.getConnection( CONN_STR2,
dbUsername,
dbPassword );
}
public static void connectDB_preprocess() throws SQLException
{
String CONN_STR_preprocess = "jdbc:" + dbaddress + "/" + databaseName+"_BN";
try
{
java.lang.Class.forName( "com.mysql.jdbc.Driver" );
}
catch ( Exception ex )
{
System.err.println( "Unable to load MySQL JDBC driver" );
}
con_preprocess = (Connection) DriverManager.getConnection( CONN_STR_preprocess,
dbUsername,
dbPassword );
}
public static void disconnectDB_preprocess() throws SQLException
{
con_preprocess.close();
}
public static void disconnectDB() throws SQLException
{
con0.close();
}
public static void disconnectDBTargetFinalCT() throws SQLException
{
con1.close();
}
public static void disconnectDBFinal()
{
try
{
conFinal.close();
}
catch (SQLException e)
{
System.out.println( "Failed to close connection to final table." );
}
}
// get the each functor, e.g. unielwin_BN
public static ArrayList<String> GetFunctors() throws SQLException
{
Statement st1 = con0.createStatement();
//System.out.println( "Getting functors for d_gender..." );
ResultSet rs = st1.executeQuery( "SELECT distinct Fid from FNodes order by Type ;" );
//+ " where Fid ='`runningtime(movies0)`';" );
// ResultSet rs = st1.executeQuery( "SELECT distinct Fid from " + "FNodes "
// + " where Type = 'Rnode' order by Type ;" ); //processing 1node first, Aug. 26
// ignore Rnode for Financial dataset , Aug.6th
//only processing the target nodes with binary values, May 1st,Sep 9th, 2014
/* rs = st1.executeQuery( "SELECT distinct Fid from FNodes "
+ " where FunctorName in "
+ "( select column_name from "
+ " (SELECT count(*) as Number , column_name FROM Attribute_Value group by column_name) C "
+ " where C.Number =2"
+ ") "
+ " and Type = '1Node' and main ='1' ;" ); // processing 1Node first, only the main functor, May 6th
*/
//+ " and Type = 'Rnode' and main ='1' ;" ); // try Rnode , May 8th
// // processing Gender in MovieLens_std, May 12
/* ResultSet rs = st1.executeQuery( "SELECT distinct Fid from FNodes "
+ " where FunctorName = 'd_gender' ;" );
*/
ArrayList<String> functors = new ArrayList<String>();
while ( rs.next() )
{
functors.add( rs.getString( 1 ) );
}
st1.close();
return functors;
}
public static ArrayList<String> GetFunctorArgs( String functorId )throws SQLException
{
Statement st1 = con0.createStatement();
System.out.println( "Getting functor args..." );
st1.execute( "USE " + databaseName2 + ";" );
System.out.println( "SELECT pvid FROM FNodes_pvars WHERE Fid = '" + functorId + "';" );
ResultSet rs = st1.executeQuery ( "SELECT pvid FROM FNodes_pvars " + "WHERE Fid = '" + functorId + "';" );
ArrayList<String> argList = new ArrayList<String>();
processingRNode = false;
if ( !rs.first() )
{
System.out.println( "FNodes_pvars: Result set is empty, go to RNodes_pvars !" );
rs.close();
rs = st1.executeQuery( "SELECT pvid FROM RNodes_pvars WHERE rnid " + "= '" + functorId + "';" );
if ( !rs.first() )
{
System.out.println( " RNodes_pvars :Result set is empty!" );
rs.close();
st1.close();
System.out.println( "Functor " + functorId + " has " + argList.size() + " arguments." );
return argList;
}
processingRNode = true;
}
do
{
System.out.println("Added an argument: " + rs.getString(1) );
argList.add( rs.getString(1) );
} while ( rs.next() );
rs.close();
st1.close();
System.out.println( "Functor " + functorId + " has " + argList.size() +" arguments." );
return argList;
}
/* check if there's `mult` column or not, if not, then return; if yes, do the processing. July 9th, 2014*/
public static void pre_process( ) throws SQLException{
/* pre processing, drop the `MULT` column for all CP tables, otherwise can NOT using natural join for _ct and _CP, April 3rd, zqian
* remapping the column headers for _cp tables, form rnid to orig_rnid April 15th, zqian
* (i.e. a --> RA(prof0,student0); b --> registration(course0,student0) )
* */
System.out.println( "\npre processing, drop the `MULT` column for all CP tables in "+ databaseName + "_BN database;" );
connectDB_preprocess();
Statement st = con_preprocess.createStatement();
Statement st1 = con_preprocess.createStatement();
Statement st2 = con_preprocess.createStatement();
ArrayList<String> cp_tables = new ArrayList<String>();
/* check if there's `mult` column or not, if not, then return; if yes, do the processing. July 9th, 2014*/
ResultSet temp_tables = st.executeQuery( "show tables from "+ databaseName + "_BN" + " like '%_CP' ;");
if ( temp_tables.next() ){
ResultSet rsColumn_t2 = st2.executeQuery ( "SHOW COLUMNS FROM `"+ temp_tables.getString(1)+ "` WHERE Field ='MULT';" );
//System.out.println( "SHOW COLUMNS FROM `"+ temp_tables.getString(1)+ "` WHERE Field ='MULT';" );
if (!rsColumn_t2.absolute(1)) {
System.out.println("\n***Already Did the Pre_Process!***\n");
}
else {
ResultSet temp_cp_tables = st.executeQuery( "show tables from "+ databaseName + "_BN" + " like '%_CP' ;");
while ( temp_cp_tables.next() ) {
System.out.println("ALTER TABLE `"+temp_cp_tables.getString(1)+"` DROP COLUMN `MULT`;");
st1.execute("ALTER TABLE `"+temp_cp_tables.getString(1)+"` DROP COLUMN `MULT`;");
cp_tables.add( temp_cp_tables.getString(1)); // get all the cp tables
}
}
}
// get the mapping of RNodes
ResultSet rNodes = st.executeQuery( "SELECT orig_rnid, rnid FROM " + databaseName + "_BN.RNodes;" );
ArrayList<String> rnids = new ArrayList<String>(); // `a`
ArrayList<String> origrnids = new ArrayList<String>(); //`RA(pfrof0,student0)`
while ( rNodes.next() )
{
origrnids.add( rNodes.getString(1));
rnids.add( rNodes.getString(2));
}
rNodes.close();
//remapping the column headers for _cp tables
for ( int j = 0; j < rnids.size(); j++ )
{
for ( int i = 0; i < cp_tables.size(); i++ )
{
System.out.println( "SHOW COLUMNS FROM " + databaseName + "_BN.`" + cp_tables.get(i) +"` WHERE Field = '" +rnids.get(j).replace("`", "") + "';" );
ResultSet rsTypes = st.executeQuery( "SHOW COLUMNS FROM " + databaseName + "_BN.`" + cp_tables.get(i) +"` WHERE Field = '" +rnids.get(j).replace("`", "") + "';" );
if ( !rsTypes.absolute(1) ) //zqian, no rnode in the header, so do not need to replace
{
System.out.println( "NO Need to do the mapping " + cp_tables.get(i) );
rsTypes.close();
continue;
}
String type = rsTypes.getString(2);
rsTypes.close();
System.out.println( "ALTER TABLE " + databaseName + "_BN.`" + cp_tables.get(i)+ "` CHANGE COLUMN " + rnids.get(j) + " " + origrnids.get(j) + " " + type + ";" );
st.execute( "ALTER TABLE " + databaseName + "_BN.`" + cp_tables.get(i)+ "` CHANGE COLUMN " + rnids.get(j) + " " + origrnids.get(j) + " " + type + ";" );
}
}
// create FNodes_Mapping, May 2nd, zqian
st1.execute("drop table if exists "+databaseName+"_BN.FNodes_Mapping; ");
st1.execute("create table "+databaseName+"_BN.FNodes_Mapping as select * from "+databaseName+"_BN.FNodes; ");
st1.execute("update "+databaseName+"_BN.FNodes_Mapping, "+databaseName+"_BN.RNodes set Fid = orig_rnid where Fid=rnid; ");
//keep the Attribute for Rnode be consistency, May 2nd
st1.execute("update "+databaseName+"_BN.Attribute_Value set value = 'F' where value = 'False' ;");
st1.execute("update "+databaseName+"_BN.Attribute_Value set value = 'T' where value = 'True' ;");
st.close();
st1.close();
st2.close();
disconnectDB_preprocess();
}
public static void postProcess( Statement st ) throws SQLException
{ //functorId = "`a`";
System.out.println("zqian: comes to postProcess!");
System.out.println( "SELECT orig_rnid, rnid FROM " + databaseName1 + "_BN.RNodes;" );
ResultSet rNodes = st.executeQuery( "SELECT orig_rnid, rnid FROM " + databaseName1 + "_BN.RNodes;" );
ArrayList<String> rnids = new ArrayList<String>();
ArrayList<String> origrnids = new ArrayList<String>();
while ( rNodes.next() )
{
origrnids.add( rNodes.getString(1));
rnids.add( rNodes.getString(2));
}
rNodes.close();
String realFunctorId = functorId; //`a`
String functorId_temp = functorId;
System.out.println("zqian: processingRNode " + processingRNode);
if ( processingRNode )
{
// functorId = "`a`";
ResultSet rNodeName_t = st.executeQuery( "SELECT orig_rnid FROM " + databaseName1 +"_BN.RNodes WHERE rnid = '" + functorId + "';" ); // `a`
if ( rNodeName_t.absolute( 1 ) )
{
functorId = rNodeName_t.getString(1); //`registration(course0,student0)`
functorId_temp =functorId;
System.out.println("zqian: functorId_temp " + functorId_temp);
}
rNodeName_t.close();
ResultSet rNodeName_t1 = st.executeQuery( "SELECT rnid FROM " + databaseName +"_BN.RNodes WHERE orig_rnid = '" + functorId + "';" );
if ( rNodeName_t1.absolute( 1 ) )
{
functorId = rNodeName_t1.getString(1); //`b`
System.out.println("zqian: functorId_ " + functorId);
}
rNodeName_t1.close();
}
Statement st_temp = con0.createStatement();
Statement st_temp1 = con0.createStatement();
for ( int i = 0; i < rnids.size(); i++ )
{
System.out.println( "SHOW COLUMNS FROM " + databaseName3 + ".`" + functorId_temp.replace("`", "") +"_parent_final_CT` " +
"WHERE Field = '" + rnids.get(i).replace("`", "") + "';" );
ResultSet rsTypes = st.executeQuery( "SHOW COLUMNS FROM " + databaseName3 + ".`" + functorId_temp.replace("`", "") +"_parent_final_CT` "
+ "WHERE Field = '" + rnids.get(i).replace("`", "") + "';");
if ( !rsTypes.absolute(1) ) //zqian, no rnode in the header, so do not need to replace
{
System.out.println( "no rnode " + origrnids.get(i) + " in column header " );
rsTypes.close();
functorId = realFunctorId; //`a`
//return;
continue;
}
String type = rsTypes.getString( 2 );
rsTypes.close();
System.out.println( "ALTER TABLE " + databaseName3 + ".`" + functorId_temp.replace("`", "") +"_parent_final_CT` CHANGE COLUMN " +
rnids.get(i) + " " + origrnids.get(i) + " " + type + ";" );
st.execute( "ALTER TABLE " + databaseName3 + ".`" + functorId_temp.replace("`", "") +"_parent_final_CT` CHANGE COLUMN " +
rnids.get(i) + " " + origrnids.get(i) + " " + type + ";" );
}
// processing target's children
//String target_children ="SELECT TargetChild FROM "+databaseName+"_BN.TargetChildren where TargetNode ='"+functorId+"' ;"; // `b`
//--if target is rnode then have to rule out the associated 2node --// April 25
String target_children ="SELECT TargetChild FROM "+databaseName+"_BN.TargetChildren where TargetNode ='"+functorId+"' "
+ " and (TargetChild) not in ( SELECT 2nid FROM "+databaseName+"_BN.RNodes_2Nodes where rnid = '"+functorId+"' );";
System.out.println( target_children );
ResultSet rstarget_children = st_temp1.executeQuery( target_children );
// zqian: for each child of the target
while ( rstarget_children.next() )
{
String current= rstarget_children.getString(1);
String current_orig=current;
System.out.println("zqian: "+current+";");
//check if current is Rnode or not, do the mapping again
ResultSet r_tt = st_temp.executeQuery( "SELECT Type FROM " + databaseName +"_BN.FNodes WHERE Fid = '" + current + "';" ); // `b`
if ( r_tt.absolute( 1 ) )
{
//System.out.println("zqian: "+r_t.getString(1)+";");
if ( r_tt.getString(1).compareTo("Rnode")==0)
{
ResultSet rNodeName_t = st_temp.executeQuery( "SELECT orig_rnid " + "FROM " + databaseName +"_BN.RNodes WHERE rnid = '" + current + "';" ); // `b`
if ( rNodeName_t.absolute( 1 ) )
{
current = rNodeName_t.getString(1); //`registration(course0,student0)`
}
rNodeName_t.close();
ResultSet rNodeName_t1 = st_temp.executeQuery( "SELECT rnid " + "FROM " + databaseName1 +"_BN.RNodes WHERE orig_rnid = '" + current + "';" );
if ( rNodeName_t1.absolute( 1 ) )
{
current = rNodeName_t1.getString(1); //`a`
}