[dialogwizard/wizard] on prefere utiliser BRANCH comme un sous-cas de CASE
Ignore-this: 1823bc0cb76f28e983b679414317e607 darcs-hash:20090825144419-bd074-f9914cafc47fc5f8ffc9f9eb573fb8c61b3adc18.gz
This commit is contained in:
parent
c2949659c0
commit
3b80db23bf
1 changed files with 35 additions and 11 deletions
|
@ -43,6 +43,18 @@ class Scenario:
|
||||||
raise TypeError("Can only bind steps")
|
raise TypeError("Can only bind steps")
|
||||||
self.steps = ('NEST', step, self.steps)
|
self.steps = ('NEST', step, self.steps)
|
||||||
|
|
||||||
|
def case(self, switch, cases, fallback = None): #revoir le fallback
|
||||||
|
u"""Calls a function (passing to it the environment), and will
|
||||||
|
call the corresponding scenario in the second arg."""
|
||||||
|
if not callable(switch):
|
||||||
|
raise TypeError("switch must be callable")
|
||||||
|
if not isinstance(cases, dict):
|
||||||
|
raise TypeError("cases must be a dict")
|
||||||
|
for case in cases.values():
|
||||||
|
if not isinstance(case, Scenario):
|
||||||
|
raise TypeError("cases must all be Scenarios")
|
||||||
|
self.steps = ('CASE', (switch, cases, fallback), self.steps)
|
||||||
|
|
||||||
def branch(self, cond, plan_A, plan_B):
|
def branch(self, cond, plan_A, plan_B):
|
||||||
u"""Makes a test (will call it passing the environnement), and
|
u"""Makes a test (will call it passing the environnement), and
|
||||||
depending on the result, will process one of the two scenarios"""
|
depending on the result, will process one of the two scenarios"""
|
||||||
|
@ -50,7 +62,8 @@ class Scenario:
|
||||||
raise TypeError("cond must be callable")
|
raise TypeError("cond must be callable")
|
||||||
if not isinstance(plan_A, Scenario) or not isinstance(plan_B, Scenario):
|
if not isinstance(plan_A, Scenario) or not isinstance(plan_B, Scenario):
|
||||||
raise TypeError("Can only branch on scenarios")
|
raise TypeError("Can only branch on scenarios")
|
||||||
self.steps = ('BRANCH', (cond, plan_A, plan_B) , self.steps)
|
# self.steps = ('BRANCH', (cond, plan_A, plan_B) , self.steps)
|
||||||
|
self.case(cond, { True: plan_A, False: plan_B })
|
||||||
|
|
||||||
def quote(self, scenario):
|
def quote(self, scenario):
|
||||||
u"""Runs a scenario as a single scenario step"""
|
u"""Runs a scenario as a single scenario step"""
|
||||||
|
@ -87,21 +100,32 @@ class Running:
|
||||||
|
|
||||||
def step(self):
|
def step(self):
|
||||||
if self.steps:
|
if self.steps:
|
||||||
# Case of a Branching
|
# Case of a Case, as a list of possible choices depending on
|
||||||
if self.steps[0] == 'BRANCH' :
|
# the result of a function call, if it is not handled, then
|
||||||
# As it is (should be) an epsilon-test we won't
|
# it does nothing.
|
||||||
# backtrack it.
|
if self.steps[0] == 'CASE' :
|
||||||
cond, plan_A, plan_B = self.steps[1]
|
switch, cases, fallback = self.steps[1]
|
||||||
self.steps = self.steps[2]
|
self.steps = self.steps[2]
|
||||||
if cond(self.env):
|
plan_steps = cases.get(switch(self.env), fallback).steps
|
||||||
plan_steps = plan_A.steps
|
|
||||||
else:
|
|
||||||
plan_steps = plan_B.steps
|
|
||||||
# Let's not forget we need to reverse the steps lists.
|
|
||||||
while plan_steps:
|
while plan_steps:
|
||||||
self.steps = plan_steps[0], plan_steps[1], self.steps
|
self.steps = plan_steps[0], plan_steps[1], self.steps
|
||||||
plan_steps = plan_steps[2]
|
plan_steps = plan_steps[2]
|
||||||
|
|
||||||
|
## # Case of a Branching
|
||||||
|
## if self.steps[0] == 'BRANCH' :
|
||||||
|
## # As it is (should be) an epsilon-test we won't
|
||||||
|
## # backtrack it.
|
||||||
|
## cond, plan_A, plan_B = self.steps[1]
|
||||||
|
## self.steps = self.steps[2]
|
||||||
|
## if cond(self.env):
|
||||||
|
## plan_steps = plan_A.steps
|
||||||
|
## else:
|
||||||
|
## plan_steps = plan_B.steps
|
||||||
|
## # Let's not forget we need to reverse the steps lists.
|
||||||
|
## while plan_steps:
|
||||||
|
## self.steps = plan_steps[0], plan_steps[1], self.steps
|
||||||
|
## plan_steps = plan_steps[2]
|
||||||
|
|
||||||
# Case of nesting
|
# Case of nesting
|
||||||
elif self.steps[0] == 'NEST':
|
elif self.steps[0] == 'NEST':
|
||||||
try:
|
try:
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue