SolverProcess.py 9.06 KB
Newer Older
Jean-Paul Smets's avatar
Jean-Paul Smets committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
# -*- coding: utf-8 -*-
##############################################################################
#
# Copyright (c) 2009 Nexedi SA and Contributors. All Rights Reserved.
#                    Jean-Paul Smets-Solanes <jp@nexedi.com>
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsability of assessing all potential
# consequences resulting from its eventual inadequacies and bugs
# End users who are looking for a ready-to-use solution with commercial
# garantees and support are strongly adviced to contract a Free Software
# Service Company
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
##############################################################################

import zope.interface
from AccessControl import ClassSecurityInfo
from Products.CMFCore.utils import getToolByName
33
from Products.ERP5Type import Permissions, PropertySheet, interfaces
Jean-Paul Smets's avatar
Jean-Paul Smets committed
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
from Products.ERP5Type.XMLObject import XMLObject
from Products.CMFActivity.ActiveProcess import ActiveProcess

class SolverProcess(XMLObject, ActiveProcess):
  """
    Solver Process class represents the decision of the user
    to solve a divergence. The data structure is the following:

    Solver Process can contain:

    - Solver Decision documents which represent the decision
      of the user to solve a divergence on a given Delivery Line
      by using a certain heuristic

    - Target Solver documents which encapsulate the resolution
      heuristic in relation with DivergenceTester (ie. each
      DivergenceTester must provide a list of Target Solver portal 
      types whch are suitable to solve a given divergence) and
      which may eventually use a Delivery Solver each time divergence
      is related to quantities.

    Every Simulation Movement affected by a Solver Process has a relation
    to the solver process through the "solver" base category.         
  """
  meta_type = 'ERP5 Solver Process'
  portal_type = 'Solver Process'
  add_permission = Permissions.AddPortalContent
  isIndexable = 0 # We do not want to fill the catalog with objects on which we need no reporting

  # Declarative security
  security = ClassSecurityInfo()
  security.declareObjectProtected(Permissions.AccessContentsInformation)

  # Default Properties
  property_sheets = ( PropertySheet.Base
                    , PropertySheet.XMLObject
                    , PropertySheet.CategoryCore
                    , PropertySheet.DublinCore
Jean-Paul Smets's avatar
Jean-Paul Smets committed
72 73
                    )

74 75 76 77 78 79
  # Declarative interfaces
  zope.interface.implements(interfaces.ISolver,
                            interfaces.IConfigurable,
                           )

  # Implementation
Jean-Paul Smets's avatar
Jean-Paul Smets committed
80 81 82 83 84 85
  def buildTargetSolverList(self):
    """
      Builds target solvers from solver decisions
    """
    solver_dict = {}
    movement_dict = {}
Kazuhiko Shiozaki's avatar
Kazuhiko Shiozaki committed
86
    types_tool = self.portal_types
Jean-Paul Smets's avatar
Jean-Paul Smets committed
87 88 89 90

    # First create a mapping between delivery movements and solvers
    #   in order to know for each movements which solvers are needed
    #   and which parameters with
Kazuhiko Shiozaki's avatar
Kazuhiko Shiozaki committed
91
    for decision in self.contentValues(portal_type="Solver Decision"):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
92
      solver = decision.getSolverValue()
93 94 95
      # do nothing if solver is not yet set.
      if solver is None:
        continue
Jean-Paul Smets's avatar
Jean-Paul Smets committed
96 97
      solver_type = solver.getId() # ex. Postpone Production Solver
      solver_conviguration_dict = decision.getConfigurationPropertyDict()
Kazuhiko Shiozaki's avatar
Kazuhiko Shiozaki committed
98
      solver_conviguration_key = tuple(solver_conviguration_dict.items())
Jean-Paul Smets's avatar
Jean-Paul Smets committed
99 100 101 102
      for movement in decision.getDeliveryValueList():
        # Detect incompatibilities
        movement_solver_dict = movement_dict.setdefault(movement.getRelativeUrl(), {})
        movement_solver_configuration_dict = movement_solver_dict.setdefault(solver_type, {})
103
        movement_solver_configuration_dict[solver_conviguration_key] = None
Jean-Paul Smets's avatar
Jean-Paul Smets committed
104 105 106 107 108 109 110 111 112 113 114 115

    # Second, make sure solvers do not conflict and configuration is valid
    for movement_url, movement_solver_dict in movement_dict.items():
      for solver_type, movement_solver_configuration_dict in movement_solver_dict.items():
        solver = types_tool[solver_type]
        for other_solver in movement_solver_dict.keys():
          if solver.conflictsWithSolver(other_solver):
            raise "Solver %s conflicts with solver %s on movement %s" % (solver_type, other_solver, movement_url)
        # Make sure multiple configuration are possible
        try:
          # Solver key contains only those properties which differentiate
          # solvers (ex. there should be only Production Reduction Solver)
