1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.data.osm;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.io.PrintWriter;
|
---|
7 | import java.io.StringWriter;
|
---|
8 | import java.io.Writer;
|
---|
9 |
|
---|
10 | import org.openstreetmap.josm.Main;
|
---|
11 | import org.openstreetmap.josm.tools.Utils;
|
---|
12 |
|
---|
13 | /**
|
---|
14 | * This class can be used to run consistency tests on dataset. Any errors found will be written to provided PrintWriter.
|
---|
15 | * <br>
|
---|
16 | * Texts here should not be translated because they're not intended for users but for josm developers.
|
---|
17 | * @since 2500
|
---|
18 | */
|
---|
19 | public class DatasetConsistencyTest {
|
---|
20 |
|
---|
21 | private static final int MAX_ERRORS = 100;
|
---|
22 | private final DataSet dataSet;
|
---|
23 | private final PrintWriter writer;
|
---|
24 | private int errorCount;
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * Constructs a new {@code DatasetConsistencyTest}.
|
---|
28 | * @param dataSet The dataset to test
|
---|
29 | * @param writer The writer used to write results
|
---|
30 | */
|
---|
31 | public DatasetConsistencyTest(DataSet dataSet, Writer writer) {
|
---|
32 | this.dataSet = dataSet;
|
---|
33 | this.writer = new PrintWriter(writer);
|
---|
34 | }
|
---|
35 |
|
---|
36 | private void printError(String type, String message, Object... args) {
|
---|
37 | errorCount++;
|
---|
38 | if (errorCount <= MAX_ERRORS) {
|
---|
39 | writer.println('[' + type + "] " + String.format(message, args));
|
---|
40 | }
|
---|
41 | }
|
---|
42 |
|
---|
43 | /**
|
---|
44 | * Checks that parent primitive is referred from its child members
|
---|
45 | */
|
---|
46 | public void checkReferrers() {
|
---|
47 | long startTime = System.currentTimeMillis();
|
---|
48 | // It's also error when referred primitive's dataset is null but it's already covered by referredPrimitiveNotInDataset check
|
---|
49 | for (Way way : dataSet.getWays()) {
|
---|
50 | if (!way.isDeleted()) {
|
---|
51 | for (Node n : way.getNodes()) {
|
---|
52 | if (n.getDataSet() != null && !n.getReferrers().contains(way)) {
|
---|
53 | printError("WAY NOT IN REFERRERS", "%s is part of %s but is not in referrers", n, way);
|
---|
54 | }
|
---|
55 | }
|
---|
56 | }
|
---|
57 | }
|
---|
58 |
|
---|
59 | for (Relation relation : dataSet.getRelations()) {
|
---|
60 | if (!relation.isDeleted()) {
|
---|
61 | for (RelationMember m : relation.getMembers()) {
|
---|
62 | if (m.getMember().getDataSet() != null && !m.getMember().getReferrers().contains(relation)) {
|
---|
63 | printError("RELATION NOT IN REFERRERS", "%s is part of %s but is not in referrers", m.getMember(), relation);
|
---|
64 | }
|
---|
65 | }
|
---|
66 | }
|
---|
67 | }
|
---|
68 | printElapsedTime(startTime);
|
---|
69 | }
|
---|
70 |
|
---|
71 | /**
|
---|
72 | * Checks for womplete ways with incomplete nodes.
|
---|
73 | */
|
---|
74 | public void checkCompleteWaysWithIncompleteNodes() {
|
---|
75 | long startTime = System.currentTimeMillis();
|
---|
76 | for (Way way : dataSet.getWays()) {
|
---|
77 | if (way.isUsable()) {
|
---|
78 | for (Node node : way.getNodes()) {
|
---|
79 | if (node.isIncomplete()) {
|
---|
80 | printError("USABLE HAS INCOMPLETE", "%s is usable but contains incomplete node '%s'", way, node);
|
---|
81 | }
|
---|
82 | }
|
---|
83 | }
|
---|
84 | }
|
---|
85 | printElapsedTime(startTime);
|
---|
86 | }
|
---|
87 |
|
---|
88 | /**
|
---|
89 | * Checks for complete nodes without coordinates.
|
---|
90 | */
|
---|
91 | public void checkCompleteNodesWithoutCoordinates() {
|
---|
92 | long startTime = System.currentTimeMillis();
|
---|
93 | for (Node node : dataSet.getNodes()) {
|
---|
94 | if (!node.isIncomplete() && node.isVisible() && !node.isLatLonKnown()) {
|
---|
95 | printError("COMPLETE WITHOUT COORDINATES", "%s is not incomplete but has null coordinates", node);
|
---|
96 | }
|
---|
97 | }
|
---|
98 | printElapsedTime(startTime);
|
---|
99 | }
|
---|
100 |
|
---|
101 | /**
|
---|
102 | * Checks that nodes can be retrieved through their coordinates.
|
---|
103 | */
|
---|
104 | public void searchNodes() {
|
---|
105 | long startTime = System.currentTimeMillis();
|
---|
106 | dataSet.getReadLock().lock();
|
---|
107 | try {
|
---|
108 | for (Node n : dataSet.getNodes()) {
|
---|
109 | // Call isDrawable() as an efficient replacement to previous checks (!deleted, !incomplete, getCoor() != null)
|
---|
110 | if (n.isDrawable() && !dataSet.containsNode(n)) {
|
---|
111 | printError("SEARCH NODES", "%s not found using Dataset.containsNode()", n);
|
---|
112 | }
|
---|
113 | }
|
---|
114 | } finally {
|
---|
115 | dataSet.getReadLock().unlock();
|
---|
116 | }
|
---|
117 | printElapsedTime(startTime);
|
---|
118 | }
|
---|
119 |
|
---|
120 | /**
|
---|
121 | * Checks that ways can be retrieved through their bounding box.
|
---|
122 | */
|
---|
123 | public void searchWays() {
|
---|
124 | long startTime = System.currentTimeMillis();
|
---|
125 | dataSet.getReadLock().lock();
|
---|
126 | try {
|
---|
127 | for (Way w : dataSet.getWays()) {
|
---|
128 | if (!w.isIncomplete() && !w.isDeleted() && w.getNodesCount() >= 2 && !dataSet.containsWay(w)) {
|
---|
129 | printError("SEARCH WAYS", "%s not found using Dataset.containsWay()", w);
|
---|
130 | }
|
---|
131 | }
|
---|
132 | } finally {
|
---|
133 | dataSet.getReadLock().unlock();
|
---|
134 | }
|
---|
135 | printElapsedTime(startTime);
|
---|
136 | }
|
---|
137 |
|
---|
138 | private void checkReferredPrimitive(OsmPrimitive primitive, OsmPrimitive parent) {
|
---|
139 | if (primitive.getDataSet() == null) {
|
---|
140 | printError("NO DATASET", "%s is referenced by %s but not found in dataset", primitive, parent);
|
---|
141 | } else if (dataSet.getPrimitiveById(primitive) == null) {
|
---|
142 | printError("REFERENCED BUT NOT IN DATA", "%s is referenced by %s but not found in dataset", primitive, parent);
|
---|
143 | } else if (dataSet.getPrimitiveById(primitive) != primitive) {
|
---|
144 | printError("DIFFERENT INSTANCE", "%s is different instance that referred by %s", primitive, parent);
|
---|
145 | }
|
---|
146 |
|
---|
147 | if (primitive.isDeleted()) {
|
---|
148 | printError("DELETED REFERENCED", "%s refers to deleted primitive %s", parent, primitive);
|
---|
149 | }
|
---|
150 | }
|
---|
151 |
|
---|
152 | /**
|
---|
153 | * Checks that referred primitives are present in dataset.
|
---|
154 | */
|
---|
155 | public void referredPrimitiveNotInDataset() {
|
---|
156 | long startTime = System.currentTimeMillis();
|
---|
157 | for (Way way : dataSet.getWays()) {
|
---|
158 | for (Node node : way.getNodes()) {
|
---|
159 | checkReferredPrimitive(node, way);
|
---|
160 | }
|
---|
161 | }
|
---|
162 |
|
---|
163 | for (Relation relation : dataSet.getRelations()) {
|
---|
164 | for (RelationMember member : relation.getMembers()) {
|
---|
165 | checkReferredPrimitive(member.getMember(), relation);
|
---|
166 | }
|
---|
167 | }
|
---|
168 | printElapsedTime(startTime);
|
---|
169 | }
|
---|
170 |
|
---|
171 | /**
|
---|
172 | * Checks for zero and one-node ways.
|
---|
173 | */
|
---|
174 | public void checkZeroNodesWays() {
|
---|
175 | long startTime = System.currentTimeMillis();
|
---|
176 | for (Way way : dataSet.getWays()) {
|
---|
177 | if (way.isUsable() && way.getNodesCount() == 0) {
|
---|
178 | printError("WARN - ZERO NODES", "Way %s has zero nodes", way);
|
---|
179 | } else if (way.isUsable() && way.getNodesCount() == 1) {
|
---|
180 | printError("WARN - NO NODES", "Way %s has only one node", way);
|
---|
181 | }
|
---|
182 | }
|
---|
183 | printElapsedTime(startTime);
|
---|
184 | }
|
---|
185 |
|
---|
186 | private void printElapsedTime(long startTime) {
|
---|
187 | if (Main.isDebugEnabled()) {
|
---|
188 | StackTraceElement item = Thread.currentThread().getStackTrace()[2];
|
---|
189 | String operation = getClass().getSimpleName() + '.' + item.getMethodName();
|
---|
190 | long elapsedTime = System.currentTimeMillis() - startTime;
|
---|
191 | Main.debug(tr("Test ''{0}'' completed in {1}",
|
---|
192 | operation, Utils.getDurationString(elapsedTime)));
|
---|
193 | }
|
---|
194 | }
|
---|
195 |
|
---|
196 | /**
|
---|
197 | * Runs test.
|
---|
198 | */
|
---|
199 | public void runTest() {
|
---|
200 | try {
|
---|
201 | long startTime = System.currentTimeMillis();
|
---|
202 | referredPrimitiveNotInDataset();
|
---|
203 | checkReferrers();
|
---|
204 | checkCompleteWaysWithIncompleteNodes();
|
---|
205 | checkCompleteNodesWithoutCoordinates();
|
---|
206 | searchNodes();
|
---|
207 | searchWays();
|
---|
208 | checkZeroNodesWays();
|
---|
209 | printElapsedTime(startTime);
|
---|
210 | if (errorCount > MAX_ERRORS) {
|
---|
211 | writer.println((errorCount - MAX_ERRORS) + " more...");
|
---|
212 | }
|
---|
213 |
|
---|
214 | } catch (RuntimeException e) {
|
---|
215 | writer.println("Exception during dataset integrity test:");
|
---|
216 | e.printStackTrace(writer);
|
---|
217 | }
|
---|
218 | }
|
---|
219 |
|
---|
220 | /**
|
---|
221 | * Runs test on the given dataset.
|
---|
222 | * @param dataSet the dataset to test
|
---|
223 | * @return the errors as string
|
---|
224 | */
|
---|
225 | public static String runTests(DataSet dataSet) {
|
---|
226 | StringWriter writer = new StringWriter();
|
---|
227 | new DatasetConsistencyTest(dataSet, writer).runTest();
|
---|
228 | return writer.toString();
|
---|
229 | }
|
---|
230 | }
|
---|