Package fuzzy :: Package doc :: Package structure :: Package dot :: Module handlers
[hide private]
[frames] | no frames]

Source Code for Module fuzzy.doc.structure.dot.handlers

  1  # -*- coding: utf-8 -*- 
  2  # 
  3  # Copyright (C) 2009  Rene Liebscher 
  4  # 
  5  # This program is free software; you can redistribute it and/or modify it under 
  6  # the terms of the GNU Lesser General Public License as published by the Free  
  7  # Software Foundation; either version 3 of the License, or (at your option) any 
  8  # later version. 
  9  # 
 10  # This program is distributed in the hope that it will be useful, but WITHOUT  
 11  # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 
 12  # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 
 13  # details. 
 14  #  
 15  # You should have received a copy of the GNU Lesser General Public License 
 16  # along with this program; if not, see <http://www.gnu.org/licenses/>.  
 17  # 
 18   
 19  """Handlers for different object types which print the object in dot format""" 
 20   
 21  __revision__ = "$Id: handlers.py,v 1.8 2010-02-17 19:52:28 rliebscher Exp $" 
 22   
 23   
 24  from fuzzy.doc.structure.dot.dot import register_handler,print_dot 
 25   
26 -def ID(obj):
27 """Get an unique ID from object for dot node names""" 28 return hex(id(obj)).replace('-','_')
29 30 # if set to 1 every adjective in every rule is unique 31 # otherwise same adjectives in different rules are collapsed 32 # into one when in same graph 33 _XXX = 0 34
35 -class DocBase(object):
36 """'Abstract' Base class for everything else"""
37 - def __init__(self):
38 self.node_style = { 39 "label":"%(label)s", 40 } 41 self.connection_style = { }
42
43 - def make_node(self,out,name,values={}):
44 opt = "" 45 for option,value in self.node_style.items(): 46 if value == None: 47 continue 48 if opt != "": 49 opt += "," 50 opt += '%s="%s"' % ( option , (value % values) ) 51 if opt != "": 52 opt = " [" + opt + "]" 53 out.write("%s%s;\n" % (name,opt))
54
55 - def make_connection(self,out,node1,node2,values={}):
56 opt = "" 57 for option,value in self.connection_style.items(): 58 if value == None: 59 continue 60 if opt != "": 61 opt += "," 62 opt += '%s="%s"' % ( option , (value % values) ) 63 if opt != "": 64 opt = " [" + opt + "]" 65 out.write("%s -> %s%s;\n" % (node1,node2,opt))
66 67 #########################
68 -class Doc_Compound(DocBase):
69 - def __init__(self):
70 super(Doc_Compound,self).__init__()
71 #self.node_style.update( 72 # { "comment":"XXX" } 73 #) 74
75 - def __call__(self,obj,out,system,parent_name):
76 norm_name = print_dot(obj.norm,out,system,parent_name) 77 for i in obj.inputs: 78 inp_node_name = print_dot(i,out,system,norm_name) 79 self.make_connection(out,inp_node_name,norm_name) 80 return norm_name
81 82 import fuzzy.operator.Compound 83 register_handler(fuzzy.operator.Compound.Compound,Doc_Compound()) 84 85 #########################
86 -class Doc_Const(DocBase):
87 - def __call__(self,obj,out,system,parent_name):
88 prefix = (parent_name+"_") if _XXX else "" 89 node_name = prefix + "CONST_" + ID(obj) 90 self.make_node(out,node_name,{"label":"%g" % obj.value}) 91 return node_name
92 93 import fuzzy.operator.Const 94 register_handler(fuzzy.operator.Const.Const,Doc_Const()) 95 96 97 #########################
98 -class Doc_Input(DocBase):
99 - def __call__(self,obj,out,system,parent_name):
100 #out.write("{rank=min;\n") 101 input_name = print_dot(obj.adjective,out,system,parent_name) 102 #out.write("}\n") 103 return input_name
104 105 import fuzzy.operator.Input 106 register_handler(fuzzy.operator.Input.Input,Doc_Input()) 107 108 109 #########################
110 -class Doc_Not(DocBase):
111 - def __init__(self):
112 super(Doc_Not,self).__init__() 113 self.node_style.update({ "label":"NOT" })
114
115 - def __call__(self,obj,out,system,parent_name):
116 prefix = (parent_name+"_") if _XXX else "" 117 node_name = prefix + "NOT_" + ID(obj) 118 self.make_node(out,node_name) 119 inp_node_name = print_dot(obj.input,out,system,node_name) 120 self.make_connection(out,inp_node_name,node_name) 121 return node_name
122 123 import fuzzy.operator.Not 124 register_handler(fuzzy.operator.Not.Not,Doc_Not()) 125 126 #########################
127 -class Doc_Norm(DocBase):
128 - def __call__(self,obj,out,system,parent_name):
129 prefix = (parent_name+"_") if _XXX else "" 130 node_name = prefix + "NORM_" + ID(obj) 131 norm_name = obj.__class__.__name__ 132 self.make_node(out,node_name,{"label":norm_name}) 133 return node_name
134 135 import fuzzy.norm.Norm 136 register_handler(fuzzy.norm.Norm.Norm,Doc_Norm()) 137 138 #########################
139 -class Doc_ParametricNorm(Doc_Norm):
140 - def __call__(self,obj,out,system,parent_name):
141 prefix = (parent_name+"_") if _XXX else "" 142 node_name = prefix + "NORM_" + ID(obj) 143 norm_name = obj.__class__.__name__ 144 self.make_node(out,node_name,{"label":"%s(%g)" % (norm_name,obj.p)}) 145 return node_name
146 147 import fuzzy.norm.ParametricNorm 148 register_handler(fuzzy.norm.ParametricNorm.ParametricNorm,Doc_ParametricNorm()) 149 150 #########################
151 -class Doc_Adjective(DocBase):
152 - def __init__(self):
153 super(Doc_Adjective,self).__init__() 154 self.node_style.update({ 155 "shape":"box", 156 "style":"filled", 157 "fillcolor":"palegreen", 158 })
159
160 - def __call__(self,obj,out,system,parent_name):
161 prefix = (parent_name+"_") if _XXX else "" 162 node_name = prefix + "ADJ_" + ID(obj) 163 adj = obj.getName(system) 164 if not(adj is None): 165 adjname = adj[1] + "." + adj[0] 166 else: 167 adjname = "???" 168 self.make_node(out,node_name,{"label":adjname}) 169 return node_name
170 171 import fuzzy.Adjective 172 register_handler(fuzzy.Adjective.Adjective,Doc_Adjective()) 173 #import fuzzy.AdjectiveProxy 174 #register_handler(fuzzy.AdjectiveProxy.AdjectiveProxy,Doc_Adjective()) 175 176 #########################
177 -class Doc_Rule(DocBase):
178 - def __init__(self):
179 super(Doc_Rule,self).__init__() 180 self.connection_style.update({ 181 "label":"%(label)s", 182 # "decorate":"true", 183 "weight":"2" 184 })
185
186 - def __call__(self,obj,out,system,parent_name):
187 node_name = "RULE_" + ID(obj) 188 name = obj.getName(system) 189 out.write( 190 """# subgraph "%(node_name)s" { 191 # label="%(name)s"; 192 """ % locals()) 193 operator_node_name = print_dot(obj.operator,out,system,node_name) 194 195 if isinstance(obj.adjective,fuzzy.Adjective.Adjective): 196 #out.write("{rank=max;\n") 197 adj_node_name = print_dot(obj.adjective,out,system,node_name) 198 #out.write("}\n") 199 self.make_connection(out,operator_node_name,adj_node_name,{"label": name + ("" if obj.certainty == 1.0 else ": %g" % obj.certainty) }) 200 elif isinstance(obj.adjective,list): 201 for adj in obj.adjective: 202 #out.write("{rank=max;\n") 203 adj_node_name = print_dot(adj,out,system,node_name) 204 #out.write("}\n") 205 self.make_connection(out,operator_node_name,adj_node_name,{"label": name + ("" if obj.certainty == 1.0 else ": %g" % obj.certainty) }) 206 else: 207 raise Exception("rule target not set.") 208 return ""
209 210 import fuzzy.Rule 211 register_handler(fuzzy.Rule.Rule,Doc_Rule()) 212 213 #################################### 214 import fuzzy.OutputVariable
215 -class Doc_Variable(DocBase):
216 - def __init__(self):
217 super(Doc_Variable,self).__init__() 218 self.node_style.update({ 219 "style":"filled", 220 "fillcolor":"pink", 221 }) 222 self.connection_style.update({ 223 "weight":"10", 224 "style":"bold" 225 })
226
227 - def __call__(self,obj,out,system,parent_name):
228 node_name = "VAR_" + ID(obj) 229 name = obj.getName(system) 230 #rank = "max" if isinstance(obj,fuzzy.OutputVariable.OutputVariable) else "min" 231 #out.write("{rank=%(rank)s;\n" % locals()) 232 self.make_node(out,node_name,{"label":name}) 233 #out.write("}\n") 234 #out.write("subgraph XXX {rank=same;\n") 235 for adj in obj.adjectives.values(): 236 adj_node_name = print_dot(adj,out,system,node_name) 237 self.make_connection(out,node_name,adj_node_name) 238 #out.write("}\n") 239 return ""
240
241 -class Doc_OutputVariable(Doc_Variable):
242 - def make_connection(self,out,node1,node2,values={}):
243 # output variables get the arrows from adjective to variable 244 super(Doc_OutputVariable,self).make_connection(out,node2,node1,values)
245 246 import fuzzy.Variable 247 register_handler(fuzzy.Variable.Variable,Doc_Variable()) 248 register_handler(fuzzy.OutputVariable.OutputVariable,Doc_OutputVariable()) 249