This commit is contained in:
Ettore Di Giacinto
2018-09-21 23:29:50 +02:00
parent c708031e2e
commit 1084941240
5 changed files with 386 additions and 0 deletions

162
pkg/package/package.go Normal file
View File

@@ -0,0 +1,162 @@
// Copyright © 2018 Ettore Di Giacinto <mudler@gentoo.org>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, see <http://www.gnu.org/licenses/>.
package pkg
import (
"encoding/base64"
"encoding/json"
"github.com/crillab/gophersat/bf"
"github.com/jinzhu/copier"
)
type Package interface {
Encode() (string, error)
SetState(state State) Package
BuildFormula() (bf.Formula, error)
IsFlagged(bool) Package
Requires([]Package) Package
Conflicts([]Package) Package
}
type DefaultPackage struct {
Name string
Version string
UseFlags []string
State State
PackageRequires []Package
PackageConflicts []Package
IsSet bool
}
type PackageUse []string
type State string
func NewPackage(name, version string, requires []Package, conflicts []Package) Package {
return &DefaultPackage{Name: name, Version: version, PackageRequires: requires, PackageConflicts: conflicts}
}
func (p *DefaultPackage) AddUse(use string) {
for _, v := range p.UseFlags {
if v == use {
return
}
}
p.UseFlags = append(p.UseFlags, use)
}
func (p *DefaultPackage) RemoveUse(use string) {
for i := len(p.UseFlags) - 1; i >= 0; i-- {
if p.UseFlags[i] == use {
p.UseFlags = append(p.UseFlags[:i], p.UseFlags[i+1:]...)
}
}
}
func (p *DefaultPackage) Encode() (string, error) {
res, err := json.Marshal(p)
if err != nil {
return "", err
}
return base64.StdEncoding.EncodeToString(res), nil
}
func (p *DefaultPackage) WithState(state State) Package {
return p.Clone().SetState(state)
}
func (p *DefaultPackage) IsFlagged(b bool) Package {
p.IsSet = b
return p
}
func (p *DefaultPackage) SetState(state State) Package {
p.State = state
return p
}
func (p *DefaultPackage) Requires(req []Package) Package {
p.PackageRequires = req
return p
}
func (p *DefaultPackage) Conflicts(req []Package) Package {
p.PackageConflicts = req
return p
}
func (p *DefaultPackage) Clone() Package {
new := &DefaultPackage{}
copier.Copy(&new, &p)
return new
}
func DecodePackage(pa string) (Package, error) {
enc, err := base64.StdEncoding.DecodeString(pa)
if err != nil {
return nil, err
}
p := &DefaultPackage{}
if err := json.Unmarshal(enc, &p); err != nil {
return nil, err
}
return p, nil
}
func (p *DefaultPackage) BuildFormula() (bf.Formula, error) {
encodedA, err := p.Encode()
if err != nil {
return nil, err
}
A := bf.Var(encodedA)
if p.IsSet {
//f = bf.And(f, bf.Var(encodedA))
} else {
//f = bf.And(f, bf.Not(bf.Var(encodedA)))
}
var formulas []bf.Formula
//formulas = append(formulas, A)
for _, required := range p.PackageRequires {
encodedB, err := required.Encode()
if err != nil {
return nil, err
}
B := bf.Var(encodedB)
formulas = append(formulas, bf.Or(bf.Not(A), B))
}
for _, required := range p.PackageConflicts {
encodedB, err := required.Encode()
if err != nil {
return nil, err
}
B := bf.Var(encodedB)
formulas = append(formulas, bf.Or(bf.Not(A),
bf.Not(B)))
}
return bf.And(formulas...), nil
}

39
pkg/solver/decoder.go Normal file
View File

@@ -0,0 +1,39 @@
// Copyright © 2019 Ettore Di Giacinto <mudler@gentoo.org>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, see <http://www.gnu.org/licenses/>.
package solver
import (
"fmt"
pkg "gitlab.com/mudler/luet/pkg/package"
)
func DecodeModel(model map[string]bool) []pkg.Package {
ass := make([]pkg.Package, 0)
for k, v := range model {
if a, err := pkg.DecodePackage(k); err == nil {
fmt.Println(a)
a.IsFlagged(false)
if v {
a.IsFlagged(true)
}
//if a.State == common.STATE_CURRENT {
ass = append(ass, a)
//} // Else, there was a state transition between Initial state and current run
}
}
return ass
}

