-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathFunctorWrapper.java
2181 lines (1857 loc) · 91.1 KB
/
FunctorWrapper.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
/* Check the RunningFlow.txt for first time running. May 20th, zqian
* */
/*May 7th,
* Using natural join to compute the _Score, we may lose some combinations occur in testing data, but NOT in training data.
* two possible solutions:
* 1. statistically, it makes sense to ignore some instance that do not occur in training data, or assign the probability to 0
* and then only evaluate the instance occurred.
* ACC = (# of correct values in _Score table compared with testing data )/(# of instances occurred in the _Score table)
* 2. or could use natural right join to make sure all combinations in the _ct_e tables occurs in the _Score table,(i.e. _cp natural right join _ct)
* but need to pay special attention to the null value by using coalesce(column,0)
* i.e. Syntax: COALESCE(x, y) = (CASE WHEN V1 IS NOT NULL THEN V1 ELSE V2 END)
* ACC = (# of correct values in _Score table compared with testing data )/(# of instances in testing data)
*
* Tried on the on Mondial_Traing1/_Test1, choose option 1 based on higher ACC ?
*
* 3. instead on set to 0, try using prior probability? May 9th
*
* */
/*May 6th, only processing 1Nodes with with binary values,
* ready for cross validation, copy the target database from testing database in MakeTargetSetup.java, modify argValues in FunctorWrapper.java
* for extended ct tables: set freq to 0 if is null
* */
/*May 5th. in the target_score_e table: sum_mult should before the natural join, otherwise the sum is double counted
* At present, divide the sum_mult by two, since we only processing binary nodes.
* But eventully, we have to do the summation before the natural join.
*
* May 1st, only processing the target nodes with binary values.
* */
/*do not do this.
* multiple classes, evaluation? missing some combinations, try to use _CP_smoothed,*/
/*April 30, compute the probability only for entries with the highest score and store them in target_score table
* */
/* the table name limit is 64 characters, so HAVE TO change the name convention. #### to do ###
* e.g. `capability(prof0,student0)_salary(prof0,student0)_final_CT_extended` is too long
* so currently using `capability(prof0,student0)_salary(prof0,student0)_CT`
* and `capability(prof0,student0)_salary(prof0,student0)_CT_e` (short for %_extended) should be fine.
*
* Name Convention:
* the local CT tables and extended CT tables
* -- for each functorId
* -- for its parents: target_parent_ct, target_parent_ct_e (i.e. natural join %_ct with Attribute_Value)
* -- for its children:
* -- for each child: target_child_ct, target_child_ct_e
*
* the local score tables (i.e. natural join %_ct_e with _CP)
* -- for each functorId
* -- for its parents: target_parent_final
* -- for its children:
* -- for each child: target_child_final
*
* the total score
* -- for each functorId
* -- target_sum
*
* */
/*Notes:
1. if the target node is rnode, then remove the associated 2Nodes in the family. (done)
-- in the BN, given by the schema edges, rnode is always the parent of associated 2node
-- so only need to handle the target_child_ct tables
2. for evaluation: create the extended _final_ct_extended table: (done)
--for each functorId
-- for each _final_ct
-- drop the target column in final_ct table,
-- create _fianl_ct_extended by join final_ct with Attribute_Value with specific target node
-- natural join _extended table with _cp table
3. if the target node is 2node, then only considering the the situation when Rnode is True. (DONE)
--for 2node in table Attribute_Value, there are only values give Rnode is True.
*/
/* April 2014, zqian, converting a BN to dependency network(DN), using FunctorWrapper to add groundings for local CT tables
*
* Author: Kurt Routley
* Date: September 23, 2013
*/
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;
public class FunctorWrapper
{
static Connection con0, con1, conFinal,con_preprocess;
static String databaseName, databaseName1, databaseName2, databaseName3, databaseName4, databaseName5;
// unielwin, unielwin_target, unielwin_target_setup, unielwin_target_final_CT, unielwin_target_CT, unielwin_target_final
static String dbUsername;
static String dbPassword;
static String dbaddress;
static int groundingCount;
static String functorId;
static boolean processingRNode = false;
// Global variable: true to use nodes in Markov Blanket, false to use only functor's parents and associated RNodes
static boolean useMarkovBlanket = true;
public static void main(String[] args) throws Exception
{
long time1=System.currentTimeMillis();
/*
* Assume @database@_target_setup is already created
* FUTURE: Need to create this if not already done.
*/
/*
* Create Markov Blanket
*/
MarkovBlanket.runMakeMarkovBlanket();
/*
*Target Database Setup
*/
MakeTargetSetup.runMakeTargetSetup();
/*
*Read for FunctorWrapper
*/
setVarsFromConfig();
//pre_process( );
connectDBTargetSetup();
ArrayList<String> functors = GetFunctors();
Statement st = con0.createStatement(); // connect to unielwin_target_setup
st.execute( "DROP SCHEMA IF EXISTS " + databaseName3 + ";" ); //unielwin_target_final_CT
st.execute( "CREATE SCHEMA " + databaseName3 + ";" );
st.execute( "DROP SCHEMA IF EXISTS " + databaseName5 + ";" ); //unielwin_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)`";
for ( int i = 0; i < functors.size(); i++ ) //zqian April 10th
{
functorId = functors.get(i); //zqian April 10th
// unielwin_target_setup
// 1Nodes
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
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
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 + "';" );
// Get pvars for functor */
System.out.println("Functor: '"+functorId+"'");
ArrayList<String> functorArgs = GetFunctorArgs( functorId );
// Perform Groundings assignments, Run CTGenerator for each Grounding
ArrayList<String> functorArgValues = new ArrayList<String>();
// counting the grounding number
groundingCount = 1;
// create the sub_CT tables with groundings
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+"') ;");
GetGroundings( functorArgs, functorArgValues, 0, functorId );
// Post-processing on *_final_CT column names for RNodes
postProcess( st );
// Extend the final_CT tables for each functorId with all possible values
Extend_Final_CT();
// remove the n/a ??
Update_CT();
// 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
createSubFinal2_zqian( functorArgs ); // using natural right join
//createFinal2_zqian( functorArgs ); // set null to 0
createFinal3_zqian( functorArgs ); // set null to -50
// Cleanup for next functor
st.execute( "DROP TABLE 1Nodes;" );
st.execute( "DROP TABLE 2Nodes;" );
st.execute( "DROP TABLE RNodes;" );
}
st.close();
disconnectDBTargetFinalCT();
disconnectDB();
disconnectDBFinal();
long time2=System.currentTimeMillis();
System.out.println( "Total FunctorWrapper time: " + ( time2 - time1 ) );
}
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";
dbUsername = conf.getProperty("dbusername");
dbPassword = conf.getProperty("dbpassword");
dbaddress = conf.getProperty("dbaddress");
}
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..." );
ResultSet rs = st1.executeQuery( "SELECT distinct Fid from " + "FNodes;" );
//only processing the target nodes with binary values, May 1st
// ResultSet 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 = '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;
}
//static String functorId_orig= functorId;
public static void GetGroundings( ArrayList<String> args, ArrayList<String> argValues, int index ,String functorId_orig) throws SQLException, Exception
{
Statement st1 = con0.createStatement();
/* Get table for pvar *//* Get pvar id name */
ResultSet rs = st1.executeQuery( "SELECT TABLE_NAME FROM PVariables WHERE pvid = '" + args.get(index) + "';" );
rs.first();
String pvidTableName = rs.getString( 1 );
rs.close();
rs = st1.executeQuery( "SELECT COLUMN_NAME FROM " + databaseName + "_BN.EntityTables WHERE TABLE_NAME = '" + pvidTableName + "';" );
if ( !rs.first() )
{
System.out.println( "Failed to get pvid." );
/* Paranoia: Cleanup */
rs.close();
st1.close();
return;
}
String pvid = rs.getString( 1 );
rs.close();
// /* Get argument values for pvar */
// System.out.println( "SELECT " + pvid + " FROM " + databaseName + "." + pvidTableName + ";" );
// rs = st1.executeQuery( "SELECT " + pvid + " FROM " + databaseName + "." + pvidTableName + ";" );
//
/* Get argument values for pvar from Target dataset, May 6th, zqian, cross validation*/
System.out.println( "SELECT " + pvid + " FROM " + databaseName1 + "." + pvidTableName + ";" );
rs = st1.executeQuery( "SELECT " + pvid + " FROM " + databaseName1 + "." + pvidTableName + ";" );
while ( !rs.isClosed() && rs.next() )
{
argValues.add( rs.getString(1) );
/* Get the rest of the argument assignments */
if ( index != args.size()-1 )
{
GetGroundings( args, argValues, index + 1, functorId_orig );
argValues.remove(index);
continue;
}
Statement st2 = con0.createStatement();
st2.execute( "DROP TABLE IF EXISTS Groundings;" );
st2.execute( "CREATE TABLE Groundings ( pvid varchar(40) NOT NULL, id varchar(256) NOT NULL, PRIMARY KEY (pvid,id) );" );
for ( int i = 0; i < args.size(); i++ )
{
System.out.println("GROUNDINGS PREP: INSERT INTO Groundings Values( '" + args.get(i) + "', '" + argValues.get(i) + "' );" );
st2.execute ( "INSERT INTO Groundings Values( '" + args.get(i) + "', '" + argValues.get(i) + "' );" );
}
int max = BayesBaseCT_SortMerge.buildCTTarget();
/*
* Store Results
* 1. Store Groundings in vertical alignment for join
* 2. Get largest rchain to get counts
* 3. Join GroundingsVertical to `<rchain>_counts`
* 4. Store in functor final CT
*/
Statement st3 = con1.createStatement();
String tableString = "CREATE TABLE GroundingsVertical (GroundingsNumber INT";
for ( int i = 0; i < args.size(); i++ )
{
tableString += ", "+args.get(i) + " INT";
}
tableString += ");";
System.out.println( tableString );
st3.execute( tableString );
String insertString = "INSERT INTO GroundingsVertical Values (" + groundingCount;
for ( int i = 0; i < argValues.size(); i++ )
{
insertString += ", " + argValues.get(i);
}
insertString += ");";
System.out.println( insertString );
st3.execute( insertString );
/* 2. */
String rchainQuery = "SELECT name FROM " + databaseName1 + "_BN.lattice_set WHERE length = " + max + ";";
System.out.println( rchainQuery );
ResultSet rsRchain = st3.executeQuery( rchainQuery );
String rchain = "";
boolean rchainExists = false;
if ( rsRchain.absolute(1))
{
rchainExists = true;
rchain = rsRchain.getString(1);
}
else
{
rchain = args.get(index);
}
rsRchain.close();
// process the rnid, April 11, zqian
if ( processingRNode )
{
ResultSet rNodeName_t = st3.executeQuery( "SELECT orig_rnid FROM " + databaseName +"_BN.RNodes WHERE rnid = '" + functorId_orig + "';" ); // `b`
if ( rNodeName_t.absolute( 1 ) )
{
functorId = rNodeName_t.getString(1); //`registration(course0,student0)`
}
rNodeName_t.close();
ResultSet rNodeName_t1 = st3.executeQuery( "SELECT rnid FROM " + databaseName1 +"_BN.RNodes WHERE orig_rnid = '" + functorId + "';" );
if ( rNodeName_t1.absolute( 1 ) )
{
functorId = rNodeName_t1.getString(1); //`a`
}
rNodeName_t1.close();
}
/********* // processing target's parents***************Begin****************/
// April 23,
int target_parents_counts=0;
String target_parents_counts_query ="SELECT count(TargetParent) FROM "+databaseName+"_BN.TargetParents where TargetNode ='"+functorId_orig+"' ;"; //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( "zqian : rstarget_parents_counts "+ target_parents_counts );
//should contain, current and its parents, zqian April 22nd, 2014, mapping the rnid?
String target_parent_select_string= "mult, "+ functorId + " ,";
String target_parents_query ="SELECT TargetParent FROM "+databaseName+"_BN.TargetParents where TargetNode ='"+functorId_orig+"' ;"; //functorId in unielwin_BN
System.out.println( "zqian : "+ target_parents_query );
Statement st_temp = con0.createStatement();
ResultSet rstarget_parents = st_temp.executeQuery( target_parents_query );
// zqian: concat each parent of the target
// begin while
while ( rstarget_parents.next() && target_parents_counts !=0 )
{
String current= rstarget_parents.getString(1);
System.out.println("zqian: "+current+";");
//check if current is Rnode or not, do the mapping again
ResultSet r_t = st3.executeQuery( "SELECT Type FROM " + databaseName +"_BN.FNodes WHERE Fid = '" + current + "';" ); // `b`
if ( r_t.absolute( 1 ) )
{
System.out.println("zqian: "+r_t.getString(1)+";");
if ( r_t.getString(1).compareTo("Rnode")==0)
{
ResultSet rNodeName_t = st3.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 = st3.executeQuery( "SELECT rnid " + "FROM " + databaseName1 +"_BN.RNodes WHERE orig_rnid = '" + current + "';" );
if ( rNodeName_t1.absolute( 1 ) )
{
current = rNodeName_t1.getString(1); //`a`
}
rNodeName_t1.close();
}
}
r_t.close();
target_parent_select_string += current + " ,";
}//endwhile
// 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= "mult, "+ functorId + " ,";
}
String tempTableString = "";
if ( rchainExists )
{
tempTableString = "CREATE TABLE tempJoin AS SELECT * FROM " + "(SELECT * FROM GroundingsVertical) G " +
"JOIN (SELECT "+target_parent_select_string.substring(0, target_parent_select_string.lastIndexOf(",")-1) +" FROM " + databaseName4 + ".`" + rchain.replace( "`","" ) + "_CT`) A;";
}
else
{ // need to update using the very similar query
tempTableString = "CREATE TABLE tempJoin AS SELECT * FROM " + "(SELECT * FROM GroundingsVertical) G " +
"JOIN (SELECT * FROM " + databaseName4 + ".`" + rchain.replace( "`","" ) + "_counts`) A;";
}
System.out.println("zqian: "+ tempTableString);
st3.execute( tempTableString );
/*
* Check if table already exists, If it doesn't, create it, Otherwise, insert new values into existing
*/
boolean exists = false;
String functorIdhld = functorId;
if ( processingRNode )
{
ResultSet rNodeName = st3.executeQuery( "SELECT orig_rnid " + "FROM " + databaseName1 +"_BN.RNodes WHERE rnid = '" + functorId + "';" );
if ( rNodeName.absolute( 1 ) )
{
functorId = rNodeName.getString(1);
}
rNodeName.close();
}
String checkCTExists = "SHOW TABLES LIKE '" + functorId.replace( "`", "" ) + "_parent_final_CT';"; //target_parent_final_ct
System.out.println( checkCTExists );
ResultSet rsExists = st3.executeQuery( checkCTExists );
if ( rsExists.absolute(1) )
{
exists = true;
}
rsExists.close();
if ( exists )
{
String insertNewGrounding = "INSERT INTO `" + functorId.replace( "`", "" ) + "_parent_final_CT` SELECT distinct * FROM " + "tempJoin;"; //target_parent_final_ct
System.out.println( insertNewGrounding );
st3.execute( insertNewGrounding );
}
else
{
String createString = "CREATE TABLE `" + functorId.replace( "`", "" ) + "_parent_final_CT` SELECT distinct * FROM " + "tempJoin;"; //target_parent_final_ct
System.out.println( createString );
st3.execute( createString );
}
functorId = functorIdhld;
st3.execute( "DROP TABLE tempJoin" );
/********* // processing target's parents******************END*************/
/********* // processing target's children*****************Begin**************/
// target_child_final_CT
//for each child of the target // current
//( --if target is rnode then have to rule out the associated 2node --) //April 25
// check the no. of its parents
// if ==0, mult,functorId, current,
// else, mult,functorId,current, current_parents
//--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_orig+"' "
+ " and (TargetChild) not in ( SELECT 2nid FROM "+databaseName+"_BN.RNodes_2Nodes where rnid = '"+functorId_orig+"' );"; //functorId in unielwin_BN
//String target_children ="SELECT TargetChild FROM "+databaseName+"_BN.TargetChildren where TargetNode ='"+functorId_orig+"' ;"; //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_orig=current; // `b`, in unielwin_BN
System.out.println("zqian: "+current+";");
//
//check if current is Rnode or not, do the mapping again
ResultSet r_t = st3.executeQuery( "SELECT Type FROM " + databaseName +"_BN.FNodes WHERE Fid = '" + current + "';" ); // `b`
if ( r_t.absolute( 1 ) )
{
System.out.println("zqian: "+r_t.getString(1)+";");
if ( r_t.getString(1).compareTo("Rnode")==0)
{
ResultSet rNodeName_t = st3.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 = st3.executeQuery( "SELECT rnid " + "FROM " + databaseName1 +"_BN.RNodes WHERE orig_rnid = '" + current + "';" );
if ( rNodeName_t1.absolute( 1 ) )
{
current = rNodeName_t1.getString(1); //`a`
}
rNodeName_t1.close();
}
}
r_t.close();
//
String current_parents_select_string= "mult, "+ functorId +" ," + current +" ,";
String current_parents_query ="SELECT TargetParent FROM "+databaseName+"_BN.TargetParents where TargetNode ='"+current_orig+"' "
+ " and ( TargetParent ) NOT IN (select '"+functorId_orig +"' );";
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("zqian: sub_current: "+sub_current+";");
//check if current is Rnode or not, do the mapping again
ResultSet sub_r_t = st3.executeQuery( "SELECT Type FROM " + databaseName +"_BN.FNodes WHERE Fid = '" + sub_current + "';" ); // `b`
if ( sub_r_t.absolute( 1 ) )
{
System.out.println("zqian: "+sub_r_t.getString(1)+";");
if ( sub_r_t.getString(1).compareTo("Rnode")==0)
{
ResultSet rNodeName_t = st3.executeQuery( "SELECT orig_rnid FROM " + databaseName +"_BN.RNodes WHERE rnid = '" + sub_current + "';" ); // `b`
if ( rNodeName_t.absolute( 1 ) )
{
sub_current = rNodeName_t.getString(1); //`registration(course0,student0)`
}
rNodeName_t.close();
ResultSet rNodeName_t1 = st3.executeQuery( "SELECT rnid " + "FROM " + databaseName1 +"_BN.RNodes WHERE orig_rnid = '" + sub_current + "';" );
if ( rNodeName_t1.absolute( 1 ) )
{
sub_current = rNodeName_t1.getString(1); //`a`
}
rNodeName_t1.close();
}
}
sub_r_t.close();
//
current_parents_select_string += sub_current + " ,";
}
// end while 2
String sub_tempTableString = "";
if ( rchainExists )
{
sub_tempTableString = "CREATE TABLE tempJoin AS SELECT * FROM " + "(SELECT * FROM GroundingsVertical) G " +
"JOIN (SELECT "+current_parents_select_string.substring(0, current_parents_select_string.lastIndexOf(",")-1) +" FROM " + databaseName4 + ".`" + rchain.replace( "`","" ) + "_CT`) A;";
}
else
{ // need to update using the very similar query
sub_tempTableString = "CREATE TABLE tempJoin AS SELECT * FROM " + "(SELECT * FROM GroundingsVertical) G " +
"JOIN (SELECT * FROM " + databaseName4 + ".`" + rchain.replace( "`","" ) + "_counts`) A;";
}
System.out.println("zqian: "+ sub_tempTableString);
st3.execute( sub_tempTableString );
// functorId_current_final_CT
//
boolean sub_exists = false;
String sub_functorIdhld = functorId;
if ( processingRNode )
{
ResultSet rNodeName = st3.executeQuery( "SELECT orig_rnid " + "FROM " + databaseName1 +"_BN.RNodes WHERE rnid = '" + functorId + "';" );
if ( rNodeName.absolute( 1 ) )
{
functorId = rNodeName.getString(1);
}
rNodeName.close();
}
String sub_checkCTExists = "SHOW TABLES LIKE '" + functorId.replace( "`", "" ) + "_"+current_orig.replace( "`", "" )+"_final_CT';"; //target_parent_final_ct
System.out.println( sub_checkCTExists );
ResultSet sub_rsExists = st3.executeQuery( sub_checkCTExists );
if ( sub_rsExists.absolute(1) )
{
sub_exists = true;
}
sub_rsExists.close();
if ( sub_exists )
{
String insertNewGrounding = "INSERT INTO `" + functorId.replace( "`", "" ) + "_"+current_orig.replace( "`", "" )+"_final_CT` SELECT distinct * FROM " + "tempJoin;"; //target_current_final_ct
System.out.println( insertNewGrounding );
st3.execute( insertNewGrounding );
}
else
{
String createString = "CREATE TABLE `" + functorId.replace( "`", "" ) + "_"+current_orig.replace( "`", "" )+"_final_CT` SELECT distinct * FROM " + "tempJoin;"; //target_current_final_ct
System.out.println( createString );
st3.execute( createString );
}
functorId = sub_functorIdhld;
st3.execute( "DROP TABLE tempJoin" );
//
st_t.close();
}
// end while 1
/********* // processing target's children**************END*****************/
st3.execute( "DROP TABLE GroundingsVertical" );
st3.close();
st2.execute( "DROP TABLE Groundings;" );
st2.close();
st_temp.close();
st_temp1.close();
s_temp.close();
argValues.remove(index);
groundingCount++;
}
rs.close();
st1.close();
}
/*updated on Sep. 29th, 2014, same as TestWrapper.java*/
/* 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 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( "pre 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();
ArrayList<String> cp_tables = new ArrayList<String>();
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();
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));
}