Module: Roby::Relations::DirectedRelationSupport

Included in:
PlanObject
Defined in:
lib/roby/relations/directed_relation_support.rb

Overview

Base support for relations. It is mixed in objects on which a Relations::Space applies on, like Task for TaskStructure and EventGenerator for EventStructure.

See also the definition of Relations::Graph#add_relation and Relations::Graph#remove_relation for the possibility to define hooks that get called when a new edge involving self as a vertex gets added and removed

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#relation_graphsObject (readonly)

Returns the value of attribute relation_graphs.



14
15
16
# File 'lib/roby/relations/directed_relation_support.rb', line 14

def relation_graphs
  @relation_graphs
end

Instance Method Details

#[](object, graph) ⇒ Object



264
265
266
# File 'lib/roby/relations/directed_relation_support.rb', line 264

def [](object, graph)
    relation_graphs[graph].edge_info(self, object)
end

#[]=(object, relation, value) ⇒ Object



268
269
270
# File 'lib/roby/relations/directed_relation_support.rb', line 268

def []=(object, relation, value)
    relation_graphs[relation].set_edge_info(self, object, value)
end

#add_child_object(child, relation, info = nil) ⇒ Object

Add a new child object in the relation relation. This calls

  • #adding_child_object on self and #adding_parent_object on child just before the relation is added

  • #added_child_object on self and #added_parent_object on child just after



176
177
178
# File 'lib/roby/relations/directed_relation_support.rb', line 176

def add_child_object(child, relation, info = nil)
    relation_graphs[relation].add_relation(self, child, info)
end

#add_parent_object(parent, relation, info = nil) ⇒ Object

Add a new parent object in the relation relation

  • #adding_child_object on parent and #adding_parent_object on self just before the relation is added

  • #added_child_object on parent and #added_child_object on self just after



185
186
187
# File 'lib/roby/relations/directed_relation_support.rb', line 185

def add_parent_object(parent, relation, info = nil)
    relation_graphs[parent].add_child_object(self, relation, info)
end

#child_object?(object, relation = nil) ⇒ Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/roby/relations/directed_relation_support.rb', line 73

def child_object?(object, relation = nil)
    relation_graphs[relation].has_edge?(self, object)
end

#child_objects(relation) ⇒ Object



167
168
169
# File 'lib/roby/relations/directed_relation_support.rb', line 167

def child_objects(relation)
    relation_graphs[relation].out_neighbours(self)
end

#clear_vertex(remove_strong: true) ⇒ Object Also known as: clear_relations

Removes self from all the graphs it is included in.



115
116
117
118
119
120
121
122
123
# File 'lib/roby/relations/directed_relation_support.rb', line 115

def clear_vertex(remove_strong: true)
    for rel in sorted_relations
        graph = relation_graphs[rel]
        if (remove_strong || !graph.strong?) && graph.remove_vertex(self)
            removed = true
        end
    end
    removed
end

#each_child_object(graph, &block) ⇒ Object



93
94
95
# File 'lib/roby/relations/directed_relation_support.rb', line 93

def each_child_object(graph, &block)
    relation_graphs[graph].each_out_neighbour(self, &block)
end

#each_in_neighbour(graph, &block) ⇒ Object



89
90
91
# File 'lib/roby/relations/directed_relation_support.rb', line 89

def each_in_neighbour(graph, &block)
    relation_graphs[graph].each_in_neighbour(self, &block)
end

#each_out_neighbour(graph, &block) ⇒ Object



97
98
99
# File 'lib/roby/relations/directed_relation_support.rb', line 97

def each_out_neighbour(graph, &block)
    relation_graphs[graph].each_out_neighbour(self, &block)
end

#each_parent_object(graph, &block) ⇒ Object



85
86
87
# File 'lib/roby/relations/directed_relation_support.rb', line 85

def each_parent_object(graph, &block)
    relation_graphs[graph].each_in_neighbour(self, &block)
end

#each_relation {|| ... } ⇒ Object

Enumerate all relations that are relevant for this plan object

Unlike #each_relation_graph, which enumerate only the graphs that include self, it enumerates all possible relations for self

Yield Parameters:



26
27
28
29
30
31
32
# File 'lib/roby/relations/directed_relation_support.rb', line 26

def each_relation
    return enum_for(__method__) unless block_given?

    relation_graphs.each do |k, g|
        yield(k) if k != g
    end
end

#each_relation_graph {|| ... } ⇒ Object

Enumerate the relation graphs that include this vertex

Yield Parameters:



37
38
39
40
41
42
43
# File 'lib/roby/relations/directed_relation_support.rb', line 37

def each_relation_graph
    return enum_for(__method__) unless block_given?

    relation_graphs.each do |k, g|
        yield(g) if g.has_vertex?(self) && (k == g)
    end
end

#each_relation_sorted(&block) ⇒ Object

Yields each relation this vertex is part of, starting with the most specialized relations



110
111
112
# File 'lib/roby/relations/directed_relation_support.rb', line 110

def each_relation_sorted(&block)
    sorted_relations.each(&block)
end

#each_root_relation_graph {|| ... } ⇒ Object

Enumerate the relation graphs that include this vertex and that are subgraphs of no other graphs

Yield Parameters:



49
50
51
52
53
54
55
# File 'lib/roby/relations/directed_relation_support.rb', line 49

def each_root_relation_graph
    return enum_for(__method__) unless block_given?

    each_relation_graph do |g|
        yield(g) if g.root_relation?
    end
end

#enum_child_objects(relation) ⇒ Object



162
163
164
165
# File 'lib/roby/relations/directed_relation_support.rb', line 162

