Class: Solve::GecodeSolver

Inherits:
Object
  • Object
show all
Defined in:
lib/solve/gecode_solver.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(graph, demands, options = {}) ⇒ GecodeSolver

Returns a new instance of GecodeSolver.

Examples:

Basic use:

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

Parameters:

  • graph (Solve::Graph)
  • demands (Array<String>, Array<Array<String, String>>)
  • options (Hash) (defaults to: {})
    • unused, only present for API compatibility with RubySolver



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

def initialize(graph, demands, options = {})
  @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



34
35
36
# File 'lib/solve/gecode_solver.rb', line 34

def demands_array
  @demands_array
end

#graphSolve::Graph (readonly)

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

Returns:



29
30
31
# File 'lib/solve/gecode_solver.rb', line 29

def graph
  @graph
end

Class Method Details

.activateObject

Attemp to load the dep_selector gem which this solver engine requires.



18
19
20
21
22
# File 'lib/solve/gecode_solver.rb', line 18

def activate
  require 'dep_selector'
rescue LoadError => e
  raise Errors::EngineNotAvailable, "dep_selector is not installed, GecodeSolver cannot be used (#{e})"
end

.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/gecode_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:



53
54
55
56
57
# File 'lib/solve/gecode_solver.rb', line 53

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:



69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/solve/gecode_solver.rb', line 69

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