work in progress: rule handling

git-svn-id: svn://svn.cy55.de/Zope3/src/cybertools/trunk@2146 fd906abe-77d9-0310-91a1-e0d9ade77398
This commit is contained in:
helmutm 2007-11-04 08:42:04 +00:00
parent f9e8a3f45c
commit 81b8564278
4 changed files with 46 additions and 22 deletions

View file

@ -8,6 +8,9 @@ Rule-based Execution of Actions
>>> from cybertools.composer.rule.base import RuleManager, Rule, Action >>> from cybertools.composer.rule.base import RuleManager, Rule, Action
>>> from cybertools.composer.rule.base import EventType, Event >>> from cybertools.composer.rule.base import EventType, Event
>>> from cybertools.composer.rule.instance import RuleInstance
>>> from cybertools.composer.rule.interfaces import IRuleInstance
>>> component.provideAdapter(RuleInstance, provides=IRuleInstance)
>>> from cybertools.composer.rule.base import ActionHandler >>> from cybertools.composer.rule.base import ActionHandler
>>> component.provideAdapter(ActionHandler, name='message') >>> component.provideAdapter(ActionHandler, name='message')
>>> component.provideAdapter(ActionHandler, name='sendmail') >>> component.provideAdapter(ActionHandler, name='sendmail')
@ -26,7 +29,8 @@ Rule-based Execution of Actions
>>> manager.handleEvent(Event(loginEvent)) >>> manager.handleEvent(Event(loginEvent))
>>> client = object() >>> from cybertools.composer.schema.client import Client
>>> client = Client()
>>> manager.handleEvent(Event(checkoutEvent, client)) >>> manager.handleEvent(Event(checkoutEvent, client))

View file

@ -30,6 +30,7 @@ from cybertools.composer.base import Component, Element, Compound
from cybertools.composer.base import Template from cybertools.composer.base import Template
from cybertools.composer.interfaces import IInstance from cybertools.composer.interfaces import IInstance
from cybertools.composer.rule.interfaces import IRuleManager, IRule from cybertools.composer.rule.interfaces import IRuleManager, IRule
from cybertools.composer.rule.interfaces import IRuleInstance
from cybertools.composer.rule.interfaces import IEvent, ICondition from cybertools.composer.rule.interfaces import IEvent, ICondition
from cybertools.composer.rule.interfaces import IAction, IActionHandler from cybertools.composer.rule.interfaces import IAction, IActionHandler
from cybertools.util.jeep import Jeep from cybertools.util.jeep import Jeep
@ -58,15 +59,19 @@ class RuleManager(object):
def handleEvent(self, event): def handleEvent(self, event):
rules = self.getRulesForEvent(event) rules = self.getRulesForEvent(event)
for r in rules: for r in rules:
for c in r.conditions: ri = IRuleInstance(event.context)
cond = component.getAdapter(r, ICondition, name=c) ri.template = r
if not cond(event): ri.event = event
continue ri.applyTemplate()
data = None #for c in r.conditions:
for action in r.actions: # cond = component.getAdapter(r, ICondition, name=c)
handler = component.getAdapter(action, IActionHandler, # if not cond(event):
name=action.handlerName) # continue
data = handler(data, event) #data = None
#for action in r.actions:
# handler = component.getAdapter(action, IActionHandler,
# name=action.handlerName)
# data = handler(data, event)
class Rule(Template): class Rule(Template):
@ -123,7 +128,7 @@ class Event(object):
class Condition(object): class Condition(object):
implements(ICondition) implements(ICondition)
adapts(IRule) adapts(IRuleInstance)
def __init__(self, context): def __init__(self, context):
self.context = context self.context = context
@ -156,7 +161,7 @@ class Action(Component):
class ActionHandler(object): class ActionHandler(object):
implements(IActionHandler) implements(IActionHandler)
adapts(IAction) adapts(IRuleInstance)
def __init__(self, context): def __init__(self, context):
self.context = context self.context = context

View file

@ -22,22 +22,29 @@ Rule instance and related classes.
$Id$ $Id$
""" """
from string import Template
from zope import component from zope import component
from zope.interface import implements from zope.component import adapts
from zope.interface import Interface, implements
from cybertools.composer.instance import Instance from cybertools.composer.instance import Instance
from cybertools.composer.interfaces import IInstance from cybertools.composer.rule.interfaces import IRuleInstance, IActionHandler
class RuleInstance(Instance): class RuleInstance(Instance):
template = client = None implements(IRuleInstance)
adapts(Interface)
def __init__(self, client, template): event = None
self.client = client
self.template = template
def applyTemplate(self, **kw): def applyTemplate(self, **kw):
pass for c in self.template.conditions:
cond = component.getAdapter(self, ICondition, name=c)
if not cond():
continue
data = None
for action in self.template.actions:
handler = component.getAdapter(self, IActionHandler,
name=action.handlerName)
data = handler(data, self.event)

View file

@ -26,7 +26,7 @@ from zope.interface import Interface, Attribute
from zope.i18nmessageid import MessageFactory from zope.i18nmessageid import MessageFactory
from zope import schema from zope import schema
from cybertools.composer.interfaces import ITemplate, IComponent from cybertools.composer.interfaces import ITemplate, IComponent, IInstance
_ = MessageFactory('zope') _ = MessageFactory('zope')
@ -83,6 +83,14 @@ class IRule(ITemplate):
actions = Attribute('Sequence of actions to be carried out by this rule.') actions = Attribute('Sequence of actions to be carried out by this rule.')
class IRuleInstance(IInstance):
""" A rule instance (typically used as an adapter) acts on some context
(client) object for executing the rule's actions.
"""
event = Attribute('The event the rule instance was created for.')
class IEvent(Interface): class IEvent(Interface):
name = Attribute('The name by which the event will be identified.') name = Attribute('The name by which the event will be identified.')
@ -92,7 +100,7 @@ class IEvent(Interface):
class ICondition(Interface): class ICondition(Interface):
def __call__(context, event, params): def __call__(context, params):
""" Should return True if the condition should be fulfilled; """ Should return True if the condition should be fulfilled;
will allow the rule to call its actions. will allow the rule to call its actions.
""" """