Example #1
0
 def __init__(self) :
     self.action_verify = RuleTable(doc="""Handles verifying actions for being
     at least somewhat logical. Should not change world state.""")
     self.action_trybefore = RuleTable(doc="""Handles a last attempt to make
     the action work (one shouldn't work with this table directly).""")
     self.action_before = RuleTable(doc="""Checks an action to see if it is
     even possible (opening a door is logical, but it's not immediately
     possible to open a locked door)""")
     self.action_when = RuleTable(doc="""Carries out the action.  Must not fail.""")
     self.action_report = RuleTable(doc="""Explains what happened with this
     action.  Should not change world state.""")
     self.verify = make_rule_decorator(self.action_verify)
     self.trybefore = make_rule_decorator(self.action_trybefore)
     self.before = make_rule_decorator(self.action_before)
     self.when = make_rule_decorator(self.action_when)
     self.report = make_rule_decorator(self.action_report)
Example #2
0
 def __init__(self):
     self.action_verify = RuleTable(
         doc="""Handles verifying actions for being
     at least somewhat logical. Should not change world state.""")
     self.action_trybefore = RuleTable(doc="""Handles a last attempt to make
     the action work (one shouldn't work with this table directly).""")
     self.action_before = RuleTable(doc="""Checks an action to see if it is
     even possible (opening a door is logical, but it's not immediately
     possible to open a locked door)""")
     self.action_when = RuleTable(
         doc="""Carries out the action.  Must not fail.""")
     self.action_report = RuleTable(doc="""Explains what happened with this
     action.  Should not change world state.""")
     self.verify = make_rule_decorator(self.action_verify)
     self.trybefore = make_rule_decorator(self.action_trybefore)
     self.before = make_rule_decorator(self.action_before)
     self.when = make_rule_decorator(self.action_when)
     self.report = make_rule_decorator(self.action_report)
