Class: Solve::Solver

Inherits:
Object
  • Object
show all
Defined in:
lib/solve/solver.rb,
lib/solve/solver/serializer.rb

Defined Under Namespace

Classes: Serializer

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(graph, demands, ui = nil) ⇒ Solver

Returns a new instance of Solver.

Examples:

Basic use:

graph = Solve::Graph.new
graph.artifacts("mysql", "1.2.0")
demands = [["mysql"]]
Solver.new(graph, demands)

Parameters:

  • graph (Solve::Graph)
  • demands (Array<String>, Array<Array<String, String>>)
  • ui (#say) (defaults to: nil)


26
27
28
29
30
31
# File 'lib/solve/solver.rb', line 26

def initialize(graph, demands, ui = nil)
  @ds_graph = DepSelector::DependencyGraph.new
  @graph = graph
  @demands_array = demands
  @timeout_ms = 1_000
end

Instance Attribute Details

#demands_arrayArray<String>, Array<Array<String, String>> (readonly)

Returns demands.

Examples:

Demands are Arrays of Arrays with an artifact name and optional constraint:

[['nginx', '= 1.0.0'], ['mysql']]

Returns:

  • (Array<String>, Array<Array<String, String>>)

    demands



16
17
18
# File 'lib/solve/solver.rb', line 16

def demands_array
  @demands_array
end

#graphSolve::Graph (readonly)

Graph object with references to all known artifacts and dependency constraints.

Returns:



11
12
13
# File 'lib/solve/solver.rb', line 11

def graph
  @graph
end

Instance Method Details

#demandsArray<Solve::Demand>

The problem demands given as Demand model objects

Returns:



35
36
37
38
39
# File 'lib/solve/solver.rb', line 35

def demands
  demands_array.map do |name, constraint|
    Demand.new(self, name, constraint)
  end
end

#resolve(options = {}) ⇒ Hash, List

Returns a hash like { “Artifact Name” => “Version”,… } unless the :sorted option is true, then it returns a list like [[“Artifact Name”, “Version],…]

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :sorted (Boolean)

    return the solution as a sorted list instead of a Hash

Returns:

  • (Hash, List)

    Returns a hash like { “Artifact Name” => “Version”,… } unless the :sorted option is true, then it returns a list like [[“Artifact Name”, “Version],…]

Raises:



51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/solve/solver.rb', line 51

def resolve(options = {})
  solution = solve_demands(demands_as_constraints)

  unsorted_solution = solution.inject({}) do |stringified_soln, (name, version)|
    stringified_soln[name] = version.to_s
    stringified_soln
  end

  if options[:sorted]
    build_sorted_solution(unsorted_solution)
  else
    unsorted_solution
  end
end