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.



12
13
14
# File 'lib/roby/relations/directed_relation_support.rb', line 12

def relation_graphs
  @relation_graphs
end

Instance Method Details

#[](object, graph) ⇒ Object



259
260
261
# File 'lib/roby/relations/directed_relation_support.rb', line 259

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

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



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

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



171
172
173
# File 'lib/roby/relations/directed_relation_support.rb', line 171

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



180
181
182
# File 'lib/roby/relations/directed_relation_support.rb', line 180

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)


68
69
70
# File 'lib/roby/relations/directed_relation_support.rb', line 68

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

#child_objects(relation) ⇒ Object



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

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.



110
111
112
113
114
115
116
117
118
119
120
# File 'lib/roby/relations/directed_relation_support.rb', line 110

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

#each_child_object(graph, &block) ⇒ Object



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

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

#each_in_neighbour(graph, &block) ⇒ Object



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

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

#each_out_neighbour(graph, &block) ⇒ Object



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

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

#each_parent_object(graph, &block) ⇒ Object



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

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:



24
25
26
27
28
29
# File 'lib/roby/relations/directed_relation_support.rb', line 24

def each_relation
    return enum_for(__method__) if !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:



34
35
36
37
38
39
# File 'lib/roby/relations/directed_relation_support.rb', line 34

def each_relation_graph
    return enum_for(__method__) if !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



105
106
107
# File 'lib/roby/relations/directed_relation_support.rb', line 105

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:



45
46
47
48
49
50
# File 'lib/roby/relations/directed_relation_support.rb', line 45

def each_root_relation_graph
    return enum_for(__method__) if !block_given?
    each_relation_graph do |g|
        yield(g) if g.root_relation?
    end
end

#enum_child_objects(relation) ⇒ Object



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

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



148
149
150
151
# File 'lib/roby/relations/directed_relation_support.rb', line 148

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



123
124
125
126
# File 'lib/roby/relations/directed_relation_support.rb', line 123

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

#leaf?(relation = nil) ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
63
64
65
66
# File 'lib/roby/relations/directed_relation_support.rb', line 60

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)


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

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

#parent_objects(relation) ⇒ Object



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

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

Returns:

  • (Boolean)


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

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



135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/roby/relations/directed_relation_support.rb', line 135

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



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

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

#relationsObject

The array of relations this object is part of



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

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.



187
188
189
190
191
192
193
194
195
# File 'lib/roby/relations/directed_relation_support.rb', line 187

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.



199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/roby/relations/directed_relation_support.rb', line 199

def remove_children(relation = nil)
    if !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.



216
217
218
# File 'lib/roby/relations/directed_relation_support.rb', line 216

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.



222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/roby/relations/directed_relation_support.rb', line 222

def remove_parents(relation = nil)
    if !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.



240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/roby/relations/directed_relation_support.rb', line 240

def remove_relations(relation = nil)
    if !relation
        for rel in sorted_relations
            remove_relations(rel)
        end
        return
    end
    relation = relation_graphs[relation]
    return if !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)


52
53
54
55
56
57
58
# File 'lib/roby/relations/directed_relation_support.rb', line 52

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

#sorted_relationsObject



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

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