Package fuzzy :: Package set :: Module Singleton
[hide private]
[frames] | no frames]

Source Code for Module fuzzy.set.Singleton

  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  """This set represents a non-fuzzy number.""" 
 20   
 21  __revision__ = "$Id: Singleton.py,v 1.18 2010-10-29 19:24:41 rliebscher Exp $" 
 22   
 23   
 24  from fuzzy.set.Polygon import Polygon 
 25  from fuzzy.utils import prop 
 26  from fuzzy.Exception import FuzzyException 
27 28 # use Polygon as base class so we don't need write all 29 # methods again 30 -class Singleton(Polygon):
31 """This set represents a non-fuzzy number. 32 33 Its membership is only for x equal 1.:: 34 35 * 36 | 37 | 38 | 39 -----+----- 40 x 41 42 See also U{http://pyfuzzy.sourceforge.net/demo/set/Singleton.png} 43 """ 44
45 - def __init__(self, x=0.0):
46 super(Singleton, self).__init__() 47 self._x = float(x) # so it is defined and makes pychecker & Co. happy 48 self.x = float(x) # update polygon (_x would be defined here in any case)
49 50 # pylint: disable=E0202,E0211,W0212 51 @prop
52 - def x(): #@NoSelf
53 """x 54 @type: float""" 55 def fget(self): # pylint: disable=W0612,C0111 56 return self._x
57 def fset(self, value): # pylint: disable=W0612,C0111 58 self._x = float(value) 59 self._update() 60 return locals() 61
62 - def _update(self):
63 """update polygon""" 64 p = super(Singleton, self) 65 p.clear() 66 p.add(self._x, 0.0) 67 p.add(self._x, 1.0) 68 p.add(self._x, 0.0)
69
70 - def __call__(self, x):
71 """Get membership of value x.""" 72 if x == self._x: 73 return 1.0 74 else: 75 return 0.0
76
77 - def getCOG(self):
78 """Return center of gravity.""" 79 return self._x
80
81 - def add(self, x, y, where=Polygon.END):
82 """Don't let anyone destroy our singleton.""" 83 raise FuzzyException()
84
85 - def remove(self, x, where=Polygon.END):
86 """Don't let anyone destroy our singleton.""" 87 raise FuzzyException()
88
89 - def clear(self):
90 """Don't let anyone destroy our singleton.""" 91 raise FuzzyException()
92
93 - def __repr__(self):
94 """Return representation of instance. 95 96 @return: representation of instance 97 @rtype: string 98 """ 99 return "%s.%s(x=%s)" % (self.__class__.__module__, self.__class__.__name__, self._x)
100