116
          solver_key = tuple(solver.reduceConfigurationList(movement_solver_configuration_dict.keys()))
Jean-Paul Smets's avatar
Jean-Paul Smets committed
117 118 119 120 121 122 123
        except:
          raise
        solver_key_dict = solver_dict.setdefault(solver_type, {})
        solver_movement_dict = solver_key_dict.setdefault(solver_key, {})
        solver_movement_dict[movement_url] = movement_solver_configuration_dict.keys()

    # Third, build target solvers
Kazuhiko Shiozaki's avatar
Kazuhiko Shiozaki committed
124
    for solver_type, solver_key_dict in solver_dict.items():
Jean-Paul Smets's avatar
Jean-Paul Smets committed
125 126 127
      for solver_key, solver_movement_dict in solver_key_dict.items():
         solver_instance = self.newContent(portal_type=solver_type)
         solver_instance._setDeliveryList(solver_movement_dict.keys())
Kazuhiko Shiozaki's avatar
Kazuhiko Shiozaki committed
128
         for movement_url, configuration_list in solver_movement_dict.iteritems():
Jean-Paul Smets's avatar
Jean-Paul Smets committed
129
           for configuration_kw in configuration_list:
130 131
             if len(configuration_kw):
               solver_instance.updateConfiguration(**dict(configuration_kw))
Jean-Paul Smets's avatar
Jean-Paul Smets committed
132

133
  # ISolver implementation
Jean-Paul Smets's avatar
Jean-Paul Smets committed
134 135 136 137 138 139 140
  # Solver Process Workflow Interface 
  #  NOTE: how can we consider that a workflow defines or provides an interface ?
  def solve(self):
    """
      Start solving
    """
    for solver in self.contentValues(portal_type=self.getPortalObject().getPortalTargetSolverTypeList()):
141
      solver.startSolving()
Kazuhiko Shiozaki's avatar
Kazuhiko Shiozaki committed
142
      solver.activate(active_process=self).solve()
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165


  # API
  def isSolverDecisionListConsistent(self):
    """
    Returns True is the Solver Process decisions do not 
    need to be rebuilt, False else. This method can be
    invoked before invoking buildSolverDecisionList if
    this helps reducing CPU time.
    """

  def buildSolverDecisionList(self, delivery_or_movement=None):
    """
    Build (or rebuild) the solver decisions in the solver process

    delivery_or_movement -- a movement, a delivery, 
                            or a list thereof
    """
    if delivery_or_movement is None:
      raise NotImplementedError
      # Gather all delivery lines already found
      # in already built solvers

166 167 168 169 170 171 172 173 174
    if not isinstance(delivery_or_movement, (tuple, list)):
      delivery_or_movement = [delivery_or_movement]
    movement_list = []
    for x in delivery_or_movement:
      if x.getPortalType() not in \
             self.getPortalObject().getPortalMovementTypeList():
        movement_list.extend(x.getMovementList())

    # We suppose here that movement_list is a list of
175 176 177 178
    # delivery lines. Let group decisions in such way
    # that a single decision is created per divergence tester instance
    # and per application level list
    solver_decision_dict = {}
179
    for movement in movement_list:
180
      for simulation_movement in movement.getDeliveryRelatedValueList():
181 182 183
        for divergence_tester in simulation_movement.getParentValue().getSpecialiseValue()._getDivergenceTesterList(exclude_quantity=False):
          if divergence_tester.compare(simulation_movement, movement):
            continue
184
          application_list = map(lambda x:x.getRelativeUrl(), 
185
                 self.getSolverDecisionApplicationValueList(movement, divergence_tester))
186
          application_list.sort()
187 188
          solver_decision_key = (divergence_tester.getRelativeUrl(), tuple(application_list))
          movement_dict = solver_decision_dict.setdefault(solver_decision_key, {})
189
          movement_dict[simulation_movement] = None
190 191 192 193 194 195

    # Now build the solver decision instances based on the previous
    # grouping
    #  XXX-JPS: pseudocode for update (ie. rebuild) is not present
    for solver_decision_key, movement_dict in solver_decision_dict.items():
      new_decision = self.newContent(portal_type='Solver Decision')
Kazuhiko Shiozaki's avatar
Kazuhiko Shiozaki committed
196
      new_decision._setDeliveryList(solver_decision_key[1])
197
      new_decision._setCausality(solver_decision_key[0])
198 199
      for simulation_movement in movement_dict.keys():
        simulation_movement.setSolverValue(new_decision)
200
      # No need to set application_list or....?
201 202 203 204 205 206

  def _generateRandomId(self):
    # call ActiveProcess._generateRandomId() explicitly otherwise
    # Folder._generateRandomId() will be called and it returns 'str' not
    # 'int' id.
    return ActiveProcess._generateRandomId(self)