Skip to content

Add custom model editor to support custom routing - #183

Merged
easbar merged 24 commits into
masterfrom
custom_model_box
Sep 2, 2022
Merged

easbar merged 24 commits into
masterfrom
custom_model_box

Conversation

@easbar

@easbar easbar commented Feb 14, 2022 •

Copy link
Copy Markdown
Member

This PR adds support for routing using so-called 'custom models' and fixes #17. A custom model is a set of rules written in JSON language that affects the outcome of the route calculation. Custom models are not used standalone, but rather they are modifications of existing profiles. So for example one can take the car profile and modify the routing using a custom model to reduce the speed on motorways. See the documentation for more details here.

Users shall be able to enter such custom models using a text field (the 'custom model editor'). For this and to guide the user when writing custom models we developed an interactive text field that supports syntax checking, error-highlighting and auto-completion here. This shall be added here.

Routing with custom models comes at a cost: The response will be slower, sometimes much slower. Therefore, we clearly need to distinguish two cases:

  1. Routing using the built-in profiles (car, foot, bike etc.) as we do so far
  2. Routing with custom models (car, foot, bike etc. + custom model).

For 2) there is a catch: Custom models are not supported for all built-in profiles. Currently we offer car,foot,bike,hike,racingbike,scooter,small_truck,truck,mtb on http://graphhopper.com/maps, but custom models are only available for car,small_truck,foot,bike,scooter (not for truck,hike,mtb,racingbike). This might be different for self-deployed GraphHopper instances etc. Unfortunately, currently we do not have this information available on the client side, so we might just ignore this for now as we do for the current GH maps app.

Here I added a simple checkbox to the sidebar that opens/closes the custom model box right in the sidebar, just like we do for current GH maps. This is probably subject to change, because the space in the sidebar is rather limited (horizontally, but also vertically when there are instructions, many via-points etc.). Also we might need even more space for the following features that we might add at a later time:

  1. Custom models can also be used to modify the routing in a certain area. Add support to visually select an area on the map.
  2. Certain custom model rules are very common, like 'avoid motorways'. Add a combo box to insert such rules into the text field to make using custom models easier.
  3. Custom model rules are applied based on the attributes of the different roads. It would be useful to visualize these attributes, e.g. 'show all roads with "road_class == primary"` etc.

So maybe at some point we need extra layout changes for these things, add tabs to the sidebar etc.

Opening/closing the custom model editor using the checkbox also defines whether we are in 'standard' or 'custom model' mode. This way we can easily compare routes with and without a custom model by opening and closing the text field.

For an open custom model box we need to distinguish three further cases:

  1. A custom model was entered but it is not even valid JSON (or the text field is just empty). We cannot parse the model at all and it is therefore not possible to perform a route request.
  2. Valid JSON was entered for the custom model and we can parse from it a JavaScript object, but it's not a valid custom model. The user needs to fix the custom model first. We allow sending the routing request regardless using the keyboard shortcut Ctrl+Enter, but otherwise the user needs to fix the custom model first.
  3. A valid custom model was entered and we are ready to send the routing request and show the route.

The custom model editor/text field helps us dealing with these three cases. For example when there are syntax errors they will already be highlighted in red and the user can hover the corresponding text to see more details about the error. The same applies when the syntax is correct, but the custom model is invalid. Still, we need a way to alert the user about these errors. For example if the user adds a new query point we usually send a routing request right away, but if the custom model is invalid we should rather tell the user about this. I use the same error notification (ErrorAction) we use to report error responses (like 'point not found' etc.) for this.

@easbar easbar added this to the 0.4 milestone Feb 14, 2022
Comment on lines +4 to +5
// todonow: this belongs to this app and we should not take it from the demo...
import 'custom-model-editor/demo/style.css'

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was not sure how to add (where to put the file) global css rules as we need them for codemirror like .cm-s-default.

Comment thread src/stores/QueryStore.ts
Comment on lines +32 to +34
readonly customModelEnabled: boolean
readonly customModelValid: boolean
readonly customModel: CustomModel | null

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added three new fields to the app's state: customModelEnabled simply tells us whether we are in 'custom model mode' or not (and right now it also determines whether the custom model editor is shown or not). customModelValid tells us whether or not the model is valid as it is determined by the editor component. customModel is the current custom model. We cannot just set the customModel field to null in case it is invalid, because we like to be able to send requests also for invalid models. Note that customModel is not used as backend for the editor's actual content. This is because if we managed the editor's content using react we would need to manage all of the editor's state (like cursor position, annotations etc.) with react (or at least keep it up to date somehow). Right now I don't see a real advantage of this.

