Skip to content

Commit

Permalink
[revise] Remove Entity and EntitySource
Browse files Browse the repository at this point in the history
Removes Entity and EntitySource. The solver takes in
the Variable Source directly. The constraints, filters, sort
or any transformation should be performed on variables before
providing it to solver.

Signed-off-by: Varsha Prasad Narsing <varshaprasad96@gmail.com>
  • Loading branch information
varshaprasad96 authored and perdasilva committed Aug 2, 2023
1 parent 35e9eff commit 7012b3f
Show file tree
Hide file tree
Showing 13 changed files with 117 additions and 239 deletions.
5 changes: 4 additions & 1 deletion cmd/dimacs/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ func solve(path string) error {
}

// build solver
so := solver.NewDeppySolver(NewDimacsEntitySource(dimacs), NewDimacsVariableSource(dimacs))
so, err := solver.NewDeppySolver(NewDimacsVariableSource(dimacs))
if err != nil {
return err
}

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

func (d *ConstraintGenerator) GetVariables(ctx context.Context, entitySource input.EntitySource) ([]deppy.Variable, error) {
func (d *ConstraintGenerator) GetVariables(ctx 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
}

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

This file was deleted.

7 changes: 5 additions & 2 deletions cmd/sudoku/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@ func NewSudokuCommand() *cobra.Command {

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

// 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,20 +4,16 @@ import (
"context"
"fmt"
"math/rand"
"strconv"
"time"

"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 @@ -27,25 +23,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(ctx 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
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.

28 changes: 0 additions & 28 deletions pkg/deppy/input/entity_test.go

This file was deleted.

62 changes: 0 additions & 62 deletions pkg/deppy/input/query.go

This file was deleted.

2 changes: 1 addition & 1 deletion pkg/deppy/input/variable_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

// VariableSource generates solver constraints given an entity querier interface
type VariableSource interface {
GetVariables(ctx context.Context, entitySource EntitySource) ([]deppy.Variable, error)
GetVariables(ctx context.Context) ([]deppy.Variable, error)
}

var _ deppy.Variable = &SimpleVariable{}
Expand Down
6 changes: 2 additions & 4 deletions pkg/deppy/solver/solver.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,21 +77,19 @@ func AddAllVariablesToSolution() Option {
// DeppySolver is a simple solver implementation that takes an entity source group and a constraint aggregator
// to produce a Solution (or error if no solution can be found)
type DeppySolver struct {
entitySource input.EntitySource
variableSource input.VariableSource
}

func NewDeppySolver(entitySource input.EntitySource, variableSource input.VariableSource) *DeppySolver {
func NewDeppySolver(variableSource input.VariableSource) *DeppySolver {
return &DeppySolver{
entitySource: entitySource,
variableSource: variableSource,
}
}

func (d DeppySolver) Solve(ctx context.Context, options ...Option) (*Solution, error) {
solutionOpts := defaultSolutionOptions().apply(options...)

vars, err := d.variableSource.GetVariables(ctx, d.entitySource)
vars, err := d.variableSource.GetVariables(ctx)
if err != nil {
return nil, err
}
Expand Down
Loading

0 comments on commit 7012b3f

Please sign in to comment.