Skip to content

Create AutomaticConvertor to automatically chain the correct convertors #484

Closed

Description

@RubenVerborgh

Currently, this is the configuration I need to support GET, PUT, and PATCH with a custom JSON format is: https://github.com/RubenVerborgh/solid-hue/blob/bd4fcd864ef89d348017891834ff75056e077aed/config/config-hue.json#L71-L90

    {
      "@id": "solid-hue:RepresentationConverter",
      "@type": "WaterfallHandler",
      "WaterfallHandler:_handlers": [
        { "@type": "HueToQuadConverter" },
        { "@type": "QuadToHueConverter" },
        {
          "@id": "solid-hue:HueToRdfConverter",
          "@type": "ChainedConverter",
          "ChainedConverter:_converters": [
            { "@type": "HueToQuadConverter" },
            { "@id": "solid-server:QuadToRdfConverter" }
          ]
        },
        {
          "@id": "solid-hue:RdfToHueConverter",
          "@type": "ChainedConverter",
          "ChainedConverter:_converters": [
            { "@id": "solid-server:RdfToQuadConverter" },
            { "@type": "QuadToHueConverter" }
          ]
        }
      ]
    }

This is because the ChainedConvertor can only execute the exact chain.

Instead, I'd like to

    {
      "@id": "solid-hue:RepresentationConverter",
      "@type": "MatrixConverter",
      "MatrixConverter:_converter": [
        { "@type": "HueToQuadConverter" },
        { "@type": "QuadToHueConverter" },
        { "@id": "solid-server:QuadToRdfConverter" },
        { "@id": "solid-server:RdfToQuadConverter" }
      ]
    }

with a MatrixConverter that finds the needed path itself per request at runtime.

