Changeset 16047 in josm


Ignore:
Timestamp:
2020-03-06T09:02:24+01:00 (4 years ago)
Author:
GerdP
Message:

fix #13805,#17737 improve progress bar of validator / Unclear scope of mapcss class (set clause)

  • change flow control so that the tests of one *.mapcss file are performed as a block without side effects on other tests (#17737). Tests which relied on this might need changes.
  • show the rules name (e.g. Geometry) in the progess bar and an element counter if the tests runs longer than 0.5 seconds. If no name was given for the rules the file name is shown (e.g. myrules.mapcss)
  • element counter is increased in steps of 10000 instead of 1000.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/validation/tests/MapCSSTagChecker.java

    r16039 r16047  
    66import java.awt.geom.Area;
    77import java.io.BufferedReader;
     8import java.io.File;
    89import java.io.IOException;
    910import java.io.InputStream;
     
    1819import java.util.List;
    1920import java.util.Map;
     21import java.util.Map.Entry;
    2022import java.util.Objects;
    2123import java.util.Optional;
     
    6769import org.openstreetmap.josm.tools.Logging;
    6870import org.openstreetmap.josm.tools.MultiMap;
     71import org.openstreetmap.josm.tools.Stopwatch;
    6972import org.openstreetmap.josm.tools.Utils;
    7073
     
    7578public class MapCSSTagChecker extends Test.TagTest {
    7679    private MapCSSStyleIndex indexData;
    77     final Map<MapCSSRule, MapCSSTagCheckerAndRule> ruleToCheckMap = new HashMap<>();
    78     private final Set<OsmPrimitive> tested = new HashSet<>();
     80    private final Map<MapCSSRule, MapCSSTagCheckerAndRule> ruleToCheckMap = new HashMap<>();
    7981    private static final Map<IPrimitive, Area> mpAreaCache = new HashMap<>();
    8082    static final boolean ALL_TESTS = true;
     
    203205
    204206    final MultiMap<String, TagCheck> checks = new MultiMap<>();
     207
     208    /** maps the source URL for a test to the title shown in the dialog where known */
     209    private final Map<String, String> urlTitles = new HashMap<>();
    205210
    206211    /**
     
    547552                final String description1 = group == null ? description : group;
    548553                final String description2 = group == null ? null : description;
     554                final String selector = matchingSelector.toString();
    549555                TestError.Builder errorBuilder = TestError.builder(tester, getSeverity(), 3000)
    550                         .messageWithManuallyTranslatedDescription(description1, description2, matchingSelector.toString());
     556                        .messageWithManuallyTranslatedDescription(description1, description2, selector);
    551557                if (fix != null) {
    552558                    errorBuilder.fix(() -> fix);
     
    558564                        if (c instanceof OsmPrimitive) {
    559565                            errorBuilder = TestError.builder(tester, getSeverity(), 3000)
    560                                     .messageWithManuallyTranslatedDescription(description1, description2,
    561                                             matchingSelector.toString());
     566                                    .messageWithManuallyTranslatedDescription(description1, description2, selector);
    562567                            if (fix != null) {
    563568                                errorBuilder.fix(() -> fix);
     
    618623        @Override
    619624        public String getSource() {
    620             return tr("URL / File: {0}", source);
     625            return source;
    621626        }
    622627    }
     
    662667                                .filter(c -> c.rule.declaration == rule.declaration)
    663668                                .findFirst()
    664                                 .map(c -> new MapCSSTagCheckerAndRule(c, e.getKey()))
     669                                .map(c -> new MapCSSTagCheckerAndRule(c, getTitle(e.getKey())))
    665670                                .orElse(null))
    666671                        .filter(Objects::nonNull)
     
    681686    }
    682687
     688    private String getTitle(String url) {
     689        return urlTitles.getOrDefault(url, tr("unknown"));
     690    }
     691
    683692    /**
    684693     * See #12627
     
    750759            addIfNotSimilar(e, errors);
    751760        }
    752         if (partialSelection) {
    753             tested.add(p);
    754         }
    755761    }
    756762
     
    788794            checks.remove(url);
    789795            checks.putAll(url, result.parseChecks);
     796            urlTitles.put(url, findURLTitle(url));
    790797            indexData = null;
    791798        }
    792799        return result;
     800    }
     801
     802    /** Find a user friendly string for the url.
     803     *
     804     * @param url the source for the set of rules
     805     * @return a value that can be used in tool tip or progress bar.
     806     */
     807    private static String findURLTitle(String url) {
     808        for (SourceEntry source : new ValidatorPrefHelper().get()) {
     809            if (url.equals(source.url) && source.title != null && !source.title.isEmpty()) {
     810                return source.title;
     811            }
     812        }
     813        if (url.endsWith(".mapcss")) // do we have others?
     814            url = new File(url).getName();
     815        if (url.length() > 33) {
     816            url = "..." + url.substring(url.length() - 30);
     817        }
     818        return url;
    793819    }
    794820
     
    796822    public synchronized void initialize() throws Exception {
    797823        checks.clear();
     824        urlTitles.clear();
    798825        indexData = null;
    799826        for (SourceEntry source : new ValidatorPrefHelper().get()) {
     
    843870        super.startTest(progressMonitor);
    844871        super.setShowElements(true);
    845         if (indexData == null) {
    846             indexData = createMapCSSTagCheckerIndex(checks, includeOtherSeverityChecks(), ALL_TESTS);
    847         }
    848         tested.clear();
    849         mpAreaCache.clear();
    850872    }
    851873
    852874    @Override
    853875    public synchronized void endTest() {
     876        // no need to keep the index, it is quickly build and doubles the memory needs
     877        indexData = null;
     878        // always clear the cache to make sure that we catch changes in geometry
     879        mpAreaCache.clear();
     880        ruleToCheckMap.clear();
     881        super.endTest();
     882    }
     883
     884    @Override
     885    public void visit(Collection<OsmPrimitive> selection) {
     886        if (progressMonitor != null) {
     887            progressMonitor.setTicksCount(selection.size() * checks.size());
     888        }
     889
     890        mpAreaCache.clear();
     891
     892        Set<OsmPrimitive> surrounding = new HashSet<>();
     893        for (Entry<String, Set<TagCheck>> entry : checks.entrySet()) {
     894            if (isCanceled()) {
     895                break;
     896            }
     897            visit(entry.getKey(), entry.getValue(), selection, surrounding);
     898        }
     899    }
     900
     901    /**
     902     * Perform the checks for one check url
     903     * @param url the url for the checks
     904     * @param checksForUrl the checks to perform
     905     * @param selection collection primitives
     906     * @param surrounding surrounding primitives, evtl. filled by this routine
     907     */
     908    private void visit(String url, Set<TagCheck> checksForUrl, Collection<OsmPrimitive> selection,
     909            Set<OsmPrimitive> surrounding) {
     910        MultiMap<String, TagCheck> currentCheck = new MultiMap<>();
     911        currentCheck.putAll(url, checksForUrl);
     912        indexData = createMapCSSTagCheckerIndex(currentCheck, includeOtherSeverityChecks(), ALL_TESTS);
     913        Set<OsmPrimitive> tested = new HashSet<>();
     914
     915
     916        String title = getTitle(url);
     917        if (progressMonitor != null) {
     918            progressMonitor.setExtraText(tr(" {0}", title));
     919        }
     920        long cnt = 0;
     921        Stopwatch stopwatch = Stopwatch.createStarted();
     922        for (OsmPrimitive p : selection) {
     923            if (isCanceled()) {
     924                break;
     925            }
     926            if (isPrimitiveUsable(p)) {
     927                check(p);
     928                if (partialSelection) {
     929                    tested.add(p);
     930                }
     931            }
     932            if (progressMonitor != null) {
     933                progressMonitor.worked(1);
     934                cnt++;
     935                // add frequently changing info to progress monitor so that it
     936                // doesn't seem to hang when test takes longer than 0.5 seconds
     937                if (cnt % 10000 == 0 && stopwatch.elapsed() >= 500) {
     938                    progressMonitor.setExtraText(tr(" {0}: {1} of {2} elements done", title, cnt, selection.size()));
     939                }
     940            }
     941        }
     942
    854943        if (partialSelection && !tested.isEmpty()) {
    855944            // #14287: see https://josm.openstreetmap.de/ticket/14287#comment:15
     
    858947            // rebuild index with a reduced set of rules (those that use ChildOrParentSelector) and thus may have left selectors
    859948            // matching the previously tested elements
    860             indexData = createMapCSSTagCheckerIndex(checks, includeOtherSeverityChecks(), ONLY_SELECTED_TESTS);
    861 
    862             Set<OsmPrimitive> surrounding = new HashSet<>();
    863             for (OsmPrimitive p : tested) {
    864                 if (p.getDataSet() != null) {
    865                     surrounding.addAll(p.getDataSet().searchWays(p.getBBox()));
    866                     surrounding.addAll(p.getDataSet().searchRelations(p.getBBox()));
    867                 }
    868             }
     949            indexData = createMapCSSTagCheckerIndex(currentCheck, includeOtherSeverityChecks(), ONLY_SELECTED_TESTS);
     950
     951            if (surrounding.isEmpty()) {
     952                for (OsmPrimitive p : tested) {
     953                    if (p.getDataSet() != null) {
     954                        surrounding.addAll(p.getDataSet().searchWays(p.getBBox()));
     955                        surrounding.addAll(p.getDataSet().searchRelations(p.getBBox()));
     956                    }
     957                }
     958            }
     959
    869960            final boolean includeOtherSeverity = includeOtherSeverityChecks();
    870961            for (OsmPrimitive p : surrounding) {
     
    877968                }
    878969            }
    879             tested.clear();
    880         }
    881         // no need to keep the index, it is quickly build and doubles the memory needs
    882         indexData = null;
    883         // always clear the cache to make sure that we catch changes in geometry
    884         mpAreaCache.clear();
    885         ruleToCheckMap.clear();
    886         super.endTest();
     970        }
    887971    }
    888972}
Note: See TracChangeset for help on using the changeset viewer.