1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
19 # Build a conflict graph from a POSet
20 class POSetConflictGraph[E
: Object]
22 # Core is composed by:
23 # * elements that have mutiple direct parents
24 # * parents of elements that have multiple direct parents
26 var core
= new HashSet[E
]
28 # Border is composed by minimal elements of the core:
29 # * that have multiple direct parents
30 # * but whose subelements are all in single inheritance
32 var border
= new HashSet[E
]
34 # The crown is composed by the elements that are:
35 # * not part of the core nor the border
36 # * are in single inheritance
38 var crown
= new HashSet[E
]
40 # Conflict graph of the POSet
41 # Elements X and Y are in conflict if either:
42 # * X and Y are the same element
43 # * Y is a subelement of X
44 # * X and Y have common sub elements
46 var conflicts
= new HashMap[E
, Set[E
]]
57 # Compute the set of elements forming the core of the poset hierarchy.
58 private fun extract_core
do
61 if poset
[e
].direct_greaters
.length
> 1 then
62 core
.add_all
(poset
[e
].greaters
)
67 # Compute the set of elements composing the border of the core
68 # Elements belonging to the `border` are removed from the `core`
69 private fun extract_border
do
72 if not is_border
(e
) then continue
75 for e
in border
do core
.remove
(e
)
78 private fun is_border
(e
: E
): Bool do
79 for child
in poset
[e
].direct_smallers
do
80 if core
.has
(child
) then return false
85 # Compute the set of elements belonging to the crown of the inheritance hierarchy.
86 private fun extract_crown
do
89 if not core
.has
(e
) and not border
.has
(e
) then crown
.add
(e
)
93 # Check for conflict in the core.
94 # Start from border and tag every ancestors
95 private fun compute_conflicts
do
97 for e
in border
do add_conflicts
(poset
[e
].greaters
)
100 private fun add_conflict
(e
, o
: E
) do
101 if not conflicts
.has_key
(e
) then conflicts
[e
] = new HashSet[E
]
102 if not conflicts
.has_key
(o
) then conflicts
[o
] = new HashSet[E
]
107 private fun add_conflicts
(es
: Collection[E
]) do
109 for e2
in es
do add_conflict
(e1
, e2
)
113 # Used for debugging only
115 #print "core: {core.join(" ")} ({core.length})"
116 #print "border: {border.join(" ")} ({border.length})"
117 #print "crown: {crown.join(" ")} ({crown.length})"
119 for e
, c
in conflicts
do print
" {e}: {c.join(" ")}"
123 # Colorize elements from a POSet
124 # Two elements from a POSet cannot have the same color if they share common subelements
138 # A: {B, C, D, E, F, G, H}
147 # A:0, B:1, C: 2, D: 1, E: 3, F:3, G:2, H:4
150 # Ducournau, R. (2011).
151 # Coloring, a versatile technique for implementing object-oriented languages.
152 # Software: Practice and Experience, 41(6), 627–659.
153 class POSetColorer[E
: Object]
155 # Is the poset already colored?
156 var is_colored
= false
159 # REQUIRE: is_colored
160 fun ids
: Map[E
, Int] do
164 private var ids_cache
= new HashMap[E
, Int]
167 # REQUIRE: is_colored
168 fun colors
: Map[E
, Int] do
172 private var colors_cache
= new HashMap[E
, Int]
174 # REQUIRE: is_colored
175 fun poset
: POSet[E
] do
179 private var poset_cache
: POSet[E
] is noinit
181 # REQUIRE: is_colored
182 fun conflicts
: Map[E
, Set[E
]] do
184 return conflicts_cache
186 private var conflicts_cache
: Map[E
, Set[E
]] is noinit
188 private var graph
: POSetConflictGraph[E
] is noinit
190 # Start coloring on given POSet
191 fun colorize
(poset
: POSet[E
]) do
193 graph
= new POSetConflictGraph[E
](poset
)
196 conflicts_cache
= graph
.conflicts
200 private fun allocate_ids
do
202 var elements
= new HashSet[E
].from
(poset_cache
.to_a
)
203 for e
in poset_cache
.linearize
(elements
) do
204 ids_cache
[e
] = ids_cache
.length
208 # Colorize core, border and crown in that order
209 private fun compute_colors
do
212 colorize_set
(graph
.border
)
213 colorize_set
(graph
.crown
)
216 # Core elements cannot have the same color than:
217 # * one of their parents
218 # * one of their conflicting elements
219 private fun colorize_core
do
220 for e
in poset_cache
.linearize
(graph
.core
) do
221 var color
= min_color
(e
)
222 var conflicts
= graph
.conflicts
[e
]
223 while not is_color_free
(color
, conflicts
) do
226 colors_cache
[e
] = color
230 # Other elements inherit color fron their direct parents
231 private fun colorize_set
(set
: Set[E
]) do
232 for e
in poset_cache
.linearize
(set
) do colors_cache
[e
] = min_color
(e
)
235 # Get the next minimal color from direct parents
236 private fun min_color
(e
: E
): Int do
238 for p
in poset_cache
[e
].direct_greaters
do
239 if not colors_cache
.has_key
(p
) then continue
240 var color
= colors_cache
[p
]
241 if color
> max_color
then max_color
= color
246 private fun is_color_free
(color
: Int, set
: Collection[E
]): Bool do
248 if colors_cache
.has_key
(e
) and colors_cache
[e
] == color
then return false
253 # Used for debugging only
256 for e
, id
in ids
do print
" {e}: {id}"
258 for e
, c
in colors
do print
" {e}: {c}"
262 # Colorize a collection of buckets
263 # Two elements cannot have the same color if they both appear in the same bucket
264 # No coloring order is garantied
267 # buckets[A] = {x1, x2}
268 # buckets[B] = {x1, x3, x4}
269 # buckets[C] = {x2, x3}
276 # x1: 0, x2: 1, x3: 2, x4: 1
277 class BucketsColorer[H
: Object, E
: Object]
278 private var colors
= new HashMap[E
, Int]
279 private var conflicts
= new HashMap[E
, Set[E
]]
281 # Start bucket coloring
282 fun colorize
(buckets
: Map[H
, Set[E
]]): Map[E
, Int] do
283 compute_conflicts
(buckets
)
285 for holder
, hbuckets
in buckets
do
286 for bucket
in hbuckets
do
287 if colors
.has_key
(bucket
) then continue
288 var color
= min_color
289 while not is_color_free
(bucket
, color
) do
292 colors
[bucket
] = color
299 private fun is_color_free
(bucket
: E
, color
: Int): Bool do
300 if conflicts
.has_key
(bucket
) then
301 for other
in conflicts
[bucket
] do
302 if colors
.has_key
(other
) and colors
[other
] == color
then return false
308 private fun compute_conflicts
(buckets
: Map[H
, Set[E
]]) do
310 for holder
, hbuckets
in buckets
do
311 for bucket
in hbuckets
do
312 if not conflicts
.has_key
(bucket
) then conflicts
[bucket
] = new HashSet[E
]
313 for obucket
in hbuckets
do
314 if obucket
== bucket
then continue
315 if not conflicts
.has_key
(obucket
) then conflicts
[obucket
] = new HashSet[E
]
316 conflicts
[bucket
].add
(obucket
)
317 conflicts
[obucket
].add
(bucket
)
324 # Colorize a collection of buckets using a poset and a conflict graph
325 # Two elements cannot have the same color if they both appear in the same bucket
326 # The use of a POSet hierarchy optimize the coloring
327 # Buckets elements are colored using linearization order starting
328 class POSetBucketsColorer[H
: Object, E
: Object]
329 private var colors
= new HashMap[E
, Int]
330 private var poset
: POSet[H
]
331 private var conflicts
: Map[H
, Set[H
]]
333 # Colorize buckets using the POSet and conflict graph
334 fun colorize
(buckets
: Map[H
, Set[E
]]): Map[E
, Int] do
336 for h
in poset
.linearize
(buckets
.keys
) do
337 var color
= min_color
(poset
[h
].direct_greaters
, buckets
)
338 for bucket
in buckets
[h
] do
339 if colors
.has_key
(bucket
) then continue
340 while not is_color_free
(color
, h
, buckets
) do color
+= 1
341 colors
[bucket
] = color
348 # Get the next available color considering used colors by other buckets
349 private fun min_color
(others
: Collection[H
], buckets
: Map[H
, Set[E
]]): Int do
351 for holder
in others
do
352 var color
= max_color
(holder
, buckets
)
353 if color
> min
then min
= color
358 # Return the max color used by a class
359 private fun max_color
(holder
: H
, buckets
: Map[H
, Set[E
]]): Int do
361 for bucket
in buckets
[holder
] do
362 if not colors
.has_key
(bucket
) then continue
363 var color
= colors
[bucket
]
364 if color
> max
then max
= color
369 # Check if the color is free for this holder
370 private fun is_color_free
(color
: Int, holder
: H
, buckets
: Map[H
, Set[E
]]): Bool do
371 if not conflicts
.has_key
(holder
) then return true
372 for conflict
in conflicts
[holder
] do
373 for bucket
in buckets
[conflict
] do
374 if not colors
.has_key
(bucket
) then continue
375 if colors
[bucket
] == color
then return false