1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
27 """Get an unique ID from object for dot node names"""
28 return hex(id(obj)).replace('-','_')
29
30
31
32
33 _XXX = 0
34
36 """'Abstract' Base class for everything else"""
38 self.node_style = {
39 "label":"%(label)s",
40 }
41 self.connection_style = { }
42
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
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
71
72
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
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
104
105 import fuzzy.operator.Input
106 register_handler(fuzzy.operator.Input.Input,Doc_Input())
107
108
109
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
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
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
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
174
175
176
179 super(Doc_Rule,self).__init__()
180 self.connection_style.update({
181 "label":"%(label)s",
182
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
197 adj_node_name = print_dot(obj.adjective,out,system,node_name)
198
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
203 adj_node_name = print_dot(adj,out,system,node_name)
204
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
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
231
232 self.make_node(out,node_name,{"label":name})
233
234
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
239 return ""
240
245
246 import fuzzy.Variable
247 register_handler(fuzzy.Variable.Variable,Doc_Variable())
248 register_handler(fuzzy.OutputVariable.OutputVariable,Doc_OutputVariable())
249