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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(graph, demands) ⇒ 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>>)


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

def initialize(graph, demands)
  @ds_graph      = DepSelector::DependencyGraph.new
  @graph         = graph
  @demands_array = demands
  @timeout_ms    = self.class.timeout
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



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

def demands_array
  @demands_array
end

#graphSolve::Graph (readonly)

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

Returns:



22
23
24
# File 'lib/solve/solver.rb', line 22

def graph
  @graph
end

Class Method Details

.timeoutInteger

The timeout (in seconds) to use when resolving graphs. Default is 10. This can be configured by setting the SOLVE_TIMEOUT environment variable.

Returns:

  • (Integer)


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

def timeout
  seconds = 30 unless seconds = ENV["SOLVE_TIMEOUT"]
  seconds.to_i * 1_000
end

Instance Method Details

#demandsArray<Solve::Demand>

The problem demands given as Demand model objects

Returns:



45
46
47
48
49
# File 'lib/solve/solver.rb', line 45

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:



61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/solve/solver.rb', line 61

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