Class: PubGrub::VersionSolver

Inherits:
Object
  • Object
show all
Defined in:
lib/pub_grub/version_solver.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source:, root: Package.root) ⇒ VersionSolver

Returns a new instance of VersionSolver.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/pub_grub/version_solver.rb', line 11

def initialize(source:, root: Package.root)
  @source = source

  # { package => [incompatibility, ...]}
  @incompatibilities = Hash.new do |h, k|
    h[k] = []
  end

  @solution = PartialSolution.new

  add_incompatibility Incompatibility.new([
    Term.new(VersionConstraint.any(root), false)
  ], cause: :root)

  propagate(root)
end

Instance Attribute Details

#solutionObject (readonly)

Returns the value of attribute solution.



9
10
11
# File 'lib/pub_grub/version_solver.rb', line 9

def solution
  @solution
end

#sourceObject (readonly)

Returns the value of attribute source.



8
9
10
# File 'lib/pub_grub/version_solver.rb', line 8

def source
  @source
end

Instance Method Details

#solveObject Also known as: result



51
52
53
54
55
# File 'lib/pub_grub/version_solver.rb', line 51

def solve
  work until solved?

  solution.decisions
end

#solved?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/pub_grub/version_solver.rb', line 28

def solved?
  solution.unsatisfied.empty?
end

#workObject

Returns true if there is more work to be done, false otherwise



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/pub_grub/version_solver.rb', line 33

def work
  return false if solved?

  next_package = choose_package_version
  propagate(next_package)

  if solved?
    logger.info "Solution found after #{solution.attempted_solutions} attempts:"
    solution.decisions.each do |package, version|
      logger.info "* #{package} #{version}"
    end

    false
  else
    true
  end
end