Example #3
0
class ActionSystem(object) :
    def __init__(self) :
        self.action_verify = RuleTable(doc="""Handles verifying actions for being
        at least somewhat logical. Should not change world state.""")
        self.action_trybefore = RuleTable(doc="""Handles a last attempt to make
        the action work (one shouldn't work with this table directly).""")
        self.action_before = RuleTable(doc="""Checks an action to see if it is
        even possible (opening a door is logical, but it's not immediately
        possible to open a locked door)""")
        self.action_when = RuleTable(doc="""Carries out the action.  Must not fail.""")
        self.action_report = RuleTable(doc="""Explains what happened with this
        action.  Should not change world state.""")
        self.verify = make_rule_decorator(self.action_verify)
        self.trybefore = make_rule_decorator(self.action_trybefore)
        self.before = make_rule_decorator(self.action_before)
        self.when = make_rule_decorator(self.action_when)
        self.report = make_rule_decorator(self.action_report)
    def verify_action(self, action, ctxt) :
        """Returns either the best reason for doing the action, or, if
        there is a reason not to do it, the worst."""
        reasons = self.action_verify.notify(action, {"ctxt" : ctxt}, {"world" : ctxt.world})
        reasons = [r for r in reasons if r is not None]
        reasons.sort(key=lambda x : x.score)
        if len(reasons) == 0 :
            return LogicalOperation()
        else :
            if not reasons[0].is_acceptible() :
                return reasons[0]
            else :
                return reasons[-1]
    def run_action(self, action, ctxt, is_implied=False, write_action=False, silently=False) :
        """Runs an action by the following steps:
        * Verify - if the action is not reasonable, then the action fails
        * Trybefore - just tries to make the world in the right state for Before to succeed.
        * Before - checks if the action is possible.  May throw DoInstead to redirect execution.
        * When - carries out the action.
        * Report - reports the action.  Executes if the silently flag is False.

        is_implied, if true forces a description of the action to be
        printed.  Also (should) prevent possibly dangerous actions
        from being carried out.

        write_action is a boolean or a string such as "(first %s)".
        If considered to be true, then describes action.

        silently, if true, prevents reporting the action."""
        if (write_action or is_implied) :
            if write_action is True : write_action = "(%s)"
            ctxt.write(write_action % action.gerund_form(ctxt))
            ctxt.write("[newline]")
        reasonable = self.verify_action(action, ctxt)
        if not reasonable.is_acceptible() :
            ctxt.write(reasonable.reason)
            raise AbortAction()
        self.action_trybefore.notify(action, {"ctxt" : ctxt}, {"world" : ctxt.world})
        try :
            self.action_before.notify(action, {"ctxt" : ctxt}, {"world" : ctxt.world})
        except DoInstead as ix :
            msg = False if ix.suppress_message or silently else "(%s instead)"
            self.run_action(ix.instead, ctxt, write_action=msg)
            return
        did_something = self.action_when.notify(action, {"ctxt" : ctxt}, {"world" : ctxt.world})
        #if not did_something :
        #    raise AbortAction("There was nothing to do.") # this doesn't seem to be the right thing to do.
        if not silently :
            self.action_report.notify(action, {"ctxt" : ctxt}, {"world" : ctxt.world})
    def do_first(self, action, ctxt, silently=False) :
        """Runs an action with a "(first /doing something/)" message.
        If silently is True, then this message is not printed."""
        self.run_action(action, ctxt=ctxt, is_implied=True, write_action="(first %s)", silently=silently)
    def copy(self) :
        """Returns a copy which behaves like the original, but for
        which modifications do not change the original."""
        newat = ActionSystem()
        newat.action_verify = self.action_verify.copy()
        newat.action_trybefore = self.action_trybefore.copy()
        newat.action_before = self.action_before.copy()
        newat.action_when = self.action_when.copy()
        newat.action_report = self.action_report.copy()
        newat.verify = make_rule_decorator(newat.action_verify)
        newat.trybefore = make_rule_decorator(newat.action_trybefore)
        newat.before = make_rule_decorator(newat.action_before)
        newat.when = make_rule_decorator(newat.action_when)
        newat.report = make_rule_decorator(newat.action_report)
        return newat
    def make_documentation(self, escape, heading_level=1) :
        hls = str(heading_level)
        print "<h"+hls+">Event system</h"+hls+">"
        print "<p>This is the documentation for the event system.</p>"
        def _make_action_docs(heading_level, table, heading) :
            shls = str(heading_level+1)
            print "<h"+shls+">"+heading+"</h"+shls+">"
            table.make_documentation(escape, heading_level=heading_level+2)
        _make_action_docs(heading_level, self.action_verify, "action_verify")
        _make_action_docs(heading_level, self.action_trybefore, "action_trybefore")
        _make_action_docs(heading_level, self.action_before, "action_before")
        _make_action_docs(heading_level, self.action_when, "action_when")
        _make_action_docs(heading_level, self.action_report, "action_report")
