Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove EntitySource #108

Merged
merged 5 commits into from
Aug 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/dimacs/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
}

// build solver
so := solver.NewDeppySolver(NewDimacsEntitySource(dimacs), NewDimacsVariableSource(dimacs))
so := solver.NewDeppySolver(NewDimacsVariableSource(dimacs))

Check warning on line 56 in cmd/dimacs/cmd.go

View check run for this annotation

Codecov / codecov/patch

cmd/dimacs/cmd.go#L56

Added line #L56 was not covered by tests

// get solution
solution, err := so.Solve(context.Background(), solver.AddAllVariablesToSolution())
Expand Down
12 changes: 5 additions & 7 deletions cmd/dimacs/dimacs_constraints.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,14 @@ func NewDimacsVariableSource(dimacs *Dimacs) *ConstraintGenerator {
}
}

func (d *ConstraintGenerator) GetVariables(ctx context.Context, entitySource input.EntitySource) ([]deppy.Variable, error) {
func (d *ConstraintGenerator) GetVariables(_ context.Context) ([]deppy.Variable, error) {
varMap := make(map[deppy.Identifier]*input.SimpleVariable, len(d.dimacs.variables))
variables := make([]deppy.Variable, 0, len(d.dimacs.variables))
if err := entitySource.Iterate(ctx, func(entity *input.Entity) error {
variable := input.NewSimpleVariable(entity.Identifier())

for _, id := range d.dimacs.variables {
variable := input.NewSimpleVariable(deppy.IdentifierFromString(id))
variables = append(variables, variable)
varMap[entity.Identifier()] = variable
return nil
}); err != nil {
return nil, err
varMap[variable.Identifier()] = variable
}

// create constraints out of the clauses
Expand Down
24 changes: 0 additions & 24 deletions cmd/dimacs/dimacs_source.go

This file was deleted.

24 changes: 24 additions & 0 deletions cmd/dimacs/dimacs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ package dimacs_test

import (
"bytes"
"context"
"testing"

"github.com/operator-framework/deppy/pkg/deppy"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

Expand Down Expand Up @@ -34,3 +37,24 @@ var _ = Describe("Dimacs", func() {
Expect(d.Clauses()).To(Equal([]string{"1 2 3"}))
})
})

var _ = Describe("Dimacs Variable Source", func() {
It("should create variables for a dimacs problem", func() {
problem := "p cnf 3 1\n1 2 3 0\n"
d, err := dimacs.NewDimacs(bytes.NewReader([]byte(problem)))
Expect(err).ToNot(HaveOccurred())
vs := dimacs.NewDimacsVariableSource(d)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
variables, err := vs.GetVariables(ctx)
Expect(err).ToNot(HaveOccurred())
Expect(variables).To(HaveLen(3))

Expect(variables[0].Identifier()).To(Equal(deppy.Identifier("1")))
Expect(variables[0].Constraints()).To(HaveLen(1))
Expect(variables[1].Identifier()).To(Equal(deppy.Identifier("2")))
Expect(variables[1].Constraints()).To(HaveLen(1))
Expect(variables[2].Identifier()).To(Equal(deppy.Identifier("3")))
Expect(variables[2].Constraints()).To(BeEmpty())
})
})
4 changes: 2 additions & 2 deletions cmd/sudoku/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ func NewSudokuCommand() *cobra.Command {

func solve() error {
// build solver
sudoku := NewSudoku()
so := solver.NewDeppySolver(sudoku, sudoku)
sudoku := &Sudoku{}
so := solver.NewDeppySolver(sudoku)

// get solution
solution, err := so.Solve(context.Background())
Expand Down
25 changes: 3 additions & 22 deletions cmd/sudoku/sudoku.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,15 @@ import (
"context"
"fmt"
"math/rand"
"strconv"

"github.com/operator-framework/deppy/pkg/deppy"
"github.com/operator-framework/deppy/pkg/deppy/constraint"
"github.com/operator-framework/deppy/pkg/deppy/input"
)

var _ input.EntitySource = &Sudoku{}
var _ input.VariableSource = &Sudoku{}

type Sudoku struct {
*input.CacheEntitySource
}
type Sudoku struct{}

func GetID(row int, col int, num int) deppy.Identifier {
n := num
Expand All @@ -26,25 +22,10 @@ func GetID(row int, col int, num int) deppy.Identifier {
}

func NewSudoku() *Sudoku {
var entities = make(map[deppy.Identifier]input.Entity, 9*9*9)
for row := 0; row < 9; row++ {
for col := 0; col < 9; col++ {
for num := 0; num < 9; num++ {
id := GetID(row, col, num)
entities[id] = *input.NewEntity(id, map[string]string{
"row": strconv.Itoa(row),
"col": strconv.Itoa(col),
"num": strconv.Itoa(num),
})
}
}
}
return &Sudoku{
CacheEntitySource: input.NewCacheQuerier(entities),
}
return &Sudoku{}
}

func (s Sudoku) GetVariables(_ context.Context, _ input.EntitySource) ([]deppy.Variable, error) {
func (s Sudoku) GetVariables(_ context.Context) ([]deppy.Variable, error) {
// adapted from: https://github.com/go-air/gini/blob/871d828a26852598db2b88f436549634ba9533ff/sudoku_test.go#L10
variables := make(map[deppy.Identifier]*input.SimpleVariable, 0)
inorder := make([]deppy.Variable, 0)
Expand Down
16 changes: 9 additions & 7 deletions internal/solver/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,33 +21,35 @@ var BenchmarkInput = func() []deppy.Variable {
nConflict = 3
)

random := rand.New(rand.NewSource(seed))

id := func(i int) deppy.Identifier {
return deppy.Identifier(strconv.Itoa(i))
}

variable := func(i int) TestVariable {
var c []deppy.Constraint
if rand.Float64() < pMandatory {
if random.Float64() < pMandatory {
c = append(c, constraint.Mandatory())
}
if rand.Float64() < pDependency {
n := rand.Intn(nDependency-1) + 1
if random.Float64() < pDependency {
n := random.Intn(nDependency-1) + 1
var d []deppy.Identifier
for x := 0; x < n; x++ {
y := i
for y == i {
y = rand.Intn(length)
y = random.Intn(length)
}
d = append(d, id(y))
}
c = append(c, constraint.Dependency(d...))
}
if rand.Float64() < pConflict {
n := rand.Intn(nConflict-1) + 1
if random.Float64() < pConflict {
n := random.Intn(nConflict-1) + 1
for x := 0; x < n; x++ {
y := i
for y == i {
y = rand.Intn(length)
y = random.Intn(length)
}
c = append(c, constraint.Conflict(id(y)))
}
Expand Down
61 changes: 0 additions & 61 deletions pkg/deppy/input/cache_entity_source.go

This file was deleted.

21 changes: 0 additions & 21 deletions pkg/deppy/input/entity.go

This file was deleted.

31 changes: 0 additions & 31 deletions pkg/deppy/input/entity_source.go

This file was deleted.

Loading