Activity

  1. changed the title [-]Create MatrixConvertor to automatically chain the correct convertors[/-] [+]Create AutomaticConvertor to automatically chain the correct convertors[/+] on Jan 9, 2021
  2. RubenVerborgh commented on Jan 9, 2021

    @RubenVerborgh
    MemberAuthor

    Idea for a testing approach

    • Create a DummyConverter (subclass of TypedRepresentationConverter) that pushes the chosen content-type to the stream
      • e.g., DummyConverter('a/b', 'd/c') would push d/c
    • Create tests for
      • no compatible input or output type: error
      • input matches output: no conversion needed
      • one-step conversion
      • two-step conversion
      • long conversion chain
        • a/b to b/c to c/d … etc.
      • prefers best-quality conversion (see below)

    Ideas for implementation

    • Perhaps the main method return a new ChainedConvertor with the constructed chain? Unless the chaining logic is simple, then we just re-implement.
    • In all of this, we need to keep in mind that the input types (and, at least in theory, output types) of the convertor might contain wildcards, so we cannot have an exhaustive list of input types. But we can have a partial list for a positive match.

    Algorithm idea for at-runtime path finding

    • main idea
      • have sets availableInputTypes and availableOutputTypes
        • they initially contain the specified types
        • but change on every iteration until a match is found
        • let them grow toward each other
      • for each entry of availableInputTypes and availableOutputTypes, keep track of the best way to get there (highest quality)
        • at the beginning, this is a path of length 0
        • the path is augmented with every converter added
        • the quality/weight of the path is determined via multiplication
          • e.g., a path from application/ld+json => application/n-quads; q=0.8 => text/turtle; q=0.7 has quality 0.56
      • limit the number of iterations (for instance, to the number of steps)
        • possibly a constructor parameter
    • on every iteration, grow the path from the smallest set to minimize branches to be checked
      • if availableInputTypes is smallest, augment it with the output types that are reachable via all converters that match the input types
      • if availableOutputTypes is smallest, augment it with the input types that are reachable via all converters that match the output types
      • if the type already exists in the set, only augment if it leads to a higher-quality version
      • can be implemented recursively with findPath function that takes availableInputTypes and availableOutputTypes and thus augments one of them by one step at every iteration
        • only the new additions to either set need to be checked at every iteration though
    • stop if maximum length is reached, found path with maximum quality (= maximum available q for given output type), or if the sets did not grow

    A simplified/intermediate version just finds a path, not the best-quality path.

  3. RubenVerborgh commented on Jan 9, 2021

    @RubenVerborgh
    MemberAuthor

    After thinking some more, I think the at-runtime matching is only relevant if there is a very high number of possible paths, which will almost never be the case: there will be few converters, and even fewer will have matching input/output.

    Therefore, we might simply want to make all possible combinations beforehand. The algorithm below is simpler and faster at runtime; only needs an initialization step at construction time

    Algorithm idea for construction-time combination creation

    • Input
      • a set of TypedRepresentationConverter instances I
    • Output
      • a set of TypedRepresentationConverter instances O, which is the initial set I augmented with all relevant ChainedRepresentationConverter instances up to a certain maximum length
    • At start
      • Set C is the set of converters created in the previous iteration, initially equal to I
    • At every iteration
      • Set new C to all ChainedRepresentationConverters that can be created for every type t in T and match c in C (where t does not yet occur in c)
        • Don't forget to multiply the q-values for the output (as mentioned above)
      • Add all in C to O
      • If C is empty, or maximum length has been reached, stop
    • At runtime when q request comes in
      • A WaterfallHandler of O should do the trick if we disregard quality
      • However, we might want to return the path with the highest quality, which involves checking the output types of all
        • Finding the best path is partially cacheable (definitely since the input content type is one value)
          • For instance, we could already key the input types by Content-Type. Because of wildcards, this list will be incomplete, but it's a good quick check. And we can key the wildcards separately. If no match, we know we can't convert.
          • We could also per output type store the ChainedRepresentationConverter that has the best match.
          • So we might want to create an abstraction for the canHandle/handle/handleSafe pattern that is based on findHandler, such as we have for instance in RoutingResourceStore and WaterfallHandler (See refactor: Async recursive handler #461)
  4. matthieubosquet commented on Jan 13, 2021

    @matthieubosquet
    Contributor

    I closed #461 which had many problems and created https://github.com/solid/community-server/tree/feat/recursive-handler-class which passes all tests and might be an alright base for what you are describing here.

    I am not sure about the details, but let me know if it makes sense and maybe should be pulled/merged.

    PS: Happy to delete the branch https://github.com/solid/community-server/tree/change/refactor-async-recursive-handler if you're ok with that.

  5. RubenVerborgh commented on Jan 13, 2021

    @RubenVerborgh
    MemberAuthor

    @matthieubosquet I'll need some explanation of what RecursiveAsyncHandler does compared to the others? The class comment didn't help me.

  6. matthieubosquet commented on Jan 13, 2021

    @matthieubosquet
    Contributor
  7. RubenVerborgh commented on Jan 13, 2021

    @RubenVerborgh
    MemberAuthor

    And RecursiveAsyncHandler would be the basis for others as well? Which ones? Where does the recursive apply?

  8. matthieubosquet commented on Jan 14, 2021

    @matthieubosquet
    Contributor

    Where does the recursive apply?

    I made a mistake, it wasn't recursive. I think that should fix it:
    https://github.com/solid/community-server/blob/2ae5c9e4a05b9e5e6f0bb5f66db7d72db3a57f58/src/util/handlers/RecursiveAsyncHandler.ts#L35-L37

    And RecursiveAsyncHandler would be the basis for others as well? Which ones?

    I think all handlers that aren't meant to handle data themselves should be recursive handlers. That way you can conveniently create trees of dependency injected handlers and reuse branches of those trees in different places.

    • Waterfall handler is a recursive handler where you use the first handler that can support the Input, which is currently the default behaviour of a RecursiveHandler
    • Sequence handler is a recursive handler where you use every handler that can support the Input
    • All handlers handler could be a recursive handler that throws if any of the handlers passed to it fails to handle and it could be a specialisation of Sequence handler...
  9. RubenVerborgh commented on Jan 14, 2021

    @RubenVerborgh
    MemberAuthor

    Sequence handler is a recursive handler where you use every handler that can support the Input

    SequenceHandler would overwrite all methods of RecursiveAsyncHandler though.

  10. jvherweg commented on Apr 9, 2021

    @jvherweg
    Member

    Finally having a look at this

    for every type t in T

    You forgot to define T, but I'm guessing it just corresponds to all possible types.

    Not going to go in-depth in all the lines, but below my general opinions and things that came up when thinking about this, some of them might repeat things that are already said above.

    I am more a fan of calculating the paths at runtime as needed. I also would not create ChainedConverters but simply run the "algorithm" that's in there when handle is being called on this class with the path that is being dynamically created. After this class is finished we can delete ChainedConverter and replace it with this one.

    I don't think that the optimization of creating all possible converters in the constructor already. This would create a lot of converters that would never be needed. We could potentially have a cache that keeps track of the last few paths that were used after they were used once, which would thus contain a list of converter instead of a ChainedConverter.

    The maximum path size is already determined by the amount of converters in the input array, so we could have a parameter for that if the number should be even lower than that but I don't think it is really needed. During path generation we can already do a lot of pruning since every converter can only appear once and I'm guessing most converters that will be added in the future will be for a specific data type which has no overlap with most other converters.

    One issue would be that the path generation is needed for both the canHandle and handle functions but this can be resolved by also overriding the handleSafe function which is used most of the time. Also if we use the cache the canHandle function can already fill that in.

    Regarding the path weight: we can do that, it's only a multiplication so not really an issue, but all converters we currently have, have no weight for their types, so they all have 1 I guess. So I would mostly focus on path length for finding the best solution, and if the algorithm is breadth first (which the suggestions above are) we would always find the shortest one anyway. If we use weights it might actually increase the algorithm length since there are potential cases where a longer path has a higher weight. In practice it would probably provide more pruning opportunities though so not against it, but I don't really see many converters supporting the weights.

  11. RubenVerborgh commented on Apr 9, 2021

    @RubenVerborgh
    MemberAuthor

    We could potentially have a cache that keeps track of the last few paths that were used after they were used once

    Yes, that's the one!

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions