Class: Wakame::Service::DependencyGraph

Inherits:
Wakame::StatusDB::Model show all
Defined in:
lib/wakame/service.rb

Constant Summary

Constants included from AttributeHelper

AttributeHelper::CLASS_TYPE_KEY, AttributeHelper::CONVERT_CLASSES, AttributeHelper::PRIMITIVE_CLASSES

Instance Method Summary collapse

Methods inherited from Wakame::StatusDB::Model

#delete, #dirty?, #id, inherited, #new_record?, #on_after_delete, #on_before_delete, #on_before_load, #reload, #save

Methods included from AttributeHelper

#dump_attrs, #retrieve_attr_attribute

Constructor Details

#initializeDependencyGraph

Returns a new instance of DependencyGraph.



688
689
690
691
692
# File 'lib/wakame/service.rb', line 688

def initialize()
  @graph = Graph.new
  @graph.edges = self.graph_edges = {}
  @graph.add_vertex(0)
end

Instance Method Details

#add_object(obj) ⇒ Object



694
695
696
697
698
699
700
# File 'lib/wakame/service.rb', line 694

def add_object(obj)
  res_id = Resource.id(obj)
  self.nodes[res_id.hash] = res_id
  @graph.add_edge(0, res_id.hash)
  self.save
  self
end

#children(res) ⇒ Object

Returns an array with the child resources of given node



728
729
730
731
# File 'lib/wakame/service.rb', line 728

def children(res)
  # delete() returns nil when nothing was removed. so use delete_if instead.
  @graph.children(Resource.id(res).hash).delete_if{|i| i == 0}.collect { |hashid| id2obj(hashid) }
end

#each_level(root = nil, &blk) ⇒ Object



744
745
746
747
748
749
750
751
752
753
# File 'lib/wakame/service.rb', line 744

def each_level(root=nil, &blk)
  root = root.nil? ? 0 : Resource.id(root).hash

  @graph.level_layout(root).each { |l|
    l.each { |hashid|
      next if hashid == 0
      blk.call(id2obj(hashid))
    }
  }
end

#levels(root = nil) ⇒ Object



733
734
735
736
737
738
739
740
741
742
# File 'lib/wakame/service.rb', line 733

def levels(root=nil)
  root = root.nil? ? 0 : Resource.id(root).hash

  n=[]
  @graph.level_layout(root).each { |l|
    next if l.size == 1 && l[0] == 0
    n << l.collect { |hashid| id2obj(hashid) }
  }
  n
end

#on_after_loadObject



755
756
757
758
# File 'lib/wakame/service.rb', line 755

def on_after_load
  # Delegate the edge data to be handled by Graph class when it is loaded from database.
  @graph.edges = self.graph_edges
end

#parents(res) ⇒ Object

Returns an array with the parent resources of given node



722
723
724
725
# File 'lib/wakame/service.rb', line 722

def parents(res)
  # delete() returns nil when nothing was removed. so use delete_if instead.
  @graph.parents(Resource.id(res).hash).delete_if{|i| i == 0}.collect { |hashid| id2obj(hashid) }
end

#set_dependency(parent_res, child_res) ⇒ Object



702
703
704
705
706
707
708
709
710
711
712
713
714
# File 'lib/wakame/service.rb', line 702

def set_dependency(parent_res, child_res)
  p_res_id = Resource.id(parent_res)
  c_res_id = Resource.id(child_res)
  return if p_res_id == c_res_id

  self.nodes[p_res_id.hash]=p_res_id
  self.nodes[c_res_id.hash]=c_res_id

  @graph.add_edge(p_res_id.hash, c_res_id.hash)
  @graph.remove_edge(0, c_res_id.hash) if @graph.has_edge?(0, c_res_id.hash)
  self.save
  self
end

#sizeObject



717
718
719
# File 'lib/wakame/service.rb', line 717

def size
  @graph.size - 1
end