-
Notifications
You must be signed in to change notification settings - Fork 1
/
GoodPlan.java
800 lines (640 loc) · 26.2 KB
/
GoodPlan.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
/*
* GoodPlan.java
* -------------
* http://java.sun.com/j2se/1.3/docs/api/index.html
* $Id: GoodPlan.java,v 1.49 2000/11/28 00:48:08 chenli Exp $
*/
// Sample gnu files: ~/Papers/1999/clindex/VLDB99-clindex/v2.3/*.com
import java.io.*;
import java.util.*;
class GoodPlan {
//---------------------------------------------------------------
final public static int randomQuery = 0;
final public static int chainQuery = 1;
final public static int starQuery = 2;
final public static int chainQueryFix = 3;
final public static int ccQuery = 4;
//final public static int simQueryType = randomQuery;
//final public static int simQueryType = chainQuery;
public static int simQueryType = ccQuery;
//---------------------------------------------------------------
public static boolean debug = false;
//public static boolean debug = true;
//---------------------------------------------------------------
public static int viewNum ; // # of views
public static int relationNum = 8; // # of relations
public static int viewSubgoalNum = 3; // # of subgoals in a view
public static int relAttrNum ; // # of rel attrs
public static int querySubgoalNum ; // # of subgoals in a query
public static int dropHeadArgNum = 0; // # of args dropped a head
public static int queryArgDomSize = 5; // size of arg domain in a query
public static int simQueryNum = 5; // # of queries
public static int simQueryStep = 5; // # of queries in a step
public static int validQueryNum ; // # of queries with rewritings
public static int groupedViewNum = 0; // real view number after grouping
public static int oldGroupedViewNum = 0; // old number
public static int repetitionNum = 1; // # of repetition of experiment
//---------------------------------------------------------------
// statistics
public static int totalNumVTs = 0; // equivalence class
public static int totalGroupedNumVTs = 0; // without shrinking
public static int totalNumMR = 0;
public static int totalNumGMR = 0;
public static int totalSizeGMR = 0;
public static long totalTimeMR = 0;
public static long totalTimeFirstGMR = 0;
public static long totalTimeAllGMR = 0;
//---------------------------------------------------------------
public static Random random = new Random(18461);
public static boolean testMiniCon = false;
public static boolean isView;
public static void main(String[] args) {
String type = args[0];
String viewFile = args[1];
String queryFile = args[2];
doOneTestYoli2(type, queryFile, viewFile);
}
public static void main2(String[] args) {
int m = Integer.parseInt(args[0]);
String tipo = args[1];
int repeticion = Integer.parseInt(args[2]);
if (simQueryType == starQuery) {
querySubgoalNum = m; // required by star queries
viewSubgoalNum = m;
relAttrNum = m; // # of rel attrs
relationNum = 10;
dropHeadArgNum = (m*(m - 1)+1)/2;
// querySubgoalNum = m; required by star queries
// viewSubgoalNum = m;
// relAttrNum = 2; # of rel attrs
// relationNum = 10;
// dropHeadArgNum = 0;
}
if (simQueryType == chainQueryFix) {
querySubgoalNum = m; // required by star queries
viewSubgoalNum = m;
relAttrNum = 2; // # of rel attrs
relationNum = 10;
dropHeadArgNum = 0;
}
if (simQueryType == randomQuery) {
querySubgoalNum = m; // required by star queries
viewSubgoalNum = m;
relAttrNum = 5; // # of rel attrs
relationNum = 10;
dropHeadArgNum = 0; // all distinguished
}
if (simQueryType == ccQuery) {
querySubgoalNum = m; // required by star queries
viewSubgoalNum = m;
relAttrNum = 5; // # of rel attrs
relationNum = 10;
dropHeadArgNum = 0; // all distinguished
}
//unitTest();
//simulation();
for (int i = 0; i < repeticion; i++) {
repetitionNum = i;
doOneTestYoli(tipo);
}
}
/**
* Does the simulation and collects data
*/
static void simulation() {
System.out.println("nView\tnDiView\tProb\tnGMRs\ttGMRs\tVTs\tgVTs ");
for (viewNum = 1; viewNum <= 1000; viewNum += simQueryStep)
//for (viewNum = 20; viewNum <= 20; viewNum += 3)
doOneTest();
}
static void doOneTestYoli(String tipo) {
Vector relations = genRelations();
Query query = genQuery(relations, simQueryType);
Vector dummyquery = new Vector();
dummyquery.add(query);
String directory = "../satcomp/expTipo"+tipo+"/expM";
// System.out.println(directory+querySubgoalNum);
printViews(dummyquery, directory+querySubgoalNum+"/consExpM"+querySubgoalNum+".txt");
// simQueryType = chainQuery;
Vector views = null;
// for (int n = 2; n <= 10; n = n + 2) {
viewNum = querySubgoalNum/2;
views = genViewsCC(relations, simQueryType);
printViews(views, directory+querySubgoalNum+"/vistasExpM"+querySubgoalNum+"N"+viewNum+".txt");
//printViews(views, directory+querySubgoalNum+"/vistasExpM"+querySubgoalNum+"N"+viewNum+"Rep"+repetitionNum+".txt");
genPlan(query, views);
//}
showSetting(relations, views);
// consider a larger number of views
showStat();
}
static void doOneTestYoli2(String type, String queryFile, String viewFile) {
Vector dummyquery = readViews(queryFile);
Query query = (Query)dummyquery.elementAt(0);
Vector views = readViews(viewFile);
genPlan2(type, query, views);
showStat();
}
static void doOneTest() {
Vector relations = genRelations();
Vector views = genViews(relations, simQueryType);
int oldNum = views.size();
views = groupViews(views);
groupedViewNum = views.size();
// consider a larger number of views
if (groupedViewNum < oldGroupedViewNum) return;
//System.out.println(views);
//System.out.println("oldViewNum = " + oldNum + " newViewNum = " + groupedViewNum);
showSetting(relations, views);
totalNumVTs = 0;
totalGroupedNumVTs = 0; // grouped
totalNumMR = 0;
totalNumGMR = 0;
totalSizeGMR = 0;
totalTimeMR = 0;
totalTimeFirstGMR = 0;
totalTimeAllGMR = 0;
validQueryNum = 0; // # of queries with rewritings
for (int queryNum = 1; queryNum <= simQueryNum; queryNum ++) {
Query query = genQuery(relations, simQueryType);
genPlan(query, views); // calls the algorithms
}
// consider a larger number of views
if (groupedViewNum < oldGroupedViewNum)
return;
oldGroupedViewNum = groupedViewNum;
showStat();
}
/**
* groups different views into equivalence classes
*/
static Vector groupViews(Vector views) {
Vector groups = new Vector();
for (int i = 0; i < views.size(); i ++) {
Query view = (Query) views.elementAt(i);
// search for equivalent views
boolean found = false;
for (int j = 0; j < groups.size(); j ++) {
Query group = (Query) groups.elementAt(j);
if ((group != view) && group.equivalent(view)) {
group.inreaseCount();
found = true;
debug = false;
break;
}
debug = false;
}
if (!found) { // new equivalence class
view.setCount(1);
groups.add(view);
}
}
return groups;
}
static void genPlan(Query query, Vector views) {
HashSet rewritings = null;
UserLib.myprintln("\nThe query is:");
UserLib.myprintln(query.toString() + "\n");
// MiniCon algorithm
/*if (testMiniCon) {*/
long startTime = 0;
long endTime = 0;
long timeMiniCon = 0;
startTime = System.currentTimeMillis();
rewritings = MiniCon.genPlan(query, views);
endTime = System.currentTimeMillis();
timeMiniCon = endTime - startTime;
UserLib.myprintln("rewritings generated by MiniCon():\n");
for (Iterator iter = rewritings.iterator(); iter.hasNext();) {
UserLib.myprintln("rewriting:\n" + (Rewriting) iter.next());
}
UserLib.myprintln("MiniCon time = " + timeMiniCon);
// System.out.print(querySubgoalNum+"\t");
// System.out.print(viewNum+"\t");
System.out.print(MiniCon.numMCDs+"\t");
System.out.print(MiniCon.numRews+"\t");
System.out.print(MiniCon.timeFormMCD+"\t");
System.out.println(MiniCon.timeCombineMCD);
/*}*/
/*
// CoreCover algorithm
rewritings = CoreCover.genPlan(query, views);
//for (Iterator iter = rewritings.iterator(); iter.hasNext();)
//UserLib.myprintln("rewriting:\n" + (Rewriting) iter.next());
if (!rewritings.isEmpty()) {
collectStat();
validQueryNum ++;
}*/
}
static void genPlan2(String type, Query query, Vector views) {
HashSet rewritings = null;
UserLib.myprintln("\nThe query is:");
UserLib.myprintln(query.toString() + "\n");
// MiniCon algorithm
/*if (testMiniCon) {*/
long startTime = 0;
long endTime = 0;
long timeMiniCon = 0;
rewritings = MiniCon.genPlan2(type, query, views);
System.out.print(MiniCon.numMCDs+"\t");
System.out.print(MiniCon.numRews+"\t");
System.out.print(MiniCon.timeFormMCD+"\t");
System.out.println(MiniCon.timeCombineMCD);
}
static void collectStat() {
totalNumVTs += CoreCover.numVTs; // ungrouped
totalGroupedNumVTs += CoreCover.groupedNumVTs; // grouped
totalNumMR += CoreCover.numMR;
totalNumGMR += CoreCover.numGMR;
//System.out.println("CoreCover.numGMR = " + CoreCover.numGMR);
totalSizeGMR += CoreCover.sizeGMR;
totalTimeMR += CoreCover.timeMR;
totalTimeFirstGMR += CoreCover.timeFirstGMR;
totalTimeAllGMR += CoreCover.timeAllGMR;
UserLib.myprintln("# of ungrouped View Tuples = " + CoreCover.numVTs);
UserLib.myprintln("# of MRs = " + CoreCover.numMR);
UserLib.myprintln("# of GMRs = " + CoreCover.numGMR);
UserLib.myprintln("subgoal # of a GMR = " + CoreCover.sizeGMR);
UserLib.myprintln("time of MR = " + CoreCover.timeMR + " ms");
UserLib.myprintln("time of first GMR = " + CoreCover.timeFirstGMR + " ms");
UserLib.myprintln("time of all GMRs = " + CoreCover.timeAllGMR + " ms");
UserLib.myprintln("------------------------------------------------");
}
static void showStat() {
/*System.out.println("# of queries = " + simQueryNum );
System.out.println("# queries w/ rews = " + validQueryNum );
if (validQueryNum == 0) {
System.out.println("------------------------------------------------");
return;
}
System.out.println("# of View Tuples = " + totalNumVTs / validQueryNum );
System.out.println("# of MRs = " + totalNumMR / validQueryNum);
System.out.println("# of GMRs = " + totalNumGMR / validQueryNum);
System.out.println("subgoal # of a GMR = " + totalSizeGMR / validQueryNum);
System.out.println("time of MR = " + totalTimeMR / validQueryNum + " ms");
System.out.println("time of first GMR = " + totalTimeFirstGMR / validQueryNum + " ms");
System.out.println("time of all GMRs = " + totalTimeAllGMR / validQueryNum + " ms");
System.out.println("------------------------------------------------");
*/
// probability of having a rewriting in terms of number of views
//System.out.println(" " + viewNum + " " +
//((float) validQueryNum) / ((float) simQueryNum));
if (validQueryNum != 0) {
// real number of views
System.out.print(" " + viewNum);
// number of distinct views
System.out.print("\t " + groupedViewNum);
// probability of having a rewriting
System.out.print("\t " + (float) validQueryNum / ((float) simQueryNum));
// number of GMRs
System.out.print("\t " + totalNumGMR / validQueryNum);
//number of MRs
//System.out.print("\t " + totalNumMR / validQueryNum);
// time of generating GMRs
System.out.print("\t " + totalTimeAllGMR / validQueryNum);
// time of generating MRs
//System.out.print("\t " + totalTimeMR / validQueryNum);
// # of ungrouped view tuples
System.out.print("\t " + totalNumVTs / validQueryNum);
// # of grouped view tuples
System.out.print("\t " + totalGroupedNumVTs / validQueryNum);
//System.out.print("\t " + validQueryNum);
System.out.println();
} else {
/*System.out.println(" " + viewNum +
"\t " + groupedViewNum +
"\t " + 0 +
"\t " + 0 +
"\t " + 0 +
"\t " + 0 +
"\t " + 0);*/
}
}
static void unitTest() {
test1();
test2();
test3();
}
// create a query: q(M,C) :- car(M,d0), loc(d0,C,C)
// create a view: v(M,D,C,E) :- car(M,D), loc(D,C,E)
// ONLY one rewriting: q(M,C) :- v(M_1,d0,C,C),v(M,d0,C_2,E_2)
static void test1() {
// create a relation
Vector relations = new Vector();
String name = "r"; // relation name
Vector schema = new Vector(); // a vector of attributes
schema = new Vector();
schema.add(new Attribute("M"));
schema.add(new Attribute("D"));
schema.add(new Attribute("C"));
relations.add(new Relation(name, schema));
// create a view: v(M,D,C,E) :- car(M,D), loc(D,C,E)
Vector views = new Vector();
String viewName = "v";
Vector viewHeadArgs = new Vector(); // head: v(M,D,C,E)
viewHeadArgs.add(new Argument("M"));
viewHeadArgs.add(new Argument("D"));
viewHeadArgs.add(new Argument("C"));
viewHeadArgs.add(new Argument("E"));
Subgoal viewHead = new Subgoal(viewName, viewHeadArgs);
Vector viewBody = new Vector();
Vector subgoalArgs = new Vector();
subgoalArgs.add(new Argument("M")); // car(M,D)
subgoalArgs.add(new Argument("D"));
viewBody.add(new Subgoal("car", subgoalArgs));
subgoalArgs = new Vector();
subgoalArgs.add(new Argument("D")); // loc(D,C,E)
subgoalArgs.add(new Argument("C"));
subgoalArgs.add(new Argument("E"));
viewBody.add(new Subgoal("loc", subgoalArgs));
views.add(new Query(viewName, viewHead, viewBody));
// create a query: q(M,C) :- car(M,d0), loc(d0,C,C)
String queryName = "q";
Vector queryHeadArgs = new Vector();
queryHeadArgs.add(new Argument("M")); // head: q(M,C)
queryHeadArgs.add(new Argument("C"));
Subgoal queryHead = new Subgoal(queryName, queryHeadArgs);
Vector queryBody = new Vector();
subgoalArgs = new Vector();
subgoalArgs.add(new Argument("M")); // car(M,d0)
subgoalArgs.add(new Argument("d0"));
queryBody.add(new Subgoal("car", subgoalArgs));
subgoalArgs = new Vector();
subgoalArgs.add(new Argument("d0")); // loc(d0,C,C)
subgoalArgs.add(new Argument("C"));
subgoalArgs.add(new Argument("C"));
queryBody.add(new Subgoal("loc", subgoalArgs));
Query query = new Query(queryName, queryHead, queryBody);
showSetting(relations, views);
genPlan(query, views);
return;
}
/*
Q(X1):-b(Y5,Y4),b(Y5,Y2),b(Y5,Y3),b(Y3,Y2),b(Y4,Y1),a(Y1,X1),a(Y2,X1),a(Y1,Y2)
v1(A1,A2):- b(A1,Z1),b(Z1,A2),b(A1,A2)
v2(B1,B2):- a(B1,B1),a(B1,B2)
There are 8 rewritings.
*/
static void test2() {
// create two relations
Vector relations = new Vector();
String name = "b"; // b(X,Y)
Vector schema = new Vector();
schema.add(new Attribute("X"));
schema.add(new Attribute("Y"));
relations.add(new Relation(name, schema));
name = "a";
schema = new Vector();
schema.add(new Attribute("Z")); // a(Z,W)
schema.add(new Attribute("W"));
relations.add(new Relation(name, schema));
// create a view: v1(A1,A2):- b(A1,Z1),b(Z1,A2),b(A1,A2)
Vector views = new Vector();
String viewName = "v1";
Vector viewHeadArgs = new Vector(); // head: v1(A1,A2)
viewHeadArgs.add(new Argument("A1"));
viewHeadArgs.add(new Argument("A2"));
Subgoal viewHead = new Subgoal(viewName, viewHeadArgs);
Vector viewBody = new Vector();
Vector subgoalArgs = new Vector();
subgoalArgs.add(new Argument("A1"));
subgoalArgs.add(new Argument("Z1")); // b(A1,Z1)
viewBody.add(new Subgoal("b", subgoalArgs));
subgoalArgs = new Vector();
subgoalArgs.add(new Argument("Z1")); // b(Z1,A2)
subgoalArgs.add(new Argument("A2"));
viewBody.add(new Subgoal("b", subgoalArgs));
subgoalArgs = new Vector();
subgoalArgs.add(new Argument("A1")); // b(A1,A2)
subgoalArgs.add(new Argument("A2"));
viewBody.add(new Subgoal("b", subgoalArgs));
views.add(new Query(viewName, viewHead, viewBody));
// create a view: v2(B1,B2):- a(B1,B1),a(B1,B2)
viewName = "v2";
viewHeadArgs = new Vector(); // head: v2(B1,B2)
viewHeadArgs.add(new Argument("B1"));
viewHeadArgs.add(new Argument("B2"));
viewHead = new Subgoal(viewName, viewHeadArgs);
viewBody = new Vector();
subgoalArgs = new Vector();
subgoalArgs.add(new Argument("B1"));
subgoalArgs.add(new Argument("B1")); // a(B1,B1)
viewBody.add(new Subgoal("a", subgoalArgs));
subgoalArgs = new Vector();
subgoalArgs.add(new Argument("B1")); // a(B1,B2)
subgoalArgs.add(new Argument("B2"));
viewBody.add(new Subgoal("a", subgoalArgs));
views.add(new Query(viewName, viewHead, viewBody));
// create a query: Q(X1):-b(Y5,Y4),b(Y5,Y2),b(Y5,Y3),b(Y3,Y2),b(Y4,Y1),a(Y1,X1),a(Y2,X1),a(Y1,Y2)
//String queryName = ((Relation) relations.elementAt(0)).getName();
String queryName = "q";
Vector queryHeadArgs = new Vector();
queryHeadArgs.add(new Argument("X1")); // Q(X1)
//queryHeadArgs.add(new Argument("Y3")); // Q(Y3) !!!!
Subgoal queryHead = new Subgoal(queryName, queryHeadArgs);
Vector queryBody = new Vector();
subgoalArgs = new Vector();
subgoalArgs.add(new Argument("Y5")); // b(Y5,Y4)
subgoalArgs.add(new Argument("Y4"));
queryBody.add(new Subgoal("b", subgoalArgs));
subgoalArgs = new Vector();
subgoalArgs.add(new Argument("Y5"));
subgoalArgs.add(new Argument("Y2")); // b(Y5,Y2),
queryBody.add(new Subgoal("b", subgoalArgs));
subgoalArgs = new Vector();
subgoalArgs.add(new Argument("Y5"));
subgoalArgs.add(new Argument("Y3")); // b(Y5,Y3)
queryBody.add(new Subgoal("b", subgoalArgs));
subgoalArgs = new Vector();
subgoalArgs.add(new Argument("Y3"));
subgoalArgs.add(new Argument("Y2")); // b(Y3,Y2),
queryBody.add(new Subgoal("b", subgoalArgs));
subgoalArgs = new Vector();
subgoalArgs.add(new Argument("Y4"));
subgoalArgs.add(new Argument("Y1")); // b(Y4,Y1)
queryBody.add(new Subgoal("b", subgoalArgs));
subgoalArgs = new Vector();
subgoalArgs.add(new Argument("Y1"));
subgoalArgs.add(new Argument("X1")); // a(Y1,X1)
queryBody.add(new Subgoal("a", subgoalArgs));
subgoalArgs = new Vector();
subgoalArgs.add(new Argument("Y2"));
subgoalArgs.add(new Argument("X1")); // a(Y2,X1)
queryBody.add(new Subgoal("a", subgoalArgs));
subgoalArgs = new Vector();
subgoalArgs.add(new Argument("Y1"));
subgoalArgs.add(new Argument("Y2")); // a(Y1,Y2)
queryBody.add(new Subgoal("a", subgoalArgs));
Query query = new Query(queryName, queryHead, queryBody);
showSetting(relations, views);
genPlan(query, views);
return;
}
// Query: q(U) :- a(U,U),c(U,X),b(X,X)
// Views: v1(A,B) :- a(A,A), b(B,B)
// v2(C,D) :- c(C,D),b(D,D)
// Rewriting: q(U):- v1(U,X),v2(U,X)
static void test3() {
// create a relation
Vector relations = new Vector();
String name = "a"; // relation name
Vector schema = new Vector(); // a vector of attributes
schema = new Vector();
schema.add(new Attribute("A")); // a(A,B)
schema.add(new Attribute("B"));
relations.add(new Relation(name, schema));
name = "b"; // relation name
schema = new Vector(); // a vector of attributes
schema = new Vector();
schema.add(new Attribute("A")); // b(A,B)
schema.add(new Attribute("B"));
relations.add(new Relation(name, schema));
name = "c"; // relation name
schema = new Vector(); // a vector of attributes
schema = new Vector();
schema.add(new Attribute("A")); // c(A,B)
schema.add(new Attribute("B"));
relations.add(new Relation(name, schema));
// create a view: v1(A,B) :- a(A,A), b(B,B)
Vector views = new Vector();
String viewName = "v1";
Vector viewHeadArgs = new Vector(); // head: v1(A,B)
viewHeadArgs.add(new Argument("A"));
viewHeadArgs.add(new Argument("B"));
Subgoal viewHead = new Subgoal(viewName, viewHeadArgs);
Vector viewBody = new Vector();
Vector subgoalArgs = new Vector();
subgoalArgs.add(new Argument("A")); // a(A,A)
subgoalArgs.add(new Argument("A"));
viewBody.add(new Subgoal("a", subgoalArgs));
subgoalArgs = new Vector();
subgoalArgs.add(new Argument("B")); // b(B,B)
subgoalArgs.add(new Argument("B"));
viewBody.add(new Subgoal("b", subgoalArgs));
views.add(new Query(viewName, viewHead, viewBody));
// create 2nd view: v2(C,D) :- c(C,D),b(D,D)
viewName = "v2";
viewHeadArgs = new Vector(); // head: v2(C,D)
viewHeadArgs.add(new Argument("C"));
viewHeadArgs.add(new Argument("D"));
viewHead = new Subgoal(viewName, viewHeadArgs);
viewBody = new Vector();
subgoalArgs = new Vector();
subgoalArgs.add(new Argument("C")); // c(C,D)
subgoalArgs.add(new Argument("D"));
viewBody.add(new Subgoal("c", subgoalArgs));
subgoalArgs = new Vector();
subgoalArgs.add(new Argument("D")); // b(D,D)
subgoalArgs.add(new Argument("D"));
viewBody.add(new Subgoal("b", subgoalArgs));
views.add(new Query(viewName, viewHead, viewBody));
// create Query: q(U) :- a(U,U),c(U,X),b(X,X)
String queryName = "q";
Vector queryHeadArgs = new Vector();
queryHeadArgs.add(new Argument("U")); // head: q(U)
Subgoal queryHead = new Subgoal(queryName, queryHeadArgs);
Vector queryBody = new Vector();
subgoalArgs = new Vector();
subgoalArgs.add(new Argument("U")); // a(U,U)
subgoalArgs.add(new Argument("U"));
queryBody.add(new Subgoal("a", subgoalArgs));
subgoalArgs = new Vector();
subgoalArgs.add(new Argument("U")); // c(U,X),b(X,X)
subgoalArgs.add(new Argument("X"));
queryBody.add(new Subgoal("c", subgoalArgs));
subgoalArgs = new Vector();
subgoalArgs.add(new Argument("X")); // b(X,X)
subgoalArgs.add(new Argument("X"));
queryBody.add(new Subgoal("b", subgoalArgs));
Query query = new Query(queryName, queryHead, queryBody);
showSetting(relations, views);
genPlan(query, views);
return;
}
static Vector readViews(String filename){
try {
parser P = new parser();
FileInputStream fis = new FileInputStream(filename);
System.setIn(fis);
Vector views = (Vector)(P.parse ()).value;
return views;
}catch(FileNotFoundException fnfe){
System.err.println("El archivo " + filename + " no existe");
return null;
}
catch(Exception e){
e.printStackTrace();
return null;
}
}
static void printViews(Vector views, String filename){
FileOutputStream out; // declare a file output object
PrintStream p; // declare a print stream object
try
{
// Create a new file output stream
// connected to "myfile.txt"
out = new FileOutputStream(filename);
// Connect print stream to the output stream
p = new PrintStream( out );
for (int i = 0; i < views.size(); i ++){
// System.out.println(views.elementAt(i).toString());
p.println(views.elementAt(i).toString());
}
p.close();
}
catch (Exception e)
{
System.err.println ("Error writing to file" + filename);
}
}
static void showSetting(Vector relations, Vector views) {
// print out the relations
UserLib.myprintln("------------------------------------------------");
UserLib.myprintln("The relations are:");
for (int i = 0; i < relations.size(); i ++)
UserLib.myprintln(relations.elementAt(i).toString());
UserLib.myprintln("------------------------------------------------");
// print out the views
UserLib.myprintln("\nThe views are:");
for (int i = 0; i < views.size(); i ++)
UserLib.myprintln(views.elementAt(i).toString());
UserLib.myprintln("------------------------------------------------");
UserLib.myprintln("# of views = " + viewNum);
UserLib.myprintln("# of relations = " + relationNum);
UserLib.myprintln("# of rel attrs = " + relAttrNum);
UserLib.myprintln("# of query subgoals = " + querySubgoalNum);
UserLib.myprintln("# of view subgoals = " + viewSubgoalNum);
UserLib.myprintln("# of dropped head args = " + dropHeadArgNum);
UserLib.myprintln("size of arg domain = " + queryArgDomSize + "\n");
UserLib.myprintln("------------------------------------------------");
}
// generate a set of relations
static Vector genRelations() {
Vector relations = new Vector();
for (int i = 1; i <= relationNum; i ++) {
relations.add(new Relation(i));
}
return relations;
}
// generate a set of views
static Vector genViews(Vector relations, int viewType) {
Vector views = new Vector();
for (int i = 1; i <= viewNum; i ++) {
views.add(new Query("v" + i, relations, viewType, viewSubgoalNum));
}
return views;
}
// generate a set of views cc
static Vector genViewsCC(Vector relations, int viewType) {
Vector views = new Vector();
for (int i = 1; i <= viewNum; i ++) {
views.add(new Query("v" + i, relations, viewType, 2*i));
}
return views;
}
// generate a conjunctive query
static Query genQuery(Vector relations, int queryType) {
return new Query("q", relations, queryType, querySubgoalNum);
}
}