1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2012 Jean Privat <jean@pryen.org>
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
17 # Pre order sets and partial order set (ie hierarchies)
20 # Pre-order set graph.
21 # This class models an incremental pre-order graph where new nodes and edges can be added (but not removed).
22 # Pre-order graph has two characteristics:
23 # * reflexivity: an element is in relation with itself (ie `self.has(e) implies self.has_edge(e,e)`)
24 # * transitivity: `(self.has_edge(e,f) and self.has_edge(f,g)) implies self.has_edge(e,g)`
26 # Nodes and edges are added to the POSet.
29 # var pos = new POSet[String]
30 # pos.add_edge("A", "B") # add A->B
31 # pos.add_edge("B", "C") # add B->C
32 # pos.add_node("D") # add unconnected node "D"
36 # assert pos.has_edge("A", "B") == true # direct
39 # Since a poset is transitive, direct and indirect edges are considered by default.
40 # Direct edges (transitive-reduction) can also be considered independently.
43 # assert pos.has_edge("A", "C") == true # indirect
44 # assert pos.has_edge("A", "D") == false # no edge
45 # assert pos.has_edge("B", "A") == false # edges are directed
47 # assert pos.has_direct_edge("A", "B") == true # direct
48 # assert pos.has_direct_edge("A", "C") == false # indirect
52 # It means that the transitivity is updated while new nodes and edges are added.
53 # The transitive-reduction (*direct edges*)) is also updated,
54 # so adding new edges can make some direct edge to disappear.
57 # pos.add_edge("A","D")
58 # pos.add_edge("D","B")
59 # pos.add_edge("A","E")
60 # pos.add_edge("E","C")
67 # assert pos.has_edge("D", "C") == true # new indirect edge
68 # assert pos.has_edge("A", "B") == true # still an edge
69 # assert pos.has_direct_edge("A", "B") == false # but no-more a direct one
72 # Thanks to the `[]` method, elements can be considered relatively to the poset.
74 class POSet[E
: Object]
78 redef type COMPARED: E
is fixed
80 redef fun iterator
do return elements
.keys
.iterator
83 private var elements
= new HashMap[E
, POSetElement[E
]]
85 redef fun has
(e
) do return self.elements
.keys
.has
(e
)
87 # Add a node (an element) to the posed
88 # The new element is added unconnected to any other nodes (it is both a new root and a new leaf).
89 # Return the POSetElement associated to `e`.
90 # If `e` is already present in the POSet then just return the POSetElement (usually you will prefer []) is this case.
91 fun add_node
(e
: E
): POSetElement[E
]
93 if elements
.keys
.has
(e
) then return self.elements
[e
]
94 var poe
= new POSetElement[E
](self, e
, elements
.length
)
97 self.elements
[e
] = poe
101 # Return a view of `e` in the poset.
102 # This allows to view the elements in their relation with others elements.
104 # var poset = new POSet[String]
105 # poset.add_chain(["A", "B", "D"])
106 # poset.add_chain(["A", "C", "D"])
108 # assert a.direct_greaters.has_exactly(["B", "C"])
109 # assert a.greaters.has_exactly(["A", "B", "C", "D"])
110 # assert a.direct_smallers.is_empty
113 fun [](e
: E
): POSetElement[E
]
115 assert elements
.keys
.has
(e
)
116 return self.elements
[e
]
119 # Add an edge from `f` to `t`.
120 # Because a POSet is transitive, all transitive edges are also added to the graph.
121 # If the edge already exists, the this function does nothing.
124 # var pos = new POSet[String]
125 # pos.add_edge("A", "B") # add A->B
126 # assert pos.has_edge("A", "C") == false
127 # pos.add_edge("B", "C") # add B->C
128 # assert pos.has_edge("A", "C") == true
131 # If a reverse edge (from `t` to `f`) already exists, a loop is created.
133 # FIXME: Do something clever to manage loops.
134 fun add_edge
(f
, t
: E
)
138 # Skip if edge already present
139 if fe
.tos
.has
(t
) then return
140 # Add the edge and close the transitivity
141 for ff
in fe
.froms
do
142 var ffe
= self.elements
[ff
]
144 var tte
= self.elements
[tt
]
149 # Update the transitive reduction
150 if te
.tos
.has
(f
) then return # Skip the reduction if there is a loop
152 for x
in te
.dfroms
.to_a
do
153 var xe
= self.elements
[x
]
154 if xe
.tos
.has
(f
) then
159 for x
in fe
.dtos
.to_a
do
160 var xe
= self.elements
[x
]
161 if xe
.froms
.has
(t
) then
170 # Add an edge between all elements of `es` in order.
173 # var pos = new POSet[String]
174 # pos.add_chain(["A", "B", "C", "D"])
175 # assert pos.has_direct_edge("A", "B")
176 # assert pos.has_direct_edge("B", "C")
177 # assert pos.has_direct_edge("C", "D")
179 fun add_chain
(es
: SequenceRead[E
])
181 if es
.is_empty
then return
191 # Is there an edge (transitive or not) from `f` to `t`?
195 # Since the POSet is reflexive, true is returned if `f == t`.
198 # var pos = new POSet[String]
200 # assert pos.has_edge("A", "A") == true
202 fun has_edge
(f
,t
: E
): Bool
204 if not elements
.keys
.has
(f
) then return false
205 var fe
= self.elements
[f
]
209 # Is there a direct edge from `f` to `t`?
212 # var pos = new POSet[String]
213 # pos.add_chain(["A", "B", "C"]) # add A->B->C
214 # assert pos.has_direct_edge("A", "B") == true
215 # assert pos.has_direct_edge("A", "C") == false
216 # assert pos.has_edge("A", "C") == true
219 # Note that because of loops, the result may not be the expected one.
220 fun has_direct_edge
(f
,t
: E
): Bool
222 if not elements
.keys
.has
(f
) then return false
223 var fe
= self.elements
[f
]
224 return fe
.dtos
.has
(t
)
227 # Write the POSet as a graphviz digraph.
229 # Nodes are identified with their `to_s`.
230 # Edges are unlabeled.
231 fun write_dot
(f
: OStream)
233 f
.write
"digraph \{\n"
234 for x
in elements
.keys
do
235 var xstr
= x
.to_s
.escape_to_dot
236 f
.write
"\"{xstr}\
";\n"
237 var xe
= self.elements
[x
]
239 var ystr
= y
.to_s
.escape_to_dot
240 if self.has_edge
(y
,x
) then
241 f
.write
"\"{xstr}\
" -> \"{ystr}\
"[dir=both];\n"
243 f
.write
"\"{xstr}\
" -> \"{ystr}\
";\n"
250 # Display the POSet in a graphical windows.
251 # Graphviz with a working -Txlib is expected.
253 # See `write_dot` for details.
256 var f
= new OProcess("dot", "-Txlib")
263 # Compare two elements in an arbitrary total order.
265 # This function is mainly used to sort elements of the set in an coherent way.
268 # var pos = new POSet[String]
269 # pos.add_chain(["A", "B", "C", "D", "E"])
270 # pos.add_chain(["A", "X", "C", "Y", "E"])
271 # var a = ["X", "C", "E", "A", "D"]
273 # assert a == ["E", "D", "C", "X", "A"]
276 # POSet are not necessarily total orders because some distinct elements may be incomparable (neither greater or smaller).
277 # Therefore this method relies on arbitrary linear extension.
278 # This linear extension is a lawful total order (transitive, anti-symmetric, reflexive, and total), so can be used to compare the elements.
280 # The abstract behavior of the method is thus the following:
283 # if a == b then return 0
284 # if has_edge(b, a) then return -1
285 # if has_edge(a, b) then return 1
286 # return -1 or 1 # according to the linear extension.
289 # Note that the linear extension is stable, unless a new node or a new edge is added.
290 redef fun compare
(a
, b
: E
): Int
292 var ae
= self.elements
[a
]
293 var be
= self.elements
[b
]
294 var res
= ae
.tos
.length
<=> be
.tos
.length
295 if res
!= 0 then return res
296 return elements
[a
].count
<=> elements
[b
].count
299 # Filter elements to return only the smallest ones
302 # var s = new POSet[String]
303 # s.add_edge("B", "A")
304 # s.add_edge("C", "A")
305 # s.add_edge("D", "B")
306 # s.add_edge("D", "C")
307 # assert s.select_smallest(["A", "B"]) == ["B"]
308 # assert s.select_smallest(["A", "B", "C"]) == ["B", "C"]
309 # assert s.select_smallest(["B", "C", "D"]) == ["D"]
311 fun select_smallest
(elements
: Collection[E
]): Array[E
]
313 var res
= new Array[E
]
316 if e
== f
then continue
317 if has_edge
(f
, e
) then continue label
324 # Filter elements to return only the greatest ones
327 # var s = new POSet[String]
328 # s.add_edge("B", "A")
329 # s.add_edge("C", "A")
330 # s.add_edge("D", "B")
331 # s.add_edge("D", "C")
332 # assert s.select_greatest(["A", "B"]) == ["A"]
333 # assert s.select_greatest(["A", "B", "C"]) == ["A"]
334 # assert s.select_greatest(["B", "C", "D"]) == ["B", "C"]
336 fun select_greatest
(elements
: Collection[E
]): Array[E
]
338 var res
= new Array[E
]
341 if e
== f
then continue
342 if has_edge
(e
, f
) then continue label
349 # Sort a sorted array of poset elements using linearization order
351 # var pos = new POSet[String]
352 # pos.add_chain(["A", "B", "C", "D", "E"])
353 # pos.add_chain(["A", "X", "C", "Y", "E"])
354 # var a = pos.linearize(["X", "C", "E", "A", "D"])
355 # assert a == ["E", "D", "C", "X", "A"]
357 fun linearize
(elements
: Collection[E
]): Array[E
] do
358 var lin
= elements
.to_a
364 # View of an objet in a poset
365 # This class is a helper to handle specific queries on a same object
367 # For instance, one common usage is to add a specific attribute for each poset a class belong.
371 # var in_some_relation: POSetElement[Thing]
372 # var in_other_relation: POSetElement[Thing]
376 # t.in_some_relation.greaters
378 class POSetElement[E
: Object]
379 # The poset self belong to
382 # The real object behind the view
385 private var tos
= new HashSet[E
]
386 private var froms
= new HashSet[E
]
387 private var dtos
= new HashSet[E
]
388 private var dfroms
= new HashSet[E
]
391 # This attribute is used to force a total order for POSet#compare
392 private var count
: Int
394 # Return the set of all elements `t` that have an edge from `element` to `t`.
395 # Since the POSet is reflexive, element is included in the set.
398 # var pos = new POSet[String]
399 # pos.add_chain(["A", "B", "C", "D"])
400 # assert pos["B"].greaters.has_exactly(["B", "C", "D"])
402 fun greaters
: Collection[E
]
407 # Return the set of all elements `t` that have a direct edge from `element` to `t`.
410 # var pos = new POSet[String]
411 # pos.add_chain(["A", "B", "C", "D"])
412 # assert pos["B"].direct_greaters.has_exactly(["C"])
414 fun direct_greaters
: Collection[E
]
419 # Return the set of all elements `f` that have an edge from `f` to `element`.
420 # Since the POSet is reflexive, element is included in the set.
423 # var pos = new POSet[String]
424 # pos.add_chain(["A", "B", "C", "D"])
425 # assert pos["C"].smallers.has_exactly(["A", "B", "C"])
427 fun smallers
: Collection[E
]
432 # Return the set of all elements `f` that have an edge from `f` to `element`.
435 # var pos = new POSet[String]
436 # pos.add_chain(["A", "B", "C", "D"])
437 # assert pos["C"].direct_smallers.has_exactly(["B"])
439 fun direct_smallers
: Collection[E
]
444 # Is there an edge from `element` to `t`?
447 # var pos = new POSet[String]
448 # pos.add_chain(["A", "B", "C", "D"])
449 # assert pos["B"] <= "D"
450 # assert pos["B"] <= "C"
451 # assert pos["B"] <= "B"
452 # assert not pos["B"] <= "A"
456 return self.tos
.has
(t
)
459 # Is `t != element` and is there an edge from `element` to `t`?
462 # var pos = new POSet[String]
463 # pos.add_chain(["A", "B", "C", "D"])
464 # assert pos["B"] < "D"
465 # assert pos["B"] < "C"
466 # assert not pos["B"] < "B"
467 # assert not pos["B"] < "A"
471 return t
!= self.element
and self.tos
.has
(t
)
474 # The length of the shortest path to the root of the poset hierarchy
477 # var pos = new POSet[String]
478 # pos.add_chain(["A", "B", "C", "D"])
479 # assert pos["A"].depth == 3
480 # assert pos["D"].depth == 0
483 if direct_greaters
.is_empty
then
487 for p
in direct_greaters
do
488 var d
= poset
[p
].depth
+ 1
489 if min
== -1 or d
< min
then