| 1 | # -*- coding: utf-8 -*- |
|---|
| 2 | # |
|---|
| 3 | # Copyright (C) 2006-2007 Emmanuel Blot <emmanuel.blot@free.fr> |
|---|
| 4 | # All rights reserved. |
|---|
| 5 | # |
|---|
| 6 | # This software is licensed as described in the file COPYING, which |
|---|
| 7 | # you should have received as part of this distribution. The terms |
|---|
| 8 | # are also available at http://trac.edgewall.com/license.html. |
|---|
| 9 | # |
|---|
| 10 | # This software consists of voluntary contributions made by many |
|---|
| 11 | # individuals. For the exact contribution history, see the revision |
|---|
| 12 | # history and logs, available at http://projects.edgewall.com/trac/. |
|---|
| 13 | # |
|---|
| 14 | |
|---|
| 15 | from revtree.api import IRevtreeOptimizer |
|---|
| 16 | from trac.core import * |
|---|
| 17 | |
|---|
| 18 | __all__ = ['DefaultRevtreeOptimizer'] |
|---|
| 19 | |
|---|
| 20 | class DefaultRevtreeOptimizer(Component): |
|---|
| 21 | """Default optmizer""" |
|---|
| 22 | |
|---|
| 23 | implements(IRevtreeOptimizer) |
|---|
| 24 | |
|---|
| 25 | def optimize(self, repos, branches): |
|---|
| 26 | """Computes the optimal placement of branches. |
|---|
| 27 | |
|---|
| 28 | Optimal placement is recommended to reduce the number of operation |
|---|
| 29 | links that cross each other on the rendered graphic. |
|---|
| 30 | This rudimentary example is FAR from providing optimal placements... |
|---|
| 31 | """ |
|---|
| 32 | # FIXME: really stupid algorithm |
|---|
| 33 | graph = {} |
|---|
| 34 | for v in repos.branches().values(): |
|---|
| 35 | k = v.name |
|---|
| 36 | src = v.source() |
|---|
| 37 | if src: |
|---|
| 38 | (rev, path) = src |
|---|
| 39 | if graph.has_key(path): |
|---|
| 40 | graph[path].append(k) |
|---|
| 41 | else: |
|---|
| 42 | graph[path] = [k] |
|---|
| 43 | density = [] |
|---|
| 44 | for (p, v) in graph.items(): |
|---|
| 45 | density.append((p,len(v))) |
|---|
| 46 | density.sort(lambda a,b: cmp(a[1],b[1])) |
|---|
| 47 | density.reverse() |
|---|
| 48 | order = [] |
|---|
| 49 | cur = 0 |
|---|
| 50 | for (branch, weight) in density: |
|---|
| 51 | order.insert(cur, branch) |
|---|
| 52 | if cur: |
|---|
| 53 | cur = 0 |
|---|
| 54 | else: |
|---|
| 55 | cur = len(order) |
|---|
| 56 | nbranches = [] |
|---|
| 57 | for br in graph.values(): |
|---|
| 58 | nbranches.extend(br) |
|---|
| 59 | nbranches.extend([br.name for br in repos.branches().values() \ |
|---|
| 60 | if br.name not in nbranches]) |
|---|
| 61 | for branch in nbranches: |
|---|
| 62 | if branch in order: |
|---|
| 63 | continue |
|---|
| 64 | order.insert(cur, branch) |
|---|
| 65 | if cur: |
|---|
| 66 | cur = 0 |
|---|
| 67 | else: |
|---|
| 68 | cur = len(order) |
|---|
| 69 | obranches = [repos.branch(name) for name in order] |
|---|
| 70 | # FIXME: use filter() |
|---|
| 71 | return [b for b in obranches if b in branches] |
|---|