Example #4
0
class ActionSystem(object):
    def __init__(self):
        self.action_verify = RuleTable(
            doc="""Handles verifying actions for being
        at least somewhat logical. Should not change world state.""")
        self.action_trybefore = RuleTable(doc="""Handles a last attempt to make
        the action work (one shouldn't work with this table directly).""")
        self.action_before = RuleTable(doc="""Checks an action to see if it is
        even possible (opening a door is logical, but it's not immediately
        possible to open a locked door)""")
        self.action_when = RuleTable(
            doc="""Carries out the action.  Must not fail.""")
        self.action_report = RuleTable(doc="""Explains what happened with this
        action.  Should not change world state.""")
        self.verify = make_rule_decorator(self.action_verify)
        self.trybefore = make_rule_decorator(self.action_trybefore)
        self.before = make_rule_decorator(self.action_before)
        self.when = make_rule_decorator(self.action_when)
        self.report = make_rule_decorator(self.action_report)

    def verify_action(self, action, ctxt):
        """Returns either the best reason for doing the action, or, if
        there is a reason not to do it, the worst."""
        reasons = self.action_verify.notify(action, {"ctxt": ctxt},
                                            {"world": ctxt.world})
        reasons = [r for r in reasons if r is not None]
        reasons.sort(key=lambda x: x.score)
        if len(reasons) == 0:
            return LogicalOperation()
        else:
            if not reasons[0].is_acceptible():
                return reasons[0]
            else:
                return reasons[-1]

    def run_action(self,
                   action,
                   ctxt,
                   is_implied=False,
                   write_action=False,
                   silently=False):
        """Runs an action by the following steps:
        * Verify - if the action is not reasonable, then the action fails
        * Trybefore - just tries to make the world in the right state for Before to succeed.
        * Before - checks if the action is possible.  May throw DoInstead to redirect execution.
        * When - carries out the action.
        * Report - reports the action.  Executes if the silently flag is False.

        is_implied, if true forces a description of the action to be
        printed.  Also (should) prevent possibly dangerous actions
        from being carried out.

        write_action is a boolean or a string such as "(first %s)".
        If considered to be true, then describes action.

        silently, if true, prevents reporting the action."""
        if (write_action or is_implied):
            if write_action is True: write_action = "(%s)"
            ctxt.write(write_action % action.gerund_form(ctxt))
            ctxt.write("[newline]")
        reasonable = self.verify_action(action, ctxt)
        if not reasonable.is_acceptible():
            ctxt.write(reasonable.reason)
            raise AbortAction()
        self.action_trybefore.notify(action, {"ctxt": ctxt},
                                     {"world": ctxt.world})
        try:
            self.action_before.notify(action, {"ctxt": ctxt},
                                      {"world": ctxt.world})
        except DoInstead as ix:
            msg = False if ix.suppress_message or silently else "(%s instead)"
            self.run_action(ix.instead, ctxt, write_action=msg)
            return
        did_something = self.action_when.notify(action, {"ctxt": ctxt},
                                                {"world": ctxt.world})
        #if not did_something :
        #    raise AbortAction("There was nothing to do.") # this doesn't seem to be the right thing to do.
        if not silently:
            self.action_report.notify(action, {"ctxt": ctxt},
                                      {"world": ctxt.world})

    def do_first(self, action, ctxt, silently=False):
        """Runs an action with a "(first /doing something/)" message.
        If silently is True, then this message is not printed."""
        self.run_action(action,
                        ctxt=ctxt,
                        is_implied=True,
                        write_action="(first %s)",
                        silently=silently)

    def copy(self):
        """Returns a copy which behaves like the original, but for
        which modifications do not change the original."""
        newat = ActionSystem()
        newat.action_verify = self.action_verify.copy()
        newat.action_trybefore = self.action_trybefore.copy()
        newat.action_before = self.action_before.copy()
        newat.action_when = self.action_when.copy()
        newat.action_report = self.action_report.copy()
        newat.verify = make_rule_decorator(newat.action_verify)
        newat.trybefore = make_rule_decorator(newat.action_trybefore)
        newat.before = make_rule_decorator(newat.action_before)
        newat.when = make_rule_decorator(newat.action_when)
        newat.report = make_rule_decorator(newat.action_report)
        return newat

    def make_documentation(self, escape, heading_level=1):
        hls = str(heading_level)
        print "<h" + hls + ">Event system</h" + hls + ">"
        print "<p>This is the documentation for the event system.</p>"

        def _make_action_docs(heading_level, table, heading):
            shls = str(heading_level + 1)
            print "<h" + shls + ">" + heading + "</h" + shls + ">"
            table.make_documentation(escape, heading_level=heading_level + 2)

        _make_action_docs(heading_level, self.action_verify, "action_verify")
        _make_action_docs(heading_level, self.action_trybefore,
                          "action_trybefore")
        _make_action_docs(heading_level, self.action_before, "action_before")
        _make_action_docs(heading_level, self.action_when, "action_when")
        _make_action_docs(heading_level, self.action_report, "action_report")