92
pkg/solver/solver.go Normal file
View File

@@ -0,0 +1,92 @@
// Copyright © 2019 Ettore Di Giacinto <mudler@gentoo.org>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, see <http://www.gnu.org/licenses/>.
package solver
import (
"errors"
"fmt"
"github.com/crillab/gophersat/bf"
pkg "gitlab.com/mudler/luet/pkg/package"
)
type State interface{ Encode() string }
type PackageSolver interface {
BuildFormula() (bf.Formula, error)
Solve() ([]pkg.Package, error)
Apply() (map[string]bool, bf.Formula, error)
}
type Solver struct {
PackageCollection []pkg.Package
InitialState []pkg.Package
}
func NewSolver(pcoll []pkg.Package, init []pkg.Package) PackageSolver {
return &Solver{PackageCollection: pcoll, InitialState: init}
}
func (s *Solver) BuildFormula() (bf.Formula, error) {
//f := bf.True
var formulas []bf.Formula
for _, a := range s.InitialState {
init, err := a.BuildFormula()
if err != nil {
return nil, err
}
//f = bf.And(f, init)
formulas = append(formulas, init)
}
for _, p := range s.PackageCollection {
solvable, err := p.BuildFormula()
if err != nil {
return nil, err
}
//f = bf.And(f, solvable)
formulas = append(formulas, solvable)
}
return bf.And(formulas...), nil
}
func (s *Solver) solve(f bf.Formula) (map[string]bool, bf.Formula, error) {
model := bf.Solve(f)
if model == nil {
return model, f, errors.New("Unsolvable")
}
return model, f, nil
}
func (s *Solver) Apply() (map[string]bool, bf.Formula, error) {
f, err := s.BuildFormula()
fmt.Println(f)
if err != nil {
return map[string]bool{}, nil, err
}
return s.solve(f)
}
func (s *Solver) Solve() ([]pkg.Package, error) {
model, _, err := s.Apply()
if err != nil {
return []pkg.Package{}, err
}
ass := DecodeModel(model)
return ass, nil
}

View File

@@ -0,0 +1,28 @@
// Copyright © 2019 Ettore Di Giacinto <mudler@gentoo.org>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, see <http://www.gnu.org/licenses/>.
package solver_test
import (
"testing"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
func TestSolver(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Solver Suite")
}

65
pkg/solver/solver_test.go Normal file
View File

@@ -0,0 +1,65 @@
// Copyright © 2019 Ettore Di Giacinto <mudler@gentoo.org>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, see <http://www.gnu.org/licenses/>.
package solver_test
import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
pkg "gitlab.com/mudler/luet/pkg/package"
. "gitlab.com/mudler/luet/pkg/solver"
)
var _ = Describe("Solver", func() {
Context("Simple set", func() {
It("Solves correctly", func() {
B := pkg.NewPackage("B", "", []pkg.Package{}, []pkg.Package{})
A := pkg.NewPackage("A", "", []pkg.Package{B}, []pkg.Package{})
C := pkg.NewPackage("C", "", []pkg.Package{}, []pkg.Package{})
C.IsFlagged(true) // installed
s := NewSolver([]pkg.Package{A.IsFlagged(true), C, B}, []pkg.Package{C, A, B})
solution, err := s.Solve()
Expect(err).ToNot(HaveOccurred())
Expect(solution).To(ContainElement(B.IsFlagged(true)))
Expect(solution).To(ContainElement(C.IsFlagged(true)))
Expect(solution).To(ContainElement(A.IsFlagged(true)))
})
})
Context("Conflict set", func() {
It("Solves correctly", func() {
C := pkg.NewPackage("C", "", []pkg.Package{}, []pkg.Package{})
B := pkg.NewPackage("B", "", []pkg.Package{}, []pkg.Package{C})
A := pkg.NewPackage("A", "", []pkg.Package{B}, []pkg.Package{})
C.IsFlagged(true) // installed
s := NewSolver([]pkg.Package{A}, []pkg.Package{C})
solution, err := s.Solve()
Expect(solution).To(Equal([]pkg.Package{C}))
Expect(err).ToNot(HaveOccurred())
})
})
})