This repository has been archived by the owner on Jan 28, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
⚠️ Remove variable sources and make Deppy accept a slice of variables instead #160
Merged
m1kola
merged 2 commits into
operator-framework:main
from
m1kola:remove_variable_sources
Nov 27, 2023
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 0 additions & 7 deletions
7
pkg/deppy/input/variable_source.go → pkg/deppy/input/simple_variable.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,21 @@ | ||
package solver | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
|
||
"github.com/operator-framework/deppy/internal/solver" | ||
"github.com/operator-framework/deppy/pkg/deppy" | ||
"github.com/operator-framework/deppy/pkg/deppy/input" | ||
) | ||
|
||
// TODO: should disambiguate between solver errors due to constraints | ||
// and other generic errors (e.g. entity source not reachable, etc.) | ||
// and other generic errors | ||
|
||
// Solution is returned by the Solver when the internal solver executed successfully. | ||
// A successful execution of the solver can still end in an error when no solution can | ||
// be found. | ||
type Solution struct { | ||
err error | ||
selection map[deppy.Identifier]deppy.Variable | ||
variables []deppy.Variable | ||
} | ||
|
||
// Error returns the resolution error in case the problem is unsat | ||
|
@@ -40,66 +37,21 @@ func (s *Solution) IsSelected(identifier deppy.Identifier) bool { | |
return ok | ||
} | ||
|
||
// AllVariables returns all the variables that were considered by the solver to obtain (or not) | ||
// a solution. Note: This is only be present if the AddAllVariablesToSolution option is passed in to the | ||
// Solve call that generated the solution. | ||
func (s *Solution) AllVariables() []deppy.Variable { | ||
return s.variables | ||
} | ||
|
||
type solutionOptions struct { | ||
addVariablesToSolution bool | ||
} | ||
|
||
func (s *solutionOptions) apply(options ...Option) *solutionOptions { | ||
for _, applyOption := range options { | ||
applyOption(s) | ||
} | ||
return s | ||
} | ||
|
||
func defaultSolutionOptions() *solutionOptions { | ||
return &solutionOptions{ | ||
addVariablesToSolution: false, | ||
} | ||
} | ||
|
||
type Option func(solutionOptions *solutionOptions) | ||
|
||
// AddAllVariablesToSolution is a Solve option that instructs the solver to include | ||
// all the variables considered to the Solution it produces | ||
func AddAllVariablesToSolution() Option { | ||
return func(solutionOptions *solutionOptions) { | ||
solutionOptions.addVariablesToSolution = true | ||
} | ||
} | ||
|
||
// DeppySolver is a simple solver implementation that takes an entity source group and a constraint aggregator | ||
// DeppySolver is a simple solver implementation that takes a slice of variables | ||
// to produce a Solution (or error if no solution can be found) | ||
type DeppySolver struct { | ||
variableSource input.VariableSource | ||
} | ||
type DeppySolver struct{} | ||
|
||
func NewDeppySolver(variableSource input.VariableSource) *DeppySolver { | ||
return &DeppySolver{ | ||
variableSource: variableSource, | ||
} | ||
func NewDeppySolver() *DeppySolver { | ||
return &DeppySolver{} | ||
} | ||
|
||
func (d DeppySolver) Solve(ctx context.Context, options ...Option) (*Solution, error) { | ||
solutionOpts := defaultSolutionOptions().apply(options...) | ||
|
||
vars, err := d.variableSource.GetVariables(ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
func (d DeppySolver) Solve(vars []deppy.Variable) (*Solution, error) { | ||
satSolver, err := solver.NewSolver(solver.WithInput(vars)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
selection, err := satSolver.Solve(ctx) | ||
selection, err := satSolver.Solve() | ||
if err != nil && !errors.As(err, &deppy.NotSatisfiable{}) { | ||
return nil, err | ||
} | ||
|
@@ -116,9 +68,5 @@ func (d DeppySolver) Solve(ctx context.Context, options ...Option) (*Solution, e | |
solution.err = unsatError | ||
} | ||
|
||
if solutionOpts.addVariablesToSolution { | ||
solution.variables = vars | ||
} | ||
Comment on lines
-119
to
-121
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Previously variables were generated by a varaible soruce. So for cases where the calling code for some reason needed to access input variables - we had an option to add them into the |
||
|
||
return solution, nil | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can get rid of
Solution
completely (not in this PR). I do not see a lot of value in it.And probably replace
DeppySolver
wrapper around internal package by exporting what we currently have in internal pacakge (with some modifications and also not in this PR).This comment is for me to look into whether it makes sense and create issues for this.
Edit: created #161 for this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Structuring the logic to be helpers in generating the inputs to and consuming the outputs of the solver library sounds like a really good idea - lets folks use as much of or as little of this helpful logic as they need and doesn't bind them to what we happen to have or not to have wrapped at some moment in time.