-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDataFly.java
442 lines (410 loc) · 17.6 KB
/
DataFly.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
/*
* K-anonymization generalization algorithm as defined by Latanya Sweeney
*/
package datafly;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;
import java.sql.*;
import java.util.Properties;
/**
* @author Dunni Adenuga
*/
public class DataFly {
private Connection conn;
public static void main(String[] args) throws FileNotFoundException, ClassNotFoundException, SQLException {
DataFly dataFly = new DataFly();
/*setting up connection to Database- real DB*/
Class.forName("org.postgresql.Driver");
String url = "jdbc:postgresql://audgendb.c9az8e0qjbgo.us-east-1.rds.amazonaws.com:5432/data";
Properties props = new Properties();
props.setProperty("user", "*****");
props.setProperty("password", "*****");
//props.setProperty("ssl", "true");
//dataFly.conn = DriverManager.getConnection(url, props); //uncomment when connecting to DB
PrivateTable myPrivateTable = dataFly.startGeneralization(dataFly.setup());
//System.out.println("Is the generated table 2-anonymous ? "
//+ dataFly.checkTable(2, myPrivateTable));//this is just to check
myPrivateTable.printFormat();
}
/**
* Set conn = con
* @param con
*/
public void setConn(Connection con){
conn = con;
}
/**
* Checks to see if a given table is k-anonymous
* @param kanon
* @param table
* @return
*/
public boolean checkTable(int kanon, PrivateTable table){
HashMap<ArrayList, Integer> freqSet = getFreqSet(table);
Integer[] freqValues = new Integer[freqSet.size()];
freqValues = freqSet.values().toArray(freqValues);
for(int i = 0; i < freqValues.length; i++){
if(freqValues[i] < kanon){
return false;
}
}
return true;
}
/**
* Create a table and choose quasi identifiers
* @return
* @throws FileNotFoundException
* @throws SQLException
*/
public PrivateTable setup() throws FileNotFoundException, SQLException{
/*Setting up*/
PrivateTable myPrivateTable = new PrivateTable();
//basically set attribute names
myPrivateTable.setRowHeadings("Race,DOB,ID,Sex,Allele 1,Allele 2");/*instead of hard-code, in future should be
user input*/
myPrivateTable.setQuasi("Race,DOB,ID,Sex");// DO THIS LATER!!!
myPrivateTable.setTableValues("/Users/adenugad/NetBeansProjects/kAnonAlgorithms/src/datafly/tableInputs.txt");
//myPrivateTable.setTableValues(conn);//uncomment this when connecting to DB
myPrivateTable = rectifyTableColumn(myPrivateTable, 2);//rectify ID in this case
//myPrivateTable.printFormat();
return myPrivateTable;
}
/**
* Generalize a table
* @param myPrivateTable
* @return
* @throws FileNotFoundException
*/
public PrivateTable startGeneralization(PrivateTable myPrivateTable) throws FileNotFoundException{
System.out.print("Please Enter k: ");
Scanner user = new Scanner(System.in);
int kAnon = user.nextInt();
/*a frequency list contains distinct sequences of values of PT[QI],
along with the number of occurrences of each sequence.*/
HashMap<Integer,Integer> columnsGeneralized = new HashMap<>();
HashMap<ArrayList, Integer> freqSet = getFreqSet(myPrivateTable);
ArrayList<DGHTree> dghTrees = createDGHTrees(myPrivateTable);
int generalizationLevel = 0;
while(seqOccursLessThanKTimes(freqSet, kAnon)){
int colToBeGeneralized = getAttributeWithMostDistinctValues(myPrivateTable, freqSet);//possibleColsToBeGeneralized[0];
if(columnsGeneralized.containsKey(colToBeGeneralized)){
columnsGeneralized.replace(colToBeGeneralized, columnsGeneralized.get(colToBeGeneralized),
columnsGeneralized.get(colToBeGeneralized)+1);
generalizationLevel = columnsGeneralized.get(colToBeGeneralized);
System.out.println("generation level: " + generalizationLevel);
}
else{
columnsGeneralized.put(colToBeGeneralized, 1);
generalizationLevel = 1;
System.out.println("generation level: " + generalizationLevel);
}
//include DGH Tree
//here I can determine thru if statements what generate w/ DGH to run
//I'm assumming Column to be generalized is 0-3
dghTrees.get(colToBeGeneralized).setDGHNodeLevels(dghTrees.get(colToBeGeneralized).root
, dghTrees.get(colToBeGeneralized).getHeight());
myPrivateTable = generateTableWithDGHTable(myPrivateTable, dghTrees.get(colToBeGeneralized),
colToBeGeneralized);
freqSet = getFreqSet(myPrivateTable);
}
myPrivateTable = suppress(myPrivateTable, kAnon);
return myPrivateTable;
}
/**
* Attribute values combination and the number of times they occur
* @param table
* @return row of quasi identifiers and the number of times they occur
* All rows are stored in a hashmap
*/
public HashMap<ArrayList, Integer> getFreqSet(PrivateTable table){
ArrayList<Integer> quasiColNum = getQuasiColNum(table);
//System.out.println(quasiColNum);
//System.out.println("here");
//check quasi
int i = 0;
HashMap<ArrayList, Integer> freqSet = new HashMap<>();
//System.out.println("no of rows in table: " + table.tableRows.size());
while(i < table.tableRows.size()){
//get quasiIden for each row
ArrayList<String> quasiIden = new ArrayList<>();
for(int x = 0; x < quasiColNum.size(); x++){
quasiIden.add(table.tableRows.get(i).data.get(x));
//System.out.println("quasiIden " + quasiIden);
}
if(freqSet.containsKey(quasiIden)){
freqSet.replace(quasiIden, freqSet.get(quasiIden),freqSet.get(quasiIden)+ 1);
}
else{
freqSet.put(quasiIden, 1);
}
i++;
}
return freqSet;
}
/**
* Get the column number of the quasi identifiers
* @param table
* @return
*/
public ArrayList<Integer> getQuasiColNum(PrivateTable table) {
//I have to get column number to get where the quasi identifiers exist
//on the table //compare quasi iden to top row header
ArrayList<Integer> quasiColNum = new ArrayList<>();
for(int i = 0; i < table.quasiIden.data.size(); i++){
for(int j = 0; j < table.topRow.getData().size(); j++){
if((table.quasiIden.data.get(i).compareTo(table.topRow.data.get(j))) == 0){
quasiColNum.add(j);
}
}
}
return quasiColNum;
}
/**
* Old method, assumes values are numeric
* @param oldTable - modify table with specified generalization
* @param columnToGeneralize
* @param generalizationLevel - because I'm assuming numeric data, this determines
* number
* @return newTable
*/
//Domain Hierarchies depend so much on type of data,
//For now, I will assume quasi-Identifiers are numeric data
//I may not need DomainGenHier class anymore
public PrivateTable generateTableWithGen(PrivateTable oldTable, int columnToGeneralize,
int generalizationLevel)
{
System.out.println("genLevel " + generalizationLevel);
PrivateTable newTable = oldTable.copy();
for(int i = 0; i < oldTable.tableRows.size(); i++){
//let me store the new value in a String
String newValue;
String oldValue = oldTable.tableRows.get(i).data.get(columnToGeneralize);
if((generalizationLevel > 1))
{
String oldValue1 = oldValue.substring(0, oldValue.indexOf('*'));
String oldValue2 = oldValue.substring(oldValue.indexOf('*'));
if(oldValue1.length()-generalizationLevel > 0){
newValue = oldValue1.substring(0, oldValue1.length()-generalizationLevel
) + "*";
}
else{
newValue = oldValue1.substring(0, oldValue1.length()) + "*";
}
newValue = newValue + oldValue2;
}
else{
newValue = oldValue.substring(0, oldValue.length()-generalizationLevel
) + "*";
}
newTable.tableRows.get(i).data.set(columnToGeneralize,
newValue);
}
return newTable;
}
/**
* Makes sure values in a specified column of table have same number of
* characters
* @param oldTable
* @param columnToRectify
* @return
*/
public PrivateTable rectifyTableColumn(PrivateTable oldTable, int columnToRectify){
PrivateTable newTable = oldTable.copy();
int max = newTable.tableRows.get(0).data.get(columnToRectify).length();
for(int i = 1; i < newTable.tableRows.size(); i++){
if(newTable.tableRows.get(i).data.get(columnToRectify).length() > max){
max = newTable.tableRows.get(i).data.get(columnToRectify).length();
}
}
for(int i = 0; i < newTable.tableRows.size(); i++){
if(newTable.tableRows.get(i).data.get(columnToRectify).length() < max){
String attache = "";
for(int j = 0; j < (max - newTable.tableRows.get(i).data.get(columnToRectify).length()); j++){
attache = attache + "0";
}
newTable.tableRows.get(i).data.set(columnToRectify, attache + newTable.tableRows.get(i).data.get(columnToRectify));
}
}
return newTable;
}
/**
* Use DGH to generalize a table
* @param oldTable
* @param dghTree
* @param columnToGeneralize
* @return
* @throws FileNotFoundException
*/
public PrivateTable generateTableWithDGHTable(PrivateTable oldTable, DGHTree dghTree, int columnToGeneralize) throws FileNotFoundException{
PrivateTable newTable = oldTable.copy();
for(int i = 0; i < oldTable.tableRows.size();i++){
String newElement = dghTree.getGeneralization(newTable.tableRows.get(i).data.get(columnToGeneralize));
newTable.tableRows.get(i).data.set(columnToGeneralize, newElement);
}
return newTable;
}
/**
* Create DGH Trees for a table's quasi identifiers
* This is entirely based on the quasi Identifiers
* This method is assuming they are in the order Race,DOB , ID,Sex in the
* returned table from the database so, 0,1,2,3
* Has to be modified depending on database and attributes..
* Some elements are universal
* @param table - need to read actual values of DOB and ID from the table
* @return
*/
public ArrayList<DGHTree> createDGHTrees(PrivateTable table) throws FileNotFoundException{
ArrayList<DGHTree> dghTrees = new ArrayList<>();
String header = "/Users/adenugad/NetBeansProjects/kAnonAlgorithms/src/datafly/";
//create DGH for Race
DGHTree dghTreeRace = new DGHTree(header + "dghRace");
dghTreeRace.setWeight(0);
dghTreeRace.setLabel("Race");
dghTreeRace.setHeight();
dghTreeRace.setDGHNodeLevels(dghTreeRace.root, dghTreeRace.getHeight()-1);
dghTrees.add(dghTreeRace);
//create DGH for DOB
ArrayList<String> dates = new ArrayList<>();
for(int i = 0; i < table.tableRows.size(); i++){
dates.add(table.tableRows.get(i).data.get(1));
}
DGHTree dghTreeDOB = new DGHTree();
dghTreeDOB = dghTreeDOB.createRangesDatesDGHTrees(dates);
dghTreeDOB.setWeight(1);
dghTreeDOB.setLabel("DOB");
dghTreeDOB.setHeight();
dghTreeDOB.setDGHNodeLevels(dghTreeDOB.root, dghTreeDOB.getHeight()-1);
dghTrees.add(dghTreeDOB);
//create DGH for ID
ArrayList<String> ids = new ArrayList<>();
for(int i = 0; i < table.tableRows.size(); i++){
ids.add(table.tableRows.get(i).data.get(2));
}
DGHTree dghTreeID = new DGHTree();
dghTreeID = dghTreeID.createDGHTree(ids);
dghTreeID.setLabel("ID");
dghTreeID.setWeight(0.5);
dghTreeID.setHeight();
dghTreeID.setDGHNodeLevels(dghTreeID.root, dghTreeID.getHeight()-1);
dghTrees.add(dghTreeID);
//create DGH for Sex
DGHTree dghTreeSex = new DGHTree(header + "dghSex");
dghTreeSex.setWeight(0);
dghTreeSex.setLabel("Sex");
dghTreeSex.setHeight();
dghTreeSex.setDGHNodeLevels(dghTreeSex.root, dghTreeSex.getHeight()-1);
dghTrees.add(dghTreeSex);
return dghTrees;
}
/**
* Find Attribute with most distinct values
* @param table
* @param freqList
* @return
*/
public int getAttributeWithMostDistinctValues(PrivateTable table, HashMap<ArrayList, Integer> freqList){
String attribute /*attribute2*/ ;
int attributeColumn = 0;
//int attributeColumn2 = 0;
TableRow quasiId = table.quasiIden;
//I will make a quasi identifier list of Lists (of all the unique values)
ArrayList<ArrayList> quasiIden = new ArrayList<>();
for (int i = 0; i < quasiId.data.size(); i++){
//it has the list of all values for every quasi identifier column
quasiIden.add(new ArrayList<>());//wtf does this do again
}
ArrayList[] setOfKeys = new ArrayList[freqList.size()];//freqList has distinct keys but it's in row form
setOfKeys = freqList.keySet().toArray(setOfKeys);
for(int i = 0; i < setOfKeys.length; i++){
for(int j = 0; j < setOfKeys[i].size(); j++){
if(quasiIden.get(j).contains(setOfKeys[i].get(j)) == false)
quasiIden.get(j).add(setOfKeys[i].get(j));
}
}
int max = 0;
//int secondMax = 0;
for(int i = 0; i < quasiIden.size(); i++){
if(quasiIden.get(i).size() > max ){
max = quasiIden.get(i ).size();
attributeColumn = i;
}
}
System.out.println("attributeColumn - " + attributeColumn);
attribute = quasiId.data.get(attributeColumn);
System.out.println(attribute);
return table.topRow.data.indexOf(attribute);
}
/**
* Checks if sequence of quasi Identifiers in a freqSet >= kAnon
* @param freqSet
* @param kAnon
* @return
*/
public boolean seqOccursLessThanKTimes(HashMap<ArrayList, Integer> freqSet, int kAnon){
Integer[] freqValues = new Integer[freqSet.size()];
freqValues = freqSet.values().toArray(freqValues);
int noOfTuplesWithDistinctSequences = 0;
for (int i = 0; i < freqValues.length; i++){
/*if(freqValues[i] < kAnon)
return true;*/
if(freqValues[i] == 1)
{
noOfTuplesWithDistinctSequences++;
//System.out.println("noOfTuplesWithDistinctSequences: " + noOfTuplesWithDistinctSequences);
}
if(noOfTuplesWithDistinctSequences >= kAnon)
return true;
}
return false;
}
/**
* Suppresses outliers
* @param table
* @param kAnon
* @return
*/
public PrivateTable suppress(PrivateTable table, int kAnon){
/* if max level of generalization is reached, then you suppress ?
Why do this when I have a while that doesn't let up until generalization is reached, how
do I combine them
*/
ArrayList<ArrayList> sequencesToSuppress = new ArrayList<>();
ArrayList <Integer> quasiIdenCol = getQuasiColNum(table);
PrivateTable newTable = table.copy();
HashMap<ArrayList, Integer> freqSet = getFreqSet(newTable);
ArrayList[] setOfKeys = new ArrayList[freqSet.size()];
setOfKeys = freqSet.keySet().toArray(setOfKeys);
for(int i = 0; i < setOfKeys.length; i++){
if(freqSet.get(setOfKeys[i]) < kAnon){
sequencesToSuppress.add(setOfKeys[i]);
}
}
//assuming the number of rows to be suppressed be less than kAnon
for(int j = 0; j < sequencesToSuppress.size(); j++){
//System.out.println("sequencesToSuppress" + sequencesToSuppress);
int k = 0;
while(k < newTable.tableRows.size()){
int oldSize = newTable.tableRows.size();
ArrayList<String> quasiIdenVal = new ArrayList<>();
//= newTable.tableRows.get(k).data;
for(int m = 0; m < quasiIdenCol.size(); m++){
quasiIdenVal.add(newTable.tableRows.get(k).data.get(quasiIdenCol.get(m)));
}
if(sequencesToSuppress.get(j).equals(quasiIdenVal)){
//System.out.println("k - " + k);
newTable.tableRows.remove(k);
}
if(oldSize > newTable.tableRows.size())
{
k = 0;
}else{
k++;
}
//k++;
}
}
return newTable;
}
}