-
Notifications
You must be signed in to change notification settings - Fork 1
/
Relation.java
131 lines (110 loc) · 2.93 KB
/
Relation.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
/*
* Relation.java
* -------------
* $Id: Relation.java,v 1.12 2000/11/25 02:34:37 chenli Exp $
*/
import java.util.*;
class Relation {
String name = "_unknown_"; // relation name
Vector schema = new Vector(); // a vector of attributes
HashSet tuples = new HashSet();
/**
* Creates a relation.
*/
Relation(String name) {
this.name = name;
}
/**
* Creates a relation given an id.
*/
Relation(int relIndex) {
name = new String("r" + relIndex);
schema = new Vector();
int attrNum = GoodPlan.relAttrNum;
for (int i = 1; i <= attrNum; i ++) {
String attrName = UserLib.getChar(i) + i;
schema.add(new Attribute(attrName));
}
}
/**
* Constructor
*/
Relation(String name, Vector schema, HashSet tuples) {
this.name = name;
this.schema = schema;
this.tuples = tuples;
}
/**
* Constructor
*/
Relation(String name, Vector schema) {
this.name = name;
this.schema = schema;
}
public Vector getSchema() {
return schema;
}
public String getName() {
return name;
}
public int getAttrNum() {
return schema.size();
}
public HashSet getTuples() {
return tuples;
}
/**
* set the query of each tuple
*/
public void setQuery(Query query) {
for (Iterator iter = tuples.iterator(); iter.hasNext();) {
Tuple tuple = (Tuple) iter.next();
tuple.setQuery(query);
}
}
public Relation project(Vector usefulArgs) {
Vector resSchema = new Vector();
HashSet resTuples = new HashSet();
// computes the schema
for (int i = 0; i < schema.size(); i ++) {
Argument arg = (Argument) schema.elementAt(i);
if (usefulArgs.contains(arg))
resSchema.add(arg);
}
// computes the tuples
for (Iterator iter = tuples.iterator(); iter.hasNext();) {
Tuple tuple = (Tuple) iter.next();
Vector resTupleArgs = new Vector();
for (int i = 0; i < schema.size(); i ++) {
Argument argSchema = (Argument) schema.elementAt(i);
Argument argVale = (Argument) tuple.getArgs().elementAt(i);
if (usefulArgs.contains(argSchema))
resTupleArgs.add(argVale);
}
// yields one tuple
resTuples.add(new Tuple(name, resTupleArgs,
tuple.getMapping(), tuple.getMapSubgoals()));
}
return new Relation(this.getName(), resSchema, resTuples);
}
public String toString() {
StringBuffer result = new StringBuffer(name);
result.append("(");
for (int i = 0; i < schema.size(); i ++) {
result.append(schema.elementAt(i).toString());
if ( i < schema.size() - 1)
result.append(",");
}
result.append(")");
if (tuples.isEmpty()) {
//result.append(", tuples = empty");
} else {
result.append("\n");
for (Iterator iter = tuples.iterator(); iter.hasNext();) {
Tuple tuple = (Tuple) iter.next();
result.append("\t" + tuple.toString() + "\n");
}
}
return result.toString();
}
}