Projekt

Obecné

Profil

Stáhnout (13.2 KB) Statistiky
| Větev: | Revize:
1
package vldc.aswi.utils;
2

    
3
import vldc.aswi.model.table.NameUserName;
4
import vldc.aswi.model.table.Node;
5
import vldc.aswi.model.table.TableColumn;
6
import vldc.aswi.model.table.ValueFunction;
7
import vldc.aswi.model.table.contingencyTable.ContingencyTableRow;
8
import vldc.aswi.model.table.contingencyTable.ContingencyTableRowCell;
9

    
10
import javax.persistence.criteria.CriteriaBuilder;
11
import java.util.ArrayList;
12
import java.util.List;
13
import java.util.Map;
14
import java.util.concurrent.TimeUnit;
15

    
16
/**
17
 * Class for converting classes.
18
 */
19
public class Converter {
20

    
21
    private List<Node> rowNodes;
22
    private List<Node> columnNodes;
23

    
24
    /**
25
     * Convert Map of TableColumns into list of ContingencyTableRow.
26
     * @param tableColumns - Map of TableColumn.
27
     * @return List of ContingencyTableRow.
28
     */
29
    public List<ContingencyTableRow> convertTableColumnsToContingencyTableRows(Map<String, TableColumn> tableColumns,
30
                                                                                      List<NameUserName> rowNames,
31
                                                                                      List<NameUserName> columnNames,
32
                                                                               List<ValueFunction> valueFunctions) {
33
        long startTime = System.nanoTime();
34
        List<ContingencyTableRow> contingencyTableRows = new ArrayList<>();
35

    
36
        long startTime2 = System.nanoTime();
37
        List<ContingencyTableRow> headerRows = createHeaderOnlyRows(tableColumns, columnNames, valueFunctions);
38
        long stopTime2 = System.nanoTime();
39
        System.out.println("Headers:" + TimeUnit.MILLISECONDS.convert((stopTime2 - startTime2), TimeUnit.NANOSECONDS));
40

    
41
        long startTime3 = System.nanoTime();
42
        List<ContingencyTableRow> dataRows = createDataRows(tableColumns, columnNames, rowNames, valueFunctions);
43
        long stopTime3 = System.nanoTime();
44
        System.out.println("Rows:" + TimeUnit.MILLISECONDS.convert((stopTime3 - startTime3), TimeUnit.NANOSECONDS));
45

    
46
        contingencyTableRows.addAll(headerRows);
47
        contingencyTableRows.addAll(dataRows);
48

    
49
        long stopTime = System.nanoTime();
50
        System.out.println("Converter:" + TimeUnit.SECONDS.convert((stopTime - startTime), TimeUnit.NANOSECONDS) + "s");
51

    
52
        return contingencyTableRows;
53
    }
54

    
55
    /**
56
     * Generates header rows.
57
     * @param tableColumns Map of TableColumn
58
     * @param columnNames list of column and user column names
59
     * @param valueFunctions list of values and their functions
60
     * @return list of contingency table rows
61
     */
62
    private List<ContingencyTableRow> createHeaderOnlyRows(Map<String, TableColumn> tableColumns,
63
                                                           List<NameUserName> columnNames, List<ValueFunction> valueFunctions) {
64
        List<List<String>> values = new ArrayList<>();
65
        columnNodes = new ArrayList<>();
66
        List<ContingencyTableRow> headerRows = new ArrayList<>();
67

    
68
        List<String> valueOptions = new ArrayList<>();
69
        for (ValueFunction valueFunction : valueFunctions) {
70
            valueOptions.add(valueFunction.getFunction() + " z " + valueFunction.getValue());
71
        }
72
        values.add(valueOptions);
73
        headerRows.add(new ContingencyTableRow(true, 0));
74

    
75
        for(NameUserName nameUserName : columnNames) {
76
            TableColumn column = tableColumns.get(nameUserName.getName());
77
            values.add(getOriginalValuesFromList(column.getValues()));
78
            headerRows.add(new ContingencyTableRow(true, 0));
79
        }
80

    
81
        for (int i = 0; i < values.size(); i++) {
82
            int colSpan = 1;
83
            for (int j = values.size() - 1; j > i; j--) {
84
                if (values.size() - 1 == j) {
85
                    colSpan = values.get(j).size();
86
                }
87
                else {
88
                    colSpan = (colSpan + 1) * values.get(j).size();
89
                }
90
            }
91
            int loopCount = 1;
92
            for (int k = 0; k < i; k++) {
93
                loopCount = loopCount * values.get(k).size();
94
            }
95
            headerRows.get(i).addTableRowCell(new ContingencyTableRowCell("", 1));
96
            for (int k = 0; k < loopCount; k++) {
97
                for (String value : values.get(i)) {
98
                    headerRows.get(i).addTableRowCell(new ContingencyTableRowCell(value, colSpan));
99
                    if (i != values.size() - 1 && i != 0) {
100
                        headerRows.get(i).addTableRowCell(new ContingencyTableRowCell(value + " Celkem", 1));
101
                    }
102
                }
103

    
104
                for (int l = 0; l < i - 1; l++) {
105
                    headerRows.get(i).addTableRowCell(new ContingencyTableRowCell("", 1));
106
                }
107
            }
108
            for(ValueFunction valueFunction: valueFunctions) {
109
                if (i == 0) {
110
                    headerRows.get(i).addTableRowCell(new ContingencyTableRowCell("Celkový " + valueFunction.getFunction() +
111
                            " z " + valueFunction.getValue(), 1));
112
                } else {
113
                    headerRows.get(i).addTableRowCell(new ContingencyTableRowCell("", 1));
114
                }
115
            }
116
        }
117

    
118
        long startTime = System.nanoTime();
119
        values.remove(0);
120
        Node node = new Node();
121
        generateColumns(0, values, columnNodes, node);
122
        long stopTime = System.nanoTime();
123
        System.out.println("Colums list:" + TimeUnit.MILLISECONDS.convert((stopTime - startTime), TimeUnit.NANOSECONDS));
124

    
125
        return headerRows;
126
    }
127

    
128
    /**
129
     * Gets original values of a given list.
130
     * @param list list from which original values should be taken
131
     * @return list of original values
132
     */
133
    private List<String> getOriginalValuesFromList(List<String> list) {
134
        List<String> origList = new ArrayList<>();
135

    
136
        for (String string : list) {
137
            if (origList.stream().noneMatch(s -> s.equals(string))) {
138
                origList.add(string);
139
            }
140
        }
141

    
142
        return origList;
143
    }
144

    
145
    /**
146
     * Creates data rows.
147
     * @param tableColumns Map of TableColumn
148
     * @param columnNames list of column and user column names
149
     * @param rowNames list of row and user row names
150
     * @param valueFunctions list of values and their functions
151
     * @return list of contingency table rows
152
     */
153
    private List<ContingencyTableRow> createDataRows(Map<String, TableColumn> tableColumns, List<NameUserName> columnNames,
154
                                                     List<NameUserName> rowNames, List<ValueFunction> valueFunctions) {
155
        List<ContingencyTableRow> dataRows = new ArrayList<>();
156
        rowNodes = new ArrayList<>();
157
        List<List<String>> values = new ArrayList<>();
158
        for(NameUserName nameUserName : rowNames) {
159
            TableColumn column = tableColumns.get(nameUserName.getName());
160
            values.add(getOriginalValuesFromList(column.getValues()));
161
        }
162

    
163
        long startTime = System.nanoTime();
164
        Node node = new Node();
165
        generateRows(dataRows, 0, values, rowNodes, node);
166
        long stopTime = System.nanoTime();
167
        System.out.println("Rows List:" + TimeUnit.MILLISECONDS.convert((stopTime - startTime), TimeUnit.NANOSECONDS));
168

    
169
        for (int i = 0; i < dataRows.size(); i++) {
170
            fillRowWithData(dataRows.get(i), tableColumns, rowNodes.get(i), rowNames, columnNames, valueFunctions);
171
        }
172

    
173
        return dataRows;
174
    }
175

    
176
    /**
177
     * Recursively generates all possible row combinations, sorted as in contingency table.
178
     * @param rows list of sorted contingency table rows
179
     * @param index index in the values list
180
     * @param values list of lists of values of each query row
181
     * @param rowNodes list of row nodes
182
     * @param prevNode parent node of the nodes
183
     */
184
    private void generateRows(List<ContingencyTableRow> rows, int index, List<List<String>> values, List<Node> rowNodes,
185
                              Node prevNode) {
186
        if (index < values.size()) {
187
            List<String> list = values.get(index);
188
            for (String s : list) {
189
                StringBuilder levelString = new StringBuilder();
190
                for (int i = 0; i < index; i++) {
191
                    levelString.append("----");
192
                }
193
                levelString.append(s);
194
                ContingencyTableRow row = new ContingencyTableRow(false, index);
195
                row.addTableRowCell(new ContingencyTableRowCell(levelString.toString(), 1));
196
                rows.add(row);
197
                Node node = new Node(prevNode);
198
                node.getValues().add(s);
199
                rowNodes.add(node);
200
                int newIndex = index + 1;
201
                generateRows(rows, newIndex, values, rowNodes, node);
202
            }
203
        }
204
    }
205

    
206
    /**
207
     * Recursively generates all possible column combinations, sorted as in contingency table.
208
     * @param index index in the values list
209
     * @param values list of lists of values of each query row
210
     * @param columnNodes list of column nodes
211
     * @param prevNode parent node of the nodes
212
     */
213
    private void generateColumns(int index, List<List<String>> values, List<Node> columnNodes, Node prevNode) {
214
        if (index < values.size()) {
215
            List<String> list = values.get(index);
216
            for (String s : list) {
217
                Node node = new Node(prevNode);
218
                node.getValues().add(s);
219
                int newIndex = index + 1;
220
                generateColumns(newIndex, values, columnNodes, node);
221
                columnNodes.add(node);
222
            }
223
        }
224
    }
225

    
226
    /**
227
     * Fills each data row with data.
228
     * @param row row to ne filled with data
229
     * @param tableColumns Map of TableColumn
230
     * @param node node of the row
231
     * @param rowNames list of row names and user names
232
     * @param columnNames list of column names and user names
233
     * @param valueFunctions list of values and their functions
234
     */
235
    private void fillRowWithData(ContingencyTableRow row, Map<String, TableColumn> tableColumns, Node node,
236
                                 List<NameUserName> rowNames, List<NameUserName> columnNames, List<ValueFunction> valueFunctions) {
237
        if (columnNodes.size() == 0) {
238
            columnNodes.add(new Node());
239
        }
240

    
241
        List<Integer> usableIDs = getUsableIDs(tableColumns, node, rowNames);
242

    
243
        int[] totals = new int[valueFunctions.size()];
244
        //hodnota
245
        for (int v = 0; v < valueFunctions.size(); v++) {
246
            ValueFunction valueFunction = valueFunctions.get(v);
247
            //sloupec v kont. tabulce
248
            for (Node columnNode : columnNodes) {
249
                int result = 0;
250
                // radek v orig. tabulce
251
                for (Integer usableID : usableIDs) {
252
                    boolean isUsable = true;
253
                    int j;
254
                    // zvoleny sloupec ze sloupcu
255
                    for (j = 0; j < columnNode.getValues().size(); j++) {
256
                        if (!tableColumns.get(columnNames.get(j).getName()).getValues().get(usableID).equals(columnNode.getValues().get(j))) {
257
                            isUsable = false;
258
                            break;
259
                        }
260
                    }
261
                    // zvoleny sloupec z hodnot
262
                    if (tableColumns.get(valueFunction.getValue()).getValues().get(usableID) == null) {
263
                        isUsable = false;
264
                    }
265

    
266
                    if (isUsable) {
267
                        if (valueFunction.getFunction().equals("COUNT")) {
268
                            result++;
269
                        } else if (valueFunction.getFunction().equals("SUM")) {
270
                            result += Integer.parseInt(tableColumns.get(valueFunction.getValue()).getValues().get(usableID));
271
                        }
272
                        if (j - 1 == 0 || j == 0) {
273
                            if (valueFunction.getFunction().equals("COUNT")) {
274
                                totals[v]++;
275
                            } else if (valueFunction.getFunction().equals("SUM")) {
276
                                totals[v] += Integer.parseInt(tableColumns.get(valueFunction.getValue()).getValues().get(usableID));
277
                            }
278
                        }
279
                    }
280
                }
281
                row.addTableRowCell(new ContingencyTableRowCell(Integer.toString(result), 0));
282
            }
283
        }
284
        for (int total : totals) {
285
            row.addTableRowCell(new ContingencyTableRowCell(Integer.toString(total), 0));
286
        }
287
    }
288

    
289
    /**
290
     * Gets a list of IDs of the query rows that fit the contingency table row settings.
291
     * @param tableColumns Map of TableColumn
292
     * @param node node of the row
293
     * @param rowNames list of row names and user names
294
     * @return list of usable IDs
295
     */
296
    private List<Integer> getUsableIDs(Map<String, TableColumn> tableColumns, Node node, List<NameUserName> rowNames) {
297
        List<Integer> ids = new ArrayList<>();
298

    
299
        for (int i = 0; i < tableColumns.get(rowNames.get(0).getName()).getValues().size(); i++) {
300
            for (int j = 0; j < node.getValues().size(); j++) {
301
                if (tableColumns.get(rowNames.get(j).getName()).getValues().get(i).equals(node.getValues().get(j))) {
302
                    ids.add(i);
303
                }
304
            }
305
        }
306

    
307
        return ids;
308
    }
309
}
(2-2/5)