| 1 |
# THIS FILE IS PART OF THE CYLC WORKFLOW ENGINE. |
| 2 |
# Copyright (C) NIWA & British Crown (Met Office) & Contributors. |
| 3 |
# |
| 4 |
# This program is free software: you can redistribute it and/or modify |
| 5 |
# it under the terms of the GNU General Public License as published by |
| 6 |
# the Free Software Foundation, either version 3 of the License, or |
| 7 |
# (at your option) any later version. |
| 8 |
# |
| 9 |
# This program is distributed in the hope that it will be useful, |
| 10 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
# GNU General Public License for more details. |
| 13 |
# |
| 14 |
# You should have received a copy of the GNU General Public License |
| 15 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 |
|
| 17 |
""" |
| 18 |
The C3 algorithm is used to linearize multiple inheritance hierarchies |
| 19 |
in Python and other languages (MRO = Method Resolution Order). The code |
| 20 |
in this doc string is by Samuele Pedroni and Michele Simionato and taken |
| 21 |
from the official Python web site, here: |
| 22 |
http://www.python.org/download/releases/2.3/mro/ |
| 23 |
|
| 24 |
For cylc, to linearize the runtime inheritance hierarchy, merge() |
| 25 |
is unchanged - it works on generic sequences - but mro() had to be |
| 26 |
modified to work with lists of names instead of Python base classes. |
| 27 |
|
| 28 |
class __metaclass__(type): |
| 29 |
"All classes are metamagically modified to be nicely printed" |
| 30 |
__repr__ = lambda cls: cls.__name__ |
| 31 |
|
| 32 |
class ex_2: |
| 33 |
"Serious order disagreement" #From Guido |
| 34 |
class O: pass |
| 35 |
class X(O): pass |
| 36 |
class Y(O): pass |
| 37 |
class A(X,Y): pass |
| 38 |
class B(Y,X): pass |
| 39 |
try: |
| 40 |
class Z(A,B): pass #creates Z(A,B) in Python 2.2 |
| 41 |
except TypeError: |
| 42 |
pass # Z(A,B) cannot be created in Python 2.3 |
| 43 |
|
| 44 |
class ex_5: |
| 45 |
"My first example" |
| 46 |
class O: pass |
| 47 |
class F(O): pass |
| 48 |
class E(O): pass |
| 49 |
class D(O): pass |
| 50 |
class C(D,F): pass |
| 51 |
class B(D,E): pass |
| 52 |
class A(B,C): pass |
| 53 |
|
| 54 |
class ex_6: |
| 55 |
"My second example" |
| 56 |
class O: pass |
| 57 |
class F(O): pass |
| 58 |
class E(O): pass |
| 59 |
class D(O): pass |
| 60 |
class C(D,F): pass |
| 61 |
class B(E,D): pass |
| 62 |
class A(B,C): pass |
| 63 |
|
| 64 |
class ex_9: |
| 65 |
"Difference between Python 2.2 MRO and C3" #From Samuele |
| 66 |
class O: pass |
| 67 |
class A(O): pass |
| 68 |
class B(O): pass |
| 69 |
class C(O): pass |
| 70 |
class D(O): pass |
| 71 |
class E(O): pass |
| 72 |
class K1(A,B,C): pass |
| 73 |
class K2(D,B,E): pass |
| 74 |
class K3(D,A): pass |
| 75 |
class Z(K1,K2,K3): pass |
| 76 |
|
| 77 |
def merge(seqs): |
| 78 |
print '\n\nCPL[%s]=%s' % (seqs[0][0],seqs), |
| 79 |
res = []; i=0 |
| 80 |
while 1: |
| 81 |
nonemptyseqs=[seq for seq in seqs if seq] |
| 82 |
if not nonemptyseqs: return res |
| 83 |
i+=1; print '\n',i,'round: candidates...', |
| 84 |
for seq in nonemptyseqs: # find merge candidates among seq heads |
| 85 |
cand = seq[0]; print ' ',cand, |
| 86 |
nothead=[s for s in nonemptyseqs if cand in s[1:]] |
| 87 |
if nothead: cand=None #reject candidate |
| 88 |
else: break |
| 89 |
if not cand: raise "Inconsistent hierarchy" |
| 90 |
res.append(cand) |
| 91 |
for seq in nonemptyseqs: # remove cand |
| 92 |
if seq[0] == cand: del seq[0] |
| 93 |
|
| 94 |
def mro(C): |
| 95 |
"Compute the class precedence list (mro) according to C3" |
| 96 |
return merge([[C]]+map(mro,C.__bases__)+[list(C.__bases__)]) |
| 97 |
|
| 98 |
def print_mro(C): |
| 99 |
print '\nMRO[%s]=%s' % (C,mro(C)) |
| 100 |
print '\nP22 MRO[%s]=%s' % (C,C.mro()) |
| 101 |
|
| 102 |
print_mro(ex_9.Z) |
| 103 |
""" |
| 104 |
|
| 105 |
|
| 106 |
from copy import copy |
| 107 |
|
| 108 |
|
| 109 |
class C3: |
| 110 |
def __init__(self, tree=None): |
| 111 |
if not tree: |
| 112 |
tree = {} |
| 113 |
self.tree = tree |
| 114 |
|
| 115 |
@staticmethod |
| 116 |
def merge(seqs, label=None): |
| 117 |
# print '\n\nCPL[%s]=%s' % (seqs[0][0],seqs), |
| 118 |
res = [] |
| 119 |
while True: |
| 120 |
nonemptyseqs = [seq for seq in seqs if seq] |
| 121 |
if not nonemptyseqs: |
| 122 |
return res |
| 123 |
for seq in nonemptyseqs: # find merge candidates among seq heads |
| 124 |
cand = seq[0] # print ' ',cand, |
| 125 |
nothead = [s for s in nonemptyseqs if cand in s[1:]] |
| 126 |
if nothead: |
| 127 |
cand = None # reject candidate |
| 128 |
else: |
| 129 |
break |
| 130 |
if not cand: |
| 131 |
prefix = "" |
| 132 |
if label: |
| 133 |
prefix = "{0}: ".format(label) |
| 134 |
raise Exception( |
| 135 |
"ERROR: {0}".format(prefix) + |
| 136 |
"bad runtime namespace inheritance hierarchy.\n" + |
| 137 |
"See the cylc documentation on multiple inheritance." |
| 138 |
) |
| 139 |
res.append(cand) |
| 140 |
for seq in nonemptyseqs: # remove cand |
| 141 |
if seq[0] == cand: |
| 142 |
del seq[0] |
| 143 |
|
| 144 |
def mro(self, C): |
| 145 |
"""Compute the precedence list (mro) according to C3""" |
| 146 |
# copy() required here for tree to remain unchanged |
| 147 |
return self.merge( |
| 148 |
[[C]] + [self.mro(x) for x in self.tree[C]] + [copy(self.tree[C])], |
| 149 |
label=C) |