def _substring(agent, term, intention): needle = pyson_str(pyson.grounded(term.args[0], intention.scope)) haystack = pyson_str(pyson.grounded(term.args[1], intention.scope)) choicepoint = object() pos = haystack.find(needle) while pos != -1: intention.stack.append(choicepoint) if pyson.unify(term.args[2], pos, intention.scope, intention.stack): yield pyson.reroll(intention.scope, intention.stack, choicepoint) pos = haystack.find(needle, pos + 1)
def dump_variables(variables, scope): not_in_scope = [] for name, variable in sorted(variables.items()): if variable in scope: print("%s = %s" % (name, pyson_str(pyson.deref(variable, scope)))) else: not_in_scope.append("%s = %s" % (name, variable)) if not_in_scope: print("%d unbound: %s" % (len(not_in_scope), ", ".join(not_in_scope)))
def _csv(agent, term, intention, _files={}): if agent in _files: f = _files[agent] else: f = open("%s.csv" % agent.name, "w") _files[agent] = f memo = {} txt = ",".join(pyson_str(pyson.freeze(t, intention.scope, memo)) for t in term.args) f.write(txt) f.write("\n") f.flush() yield
def _print(agent, term, intention, _color_map={}, _current_color=[0]): if agent in _color_map: color = _color_map[agent] else: color = COLORS[_current_color[0]] _current_color[0] = (_current_color[0] + 1) % len(COLORS) _color_map[agent] = color memo = {} text = " ".join( pyson_str(pyson.freeze(t, intention.scope, memo)) for t in term.args) with colorama.colorama_text(): print(color[0], color[1], agent.name, colorama.Fore.RESET, colorama.Back.RESET, " ", text, sep="") yield