Modify

Opened 4 years ago

Last modified 19 months ago

#19648 new enhancement

[patch] Show warning on invalid MapCSS validator rule parsing

Reported by: skyper Owned by: team
Priority: normal Milestone:
Component: Core Version:
Keywords: template_report rule validation mapcss class Cc:

Description

What steps will reproduce the problem?

  1. Have a typo in a validator mapcss rule file, like the missing y below:
    way[highway =~ /^(motorway|motorway_link|trunk|trunk_link)$/] {
      set MotorwayTrunk;
    }
    way.MotorwaTrunk[lanes][turn:lanes              ][tag(lanes)!=eval(count(split("|", tag("turn:lanes"))))],
    way.MotorwaTrunk[lanes][change:lanes            ][tag(lanes)!=eval(count(split("|", tag("change:lanes"))))],
    way.MotorwaTrunk[lanes][maxspeed:lanes          ][tag(lanes)!=eval(count(split("|", tag("maxspeed:lanes"))))],
    way.MotorwaTrunk[lanes][minspeed:lanes          ][tag(lanes)!=eval(count(split("|", tag("minspeed:lanes"))))],
    way.MotorwaTrunk[lanes][destination:lanes       ][tag(lanes)!=eval(count(split("|", tag("destination:lanes"))))],
    way.MotorwaTrunk[lanes][destination:ref:lanes   ][tag(lanes)!=eval(count(split("|", tag("destination:ref:lanes"))))],
    way.MotorwaTrunk[lanes][destination:symbol:lanes][tag(lanes)!=eval(count(split("|", tag("destination:symbol:lanes"))))] {
        throwWarning: tr("Different number of lanes in the keys {0} and {1}", "{1.key}", "{2.key}");
        group: tr("suspicious tag combination");
    }
    
  2. (Re-)Load file

What is the expected result?

A warning about an unknown class in use.

What happens instead?

No warning

Please provide any additional information below. Attach a screenshot if possible.

Some Information about existing but not use classes would be nice, too.

Relative:URL: ^/trunk
Repository:UUID: 0c6e7542-c601-0410-84e7-c038aed88b3b
Last:Changed Date: 2020-08-06 21:47:58 +0200 (Thu, 06 Aug 2020)
Revision:16851
Build-Date:2020-08-07 01:30:47
URL:https://josm.openstreetmap.de/svn/trunk

Attachments (1)

josm_19648.patch (2.3 KB ) - added by gaben 2 years ago.

Download all attachments as: .zip

Change History (19)

comment:1 by gaben, 2 years ago

Summary: MapCSS Rule validation: Warn about unknown class.Show warning on invalid MapCSS validator rule parsing

comment:2 by gaben, 2 years ago

While experimenting with a custom MapCSS rule, I noticed parsing of invalid rules silently fail on the GUI side. In case of error, the console prints detailed information. Add this or parts to a warning dialog to help diagnose the problem.

URL:https://josm.openstreetmap.de/svn/trunk
Repository:UUID: 0c6e7542-c601-0410-84e7-c038aed88b3b
Last:Changed Date: 2022-01-09 17:28:53 +0100 (Sun, 09 Jan 2022)
Build-Date:2022-01-10 02:31:03
Revision:18364
Relative:URL: ^/trunk

comment:3 by gaben, 2 years ago

The issue is coming from the MapCSSParser.jj file. I have a patch that makes JOSM print the messages but at the same time introduces some GUI exceptions (EDT violations).

comment:4 by gaben, 2 years ago

Summary: Show warning on invalid MapCSS validator rule parsing[WIP patch] Show warning on invalid MapCSS validator rule parsing
  • src/org/openstreetmap/josm/gui/mappaint/mapcss/MapCSSParser.jj

     
    1414import java.util.Collections;
    1515import java.util.List;
    1616
     17import javax.swing.JOptionPane;
     18
    1719import org.openstreetmap.josm.data.preferences.NamedColorProperty;
     20import org.openstreetmap.josm.gui.MainApplication;
    1821import org.openstreetmap.josm.gui.mappaint.Keyword;
    1922import org.openstreetmap.josm.gui.mappaint.Range;
    2023import org.openstreetmap.josm.gui.mappaint.mapcss.Condition;
     
    7881            this.idx = idx;
    7982        }
    8083    }
    81    
     84
    8285    /**
    8386     * Constructor which initializes the parser with a certain lexical state.
    8487     * @param in input
     
    11641167   
    11651168    Logging.error("Skipping to the next rule, because of an error:");
    11661169    Logging.error(e);
     1170
     1171    JOptionPane.showMessageDialog(MainApplication.getMainFrame(), e.getMessage(), "MapCSS rule parsing error", JOptionPane.ERROR_MESSAGE);
    11671172    if (sheet != null) {
    11681173        sheet.logError(e);
    11691174    }

The patch introduces some EDT violations, which needs to be solved first. If someone knows how to fix it, please do it.

comment:5 by GerdP, 2 years ago

See GuiHelper.runInEDT() or one of the variants.

by gaben, 2 years ago

Attachment: josm_19648.patch added

comment:6 by gaben, 2 years ago

Summary: [WIP patch] Show warning on invalid MapCSS validator rule parsing[patch] Show warning on invalid MapCSS validator rule parsing

Thank you @GerdP! Now the EDT violations are gone. I think the patch is good enough, but please review it.

comment:7 by GerdP, 2 years ago

You can simplify that to GuiHelper.runInEDT(() -> JOptionPane.showMessageDialog(..));
like in MainLayerManager:

            GuiHelper.runInEDT(() -> JOptionPane.showMessageDialog(MainApplication.getMainFrame(),
                    tr("Trying to delete the layer with background upload. Please wait until the upload is finished."))); 
}

comment:8 by gaben, 2 years ago

I tried that, but JavaCC didn't like it :/

comment:9 by taylor.smock, 2 years ago

Looking at the release-notes.md, v7.0.5 "[added] support for Java7 language features". So I don't think Java 8 language features are supported.

comment:10 by GerdP, 2 years ago

Ah, sorry, I got carried away and forgot to check why. If you think it's useful please commit it. I only tried to help reg. the EDT stuff.

comment:11 by gaben, 2 years ago

Sorry, I wasn't specific by writing JavaCC doesn't like lambdas, but as taylor.smock wrote, JavaCC supports Java up to version 7.

in reply to:  10 ; comment:12 by taylor.smock, 2 years ago

Replying to GerdP:

Ah, sorry, I got carried away and forgot to check why. If you think it's useful please commit it. I only tried to help reg. the EDT stuff.

I thought you had been looking at the patch. I just figured I'd look into it to see if one of the version updates for JavaCC had added Java 8 language feature support (I think I recently updated our version of JavaCC).

In that case, @gaben, what problem (exactly) are you trying to solve? The supplied patch does nothing to show a warning about a missing class. It just adds another method for informing a user that something might be wrong, and doesn't even trigger on the example mapcss.

Here is some broken mapcss for testing (i.e., what currently happens):

way[highway =~ /^(motorway|motorway_link|trunk|trunk_link)$/] {
  set MotorwayTrunk;
}
/* Should be broken */
way.MotorwaTrunk[lanes][destination:symbol:lanes][tag(lanes)!=eval(count(split("|", tag("destination:symbol:lanes"))))] {
    throwWarning: tr("Different number of lanes in the keys {0} and {1}", "{1.key}", "{2.key}");
    group: tr("suspicious tag combination");
}

/* Broken */
way[hhhh===dddd] {}
way[highway=residential] { throwMessage("Not a valid thing");}
way3 {}

You will notice that the Map Paint Styles toggle dialog will show the paint style with an exclamation mark. Right-click -> Info -> Errors will then show the problems. If you look at the console output, you will see more output (from the Logging calls).

So we need to throw an exception when we encounter a class that does not currently exist.

(Also, JOSM now has a validate command line switch. Example usage: java -jar dist/josm-custom.jar validate --input test.mapcss)

comment:13 by gaben, 2 years ago

Good catch! I need to revisit what I've done and why, but I remember we worked together with @skyper on a different ticket, and the validator did not warn us about invalid MapCSS rules in runtime, hence the patch. I just ignored the example, probably.

in reply to:  12 ; comment:14 by gaben, 2 years ago

Replying to taylor.smock:

In that case, @gaben, what problem (exactly) are you trying to solve?

If I remember correctly, there is a "hot reload" feature in JOSM, which allows rules reloading simply by saving the rule file. If there is an invalid rule, this feature will stop working silently until JOSM restart. With the patch, at least the user will know there is an issue and why isn't it working (detailed message printed).

in reply to:  14 comment:15 by taylor.smock, 2 years ago

Replying to gaben:

If I remember correctly, there is a "hot reload" feature in JOSM, which allows rules reloading simply by saving the rule file. If there is an invalid rule, this feature will stop working silently until JOSM restart. With the patch, at least the user will know there is an issue and why isn't it working (detailed message printed).

Yep, there is a hot reload feature. I haven't seen it stop working after an invalid rule though. If you have an example, we should probably fix that.

comment:16 by skyper, 2 years ago

Yes, the auto reload does stop working once there is a syntax error (e.g. unexpected symbol). Manually reloading will still work but for auto-reload you need to restart JOSM. (Do not know if it is the same with mappaint styles).

comment:17 by taylor.smock, 2 years ago

Looks like there is a difference between MapCSSTagChecker.java and MapPaintStyles.java.

This can probably be fixed by moving the FileWatcher#registerSource call above the MapCSSTagChecker#addMapCss call.

EDIT: Note that this appears to only be a problem on _first_ load of the validator.mapcss rule.

  • src/org/openstreetmap/josm/data/validation/tests/MapCSSTagChecker.java

    diff --git a/src/org/openstreetmap/josm/data/validation/tests/MapCSSTagChecker.java b/src/org/openstreetmap/josm/data/validation/tests/MapCSSTagChecker.java
    index ef9c500e49..40e9b5f991 100644
    a b public class MapCSSTagChecker extends Test.TagTest {  
    331331                } else if (Logging.isDebugEnabled()) {
    332332                    Logging.debug(tr("Adding {0} to tag checker", i));
    333333                }
    334                 addMapCSS(i);
    335334                if (Config.getPref().getBoolean("validator.auto_reload_local_rules", true) && source.isLocal()) {
    336335                    FileWatcher.getDefaultInstance().registerSource(source);
    337336                }
     337                addMapCSS(i);
    338338            } catch (IOException | IllegalStateException | IllegalArgumentException ex) {
    339339                Logging.warn(tr("Failed to add {0} to tag checker", i));
    340340                Logging.log(Logging.LEVEL_WARN, ex);
Last edited 2 years ago by taylor.smock (previous) (diff)

comment:18 by gaben, 19 months ago

Hm didn't check it but your change should solve the code break, while mine will notify if something is off.

Modify Ticket

Change Properties
Set your email in Preferences
Action
as new The owner will remain team.
as The resolution will be set. Next status will be 'closed'.
to The owner will be changed from team to the specified user.
Next status will be 'needinfo'. The owner will be changed from team to skyper.
as duplicate The resolution will be set to duplicate. Next status will be 'closed'. The specified ticket will be cross-referenced with this ticket.
The owner will be changed from team to anonymous. Next status will be 'assigned'.

Add Comment


E-mail address and name can be saved in the Preferences .
 
Note: See TracTickets for help on using tickets.