class Solve::Solver::Serializer

def deserialize(problem_data)

Returns:
  • (Solve::Problem) -

Parameters:
  • solver (Hash, #to_s) -- a json string or a hash representing a solver
def deserialize(problem_data)
  unless problem_data.is_a?(Hash)
    problem_data = JSON.parse(problem_data.to_s)
  end
  graph_spec = problem_data["graph"]
  demands_spec = problem_data["demands"]
  graph = load_graph(graph_spec)
  demands = load_demands(demands_spec)
  Solve::Problem.new(graph, demands)
end

def format_artifact(artifact)

def format_artifact(artifact)
  dependencies = artifact.dependencies.inject([]) do |list, dependency|
    list << format_dependency(dependency)
  end
  {
    "name" => artifact.name,
    "version" => artifact.version.to_s,
    "dependencies" => dependencies,
  }
end

def format_demand(demand)

def format_demand(demand)
  {
    "name" => demand[0],
    "constraint" => demand[1],
  }
end

def format_demands(demands)

def format_demands(demands)
  demands_list = demands.inject([]) do |list, demand|
    list << format_demand(demand)
  end
  { "demands" => demands_list }
end

def format_dependency(dependency)

def format_dependency(dependency)
  {
    "name" => dependency.name,
    "constraint" => dependency.constraint.to_s,
  }
end

def format_graph(graph)

def format_graph(graph)
  artifacts = graph.artifacts.inject([]) do |list, artifact|
    list << format_artifact(artifact)
  end
  { "graph" => artifacts }
end

def load_artifact(graph, artifact_spec)

def load_artifact(graph, artifact_spec)
  artifact = graph.artifact(artifact_spec["name"], artifact_spec["version"])
  artifact_spec["dependencies"].each do |dependency_spec|
    load_dependency(artifact, dependency_spec)
  end
  artifact
end

def load_demands(demand_specs)

def load_demands(demand_specs)
  demand_specs.inject([]) do |list, demand_spec|
    list << [demand_spec["name"], demand_spec["constraint"]]
  end
end

def load_dependency(artifact, dependency_spec)

def load_dependency(artifact, dependency_spec)
  artifact.depends(dependency_spec["name"], dependency_spec["constraint"])
end

def load_graph(artifacts_list)

def load_graph(artifacts_list)
  graph = Solve::Graph.new
  artifacts_list.each do |artifact_spec|
    load_artifact(graph, artifact_spec)
  end
  graph
end

def serialize(problem)

Returns:
  • (String) -

Parameters:
  • problem (Solve::Problem) -- struct
def serialize(problem)
  graph = problem.graph
  demands = problem.demands
  graph_hash = format_graph(graph)
  demands_hash = format_demands(demands)
  problem_data = graph_hash.merge(demands_hash)
  problem_data.to_json
end