/
TestFixedGrid.java
167 lines (134 loc) · 4.89 KB
/
TestFixedGrid.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
/*******************************************************************************
*
* Copyright (C) 2015-2018 the BBoxDB project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*******************************************************************************/
package org.bboxdb.experiments.conference;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.stream.Collectors;
import org.bboxdb.commons.MathUtil;
import org.bboxdb.commons.math.Hyperrectangle;
import org.bboxdb.experiments.ExperimentHelper;
import org.bboxdb.storage.entity.CellGrid;
import org.bboxdb.tools.TupleFileReader;
public class TestFixedGrid implements Runnable {
/**
* The cell sizes
*/
private final List<Integer> cellSizes;
/**
* The files to process
*/
private final Map<String, String> filesAndFormats;
public TestFixedGrid(final Map<String, String> filesAndFormats, final List<Integer> cellSizes) {
this.filesAndFormats = filesAndFormats;
this.cellSizes = cellSizes;
}
@Override
public void run() {
final Hyperrectangle boundingBox = filesAndFormats
.entrySet()
.stream()
.map(e -> ExperimentHelper.determineBoundingBox(e.getKey(), e.getValue()))
.reduce((b1, b2) -> Hyperrectangle.getCoveringBox(b1, b2))
.orElseThrow(() -> new IllegalArgumentException("Unable to calculate bounding box"));
for(final Integer cellsPerDimension: cellSizes) {
System.out.println("Cells per Dimension: " + cellsPerDimension
+ " BBox: " + boundingBox.toCompactString());
final CellGrid cellGrid = CellGrid.buildWithFixedAmountOfCells(boundingBox, cellsPerDimension);
runExperiment(cellGrid);
}
}
/**
* Run this experiment
* @param cellGrid
*/
private void runExperiment(final CellGrid cellGrid) {
final Map<Hyperrectangle, Integer> bboxes = new HashMap<>();
for(final Entry<String, String> file : filesAndFormats.entrySet()) {
final TupleFileReader tupleFile = new TupleFileReader(file.getKey(), file.getValue());
tupleFile.addTupleListener(t -> {
Hyperrectangle boundingBox = t.getBoundingBox();
final Set<Hyperrectangle> intersectedBoxes = cellGrid.getAllInersectedBoundingBoxes(boundingBox);
if(intersectedBoxes.isEmpty()) {
System.err.println("Unable to find Boundig Box for " + boundingBox
+ " / World: " + cellGrid.getCoveringBox());
System.exit(-1);
}
for(final Hyperrectangle box : intersectedBoxes) {
final int oldValue = bboxes.getOrDefault(box, 0);
bboxes.put(box, oldValue + 1);
}
});
try {
System.out.println("# Processing tuples in file: " + file.getKey());
tupleFile.processFile();
} catch (Exception e) {
System.err.println("Got an Exception during experiment: " + e);
System.exit(-1);
}
}
calculateResult(bboxes, cellGrid);
}
/**
* Calculate the result
* @param bboxes
* @param cellGrid
*/
private void calculateResult(final Map<Hyperrectangle, Integer> bboxes, final CellGrid cellGrid) {
System.out.println("# Calculating node results: " + cellGrid);
System.out.println("#Cell\tValues");
for(final Hyperrectangle cell : cellGrid.getAllCells()) {
System.out.format("%d%n", bboxes.get(cell));
}
System.out.println("");
}
/**
* Main * Main * Main
*/
public static void main(final String[] args) throws IOException {
// Check parameter
if(args.length < 2) {
System.err.println("Usage: programm <cells per dimension> <filename1:format1> <filenameN:formatN> ");
System.exit(-1);
}
final List<Integer> cellSizes = Arrays.asList(args[0].split(","))
.stream()
.map(s -> MathUtil.tryParseIntOrExit(s, () -> "Unable to parse: " + s))
.collect(Collectors.toList());
final Map<String, String> filesAndFormats = new HashMap<>();
for(int pos = 1; pos < args.length; pos++) {
final String element = args[pos];
if(! element.contains(":")) {
System.err.println("Element does not contain format specifier: " + element);
System.exit(-1);
}
final String[] splitFile = element.split(":");
if(splitFile.length != 2) {
System.err.println("Unable to get two elements after format split: " + element);
System.exit(-1);
}
filesAndFormats.put(splitFile[0], splitFile[1]);
}
final TestFixedGrid testSplit = new TestFixedGrid(filesAndFormats, cellSizes);
testSplit.run();
}
}