-
Notifications
You must be signed in to change notification settings - Fork 1
/
Tuple.java
167 lines (137 loc) · 3.92 KB
/
Tuple.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
/*
* Tuple.java
* -------------
* $Id: Tuple.java,v 1.8 2000/11/23 22:22:44 chenli Exp $
*/
import java.util.*;
class Tuple {
String name = null;
Vector args = null; // a list of arguments
Mapping phi = null; // mapping from query args to the database deriving
// the tuple
Subgoal subgoal = null; // the subgoal that produces this tuple in canDb.
HashMap mapSubgoals = null;
Query query = null;
HashSet core = new HashSet();
HashSet equTuples = new HashSet(); // set of view tuples with the same tuple-core
Tuple(Subgoal subgoal) {
this.subgoal = subgoal;
this.name = subgoal.getName();
this.args = (Vector) subgoal.getArgs().clone();
}
Tuple(String name, Vector args, Mapping phi, HashMap mapSubgoals) {
this.name = name;
this.args = args;
this.phi = phi;
this.mapSubgoals = mapSubgoals;
}
public String getName() {
return name;
}
public Vector getArgs() {
return args;
}
public Mapping getMapping() {
return phi;
}
public Query getQuery() {
return query;
}
public Subgoal getSubgoal() {
return subgoal;
}
public HashMap getMapSubgoals() {
return mapSubgoals;
}
// set the query of the tuple, and compute the head of each tuple
public void setQuery(Query query) {
this.query = query;
this.name = query.getName();
// compute the new head under the phi
Subgoal head = query.getHead();
if (head.size() != args.size())
UserLib.myerror("Tuple.setQuery(), wrong args!");
Subgoal newHead = phi.apply(head);
args = newHead.getArgs(); // set the new args
}
public void setCore(HashSet core) {
this.core = core;
}
public HashSet getCore() {
return core;
}
/**
* gets the target query subgoals under the mapping
*/
public HashSet getTargetSubgoals() {
HashSet result = new HashSet();
Set entrySet = (Set) mapSubgoals.entrySet();
for (Iterator iter = entrySet.iterator(); iter.hasNext();) {
Map.Entry mapEntry = (Map.Entry) iter.next();
Subgoal querySubgoal = (Subgoal) mapEntry.getValue();
result.add(querySubgoal);
}
return result;
}
/**
* Indicates whether some other tuple is "equal to" this one.
* Notice that the parameter must be an "Object" class in order to
* override Object.equals(), not an "Tuple" class.
*
* @param argument The reference object with which to compare.
* @return <code>true(false)</code> if this item is the same(not same) as
* the argument.
*/
public boolean equals(Object tuple){
Tuple t = (Tuple) tuple;
// same name
if (!this.getName().equals(t.getName())) return false;
Vector tArgs = t.getArgs();
if (args.size() != tArgs.size()) return false;
for (int i = 0; i < args.size(); i ++) {
Argument arg = (Argument) args.elementAt(i);
Argument tArg = (Argument) tArgs.elementAt(i);
if (!arg.equals(tArg)) return false;
}
return true;
}
/**
*
*/
public boolean sameCore(Tuple viewTuple) {
return this.getCore().equals(viewTuple.getCore());
}
/**
* adds itself to the equivalence class
*/
public void setEquSelf() {
equTuples.add(this); // add itself
}
/**
* adds a view tuple to the equivalence class
*/
public void addEquTuple(Tuple viewTuple) {
equTuples.add(viewTuple);
}
/**
* Overrides the hash function Object.hasCode(). As an item in a
* HashSet collection, called when HashSet.equals() is called.
*/
public int hashCode() {
int hashCode = this.getName().hashCode();
for (int i = 0; i < args.size(); i ++) {
Argument arg = (Argument) args.elementAt(i);
hashCode += arg.hashCode();
}
return hashCode;
}
public String toString() {
StringBuffer result = new StringBuffer();
if (name != null)
result.append(name);
result.append(args.toString());
/*if (phi != null)
result.append("; " + phi.toString());*/
return (result.toString());
}
}