Class: Wakame::Service::DependencyGraph
- Inherits:
-
Wakame::StatusDB::Model
- Object
- Wakame::StatusDB::Model
- Wakame::Service::DependencyGraph
- 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
- #add_object(obj) ⇒ Object
-
#children(res) ⇒ Object
Returns an array with the child resources of given node.
- #each_level(root = nil, &blk) ⇒ Object
-
#initialize ⇒ DependencyGraph
constructor
A new instance of DependencyGraph.
- #levels(root = nil) ⇒ Object
- #on_after_load ⇒ Object
-
#parents(res) ⇒ Object
Returns an array with the parent resources of given node.
- #set_dependency(parent_res, child_res) ⇒ Object
- #size ⇒ Object
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
#initialize ⇒ DependencyGraph
Returns a new instance of DependencyGraph.
679 680 681 682 683 |
# File 'lib/wakame/service.rb', line 679 def initialize() @graph = Graph.new @graph.edges = self.graph_edges = {} @graph.add_vertex(0) end |
Instance Method Details
#add_object(obj) ⇒ Object
685 686 687 688 689 690 691 |
# File 'lib/wakame/service.rb', line 685 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
719 720 721 722 |
# File 'lib/wakame/service.rb', line 719 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
735 736 737 738 739 740 741 742 743 744 |
# File 'lib/wakame/service.rb', line 735 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
724 725 726 727 728 729 730 731 732 733 |
# File 'lib/wakame/service.rb', line 724 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_load ⇒ Object
746 747 748 749 |
# File 'lib/wakame/service.rb', line 746 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
713 714 715 716 |
# File 'lib/wakame/service.rb', line 713 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
693 694 695 696 697 698 699 700 701 702 703 704 705 |
# File 'lib/wakame/service.rb', line 693 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 |
#size ⇒ Object
708 709 710 |
# File 'lib/wakame/service.rb', line 708 def size @graph.size - 1 end |