diff options
author | kanzure <kanzure@gmail.com> | 2009-09-25 12:43:50 -0500 |
---|---|---|
committer | kanzure <kanzure@gmail.com> | 2009-09-25 12:43:50 -0500 |
commit | a312db3e6828b1a6050f715bd6c45ad3c415cae2 (patch) | |
tree | 0537693b311f0bf6640fb5325fc14cacd5fca6e6 /doc | |
parent | 1d4cbcdfbd70becd610d6cb4ac005ecca1bbb35e (diff) | |
download | skdb-a312db3e6828b1a6050f715bd6c45ad3c415cae2.tar.gz skdb-a312db3e6828b1a6050f715bd6c45ad3c415cae2.zip |
refactoring the build method in the pie example
Diffstat (limited to 'doc')
-rw-r--r-- | doc/proposals/action.py | 80 |
1 files changed, 59 insertions, 21 deletions
diff --git a/doc/proposals/action.py b/doc/proposals/action.py index b1a315b..1415c87 100644 --- a/doc/proposals/action.py +++ b/doc/proposals/action.py @@ -10,18 +10,17 @@ import unittest #boring stuff, setting up the problem class Agent: def insert(self, object=None, into=None, surface=None): - '''insert the subject into the _into''' #obj2.affixed must be afficed to the "surface" variable print "Agent.insert: pretending to look at the surface" print "Add the %s into the %s" % (object, into) return -class Tree(tinytree.Tree): +class Plan(tinytree.Tree): pass -class ActionContainer(Node): +class Operation(Node): '''i made this so that i dont have to call functools.partial in a build method. - >>> action1 = ActionContainer(method="append", obj="a") + >>> action1 = Operation(method="append", obj="a") >>> #later you want to actually do this action >>> action1.do(agent=Human()) ''' @@ -34,14 +33,23 @@ class ActionContainer(Node): pass #meanwhile somewhere in the pie package.. -class Pie(Part, Tree): #or maybe Part should inherit from Tree? - #but isn't a Part also an Assembly or Graph? +class Pie(Part, Plan): #or maybe Part should inherit from Plan? + #def build(self, pie_name=None): + #step 0. make the crust and slice the apples + #step 1. affix pan to a surface + #step 2. affix crust to the pan (don't do this in midair) #or it should be made in the pan + #step 3. insert sliced apples into crust in the pan on the surface + #step 4. detach the pan from the surface + #step 5. bake the pan (and the food inside) into a mouth watering apple pie + def build(self, pie_name=None): - #first we look at pie_name and come up with a pie if it's there if pie_name is not None: #come up with some parameters by parsing pie_name self.crust = Crust() #another Part defined somewhere self.apples = Apples() + else: + #parameters were set in the __init__ or later + pass #step 0. make the crust and slice the apples. crust = self.crust @@ -49,25 +57,55 @@ class Pie(Part, Tree): #or maybe Part should inherit from Tree? crust.build() apples.build() - #step 1. affix crust to some surface (don't do this in midair) - surface1 = Surface() - affix = ActionContainer("affix", object=crust, _to=surface1) - affixed_crust = Tree(objects=[crust, surface1], connector=affix) + #allocate a surface + some_surface = Surface() + some_surface.build() + #TODO: a new surface is not necessary if you have a surface already + #but it's possible that other recipes (say a biology lab protocol) will require a freshly sterilized and allocated surface + + #sterilize the surface (or should we assume all allocated items are sterilized?) + sterilize = Operation("sterilize", object=some_surface) + sterilized = Plan(objects=[some_surface], connector=sterilize) + internal_steralize(some_surface) #pretend to do it + + #allocate a pan + pan = CookingPan() + pan.build() + + #TODO: a new pan is not necessary if you have the crust in one already + def check_for_pan(crust): + if crust.is_in(CookingPan): #er, how would this be done? + return False + else: return True + pan_the_crust = ConditionalOperation("insert", object=crust, into=affixed_pan, conditional=check_for_pan) + panned_crust = Plan(objects=[sterilized, crust], connector=insert) + + #sterilize the pan? + + #affix pan to the surface + affix_pan_op = Operation("affix", object=pan, _to=sterilized) + affixed_pan = Plan(objects=[pan, sterilized], connector=affix_pan_op) + + #affix crust to the pan + affix = Operation("affix", object=crust, _to=affixed_pan) + affixed_crust = Plan(objects=[crust, affixed_pan], connector=affix) - #step 2. insert sliced apples into crust - insert = ActionContainer("insert", object=apples, into=crust) - inserted = Tree(objects=[affixed_crust, apples], connector=insert) + #TODO: apples may be in a container. what then? + + #insert sliced apples into crust + insert = Operation("insert", object=apples, into=crust) + inserted = Plan(objects=[affixed_crust, apples], connector=insert) - #step 3. bake the crust-and-apples into a mouth watering apple pie - bake = ActionContainer("bake", object=inserted, temperature="450 celsius", time="2 hr") - #finally we set "self" to be this baked pie - Tree.__init__(self, objects=[inserted], connector=bake) + #bake the crust-and-apples into a mouth watering apple pie + bake = Operation("bake", object=inserted, temperature="450 celsius", time="2 hr") + #final step: turn self into a plan for a baked pie + Plan.__init__(self, objects=[inserted], connector=bake) - #you are now free to traverse this tree/part + #you are now free to traverse this part and part plan class TestInstructions(unittest.TestCase): - def test_tree(self): - #test that Part.build() makes the part into a tree + def test_plan(self): + #test that Part.build() makes the part into a plan pass def test_apple_pie(self): apple_pie = Pie("apple") |