def enum_child_objects(relation)
    Roby.warn_deprecated "#enum_child_objects is deprecated, use #parent_objects or #each_parent_object instead"
    child_objects(relation)
end

#enum_parent_objects(relation) ⇒ Object



153
154
155
156
# File 'lib/roby/relations/directed_relation_support.rb', line 153

def enum_parent_objects(relation)
    Roby.warn_deprecated "#enum_parent_objects is deprecated, use #parent_objects or #each_parent_object instead"
    parent_objects(relation)
end

#enum_relationsObject



126
127
128
129
# File 'lib/roby/relations/directed_relation_support.rb', line 126

def enum_relations
    Roby.warn_deprecated "DirectedRelationSupport#enum_relations is deprecated, use #each_relation instead"
    each_relation
end

#leaf?(relation = nil) ⇒ Boolean

Returns:

  • (Boolean)


65
66
67
68
69
70
71
# File 'lib/roby/relations/directed_relation_support.rb', line 65

def leaf?(relation = nil)
    if relation
        relation_graphs[relation].leaf?(self)
    else
        each_relation_graph.all? { |g| g.leaf?(self) }
    end
end

#parent_object?(object, relation = nil) ⇒ Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/roby/relations/directed_relation_support.rb', line 77

def parent_object?(object, relation = nil)
    relation_graphs[relation].has_edge?(object, self)
end

#parent_objects(relation) ⇒ Object



158
159
160
# File 'lib/roby/relations/directed_relation_support.rb', line 158

def parent_objects(relation)
    relation_graphs[relation].in_neighbours(self)
end

Returns:

  • (Boolean)


81
82
83
# File 'lib/roby/relations/directed_relation_support.rb', line 81

def related_object?(object, relation = nil)
    parent_object?(object, relation) || child_object?(object, relation)
end

Computes and returns the set of objects related with this one (parent or child). If relation is given, enumerate only for this relation, otherwise enumerate for all relations. If result is given, it is a Set in which the related objects are added



140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/roby/relations/directed_relation_support.rb', line 140

def related_objects(relation = nil, result = Set.new)
    if relation
        result.merge(parent_objects(relation))
        result.merge(child_objects(relation))
    else
        each_root_relation_graph do |g|
            result.merge(g.in_neighbours(self))
            result.merge(g.out_neighbours(self))
        end
    end
    result
end

#relation_graph_for(rel) ⇒ Object



16
17
18
# File 'lib/roby/relations/directed_relation_support.rb', line 16

def relation_graph_for(rel)
    relation_graphs.fetch(rel)
end

#relationsObject

The array of relations this object is part of



132
133
134
# File 'lib/roby/relations/directed_relation_support.rb', line 132

def relations
    each_relation.to_a
end

#remove_child_object(child, relation = nil) ⇒ Object

Remove all edges in which self is the source and child the target. If relation is given, it removes only the edge in that relation graph.



192
193
194
195
196
197
198
199
200
# File 'lib/roby/relations/directed_relation_support.rb', line 192

def remove_child_object(child, relation = nil)
    if !relation
        for rel in sorted_relations
            rel.remove_relation(self, child)
        end
    else
        relation_graphs[relation].remove_relation(self, child)
    end
end

#remove_children(relation = nil) ⇒ Object

Remove all edges in which self is the source. If relation is given, it removes only the edges in that relation graph.



204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/roby/relations/directed_relation_support.rb', line 204

def remove_children(relation = nil)
    unless relation
        for rel in sorted_relations
            remove_children(rel)
        end
        return
    end

    children = child_objects(relation).to_a
    for child in children
        remove_child_object(child, relation)
    end
end

#remove_parent_object(parent, relation = nil) ⇒ Object

Remove all edges in which child is the source and self the target. If relation is given, it removes only the edge in that relation graph.



221
222
223
# File 'lib/roby/relations/directed_relation_support.rb', line 221

def remove_parent_object(parent, relation = nil)
    parent.remove_child_object(self, relation)
end

#remove_parents(relation = nil) ⇒ Object

Remove all edges in which self is the target. If relation is given, it removes only the edges in that relation graph.



227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/roby/relations/directed_relation_support.rb', line 227

def remove_parents(relation = nil)
    unless relation
        for rel in sorted_relations
            remove_parents(rel)
        end
        return
    end

    parents = parent_objects(relation).to_a
    for parent in parents
        remove_parent_object(relation, parent)
    end
end

#remove_relations(relation = nil) ⇒ Object

Remove all relations that point to or come from to If to is nil, it removes all edges in which self is involved.

If relation is not nil, only edges of that relation graph are removed.



245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/roby/relations/directed_relation_support.rb', line 245

def remove_relations(relation = nil)
    unless relation
        for rel in sorted_relations
            remove_relations(rel)
        end
        return
    end
    relation = relation_graphs[relation]
    return unless relation.has_vertex?(self)

    each_parent_object(relation).to_a.each do |parent|
        relation.remove_relation(parent, self)
    end

    each_child_object(relation).to_a.each do |child|
        relation.remove_relation(self, child)
    end
end

#root?(relation = nil) ⇒ Boolean

Returns:

  • (Boolean)


57
58
59
60
61
62
63
# File 'lib/roby/relations/directed_relation_support.rb', line 57

def root?(relation = nil)
    if relation
        relation_graphs[relation].root?(self)
    else
        each_relation_graph.all? { |g| g.root?(self) }
    end
end

#sorted_relationsObject



101
102
103
104
105
106
# File 'lib/roby/relations/directed_relation_support.rb', line 101

def sorted_relations
    Relations.all_relations
        .find_all do |rel|
            (rel = relation_graphs.fetch(rel, nil)) && rel.has_vertex?(self)
        end
end