Comment thread src/stores/QueryStore.ts Resolved
Comment thread src/sidebar/CustomModelBox.tsx Outdated
}
}
}
Dispatcher.dispatch(new SetCustomModel(initialCustomModel, true))

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to update the app state here. This is the price we pay for not using the app's custom model state as the editor's backend. Afterwards we just update the customModel state whenever the editor evaluated the input's validity (see validListener above).

@easbar easbar changed the title Custom model box Add custom model editor to support custom routing Feb 14, 2022
@easbar

easbar commented Feb 14, 2022 •

Copy link
Copy Markdown
Member Author

Currently it looks like this (the checkbox opens/closes the editor):

image

but I just tried to make it work first, so I am sure there is room for improvements

@Janekdererste

Janekdererste commented Mar 29, 2022 •

Copy link
Copy Markdown
Collaborator

I tried the branch and it seems to work. Let's fix the Dispatch of the Error Actions, then this could be merged I think.

One thing to think about is optics. Would you like the editor to be where it is at the moment, or should we think of something else? Instead of a check box we could also have an icon for this and a "closing x". Also, 15rem is quite large, if the model is simple.

@easbar

easbar commented Mar 30, 2022 •

Copy link
Copy Markdown
Member Author

I tried the branch and it seems to work. Let's fix the Dispatch of the Error Actions, then this could be merged I think.

Thanks for taking a look! Sorry, but which error action dispatch issue do you mean?

One thing to think about is optics. Would you like the editor to be where it is at the moment, or should we think of something else? Instead of a check box we could also have an icon for this and a "closing x". Also, 15rem is quite large, if the model is simple.

You are more than welcome to improve on this. I just put it anywhere really so I could go on implementing the functionality.

Comment thread src/stores/ErrorStore.ts Resolved
Comment on lines +63 to +71
const triggerRouting = useCallback(
(event: React.KeyboardEvent<HTMLInputElement>) => {
if (event.ctrlKey && event.key === 'Enter') {
dispatchCustomModel(editor.value, isValid, true)
}
},
[editor, isValid]
)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if this is better or worse, but we can also use the event handling of the text component using this API: https://github.com/graphhopper/custom-model-editor/blob/693a44097271ed84f603f46efbfcfc56ab97b608/src/custom_model_editor.js#L84-L86

This is how we did it in GH maps 1.0 as well: https://github.com/graphhopper/graphhopper/blob/876ffaa9fb990e2ea812c8956091809084025dcd/web-bundle/src/main/resources/com/graphhopper/maps/js/main-template.js#L153

And is it possible to send the request/json without validation when pressing Ctrl+Enter? @karussell should we do this? This is what we did for the old maps client.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@karussell should we do this?

👍

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using Ctrl+Enter we can now force the request regardless of the client side validation. 78c612c

@easbar

easbar commented Apr 13, 2022

Copy link
Copy Markdown
Member Author

Maybe it would be useful to add some kind of hint that advises the user to use the Ctrl+Space shortcut for auto-completion?

@easbar

easbar commented Aug 2, 2022

Copy link
Copy Markdown
Member Author

@Janekdererste Are you planning to work on this? Or is there something I should change? Or can we merge this and do further improvements later?

You wrote:

Let's fix the Dispatch of the Error Actions, then this could be merged I think.

But unfortunately I don't know what you meant?

Here are a few things that could be still done, but that might not be strictly required before merging:

  • Better layout: Do not squeeze the custom model box into the sidebar, also see my initial post.
  • The 'custom' checkbox used to enable/disable the custom model is quite ugly as you already pointed out. Same for the 'Help' and 'Example' buttons underneath the custom model box. I just added them as they were in GH maps 1. The 'Example' button also seems a bit useless to me, because the example custom model is used already initially.
  • Better UX: We do not automatically trigger a routing request when we edit the custom model. This is intended, because there would just be too many requests if we did this for every edit. On the other hand this might be confusing for users, because it is hard to understand what they need to do to trigger the request after editing the model. In GH maps 1 there is a 'Search' button which is greyed out whenever the mode becomes invalid. I think this makes it quite obvious one has to click this button after editing the model. Here we decided against adding such a search button, so we need to focus one of the query point boxes and press 'Enter' to trigger the request instead. The other option is pressing Ctrl+Enter, but this is even less obvious.

@easbar
easbar merged commit 8729f9a into master Sep 2, 2022
@easbar
easbar deleted the custom_model_box branch September 2, 2022 07:40
ZeroGxMax pushed a commit to minhhpkp/graphhopper-maps that referenced this pull request Nov 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

custom model box

3 participants