Skip to content

Make encoders/profiles available to a scripting language #193

Closed

Description

@crowmagnumb

I have an application in which I am using a walking path (using Graphhopper (Awesome!)) to figure out the distance to a nearest gas main from a person's house. It is to calculate how much it would cost to connect that person up to the network. Been working fabulous for over a year now but then a request came in from a person on a private drive. "access" = "private". And suddenly the routine didn't work. So I tracked it down to the line in FootFlagEncoder.

    restrictedValues.add("private");

I have forked the code, commented that above line out, recompiled, reran the data import and voila it works now on private drives!

But I would love to submit a pull in which this is configurable through say the config.properties, but the problem is you use reflection to create this class and I don't see how I can read the props from FootFlagEncoder. Wonder if you could ponder how to make this happen?

Cheers and fantastic work!!

Ken

Activity

  1. karussell commented on May 2, 2014

    @karussell
    Member

    You can create a subclass which removes this property and handing over this subclass to the encodingmanager via the other constructor. Or do you want/meant something like scripting support e.g. via JavaScript or something?

  2. crowmagnumb commented on May 6, 2014

    @crowmagnumb
    Author

    Well, I guess I wa looking for a solution using just the properties files
    since i already have a few changes to the default already. But I can make
    this work I suppose. Cheers.

    On Fri, May 2, 2014 at 3:27 AM, Peter [email protected] wrote:

    You can create a subclass which removes this property and handing over
    this subclass to the encodingmanager via the other constructor. Or do you
    want/meant something like scripting support e.g. via JavaScript or
    something?

    —
    Reply to this email directly or view it on GitHubhttps://github.com//issues/193#issuecomment-42014435
    .

  3. karussell commented on May 6, 2014

    @karussell
    Member

    We cannot use the properties for this. But something like this will come via interpreted languages ala "foot-profile.js" at a later stage.

  4. crowmagnumb commented on May 6, 2014

    @crowmagnumb
    Author

    Great! That sounds exciting!

    On Tue, May 6, 2014 at 3:09 AM, Peter [email protected] wrote:

    We cannot use the properties for this. But something like this will come
    via interpreted languages ala "foot-profile.js" at a later stage.

    —
    Reply to this email directly or view it on GitHubhttps://github.com//issues/193#issuecomment-42285358
    .

  5. changed the title [-]FootFlagEncoder configurable[/-] [+]Make encoders/profiles available to a scripting language[/+] on May 17, 2014
  6. ratrun commented on Apr 6, 2015

    @ratrun
    Contributor

    I thought a bit about a more detailed design for such a scripting feature. This is my current conclusion:

    I would start with implementing a brand new flagEncoder and configure it by something like this:
    graph.flagEncoders=script|scriptpath=FullQualifiedPathToScript.js
    Then I would try to implement such a new script encoder, duplicate one of the existing tests (e.g. for one of the bicycle types) and attempt to pass the existing tests by putting as much of the configuration logic into the JS file and in parallel implement the scripted encoder.
    I'm sure many more questions about a good interface between the script and the script encoder will arise and need to be answered during the implementation.
    One problem is the deployment of such server sided java scripts to servers hosted on the Internet. The coolest feature would be to offer users the possiblity to provide their own scripts. But in this case one needs to be very cautious about the security issues as I guess that some people might start hacking the server with all the capability java script provides. But maybe this could be solved by some very restrictive sophisticated parsing of the script. But I'm not sure if it eventually would not be better to invent a dedicated own simple syntax (and an interpreter for such a syntax) similar as to what brouter does in its profiles (http://brouter.de/brouter/costfunctions.html), because in this case you don't need to care that much about security issues. Anyhow I guess the implementation effort for such a solution would be higher and the long-term flexibility much lower. What do you think about the approach?

  7. dos65 commented on Apr 6, 2015

    @dos65

    @ratrun What do you think about Nashorn or Groovy? With one of them you can just extend existing encoders and override speed's definition or "accept node-way" logic or create new.

  8. ratrun commented on Apr 6, 2015

    @ratrun
    Contributor

    Thanks for pointing out the usage of Nashorn (http://www.oracle.com/technetwork/articles/java/jf14-nashorn-2126515.html). Its convenient way of extending Java types from JavaScript would be beneficial for the Java script approach. But it does not help with the security aspects, please correct me if I'm wrong.
    I did not know Groovy and having read its high level decription I do not get the point what benefits it would bring in our use-case.

    Extending just one of the existing encoders would be the easiest. Actually I have started with this playing around a bit. But I see following drawbacks:

    • You need to implement the extension capability in every single encoder
    • It will be hard and next to impossible to maintain the same extension capabilities in every encoder.
    • In case that you choose to provide different extension possibilities per encoder, it well be next to impossible to document and explain the feature set for a sprecific encoder to the users. If you provide feature X for "bicycle", then it is quite sure that somebody else wants to use this feature also for "foot".
    • I'm sure we want to keep the existing none flexible encoders because of better runtime. If you move all configurations to the Java Script, then tehy do not work without extensions any longer. In case that you only modifiy the current behaviour via the script, then the overall behaviour will is very complicated because you need to know both implementations. And if we change something in the existing Java encoders the influence on the Java script extensions maintained by useres is very hard to predict.
  9. dos65 commented on Apr 6, 2015

    @dos65

    There is way to restrict scripting access in Nashorn javadoc

  10. karussell commented on Apr 7, 2015

    @karussell
    Member

    The way we plan to integrate this into GraphHopper is more high level so it could theoretically work for data which is not OSM:

    • implement a generic FlagEncoder which stores max-height, max speed for several vehicles and directions, access properties for various vehicles, surface, etc
    • then provide certain properties one could configure like access to max-speed for car or preferring route relations for big etc. This would be done within a custom ScriptedWeighting class
    • Finally allow passing parameters to the ScriptedWeighting class OR writing such classes via server-side groovy or JS scripts or a new lang called painless or janino. We would not allow client side scripts for now (see below) but as we allow the params to change this is not that bad I think

    The coolest feature would be to offer users the possiblity to provide their own scripts.

    Yes, but a big risc. ElasticSearch has done this and tried this with Groovy as it has a sandbox, but currently there seems to be no safe solution: http://www.elastic.co/guide/en/elasticsearch/reference/1.x/modules-scripting.html also JS seems to not have a sandbox not sure if the classfilter mentioned from @dos65 could solve this (as the guys from ES have looked into JS too)

  11. karussell commented on Apr 7, 2015

    @karussell
    Member

    Anyhow I guess the implementation effort for such a solution would be higher and
    the long-term flexibility much lower. What do you think about the approach?

    I've emailed with the author of brouter and he is willing to put this under the Apache license. Although I would find this a powerful solution and it would be very valueable for the GraphHopper community it has the drawback of a relative complex scripting languages (polnish notation etc) and the dependency on the OSM notation etc.

    The configuration-approach we would try first (ScriptedWeighting with params) will be less powerful but faster than a scripting language

  12. devemux86 commented on Apr 7, 2015

    @devemux86
    Contributor

    I've emailed with the author of brouter and he is willing to put this under the Apache license.

    Very interesting news!

  13. HarelM commented on Oct 13, 2015

    @HarelM
    Contributor

    Any progress with this?
    here is how I imagined it before reading this thread:

    1. A user will be able to create an XML or JSON or something that can be read easily (not sure what's native to Java) defining the properties in the encoder flag.
    2. Implement a FileFlagEncoder that can use this data in order to define everything it needs.
    3. Define in the config the file names to use - each file name will represent the name of the flag encoder.
    4. define a folder in the config so that GH will know where to look for the files.

    This way you can implement any type of routing. no security. and you can change it without compiling GH.
    I'm currently testing the current profiles and I feel that I need to be able to tweak them without recompiling every change - just delete the cache and refresh the server.
    What do you think?
    I think it will be easier to implement...

    Here's an example of a json file:

    {
       turnCosts: true,
       restrictions: ["motorcar"],
       restrictionsValues: ["private", "agricultural"],
       intendedValues: ["yes", "permissive"],
       trackTypeSpeedMap: [{"grade1": 20}, {"grade2": 15}],
       ...
    }
  14. karussell commented on Oct 14, 2015

    @karussell
    Member

    Yes, something like this is planned in the near future. It goes even further that you'll be able to send these json's over HTTP and the profile is completely configurable even per request.

    See https://discuss.graphhopper.com/t/more-flexibility-in-gh/71

  15. HarelM commented on Oct 14, 2015

    @HarelM
    Contributor

    I'd love to try it out as I really need to fine tune the profiles before releasing the routing to production.
    If you have something I can tryout it will be great! (the sooner the better).

  16. HarelM commented on Apr 26, 2016

    @HarelM
    Contributor

    Any updates on this issue? I'm having troubles with fords now... :-/

  17. karussell commented on Apr 26, 2016

    @karussell
    Member

    Sorry, not yet. Too many things on the table although this is very exciting.

    Regarding fords: what is your problem? You can enable them already. When reading the referenced issue:

    As you said, this will probably require forking the GH repository here and
    creating propiotary FlagEncoders.

    then there seems to be a misconception. Just create the FlagEncoder and enable fords ...

    Also we should move the discussion to our forum.

  18. karussell commented on Jul 18, 2019

    @karussell
    Member

    This issue needs to be split into several sub issues:

    • define a vehicle profile via yaml/json config
    • define a vehicle profile via scripting (as a start use janino), which requires making GH running with a SecurityManager
    • make it possible to specify such a profile per-request, i.e. a new POST endpoint => include ideas from Improvements to JSON format of web module #437
    • make it possible to specify such a profile on import (this likely requires adding the jackson dependency and the scripting languages like janino to core, which we should avoid to keep it lean)
    • create new vehicle profiles like trucks and make testing very easy

    The work for this was started in flex_vehicleprofile branch but the most recent work (not all features yet) happens in https://github.com/graphhopper/graphhopper/compare/flex_vehicleprofile2

  19. karussell commented on Nov 1, 2019

    @karussell
    Member

    Closing in favor of #1776

  20. karussell commented on Feb 21, 2020

    @karussell
    Member

    For later reference: instead of a "full" scripting language like janino that comes with security challenges we can try exp4j that can also support boolean operators.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions