1
+ package commitminer .classify ;
2
+
3
+ import commitminer .analysis .Commit ;
4
+ import commitminer .analysis .FeatureVector ;
5
+ import commitminer .analysis .Commit .Type ;
6
+
7
+ /**
8
+ * Stores a feature vector for a pattern query.
9
+ *
10
+ * The feature vector includes information that can be used to localize the
11
+ * pattern, and a description of the patterns itself.
12
+ *
13
+ * Patterns must be specified as Datalog queries passed to the constructor.
14
+ */
15
+ public class ClassifierFeatureVector extends FeatureVector {
16
+
17
+ /** The version of the file (SOURCE or DESTINATION) **/
18
+ public String version ;
19
+
20
+ /** The class that was analyzed (if at or below class granularity). **/
21
+ public String klass ;
22
+
23
+ /** The method that was analyzed (if at or below method granularity). **/
24
+ public String method ;
25
+
26
+ /** The line number for the alert. **/
27
+ public String line ;
28
+
29
+ /** The absolute position of the alert in the file. **/
30
+ public String absolutePosition ;
31
+
32
+ /** The length of the highlighting in the file. **/
33
+ public String length ;
34
+
35
+ /** The type of pattern found. **/
36
+ public String type ;
37
+
38
+ /** The subtype of pattern found. **/
39
+ public String subtype ;
40
+
41
+ /** A description of the pattern found. **/
42
+ public String description ;
43
+
44
+ /**
45
+ * @param commit The commit that the features were extracted from.
46
+ * @param klass The class that the features were extracted from.
47
+ * @param method The method that the features were extracted from.
48
+ */
49
+ @ Deprecated
50
+ public ClassifierFeatureVector (Commit commit , String version ,
51
+ String klass , String method ,
52
+ String line ,
53
+ String type , String subtype ,
54
+ String description ) {
55
+ super (commit );
56
+ this .version = version ;
57
+ this .klass = klass ;
58
+ this .method = method ;
59
+ this .line = line ;
60
+ this .absolutePosition = "0" ;
61
+ this .length = "0" ;
62
+ this .type = type ;
63
+ this .subtype = subtype ;
64
+ this .description = description ;
65
+ }
66
+
67
+ /**
68
+ * @param commit The commit that the features were extracted from.
69
+ * @param klass The class that the features were extracted from.
70
+ * @param method The method that the features were extracted from.
71
+ */
72
+ public ClassifierFeatureVector (Commit commit , String version ,
73
+ String klass , String method ,
74
+ String line ,
75
+ String absolutePosition ,
76
+ String length ,
77
+ String type , String subtype ,
78
+ String description ) {
79
+ super (commit );
80
+ this .version = version ;
81
+ this .klass = klass ;
82
+ this .method = method ;
83
+ this .line = line ;
84
+ this .absolutePosition = absolutePosition ;
85
+ this .length = length ;
86
+ this .type = type ;
87
+ this .subtype = subtype ;
88
+ this .description = description ;
89
+ }
90
+
91
+ /**
92
+ * This constructor should only be used if making a feature vector from
93
+ * serial. Otherwise the other constructor should be used so the ID is
94
+ * automatically generated.
95
+ * @param commit The commit that the features were extracted from.
96
+ * @param klass The class that the features were extracted from.
97
+ * @param method The method that the features were extracted from.
98
+ * @param id The unique id for the alert.
99
+ */
100
+ public ClassifierFeatureVector (Commit commit , String version ,
101
+ String klass , String method ,
102
+ String line , String type , String subtype ,
103
+ String description , int id ) {
104
+ super (commit , id );
105
+ this .version = version ;
106
+ this .klass = klass ;
107
+ this .method = method ;
108
+ this .line = line ;
109
+ this .type = type ;
110
+ this .subtype = subtype ;
111
+ this .description = description ;
112
+ }
113
+
114
+ /**
115
+ * This method serializes the alert. This is useful when writing
116
+ * a data set to the disk.
117
+ * @return The serialized version of the alert.
118
+ * Has the format [ID, ProjectID, URL, BuggyCommit, RepairedCommit, [KeywordList]]
119
+ * where KeywordList = [Type:Context:ChangeType:Package:Keyword:COUNT].
120
+ */
121
+ public String serialize () {
122
+
123
+ String serialized = id + "," + this .commit .projectID
124
+ + "," + this .commit .commitMessageType .toString ()
125
+ + "," + this .commit .url + "/commit/" + this .commit .repairedCommitID
126
+ + "," + this .commit .buggyCommitID + "," + this .commit .repairedCommitID
127
+ + "," + this .version
128
+ + "," + this .klass + "," + this .method
129
+ + "," + this .line
130
+ + "," + this .absolutePosition
131
+ + "," + this .length
132
+ + "," + this .type
133
+ + "," + this .subtype + "," + this .description ;
134
+
135
+ return serialized ;
136
+
137
+ }
138
+
139
+ /**
140
+ * This method de-serializes a feature vector. This is useful when reading
141
+ * a data set from the disk.
142
+ * @param serialized The serialized version of a feature vector.
143
+ * @return The feature vector represented by {@code serialized}.
144
+ */
145
+ public static ClassifierFeatureVector deSerialize (String serialized ) throws Exception {
146
+
147
+ String [] features = serialized .split ("," );
148
+
149
+ if (features .length < 8 ) throw new Exception ("De-serialization exception. Serial format not recognized." );
150
+
151
+ Commit commit = new Commit (features [1 ], features [3 ], features [4 ],
152
+ features [5 ], Type .valueOf (features [2 ]));
153
+
154
+ ClassifierFeatureVector featureVector = new ClassifierFeatureVector (commit ,
155
+ features [6 ], features [7 ],
156
+ features [8 ], features [9 ], features [10 ],
157
+ features [11 ], features [12 ],
158
+ Integer .parseInt (features [0 ]));
159
+
160
+ return featureVector ;
161
+
162
+ }
163
+
164
+ @ Override
165
+ public String toString () {
166
+ return this .serialize ();
167
+ }
168
+
169
+ @ Override
170
+ public boolean equals (Object o ) {
171
+ if (o instanceof ClassifierFeatureVector ) {
172
+ ClassifierFeatureVector sa = (ClassifierFeatureVector )o ;
173
+ if (this .commit .equals (sa .commit )
174
+ && this .version .equals (sa .version )
175
+ && this .klass .equals (sa .klass )
176
+ && this .method .equals (sa .method )
177
+ && this .line .equals (sa .line )
178
+ && this .absolutePosition .equals (sa .absolutePosition )
179
+ && this .length .equals (sa .length )
180
+ && this .type .equals (sa .type )
181
+ && this .subtype .equals (sa .subtype )
182
+ && this .description .equals (sa .description )) return true ;
183
+ }
184
+ return false ;
185
+ }
186
+
187
+ @ Override
188
+ public int hashCode () {
189
+ return (this .commit .projectID + this .commit .repairedCommitID + this .klass + this .method ).hashCode ();
190
+ }
191
+
192
+ }
0 commit comments