/
solver.go
186 lines (151 loc) · 4.14 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
/*
* Copyright 2019 Zachary Schneider
*/
package zps
import (
"errors"
"sort"
"github.com/fezz-io/sat"
//"github.com/davecgh/go-spew/spew"
)
type Solver struct {
pool *Pool
policy Policy
request *Request
satSolutions []*sat.Solution
solutions Solutions
seen map[string]bool
solver *sat.Solver
}
func NewSolver(pool *Pool, policy Policy) *Solver {
solver := &Solver{pool, policy, nil, nil, nil, make(map[string]bool), sat.NewSolver()}
return solver
}
func (s *Solver) Solve(request *Request) (*Solution, error) {
var satisfiable bool
s.request = request
s.solutions = nil
s.addClauses()
satisfiable, s.satSolutions = s.solver.Satisfiable()
if satisfiable == false {
return nil, errors.New("zps.solver: No solution for requested jobs")
}
s.generateSolutions()
return s.getSolution(), nil
}
func (s *Solver) Cnf() string {
return s.solver.String()
}
func (s *Solver) SatSolutions() []*sat.Solution {
return s.satSolutions
}
func (s *Solver) Solutions() Solutions {
return s.solutions
}
func (s *Solver) addClauses() {
// Add unary clauses
for _, job := range s.request.Jobs() {
switch job.Op() {
case "install":
var clause sat.LiteralEncoder
candidate := s.policy.SelectRequest(s.pool.WhatProvides(job.Requirement()))
if candidate != nil {
clause = sat.NewVariable(candidate.Id())
s.solver.AddClause(clause)
s.addReqClauses(candidate)
}
case "remove":
var clause sat.LiteralEncoder
candidate := s.pool.Installed(job.Requirement())
if candidate != nil {
clause = sat.NewVariable(candidate.Id()).Not()
s.solver.AddClause(clause)
s.addRmClauses(candidate)
}
default:
continue
}
}
}
func (s *Solver) addReqClauses(solvable Solvable) {
for _, req := range solvable.Requirements() {
// Continue if a requirement references itself
if solvable.Name() == req.Name {
continue
}
switch req.Method {
case "depends":
var clause []sat.LiteralEncoder
clause = append(clause, sat.NewVariable(solvable.Id()).Not())
provides := s.policy.PruneProvides(s.pool.WhatProvides(req))
for _, candidate := range provides {
clause = append(clause, sat.NewVariable(candidate.Id()))
// recurse if candidate has not been processed
if s.seen[candidate.Id()] {
continue
}
s.seen[candidate.Id()] = true
s.addReqClauses(candidate)
}
s.solver.AddClause(clause...)
for index, provided := range provides {
current := index + 1
for current <= len(provides)-1 {
if provided.Id() != provides[current].Id() {
s.solver.AddClause(sat.NewVariable(provided.Id()).Not(), sat.NewVariable(provides[current].Id()).Not())
}
current++
}
}
case "conflicts":
for _, candidate := range s.policy.PruneProvides(s.pool.WhatProvides(req)) {
s.solver.AddClause(sat.NewVariable(solvable.Id()).Not(), sat.NewVariable(candidate.Id()).Not())
}
default:
continue
}
}
}
func (s *Solver) addRmClauses(solvable Solvable) {
for _, dep := range s.pool.WhatDepends(solvable.Name()) {
clause := sat.NewVariable(dep.Id()).Not()
s.solver.AddClause(clause)
// recurse
s.addRmClauses(dep)
}
}
func (s *Solver) generateSolutions() {
for _, satSol := range s.satSolutions {
solution := NewSolution()
keys := make([]string, 0)
for key := range *satSol {
keys = append(keys, key)
}
sort.Strings(keys)
for _, key := range keys {
req, _ := NewRequirementFromSimpleString(key)
pkg := s.policy.SelectRequest(s.pool.WhatProvides(req))
if satSol.Value(key) {
if pkg.Priority() > -1 {
solution.AddOperation(NewOperation("install", pkg))
} else {
solution.AddOperation(NewOperation("noop", pkg))
}
} else {
if pkg.Priority() == -1 {
solution.AddOperation(NewOperation("remove", pkg))
}
}
}
s.solutions = append(s.solutions, *solution)
}
sort.Sort(s.solutions)
}
func (s *Solver) getSolution() *Solution {
return s.policy.SelectSolution(s.solutions)
}