-
Notifications
You must be signed in to change notification settings - Fork 21
/
solver.go
72 lines (58 loc) · 1.97 KB
/
solver.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package solver
import (
"errors"
"github.com/operator-framework/deppy/internal/solver"
"github.com/operator-framework/deppy/pkg/deppy"
)
// TODO: should disambiguate between solver errors due to constraints
// 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
}
// Error returns the resolution error in case the problem is unsat
// on successful resolution, it will return nil
func (s *Solution) Error() error {
return s.err
}
// SelectedVariables returns the variables that were selected by the solver
// as part of the solution
func (s *Solution) SelectedVariables() map[deppy.Identifier]deppy.Variable {
return s.selection
}
// IsSelected returns true if the variable identified by the identifier was selected
// in the solution by the resolver. It will return false otherwise.
func (s *Solution) IsSelected(identifier deppy.Identifier) bool {
_, ok := s.selection[identifier]
return ok
}
// 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{}
func NewDeppySolver() *DeppySolver {
return &DeppySolver{}
}
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()
if err != nil && !errors.As(err, &deppy.NotSatisfiable{}) {
return nil, err
}
selectionMap := map[deppy.Identifier]deppy.Variable{}
for _, variable := range selection {
selectionMap[variable.Identifier()] = variable
}
solution := &Solution{selection: selectionMap}
if err != nil {
unsatError := deppy.NotSatisfiable{}
errors.As(err, &unsatError)
solution.err = unsatError
}
return solution, nil
}