Commit 58bf5663 authored by Julien Muchembled's avatar Julien Muchembled

Equivalence testers: refactoring

git-svn-id: https://svn.erp5.org/repos/public/erp5/sandbox/amount_generator@39543 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 78f5044a
......@@ -26,11 +26,9 @@
#
##############################################################################
import zope.interface
from AccessControl import ClassSecurityInfo
from Products.ERP5.Document.Predicate import Predicate
from Products.ERP5Type import Permissions, PropertySheet, interfaces
from Products.ERP5Type import Permissions, PropertySheet
from Products.ERP5.mixin.equivalence_tester import EquivalenceTesterMixin
class CategoryMembershipEquivalenceTester(Predicate, EquivalenceTesterMixin):
......@@ -56,8 +54,9 @@ class CategoryMembershipEquivalenceTester(Predicate, EquivalenceTesterMixin):
, PropertySheet.SolverSelection
)
# Declarative interfaces
zope.interface.implements( interfaces.IEquivalenceTester, )
@staticmethod
def _getTestedPropertyValue(movement, property):
return movement.getPropertyList(property)
def _compare(self, prevision_movement, decision_movement):
"""
......@@ -69,8 +68,10 @@ class CategoryMembershipEquivalenceTester(Predicate, EquivalenceTesterMixin):
lambda x:False)(tested_property):
decision_value = decision_movement.getRecordedProperty(tested_property)
else:
decision_value = decision_movement.getPropertyList(tested_property)
prevision_value = prevision_movement.getPropertyList(tested_property)
decision_value = self._getTestedPropertyValue(decision_movement,
tested_property)
prevision_value = self._getTestedPropertyValue(prevision_movement,
tested_property)
# XXX do we have configurable parameter for this divergence tester ?
# like ambiguity...
......@@ -80,34 +81,3 @@ class CategoryMembershipEquivalenceTester(Predicate, EquivalenceTesterMixin):
'The values of ${property_name} category are different between decision and prevision.',
dict(property_name=tested_property))
return None
def generateHashKey(self, movement):
"""
Returns a hash key which can be used to optimise the
matching algorithm between movements. The purpose
of this hash key is to reduce the size of lists of
movements which need to be compared using the compare
method (quadratic complexity).
If decision_movement is a simulation movement, use
the recorded properties instead of the native ones.
"""
tested_property = self.getTestedProperty()
if movement.isPropertyRecorded(tested_property):
value = movement.getRecordedProperty(tested_property)
else:
value = movement.getPropertyList(tested_property)
return '%s/%r' % (tested_property, value)
def getUpdatablePropertyDict(self, prevision_movement, decision_movement):
"""
Returns a list of properties to update on decision_movement
prevision_movement so that next call to compare returns True.
prevision_movement -- a simulation movement (prevision)
decision_movement -- a delivery movement (decision)
"""
tested_property = self.getTestedProperty()
prevision_value = prevision_movement.getPropertyList(tested_property)
return {tested_property:prevision_value}
......@@ -26,12 +26,10 @@
#
##############################################################################
import zope.interface
from AccessControl import ClassSecurityInfo
from DateTime import DateTime
from Products.ERP5.Document.Predicate import Predicate
from Products.ERP5Type import Permissions, PropertySheet, interfaces
from Products.ERP5Type import Permissions, PropertySheet
from Products.ERP5.mixin.equivalence_tester import EquivalenceTesterMixin
class DateTimeEquivalenceTester(Predicate, EquivalenceTesterMixin):
......@@ -58,9 +56,6 @@ class DateTimeEquivalenceTester(Predicate, EquivalenceTesterMixin):
PropertySheet.SolverSelection
)
# Declarative interfaces
zope.interface.implements(interfaces.IEquivalenceTester,)
def _compare(self, prevision_movement, decision_movement):
"""
If prevision_movement and decision_movement don't match, it returns a
......@@ -71,8 +66,10 @@ class DateTimeEquivalenceTester(Predicate, EquivalenceTesterMixin):
lambda x:False)(tested_property):
decision_value = decision_movement.getRecordedProperty(tested_property)
else:
decision_value = decision_movement.getProperty(tested_property) or DateTime(0)
prevision_value = prevision_movement.getProperty(tested_property) or DateTime(0)
decision_value = self._getTestedPropertyValue(
decision_movement, tested_property) or DateTime(0)
prevision_value = self._getTestedPropertyValue(
prevision_movement, tested_property) or DateTime(0)
delta = decision_value - prevision_value
# XXX we should use appropriate property sheets and getter methods
......@@ -95,16 +92,3 @@ class DateTimeEquivalenceTester(Predicate, EquivalenceTesterMixin):
'The difference of ${property_name} between decision and prevision is larger than ${value}.',
dict(property_name=tested_property,
value=absolute_tolerance_max))
def getUpdatablePropertyDict(self, prevision_movement, decision_movement):
"""
Returns a list of properties to update on decision_movement
prevision_movement so that next call to compare returns True.
prevision_movement -- a simulation movement (prevision)
decision_movement -- a delivery movement (decision)
"""
tested_property = self.getTestedProperty()
prevision_value = prevision_movement.getProperty(tested_property)
return {tested_property:prevision_value}
......@@ -28,11 +28,9 @@
#
##############################################################################
import zope.interface
from AccessControl import ClassSecurityInfo
from Products.ERP5.Document.Predicate import Predicate
from Products.ERP5Type import Permissions, PropertySheet, interfaces
from Products.ERP5Type import Permissions, PropertySheet
from Products.ERP5.mixin.equivalence_tester import EquivalenceTesterMixin
class FloatEquivalenceTester(Predicate, EquivalenceTesterMixin):
......@@ -56,9 +54,6 @@ class FloatEquivalenceTester(Predicate, EquivalenceTesterMixin):
, PropertySheet.DecimalOption
)
# Declarative interfaces
zope.interface.implements( interfaces.IEquivalenceTester, )
def _compare(self, prevision_movement, decision_movement):
"""
If prevision_movement and decision_movement don't match, it returns a
......@@ -69,8 +64,10 @@ class FloatEquivalenceTester(Predicate, EquivalenceTesterMixin):
lambda x:False)(tested_property):
decision_value = decision_movement.getRecordedProperty(tested_property)
else:
decision_value = self._getPropertyValue(decision_movement, tested_property) or 0.0
prevision_value = self._getPropertyValue(prevision_movement, tested_property) or 0.0
decision_value = self._getTestedPropertyValue(decision_movement,
tested_property) or 0.0
prevision_value = self._getTestedPropertyValue(prevision_movement,
tested_property) or 0.0
# use delivery_ratio if specified
if self.getProperty('use_delivery_ratio') and \
......@@ -173,19 +170,3 @@ class FloatEquivalenceTester(Predicate, EquivalenceTesterMixin):
ROUND_DOWN)
return Decimal(str(value)).quantize(Decimal(self.getDecimalExponent()),
rounding=rounding_option)
def getUpdatablePropertyDict(self, prevision_movement, decision_movement):
"""
Returns a list of properties to update on decision_movement
prevision_movement so that next call to compare returns True.
prevision_movement -- a simulation movement (prevision)
decision_movement -- a delivery movement (decision)
"""
tested_property = self.getTestedProperty()
prevision_value = self._getPropertyValue(prevision_movement, tested_property)
return {tested_property:prevision_value}
def _getPropertyValue(self, document, property):
return document.getProperty(property)
......@@ -28,11 +28,9 @@
#
##############################################################################
import zope.interface
from AccessControl import ClassSecurityInfo
from Products.ERP5.Document.Predicate import Predicate
from Products.ERP5Type import Permissions, PropertySheet, interfaces
from Products.ERP5Type import Permissions, PropertySheet
from Products.ERP5.mixin.equivalence_tester import EquivalenceTesterMixin
class StringEquivalenceTester(Predicate, EquivalenceTesterMixin):
......@@ -58,10 +56,6 @@ class StringEquivalenceTester(Predicate, EquivalenceTesterMixin):
, PropertySheet.SolverSelection
)
# Declarative interfaces
zope.interface.implements( interfaces.IEquivalenceTester, )
def _compare(self, prevision_movement, decision_movement):
"""
If prevision_movement and decision_movement don't match, it returns a
......@@ -72,8 +66,10 @@ class StringEquivalenceTester(Predicate, EquivalenceTesterMixin):
lambda x:False)(tested_property):
decision_value = decision_movement.getRecordedProperty(tested_property)
else:
decision_value = decision_movement.getProperty(tested_property)
prevision_value = prevision_movement.getProperty(tested_property)
decision_value = self._getTestedPropertyValue(decision_movement,
tested_property)
prevision_value = self._getTestedPropertyValue(prevision_movement,
tested_property)
# XXX do we have configurable parameter for this divergence tester ?
# like ambiguity...
......@@ -83,16 +79,3 @@ class StringEquivalenceTester(Predicate, EquivalenceTesterMixin):
'The value of ${property_name} is different between decision and prevision.',
dict(property_name=tested_property))
return None
def getUpdatablePropertyDict(self, prevision_movement, decision_movement):
"""
Returns a list of properties to update on decision_movement
prevision_movement so that next call to compare returns True.
prevision_movement -- a simulation movement (prevision)
decision_movement -- a delivery movement (decision)
"""
tested_property = self.getTestedProperty()
prevision_value = prevision_movement.getProperty(tested_property)
return {tested_property:prevision_value}
......@@ -26,11 +26,9 @@
#
##############################################################################
import zope.interface
from AccessControl import ClassSecurityInfo
from Products.ERP5.Document.Predicate import Predicate
from Products.ERP5Type import Permissions, PropertySheet, interfaces
from Products.ERP5Type import Permissions, PropertySheet
from Products.ERP5.mixin.equivalence_tester import EquivalenceTesterMixin
class VariationEquivalenceTester(Predicate, EquivalenceTesterMixin):
......@@ -56,9 +54,6 @@ class VariationEquivalenceTester(Predicate, EquivalenceTesterMixin):
, PropertySheet.SolverSelection
)
# Declarative interfaces
zope.interface.implements( interfaces.IEquivalenceTester, )
def _compare(self, prevision_movement, decision_movement):
"""
If prevision_movement and decision_movement don't match, it returns a
......@@ -70,8 +65,10 @@ class VariationEquivalenceTester(Predicate, EquivalenceTesterMixin):
lambda x:False)(tested_property):
decision_value = decision_movement.getRecordedProperty(tested_property)
else:
decision_value = decision_movement.getProperty(tested_property)
prevision_value = prevision_movement.getProperty(tested_property)
decision_value = self._getTestedPropertyValue(decision_movement,
tested_property)
prevision_value = self._getTestedPropertyValue(prevision_movement,
tested_property)
if isinstance(prevision_value, (list, tuple)):
result = sorted(decision_value) == sorted(prevision_value)
......@@ -105,7 +102,8 @@ class VariationEquivalenceTester(Predicate, EquivalenceTesterMixin):
if movement.isPropertyRecorded(tested_property):
value_list.append(movement.getRecordedProperty(tested_property))
else:
value_list.append(movement.getProperty(tested_property))
value_list.append(self._getTestedPropertyValue(movement,
tested_property))
return 'variation/%r' % (value_list)
def getUpdatablePropertyDict(self, prevision_movement, decision_movement):
......@@ -117,9 +115,7 @@ class VariationEquivalenceTester(Predicate, EquivalenceTesterMixin):
decision_movement -- a delivery movement (decision)
"""
property_dict = {}
get = self._getTestedPropertyValue
return dict((tested_property, get(prevision_movement, tested_property))
for tested_property in ('variation_category_list',
'variation_property_dict'):
prevision_value = prevision_movement.getProperty(tested_property)
property_dict[tested_property] = prevision_value
return property_dict
'variation_property_dict'))
......@@ -91,6 +91,13 @@ class EquivalenceTesterMixin:
mapping=mapping
)
@staticmethod
def _getTestedPropertyValue(movement, property):
"""
Getter returning the value for the given tested property
"""
return movement.getProperty(property)
def generateHashKey(self, movement):
"""
Returns a hash key which can be used to optimise the
......@@ -106,7 +113,7 @@ class EquivalenceTesterMixin:
if movement.isPropertyRecorded(tested_property):
value = movement.getRecordedProperty(tested_property)
else:
value = movement.getProperty(tested_property)
value = self._getTestedPropertyValue(movement, tested_property)
return '%s/%r' % (tested_property, value)
def compare(self, prevision_movement, decision_movement):
......@@ -193,7 +200,7 @@ class EquivalenceTesterMixin:
"""
raise NotImplementedError
def getUpdatablePropertyDict(prevision_movement, decision_movement):
def getUpdatablePropertyDict(self, prevision_movement, decision_movement):
"""
Returns a list of properties to update on decision_movement
prevision_movement so that next call to compare returns True.
......@@ -202,4 +209,6 @@ class EquivalenceTesterMixin:
decision_movement -- a delivery movement (decision)
"""
raise NotImplementedError
tested_property = self.getTestedProperty()
return {tested_property: self._getTestedPropertyValue(prevision_movement,
tested_property)}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment