Changeset 15667 in josm


Ignore:
Timestamp:
2020-01-09T23:42:30+01:00 (3 months ago)
Author:
Don-vip
Message:

fix #18455 - smarter error detection, should lead to less false positives

Location:
trunk
Files:
5 edited

Legend:

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

    r15645 r15667  
    2323import java.util.Set;
    2424import java.util.regex.Pattern;
     25import java.util.stream.Collectors;
    2526
    2627import javax.swing.JCheckBox;
     
    3536import org.openstreetmap.josm.data.osm.OsmPrimitive;
    3637import org.openstreetmap.josm.data.osm.Tag;
     38import org.openstreetmap.josm.data.osm.TagMap;
    3739import org.openstreetmap.josm.data.osm.Tagged;
    3840import org.openstreetmap.josm.data.preferences.sources.ValidatorPrefHelper;
     
    615617
    616618        if (checkPresetsTypes) {
    617             for (TaggingPreset tp : TaggingPresets.getMatchingPresets(null, p.getKeys(), false)) {
    618                 TaggingPresetType presetType = TaggingPresetType.forPrimitive(p);
    619                 if (!tp.typeMatches(EnumSet.of(presetType))) {
     619            TagMap tags = p.getKeys();
     620            TaggingPresetType presetType = TaggingPresetType.forPrimitive(p);
     621            EnumSet<TaggingPresetType> presetTypes = EnumSet.of(presetType);
     622            Collection<TaggingPreset> matchingPresets = TaggingPresets.getMatchingPresets(null, tags, false);
     623            Collection<TaggingPreset> matchingPresetsOK = matchingPresets.stream().filter(
     624                    tp -> tp.typeMatches(presetTypes)).collect(Collectors.toList());
     625            Collection<TaggingPreset> matchingPresetsKO = matchingPresets.stream().filter(
     626                    tp -> !tp.typeMatches(presetTypes)).collect(Collectors.toList());
     627
     628            for (TaggingPreset tp : matchingPresetsKO) {
     629                // Potential error, unless matching tags are all known by a supported preset
     630                Map<String, String> matchingTags = tp.data.stream()
     631                    .filter(i -> Boolean.TRUE.equals(i.matches(tags)))
     632                    .filter(i -> i instanceof KeyedItem).map(i -> ((KeyedItem) i).key)
     633                    .collect(Collectors.toMap(k -> k, tags::get));
     634                if (matchingPresetsOK.stream().noneMatch(
     635                        tp2 -> matchingTags.entrySet().stream().allMatch(
     636                                e -> tp2.data.stream().anyMatch(
     637                                        i -> i instanceof KeyedItem && ((KeyedItem) i).key.equals(e.getKey()))))) {
    620638                    errors.add(TestError.builder(this, Severity.OTHER, INVALID_PRESETS_TYPE)
    621639                            .message(tr("Object type not in preset"),
  • trunk/src/org/openstreetmap/josm/gui/tagging/presets/TaggingPresetItem.java

    r15293 r15667  
    7676     * @return {@code true} if matches (positive), {@code null} if neutral, {@code false} if mismatches (negative).
    7777     */
    78     protected Boolean matches(Map<String, String> tags) {
     78    public Boolean matches(Map<String, String> tags) {
    7979        return null;
    8080    }
  • trunk/src/org/openstreetmap/josm/gui/tagging/presets/items/CheckGroup.java

    r9665 r15667  
    5353
    5454    @Override
    55     protected Boolean matches(Map<String, String> tags) {
     55    public Boolean matches(Map<String, String> tags) {
    5656        for (Check check : checks) {
    5757            if (Boolean.TRUE.equals(check.matches(tags))) {
  • trunk/src/org/openstreetmap/josm/gui/tagging/presets/items/KeyedItem.java

    r14706 r15667  
    191191
    192192    @Override
    193     protected Boolean matches(Map<String, String> tags) {
     193    public Boolean matches(Map<String, String> tags) {
    194194        switch (MatchType.ofString(match)) {
    195195        case NONE:
  • trunk/test/unit/org/openstreetmap/josm/data/validation/tests/TagCheckerTest.java

    r15614 r15667  
    336336        assertFalse(TagChecker.containsUnusualUnicodeCharacter("name", "Hökumət Evi"));
    337337    }
     338
     339    /**
     340     * Detects objects with types not supported by their presets.
     341     * @throws IOException in case of I/O error
     342     */
     343    @Test
     344    public void testObjectTypeNotSupportedByPreset() throws IOException {
     345        List<TestError> errors = test(OsmUtils.createPrimitive("relation waterway=river"));
     346        assertEquals(1, errors.size());
     347        assertEquals(TagChecker.INVALID_PRESETS_TYPE, errors.get(0).getCode());
     348        errors = test(OsmUtils.createPrimitive("relation type=waterway waterway=river"));
     349        assertTrue(errors.toString(), errors.isEmpty());
     350    }
    338351}
Note: See TracChangeset for help on using the changeset viewer.