def call(self, agent, bindings, zexpr): l = [termEvalErr(agent, bindings, zitem) for zitem in zexpr] if not l: raise LocatedError(zexpr, "*list function requires at least one argument") else: try: temp = list(l[0]) except TypeError: raise LocatedError(zexpr, "First argument to *list is not a sequence") return List(temp + l[1:])
def call(self, agent, bindings, zexpr): l = [termEvalErr(agent, bindings, zitem) for zitem in zexpr] if not l: raise LocatedError(zexpr, "list* function requires at least one argument") else: result = l[:-1] try: result.extend(l[-1]) except TypeError: raise LocatedError(zexpr, "Last argument to list* is not a sequence") return List(result)
def _calcChildren(self, expr): # expr = [...] for task in expr: impKey = task.impKey try: id = impKey.id except AttributeError: raise LocatedError(expr, "Cannot process expression") if id == "do:": yield DoTaskInterface(self, task, self.level + 1) elif id == "seq:" or id == "parallel:": for subtask in task: for child in self._calcChildren(subtask): yield child elif id == "try:": yield TryUnit(self, task, self.level + 1) elif id == "wait:": yield WaitInterface(self, task, self.level + 1) elif id == "select:": yield SelectInterface(self, task, self.level + 1) elif id == "succeed:" or id == "fail:": yield SucceedFailInterface(self, task, self.level + 1) elif id == "context:" or id == "retract:" or id == "retractall:": yield ConditionInterface(self, task, self.level + 1) elif id == "conclude:": yield ConcludeInterface(self, task, self.level + 1) elif id == "achieve:": yield AchieveInterface(self, task, self.level + 1) elif id == "set:": yield SetInterface(self, task, self.level + 1) expr = task[1] elif id == "forall:" or id == "forallp:" or id == "while:" or id == "forin:": yield LoopUnit(self, expr, self.level + 1) else: if not (False): raise AssertionError, \ "Cannot handle %r"%expr
def defprocedure_exprs_name(agent, exprs): if not (isList(exprs)): raise AssertionError if (len(exprs) != 1): raise AssertionError pfe = exprs[0] if pfe.functor != Symbol('defprocedure{}'): raise LocatedError(pfe, "must be {defprocedure ...}") return pfe[0].asValue()
def tenter(self, agent, tframe, zexpr): bindings = tframe.getBaseBindings() step_fn(agent, bindings, EXECUTING, zexpr) value = termEvalErr(agent, bindings, zexpr[1]) if not termMatch(agent, bindings, zexpr[0], value): step_fn(agent, bindings, FAILED, zexpr) s = bindings.constraintString(zexpr[0]) raise LocatedError( zexpr, "Cannot set term %s to the calculated value" % s) step_fn(agent, bindings, SUCCEEDED, zexpr) return tframe.tfcompleted(agent, SUCCESS)
def bind_result(self, agent, bindings, zexpr, result): if result is None: # None does not matching anything return False output_indices = self._output_indices has_rest_output = self._rest_mode in ("?", "-") l = len(output_indices) if l > 1 or has_rest_output: # Treat result as a tuple of values if not (operator.isSequenceType(result)): raise AssertionError, \ "Python function should return a sequence but returned %r instead"%(result,) minOut = len(self._output_range) actualOut = len(result) if has_rest_output: if actualOut < minOut: raise LocatedError(zexpr, "Expecting python function to return at least %s elements, not %r"%(minOut, result)) else: if actualOut != minOut: raise LocatedError(zexpr, "Expecting python function to return exactly %s elements, not %r"%(minOut, result)) for i in self._output_range: if not termMatch(agent, bindings, zexpr[output_indices[i]], result[i]): return False if has_rest_output: limit = len(zexpr) i = self._nrequired offset = i - len(self._output_indices) while i < limit: if termEvalP(agent, bindings, zexpr[i]): pass # No need to match the argument elif not termMatch(agent, bindings, zexpr[i], result[i-offset]): return False i = i+1 return True elif l == 1: # Exactly one output parameter # Treat result as a value return termMatch(agent, bindings, zexpr[output_indices[0]], result) elif result: # l == 0, no output parameters return True else: return False
def termMatchErr(agent, bindings, expr, value): if not bindings.bTermMatch(agent, expr, value): raise LocatedError(expr, "Term does not match the given value")
def termEvalErr(agent, bindings, expr): if bindings.bTermEvalP(agent, expr): return bindings.bTermEval(agent, expr) else: raise LocatedError(expr, "Term not evaluable")