You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Create AutomaticConvertor to automatically chain the correct convertors #484
changed the title [-]Create MatrixConvertor to automatically chain the correct convertors[/-][+]Create AutomaticConvertor to automatically chain the correct convertors[/+]on Jan 9, 2021
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.
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)
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...
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.
Activity
[-]Create MatrixConvertor to automatically chain the correct convertors[/-][+]Create AutomaticConvertor to automatically chain the correct convertors[/+]RubenVerborgh commentedon Jan 9, 2021
Idea for a testing approach
DummyConverter(subclass ofTypedRepresentationConverter) that pushes the chosen content-type to the streamDummyConverter('a/b', 'd/c')would pushd/cIdeas for implementation
ChainedConvertorwith the constructed chain? Unless the chaining logic is simple, then we just re-implement.Algorithm idea for at-runtime path finding
availableInputTypesandavailableOutputTypesavailableInputTypesandavailableOutputTypes, keep track of the best way to get there (highest quality)application/ld+json=>application/n-quads; q=0.8=>text/turtle; q=0.7has quality 0.56availableInputTypesis smallest, augment it with the output types that are reachable via all converters that match the input typesavailableOutputTypesis smallest, augment it with the input types that are reachable via all converters that match the output typesfindPathfunction that takesavailableInputTypesandavailableOutputTypesand thus augments one of them by one step at every iterationA simplified/intermediate version just finds a path, not the best-quality path.
RubenVerborgh commentedon Jan 9, 2021
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
TypedRepresentationConverterinstances ITypedRepresentationConverterinstances O, which is the initial set I augmented with all relevantChainedRepresentationConverterinstances up to a certain maximum lengthChainedRepresentationConverters that can be created for every type t in T and match c in C (where t does not yet occur in c)WaterfallHandlerof O should do the trick if we disregard qualityContent-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.ChainedRepresentationConverterthat has the best match.canHandle/handle/handleSafepattern that is based onfindHandler, such as we have for instance inRoutingResourceStoreandWaterfallHandler(See refactor: Async recursive handler #461)matthieubosquet commentedon Jan 13, 2021
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.
RubenVerborgh commentedon Jan 13, 2021
@matthieubosquet I'll need some explanation of what
RecursiveAsyncHandlerdoes compared to the others? The class comment didn't help me.matthieubosquet commentedon Jan 13, 2021
@RubenVerborgh I improved a bit on the code and added relevant comments.
https://github.com/solid/community-server/tree/feat/recursive-handler-class
RubenVerborgh commentedon Jan 13, 2021
And
RecursiveAsyncHandlerwould be the basis for others as well? Which ones? Where does therecursiveapply?matthieubosquet commentedon Jan 14, 2021
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
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.
RubenVerborgh commentedon Jan 14, 2021
SequenceHandlerwould overwrite all methods ofRecursiveAsyncHandlerthough.jvherweg commentedon Apr 9, 2021
Finally having a look at this
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 whenhandleis being called on this class with the path that is being dynamically created. After this class is finished we can deleteChainedConverterand 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
canHandleandhandlefunctions but this can be resolved by also overriding thehandleSafefunction which is used most of the time. Also if we use the cache thecanHandlefunction 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.
RubenVerborgh commentedon Apr 9, 2021
Yes, that's the one!