def recordIdUsed(self, id, obj): """Called when object is used""" if not self._info.has_key(id): # make sure that we don't loop when getting state info self._info[id] = None # get exists and state info # this will call recordIdUsed for anything obj references cl_name = obj.__class__.__name__ if not (CONSTRUCTOR_NAME_CLASS.has_key(cl_name)): print "ERROR: No constructor has been installed for %s" % cl_name try: existIEArgs = [inverse_eval(x) for x in obj.constructor_args()] existIEArgs.insert(0, cl_name) self._existsIEFacts.append((id, List(existIEArgs))) except: errid = NEWPM.displayError() print print "ERROR finding constructor_args() for instance %d of %s" \ % (id, cl_name) print try: state_args = obj.state_args() if state_args: stateIEArgs = [inverse_eval(x) for x in state_args] self._stateIEFacts.append((id, List(stateIEArgs))) except: errid = NEWPM.displayError() print print "ERROR finding state_args() for instance %d of %s" \ % (id, cl_name) print self._info[id] = obj
def PRINT_TRACE(agent, bindings, event, expr, info=""): passes_filters = get_trace_filters().passes_filters(agent, bindings, event, expr, info) # check to see if we should do the effort of creating the debug string if not passes_filters and log_file is None: return try: varvals = variables(bindings, event, expr) except: errid = NEWPM.displayError() varvals = "ERROR PRINTING VARS" # kwc: i wrote the print to stdout and file this way so that there is a minimal # performance hit. the shorter way of writing this with just print statement # formatting was making the logging horribly slow. blevel = bindings_level(bindings) try: event_str = str(event) except: errid = NEWPM.displayError() event_str = "ERROR PRINTING EVENT" term_str = str(expr) vars_str = str(varvals) print_str = "*" * blevel + " " + event_str + " " + term_str + " BINDINGS: " + vars_str + info # trace filtering # - we filter stuff printed to stdout. # - everything gets logged to the logfile (no filtering) if passes_filters: print print_str if log_file is not None: log_file.write(print_str + "\n")
def PRINT_TRACE(agent, bindings, event, expr, info=""): passes_filters = get_trace_filters().passes_filters( agent, bindings, event, expr, info) #check to see if we should do the effort of creating the debug string if not passes_filters and log_file is None: return try: varvals = variables(bindings, event, expr) except: errid = NEWPM.displayError() varvals = "ERROR PRINTING VARS" # kwc: i wrote the print to stdout and file this way so that there is a minimal # performance hit. the shorter way of writing this with just print statement # formatting was making the logging horribly slow. blevel = bindings_level(bindings) try: event_str = str(event) except: errid = NEWPM.displayError() event_str = "ERROR PRINTING EVENT" term_str = str(expr) vars_str = str(varvals) print_str = "*" * blevel + ' ' + event_str + ' ' + term_str + ' BINDINGS: ' + vars_str + info # trace filtering # - we filter stuff printed to stdout. # - everything gets logged to the logfile (no filtering) if passes_filters: print print_str if log_file is not None: log_file.write(print_str + '\n')
def recordIdUsed(self, id, obj): """Called when object is used""" if not self._info.has_key(id): # make sure that we don't loop when getting state info self._info[id] = None # get exists and state info # this will call recordIdUsed for anything obj references cl_name = obj.__class__.__name__ if not (CONSTRUCTOR_NAME_CLASS.has_key(cl_name)): print "ERROR: No constructor has been installed for %s"%cl_name try: existIEArgs = [inverse_eval(x) for x in obj.constructor_args()] existIEArgs.insert(0, cl_name) self._existsIEFacts.append((id, List(existIEArgs))) except: errid = NEWPM.displayError() print print "ERROR finding constructor_args() for instance %d of %s" \ % (id, cl_name) print try: state_args = obj.state_args() if state_args: stateIEArgs = [inverse_eval(x) for x in state_args] self._stateIEFacts.append((id, List(stateIEArgs))) except: errid = NEWPM.displayError() print print "ERROR finding state_args() for instance %d of %s" \ % (id, cl_name) print self._info[id] = obj
def removeFile(filename): import os try: print "=== REMOVING FILE %s ==="%filename os.remove(filename) print "=== SUCCEEDED IN REMOVING FILE %s ==="%filename return True except: NEWPM.displayError() return False
def removeFile(filename): import os try: print "=== REMOVING FILE %s ===" % filename os.remove(filename) print "=== SUCCEEDED IN REMOVING FILE %s ===" % filename return True except: NEWPM.displayError() return False
def __XXXrepr__(self): try: if self._estack: return "<TF %s %s>" % (self._root_taskexpr, self._estack[-1]) elif self.completed(): return "<TF %s SUCCEEDED>" % (self._root_taskexpr,) else: return "<TF %s FAILED>" % (self._root_taskexpr,) except: NEWPM.displayError() return "!" + object.__repr__(self)
def return_solutions(self): answer = IclList(self._answerlist) debug("ExternalSolve returning solutions %r", answer) try: self._oaa.oaaReturnDelayedSolutions(self._id, answer) debug("ExternalSolve called oaaReturnDelayedSolutions, %s, %s",\ self._id, answer) except AnyException: print "Exception while returning solutions to oaa", self._id NEWPM.displayError() debug("ExternalSolve return_solutions finishing")
def __XXXrepr__(self): try: if self._estack: return "<TF %s %s>"%(self._root_taskexpr, \ self._estack[-1]) elif self.completed(): return "<TF %s SUCCEEDED>" % (self._root_taskexpr, ) else: return "<TF %s FAILED>" % (self._root_taskexpr, ) except: NEWPM.displayError() return "!" + object.__repr__(self)
def features(self, agent): try: sym = self.goalsym() if sym.modname is None: # DNM - Hack for @exteval return () facts = agent.factslist1(P_Features, sym) if facts: return facts[0][1] else: return () except: NEWPM.displayError() return ()
def process_result(self, agent, val): if not self._basicact._output_indices: # i.e., length == 0: # Note that a function returning None is treated as SUCCESS if val is False: result = MessageFailure(self, "%s implementing action returned False"%self._basicact.function_name()) elif isinstance(val, Failure): result = val else: result = SUCCESS elif self._basicact.bind_result(agent, self._bindings, self._zexpr, val): result = SUCCESS elif val == None: result = MessageFailure(self, "%s implementing action returned None" %self._basicact.function_name()) else: try: raise Exception("%s implementing action returned a value %r that does not match the output params %r"%(self._basicact.function_name(), val, self._basicact._output_indices)) except AnyException, e: errid = NEWPM.displayError() #@-timing# T_callfn.stop() #@-timing# T_processresult.start() # TODO: work out if this is the right way to handle this case new_err = CapturedError(self._zexpr, errid, "executing") self.process_exception(agent, new_err, errid) return
def load_module(agent, modname): chron = time.time() succeeds = 1 try: #PERSIST:INSTRUMENT report_instrumentation_point(agent, PRE_USER_MODULE_LOAD) mod = ensure_modpath_installed(Symbol(modname)) if not mod: print "=== COULD NOT INSTALL MODULE %s ==="%modname succeeds = 0 currMod = get_default_module() #check for equality here because we are reporting instrumentation points if mod is not currMod: set_default_module(mod) if not ensure_default_module_loaded(agent): print "=== COULD NOT LOAD MODULE %s ==="%modname succeeds = 0 #PERSIST:INSTRUMENT report_instrumentation_point(agent, POST_USER_MODULE_LOAD) except SPARKException: errid = NEWPM.displayError() chron = time.time() - chron print "Total Loading Time: %3.4f seconds."%chron #-*-*-*-*-*-* This is a hack to remove the logs from the console. A better solution is needed later. from spark.util.logger import get_sdl get_sdl().log("Default Module: %s", modname) return succeeds
def resume_sources(): """resume loads any non-agent related persistence data. it is a pre-requisite to loading the agent KBs""" if not _resumeState: return console("Resuming sources...") persist_root_dir = get_persist_root_dir() persist_src_dir = get_persist_src_dir() if not os.path.exists(persist_root_dir) or not os.path.exists(persist_src_dir): console("Could not find %s or %s" % (persist_root_dir, persist_src_dir)) return try: from spark.internal.parse.processing import SPU_RECORD SPU_RECORD.restore() _init_resume_data() # set_resuming(True) # set_source_locator(persist__must_find_file) # _load_persisted_sources() _load_default_module() # set_source_locator(default__must_find_file) # set_resuming(False) console("DONE resuming sources") except AnyException, e: console("ERROR resuming sources") errid = NEWPM.displayError() NEWPM.pm(errid)
def task_icl_mode(agent, task, mode): try: kind = task.kind_string() namesym = task.goalsym() bindings = task.getBindings() patexprs = task.getZexpr() if not (isinstance(kind, basestring)): raise AssertionError if not (isSymbol(namesym)): raise AssertionError except AnyException: errid = NEWPM.displayError() return makeIclStr("<cannot handle task %s>" % task) try: return do_task_icl(agent, bindings, patexprs, kind, namesym, mode) except AnyException: errid = NEWPM.displayError() return "<err: %s>" % task
def optionalGetVariableValueString(bindings, var): try: return value_str(bindings.getVariableValue(ABSTRACT, var)) except LowError: errid = NEWPM.displayError() print "VARIABLE %s WAS NOT BOUND CORRECTLY!!!" % var return "???"
def resume_sources(): """resume loads any non-agent related persistence data. it is a pre-requisite to loading the agent KBs""" if not _resumeState: return console("Resuming sources...") persist_root_dir = get_persist_root_dir() persist_src_dir = get_persist_src_dir() if not os.path.exists(persist_root_dir) or not os.path.exists( persist_src_dir): console("Could not find %s or %s" % (persist_root_dir, persist_src_dir)) return try: from spark.internal.parse.processing import SPU_RECORD SPU_RECORD.restore() _init_resume_data() # set_resuming(True) # set_source_locator(persist__must_find_file) # _load_persisted_sources() _load_default_module() # set_source_locator(default__must_find_file) # set_resuming(False) console("DONE resuming sources") except AnyException, e: console("ERROR resuming sources") errid = NEWPM.displayError() NEWPM.pm(errid)
def task_icl_mode(agent, task, mode): try: kind = task.kind_string() namesym = task.goalsym() bindings = task.getBindings() patexprs = task.getZexpr() if not (isinstance(kind, basestring)): raise AssertionError if not (isSymbol(namesym)): raise AssertionError except AnyException: errid = NEWPM.displayError() return makeIclStr("<cannot handle task %s>"%task) try: return do_task_icl(agent, bindings, patexprs, kind, namesym, mode) except AnyException: errid = NEWPM.displayError() return "<err: %s>"%task
def load_module(agent, modname): chron = time.time() succeeds = 1 try: # PERSIST:INSTRUMENT report_instrumentation_point(agent, PRE_USER_MODULE_LOAD) mod = ensure_modpath_installed(Symbol(modname)) if not mod: print "=== COULD NOT INSTALL MODULE %s ===" % modname succeeds = 0 currMod = get_default_module() # check for equality here because we are reporting instrumentation points if mod is not currMod: set_default_module(mod) if not ensure_default_module_loaded(agent): print "=== COULD NOT LOAD MODULE %s ===" % modname succeeds = 0 # PERSIST:INSTRUMENT report_instrumentation_point(agent, POST_USER_MODULE_LOAD) except SPARKException: errid = NEWPM.displayError() chron = time.time() - chron print "Total Loading Time: %3.4f seconds." % chron # -*-*-*-*-*-* This is a hack to remove the logs from the console. A better solution is needed later. from spark.util.logger import get_sdl get_sdl().log("Default Module: %s", modname) return succeeds
def task_pyMode(agent, task, mode): try: kind = task.kind_string() namesym = task.goalsym() bindings = task.getBindings() zexpr = task.getZexpr() patexprs = [zexpr[i] for i in range(len(zexpr))] if not (isinstance(kind, basestring)): raise AssertionError if not (isSymbol(namesym)): raise AssertionError except: NEWPM.displayError() return "<cannot handle task %s>" % task try: return do_task_py(agent, bindings, patexprs, kind, namesym, mode) except AnyException: NEWPM.displayError() return "<err: %s>" % task
def value_to_icl(self, value): from spark.io.oaa import InvalidTermError, value_to_icl try: return value_to_icl(value) except InvalidTermError: errid = NEWPM.displayError() result = "<NO ICL EQUIVALENT:%s>"%value_str(value) return value_to_icl(result)
def value_to_icl(self, value): from spark.io.oaa import InvalidTermError, value_to_icl try: return value_to_icl(value) except InvalidTermError: errid = NEWPM.displayError() result = "<NO ICL EQUIVALENT:%s>" % value_str(value) return value_to_icl(result)
def task_pyMode(agent, task, mode): try: kind = task.kind_string() namesym = task.goalsym() bindings = task.getBindings() zexpr = task.getZexpr() patexprs = [zexpr[i] for i in range(len(zexpr))] if not (isinstance(kind, basestring)): raise AssertionError if not (isSymbol(namesym)): raise AssertionError except: NEWPM.displayError() return "<cannot handle task %s>"%task try: return do_task_py(agent, bindings, patexprs, kind, namesym, mode) except AnyException: NEWPM.displayError() return "<err: %s>"%task
def writePredicates(agent, predicates, output, errOutput): """Write the current state of the given predicates to output. Report errors to errOutput.""" #Testing Script. Begin: # global chronometers #Testing Script. End. predimpls = [] for pred in predicates: impl = agent.getImp(pred) if impl is None: continue try: if isinstance(impl, PersistablePredImpInt) \ and impl.symbol not in SPU_LOAD_INFO_PREDICATES: predimpls.append(impl) except AnyException, f: NEWPM.displayError() errOutput.write('[%s.%s]: %s\n'%(pred.name, impl.__class__.__name__, str(f)))
def resume_conclude(self, agent, bindings, zexpr): if (len(zexpr) != 2): raise AssertionError num = termEvalErr(agent, bindings, zexpr[0]) args = termEvalErr(agent, bindings, zexpr[1]) try: _ID_SAVE.constructIdObject(agent, num, *args) except: errid = NEWPM.displayError() raise LowError("Error recreating object %s %s [%s]"%(num, value_str(args), errid))
def _dispatch(self, method, params): debug("incoming request method %r args %r", method, params) try: debug("Decoding parameters %r", params) decoded = decodeXMLValue(params) debug("Decoded parameters %r", decoded) (errnum, rid) = initiate_request(self.agent, S_XMLRPC, 0, method, decoded) except AnyException, e: errid = NEWPM.displayError() raise Fault(INTERNAL_ERROR, "Error [%s] %s"%(errid, e))
def writePredicates(agent, predicates, output, errOutput): """Write the current state of the given predicates to output. Report errors to errOutput.""" #Testing Script. Begin: # global chronometers #Testing Script. End. predimpls = [] for pred in predicates: impl = agent.getImp(pred) if impl is None: continue try: if isinstance(impl, PersistablePredImpInt) \ and impl.symbol not in SPU_LOAD_INFO_PREDICATES: predimpls.append(impl) except AnyException, f: NEWPM.displayError() errOutput.write('[%s.%s]: %s\n' % (pred.name, impl.__class__.__name__, str(f)))
def resume_conclude(self, agent, bindings, zexpr): if (len(zexpr) != 2): raise AssertionError num = termEvalErr(agent, bindings, zexpr[0]) args = termEvalErr(agent, bindings, zexpr[1]) try: _ID_SAVE.constructIdObject(agent, num, *args) except: errid = NEWPM.displayError() raise LowError("Error recreating object %s %s [%s]" % (num, value_str(args), errid))
def run(self, *args): try: #print " Starting fun" self._thread_result = self._basicact._function(*args) #print " Finished fun" except AnyException, e: #print " Exception in fun" errid = NEWPM.displayError() self._thread_exception = e self._errid = errid
def __init__(self, location, extra = (), fn = None): ConstructibleValue.__init__(self) self._location = location ensure_dir(os.path.abspath(location)) self.name = "ExecutionTracer" self._desc = "ExecutionTracer" if os.path.isabs(location): # if it is not an absolute path: self.abs_location = location else: self.abs_location = os.path.join(get_persist_root_dir(), location) ensure_dir(self.abs_location) try: if fn != None and os.path.exists(self.abs_location + "/" + fn): # Reopen the old file resuming = True self._filename = fn ffn = self.abs_location + "/" + fn else: #Creating a new file. resuming = False self._filename = "sparklog_%04d%02d%02dT%02d%02d%02d.xml"%time.localtime()[0:6] ffn = self.abs_location + "/" + self._filename except: ConstructibleValue.__init__(self) NEWPM.displayError() debug("Error constructing the log file name.") return try: self.descriptor = FileWrapper(ffn, "a") self.writeLog("<?xml version=\"1.0\" ?>\n") self.writeLog("<ExecutionTracer>\n") if not resuming: global machineName self.writeLog("\t<Initiator>TaskManager</Initiator>\n") self.writeLog("\t<TimeZone>%s</TimeZone>\n"%time.tzname[0]) self.writeLog("\t<MachineName>%s</MachineName>\n"%machineName) for x in extra: key, value = x self.writeLog("\t<%s>%s</%s>\n"%(key, value, key)) except: NEWPM.displayError() debug("Error initiating the log file.")
def loadFactsFromFile(agent, filename, failedConcludes, diffIo, missingPredsList): from spark.internal.parse.basicvalues import VALUE_CONSTRUCTOR, Structure, isStructure from spark.internal.parse.generic_tokenizer import FileSource #from spark.internal.parse.sparkl_parser import EOF_DELIMITER, SPARKLTokenizer, SPARKLParser, BogusValue from spark.internal.parse.sparkl_parser import parseSPARKL from spark.lang.builtin_eval import builtin_evaluate from spark.internal.repr.varbindings import valuesBZ from spark.pylang.implementation import PersistablePredImpInt from spark.internal.parse.usagefuns import termEvalErr print "RESUMING KB FROM", filename #parser = SPARKLParser(VALUE_CONSTRUCTOR, SPARKLTokenizer(FileSource(filename))) #parseVal = parser.terms_and_taggeds(True, EOF_DELIMITER, "end of input") f = open(filename, 'rb') string = f.read() f.close() parseVal = parseSPARKL(string, "File "+filename, VALUE_CONSTRUCTOR) facts = [] # keep track of all facts in file for val in parseVal: # if isinstance(val, BogusValue): # bogusValues.append(val) # continue if not (isStructure(val)): raise AssertionError functor = val.functor try: imp = agent.getImp(functor) except LowError, e: if functor not in missingPredsList: #TODO: make warning only once per prediate functor console_warning("Predicate %s is no longer part of the SPARK process models and facts for it will be purged", functor.name) missingPredsList.append(functor) continue if not (isinstance(imp, PersistablePredImpInt)): raise AssertionError #evaluate each of the args before putting into the zexpr try: fact = [builtin_evaluate(agent, arg) for arg in val] facts.append(Structure(functor, fact)) b, z = valuesBZ(fact) except: errid = NEWPM.displayError() console_error("(persist) unable to resume knowledge base fact \n\t%s\n", val) continue bindings_altzexpr = imp.resume_conclude(agent, b, z) if bindings_altzexpr is not None: (bindings, altzexpr) = bindings_altzexpr failedConcludes.append((imp, bindings, altzexpr,)) diffIo.write("-(%s %s)\n"%(val.functor.name, \ " ".join([persist_strrep(v) for v in val]))) diffIo.write("+(%s %s)\n"%(val.functor.name, \ " ".join([persist_strrep(termEvalErr(agent, bindings, z)) for z in altzexpr])))
def NO_TRACE(agent, bindings, event, expr, info=""): # even though we're not tracing, still have to log if log_file is None: return try: varvals = variables(bindings, event, expr) except: errid = NEWPM.displayError() varvals = "ERROR PRINTING VARS" blevel = bindings_level(bindings) event_str = str(event) term_str = str(expr) vars_str = str(varvals) log_file.write("*" * blevel + " " + event_str + " " + term_str + " BINDINGS: " + vars_str + info + "\n")
class BasicImpTFrame(TFrame): __slots__ = ( "_basicact", "_bindings", "_zexpr", ) def cvname(self): return self._basicact.cvname() def __init__(self, name, event, basicact, bindings, zexpr): TFrame.__init__(self, name, event) self._basicact = basicact self._bindings = bindings self._zexpr = zexpr def constructor_args(self): return TFrame.constructor_args(self) + [self._basicact, self._bindings, self._zexpr] constructor_mode = "VIIIV" def tfcont(self, agent): #@-timing# T_tfcont.start() try: #debug("Creating args for %r", self) #@-timing# T_genargs.start() args = self._basicact.generate_args(agent, self._bindings, self._zexpr) #@-timing# T_genargs.stop() #debug("Calling %r on %r", self._basicact._function, args) #@-timing# T_callfn.start() val = self._basicact._function(*args) #@-timing# T_callfn.stop() except LocatedError, e: errid = NEWPM.displayError() #@-timing# T_callfn.stop() #@-timing# T_processresult.start() self.process_exception(agent, e, errid) #@-timing# T_processresult.stop() #@-timing# T_tfcont.stop() return except AnyException, e: errid = NEWPM.displayError() #@-timing# T_callfn.stop() #@-timing# T_processresult.start() # TODO: work out if this is the right way to handle this case new_err = CapturedError(self._zexpr, errid, "executing") self.process_exception(agent, new_err, errid) #@-timing# T_processresult.stop() #@-timing# T_tfcont.stop() return
def NO_TRACE(agent, bindings, event, expr, info=""): #even though we're not tracing, still have to log if log_file is None: return try: varvals = variables(bindings, event, expr) except: errid = NEWPM.displayError() varvals = "ERROR PRINTING VARS" blevel = bindings_level(bindings) event_str = str(event) term_str = str(expr) vars_str = str(varvals) log_file.write("*" * blevel + ' ' + event_str + ' ' + term_str + ' BINDINGS: ' + vars_str + info + '\n')
def persist_point(agent, point): #print "persist_point()" try: #the instrumentation layer has already verified that is_persist_point() is #True, so we can just directly call persist_kb() from here if we're not #in a resuming state if not is_resuming() and _persistState: #print "persist_point()" persist_version() #print "persist_version() complete" persist_kb(agent) #print "persist_kb() complete" persist_default_module() #print "persist_point() complete" except: #we can't allow the persist operation to fubar the executor state, so we catch #and print here for now errid = NEWPM.displayError()
def doOAAEvent(self, goal, params, _answers): debug("doOAAEvent(%r, %r, %r)", goal, params, _answers) try: goalname = getFunctor(goal) logInfo("doOAAEvent[%s]" % goalname) if goalname == "ev_solved": print "skipping the ev_solved: %s" % goal return True ext = ExternalSolve(self._oaa, goal, params) debug("doOAAEvent calling oaadelaySolution") self._oaa.oaaDelaySolution(ext.get_id()) debug("doOAAEvent adding external %r", ext.get_id()) self._agent.add_external(ext) return True except AnyException: #print "Rejected OAA request" errid = NEWPM.displayError() return False
def tfcont(self, agent): """tfcont will call agent.setChanged if something changed""" debug("=> tfcont - TOS=%r", self._estack[-1]) if self._subgoal_event: # subgoal has not been processed debug("== tfcont - existing subgoal = %r", self._subgoal_event) failure = NoGoalResponseFailure(self, self._subgoal_event) return self.tfhandlecfin(agent, self._subgoal_event, failure) else: try: taskexpr = self._estack[-1] debug("=== .tresume %r", taskexpr) imp = taskImp(agent, self, taskexpr) return imp.tresume(agent, self, taskexpr) except AnyException, err: estack = self._estack[:] # a copy for when we debug errid = NEWPM.displayError() # we are trapping error so print it failure = ExceptionFailure(self, err, errid) return self.tfcompleted(agent, failure)
def doOAAEvent(self, goal, params, _answers): debug("doOAAEvent(%r, %r, %r)", goal, params, _answers) try: goalname = getFunctor(goal) logInfo("doOAAEvent[%s]"%goalname) if goalname == "ev_solved": print "skipping the ev_solved: %s"%goal return True ext = ExternalSolve(self._oaa, goal, params) debug("doOAAEvent calling oaadelaySolution") self._oaa.oaaDelaySolution(ext.get_id()) debug("doOAAEvent adding external %r", ext.get_id()) self._agent.add_external(ext) return True except AnyException: #print "Rejected OAA request" errid = NEWPM.displayError() return False
def tfcont(self, agent): """tfcont will call agent.setChanged if something changed""" debug("=> tfcont - TOS=%r", self._estack[-1]) if self._subgoal_event: # subgoal has not been processed debug("== tfcont - existing subgoal = %r", self._subgoal_event) failure = NoGoalResponseFailure(self, self._subgoal_event) return self.tfhandlecfin(agent, self._subgoal_event, failure) else: try: taskexpr = self._estack[-1] debug("=== .tresume %r", taskexpr) imp = taskImp(agent, self, taskexpr) return imp.tresume(agent, self, taskexpr) except AnyException, err: estack = self._estack[:] # a copy for when we debug errid = NEWPM.displayError( ) # we are trapping error so print it failure = ExceptionFailure(self, err, errid) return self.tfcompleted(agent, failure)
def tfcont(self, agent): #@-timing# T_tfcont.start() try: #debug("Creating args for %r", self) #@-timing# T_genargs.start() args = self._basicact.generate_args(agent, self._bindings, self._zexpr) #@-timing# T_genargs.stop() #debug("Calling %r on %r", self._basicact._function, args) #@-timing# T_callfn.start() val = self._basicact._function(*args) #@-timing# T_callfn.stop() except LocatedError, e: errid = NEWPM.displayError() #@-timing# T_callfn.stop() #@-timing# T_processresult.start() self.process_exception(agent, e, errid) #@-timing# T_processresult.stop() #@-timing# T_tfcont.stop() return
def initial_sdl(logParams={}): global sdlogger name = "SDL" if not logParams.has_key('sparkhome'): from spark.util.misc import getSparkHomeDirectory logParams['sparkhome'] = getSparkHomeDirectory() if not logParams.has_key('logdir'): # Create log directory if not present logParams['logdir'] = os.path.join(logParams['sparkhome'], "log") #keep logging.config happy since it expects string literals logParams['logdir'] = logParams['logdir'].replace("\\", "\\\\") # print "logdir=", logParams['logdir'] # print "sparkhome=", logParams['sparkhome'] from spark.internal.persist_aux import ensure_dir ensure_dir(logParams['logdir']) # try: import logging.config config_log_file = os.path.join(logParams['sparkhome'], "config", "logger.cfg") if os.path.isfile(config_log_file): logging.config.fileConfig(config_log_file, logParams) else: # Use default setting if it can not find the config file print "Could not find %s. Using default logger configuration..." % config_log_file logger = logging.getLogger(name) sparklog = os.path.join(logParams['logdir'], 'spark.log') #print "sparklog=", sparklog hdlr = logging.FileHandler(sparklog) formatter = logging.Formatter( '%(created)f [%(thread)d] %(levelname)s %(name)s - %(message)s' ) hdlr.setFormatter(formatter) logger.addHandler(hdlr) print "%s initiated." % name except AnyException, e: errid = NEWPM.displayError() print "Error initiating %s." % e
def oaaPSolve(agent, namestring, params, *args): ans = None #Testing Script. Begin: # global chronometers # chronometers["oaaPSolve"].start() #Testing Script. End. oaa = _oaa(agent) goal = construct_goal(agent, namestring, args) iclanswers = IclList() debug("calling oaaSolve on %s", goal) completed_without_exception = False functor = goal.functor #------- #print "Goal: %s, params: %s"%(goal, params) try: #debug("Calling oaaSolve(%s, %s, %s)", goal, value_to_icl(params), iclanswers) logInfo("oaasolve[%s]" % functor) result = oaa.oaaSolve(goal, value_to_icl(params), iclanswers) #print "RESULT is %s"%result #debug("Call to oaaSolve returned %s with answers %s", result, iclanswers) completed_without_exception = True finally: if not completed_without_exception: #record exception on its way up print "DEBUG: oaaSolve raised an exception" errid = NEWPM.displayError() if completed_without_exception: logInfo("oaasolve complete[%s]" % functor) if result: debug("oaaSolve returned success: %s", iclanswers) answers = deconstruct_answers(namestring, args, iclanswers) logInfo("oaasolve answers deconstructed[%s]" % functor) debug("deconstructed: %s", answers) if answers: ans = answers[0] else: debug("oaaSolve return failure: %s", iclanswers) raise OAAError("The call to oaaSolve [goal %s] failed and returned %s"\ %(functor, iclanswers)) #Testing Script. Begin: # chronometers["oaaPSolve"].stop() #Testing Script. End. return ans
def oaaPSolve(agent, namestring, params, *args): ans = None #Testing Script. Begin: # global chronometers # chronometers["oaaPSolve"].start() #Testing Script. End. oaa = _oaa(agent) goal = construct_goal(agent, namestring, args) iclanswers = IclList() debug("calling oaaSolve on %s", goal) completed_without_exception = False functor = goal.functor #------- #print "Goal: %s, params: %s"%(goal, params) try: #debug("Calling oaaSolve(%s, %s, %s)", goal, value_to_icl(params), iclanswers) logInfo("oaasolve[%s]"%functor) result = oaa.oaaSolve(goal, value_to_icl(params), iclanswers) #print "RESULT is %s"%result #debug("Call to oaaSolve returned %s with answers %s", result, iclanswers) completed_without_exception = True finally: if not completed_without_exception: #record exception on its way up print "DEBUG: oaaSolve raised an exception" errid = NEWPM.displayError() if completed_without_exception: logInfo("oaasolve complete[%s]"%functor) if result: debug("oaaSolve returned success: %s", iclanswers) answers = deconstruct_answers(namestring, args, iclanswers) logInfo("oaasolve answers deconstructed[%s]"%functor) debug("deconstructed: %s", answers) if answers: ans = answers[0] else: debug("oaaSolve return failure: %s", iclanswers) raise OAAError("The call to oaaSolve [goal %s] failed and returned %s"\ %(functor, iclanswers)) #Testing Script. Begin: # chronometers["oaaPSolve"].stop() #Testing Script. End. return ans
def initial_sdl(logParams={}): global sdlogger name = "SDL" if not logParams.has_key('sparkhome'): from spark.util.misc import getSparkHomeDirectory logParams['sparkhome'] = getSparkHomeDirectory() if not logParams.has_key('logdir'): # Create log directory if not present logParams['logdir'] = os.path.join(logParams['sparkhome'], "log") #keep logging.config happy since it expects string literals logParams['logdir'] = logParams['logdir'].replace("\\", "\\\\") # print "logdir=", logParams['logdir'] # print "sparkhome=", logParams['sparkhome'] from spark.internal.persist_aux import ensure_dir ensure_dir(logParams['logdir']) # try: import logging.config config_log_file = os.path.join(logParams['sparkhome'], "config", "logger.cfg") if os.path.isfile(config_log_file): logging.config.fileConfig(config_log_file, logParams) else: # Use default setting if it can not find the config file print "Could not find %s. Using default logger configuration..."%config_log_file logger = logging.getLogger(name) sparklog = os.path.join(logParams['logdir'], 'spark.log') #print "sparklog=", sparklog hdlr = logging.FileHandler(sparklog) formatter = logging.Formatter('%(created)f [%(thread)d] %(levelname)s %(name)s - %(message)s') hdlr.setFormatter(formatter) logger.addHandler(hdlr) print "%s initiated."%name except AnyException, e: errid = NEWPM.displayError() print "Error initiating %s."%e
def process_command(agent, next): """Process a command in the interpreter loop. Return True if interpreter should exit, False otherwise""" # NOTE: updates to the commands in this list need to be mirrored in print_help() try: # Things that can be done whether we are stepping or not if next.startswith("module "): # change module text = next[6:].strip() if len(text): load_module(agent, text) else: print 'Please enter in a module name, e.g. "module spark.lang.builtin"' elif next == "trace": print "Turning tracing on." enable_tracing() elif next == "notrace": print "Turning tracing off." disable_tracing() elif next == "persist": print "Persisting SPARK agent knowledge base" user_directed_persist(agent) elif next == "step" or next == "pause": print "Turning stepping on." enable_stepping() elif next == "nostep" or next == "resume": print "Turning stepping off." disable_stepping() elif next.startswith("monitor "): text = next[8:].strip() if len(text): add_trace_monitor(text) else: print 'Please type in a string to ignore during tracing, e.g. "ignore EXECUTING"' elif next.startswith("ignore "): text = next[7:].strip() if len(text): add_trace_ignore(text) else: print 'Please type in a string to monitor for during tracing, e.g. "monitor FAILED"' elif next == "exit": print "exiting SPARK interpreter" return True elif next == "help": print_help() elif next == "clear all": # essentially undoes everything from this session print "removing all new facts and intentions..." clear_all(agent) print "session refreshed" elif next == "clear filters": clear_filters() print "step/trace filters cleared" elif next.startswith("get "): if next == "get intentions": # just prints intention ID #s # XXX: get rid of java calls (kwc) print "Root Objectives:" print "--------------------" intentions = agent.get_intentions() for intent in intentions: print " ", intent try: from com.sri.ai.spark.model.task import IntentionStructure from com.sri.ai.spark.model.util import TextModel print "" print "Task structure:" print "--------------------" structure = IntentionStructure(agent, agent._intention_structure) print TextModel.getIntentionStructureModel(structure) except ImportError: pass # XXX:TODO:there is an excessive number of e_d_m_l calls here. # talk to dm to see if this is necessary elif next == "get predicates": ensure_default_module_loaded(agent) names = get_local_predicates(agent) _print_sym_list(agent, "displaying local predicates", names) elif next == "get functions": ensure_default_module_loaded(agent) names = get_local_functions(agent) _print_sym_list(agent, "displaying local functions", names) elif next == "get tasks": ensure_default_module_loaded(agent) names = get_local_tasks(agent) _print_sym_list(agent, "displaying actions", names) elif next == "get all predicates": ensure_default_module_loaded(agent) names = get_all_predicates(agent) _print_sym_list(agent, "displaying all predicates", names) elif next == "get all functions": ensure_default_module_loaded(agent) names = get_all_functions(agent) _print_sym_list(agent, "displaying all functions", names) elif next == "get all tasks": ensure_default_module_loaded(agent) names = get_all_tasks(agent) _print_sym_list(agent, "displaying actions", names) else: print "Invalid command: don't know how to get '%s'" % next[4:] elif next.startswith("debugon "): if next == "debugon oaa": M.spark__io__oaa.debug.on() elif next == "debugon pubsub": M.iris__events.debug.on() elif next == "debugon tir": M.task_manager__tir.debug.on() else: print "Invalid command: don't know how to debugon '%s'" % next[8:] elif next.startswith("debugoff "): if next == "debugoff oaa": M.spark__io__oaa.debug.off() elif next == "debugoff pubsub": M.iris__events.debug.off() elif next == "debugoff tir": M.task_manager__tir.debug.off() else: print "Invalid command: don't know how to debugoff '%s'" % next[9:] elif next.startswith("debug"): debug_arg = next[5:].strip() id_num = None if debug_arg != "": try: id_num = string.atoi(debug_arg) except AnyException: errid = NEWPM.displayError() NEWPM.pm(id_num) elif next == "python": runPyConsole() elif next == "" and get_step_controller().step(): pass elif next == "": print "" # ignoring blank input line" # Remaining commands require the kb lock (don't block!) else: # We have the KB lock if next.startswith("test "): agent.test(next[4:], get_modname()) elif next.startswith("eval ") or next.startswith("evaluate "): term = next[5:] if next.startswith("evaluate "): term = term[4:] result = agent.eval(term, get_modname()) print "->", value_str(result) elif next.startswith("run "): term = next[4:] print "synchronously running command" ext = agent.run(term, get_modname()) ext.thread_event.wait() if ext.result is SUCCESS: print "SUCCEEDED" else: print "FAILED", ext.result elif next.startswith("addfact "): print "adding fact to KB" agent.run("[conclude: " + next[7:] + "]", get_modname()) elif next.startswith("removefact "): print "adding fact to KB" agent.run("[retract: " + next[10:] + "]", get_modname()) elif next.startswith("unload "): # drop module modname = next[6:].strip() agent.unload_modpath(Symbol(modname)) elif next.startswith("["): print "running command" agent.run(next, get_modname()) else: print "ignoring unrecognized request:", next except AnyException: errid = NEWPM.displayError() return False
def loadFactsFromFile(agent, filename, failedConcludes, diffIo, missingPredsList): from spark.internal.parse.basicvalues import VALUE_CONSTRUCTOR, Structure, isStructure from spark.internal.parse.generic_tokenizer import FileSource #from spark.internal.parse.sparkl_parser import EOF_DELIMITER, SPARKLTokenizer, SPARKLParser, BogusValue from spark.internal.parse.sparkl_parser import parseSPARKL from spark.lang.builtin_eval import builtin_evaluate from spark.internal.repr.varbindings import valuesBZ from spark.pylang.implementation import PersistablePredImpInt from spark.internal.parse.usagefuns import termEvalErr print "RESUMING KB FROM", filename #parser = SPARKLParser(VALUE_CONSTRUCTOR, SPARKLTokenizer(FileSource(filename))) #parseVal = parser.terms_and_taggeds(True, EOF_DELIMITER, "end of input") f = open(filename, 'rb') string = f.read() f.close() parseVal = parseSPARKL(string, "File " + filename, VALUE_CONSTRUCTOR) facts = [] # keep track of all facts in file for val in parseVal: # if isinstance(val, BogusValue): # bogusValues.append(val) # continue if not (isStructure(val)): raise AssertionError functor = val.functor try: imp = agent.getImp(functor) except LowError, e: if functor not in missingPredsList: #TODO: make warning only once per prediate functor console_warning( "Predicate %s is no longer part of the SPARK process models and facts for it will be purged", functor.name) missingPredsList.append(functor) continue if not (isinstance(imp, PersistablePredImpInt)): raise AssertionError #evaluate each of the args before putting into the zexpr try: fact = [builtin_evaluate(agent, arg) for arg in val] facts.append(Structure(functor, fact)) b, z = valuesBZ(fact) except: errid = NEWPM.displayError() console_error( "(persist) unable to resume knowledge base fact \n\t%s\n", val) continue bindings_altzexpr = imp.resume_conclude(agent, b, z) if bindings_altzexpr is not None: (bindings, altzexpr) = bindings_altzexpr failedConcludes.append(( imp, bindings, altzexpr, )) diffIo.write("-(%s %s)\n"%(val.functor.name, \ " ".join([persist_strrep(v) for v in val]))) diffIo.write("+(%s %s)\n"%(val.functor.name, \ " ".join([persist_strrep(termEvalErr(agent, bindings, z)) for z in altzexpr])))
def process_command(agent, next): """Process a command in the interpreter loop. Return True if interpreter should exit, False otherwise""" #NOTE: updates to the commands in this list need to be mirrored in print_help() try: # Things that can be done whether we are stepping or not if next.startswith("module "): # change module text = next[6:].strip() if (len(text)): load_module(agent, text) else: print 'Please enter in a module name, e.g. "module spark.lang.builtin"' elif next == "trace": print "Turning tracing on." enable_tracing() elif next == "notrace": print "Turning tracing off." disable_tracing() elif next == "persist": print "Persisting SPARK agent knowledge base" user_directed_persist(agent) elif next == "step" or next == "pause": print "Turning stepping on." enable_stepping() elif next == "nostep" or next == "resume": print "Turning stepping off." disable_stepping() elif next.startswith("monitor "): text = next[8:].strip() if (len(text)): add_trace_monitor(text) else: print 'Please type in a string to ignore during tracing, e.g. "ignore EXECUTING"' elif next.startswith("ignore "): text = next[7:].strip() if (len(text)): add_trace_ignore(text) else: print 'Please type in a string to monitor for during tracing, e.g. "monitor FAILED"' elif next == "exit": print "exiting SPARK interpreter" return True elif next == "help": print_help() elif next == "clear all": # essentially undoes everything from this session print "removing all new facts and intentions..." clear_all(agent) print "session refreshed" elif next == "clear filters": clear_filters() print "step/trace filters cleared" elif next.startswith("get "): if next == "get intentions": # just prints intention ID #s #XXX: get rid of java calls (kwc) print "Root Objectives:" print "--------------------" intentions = agent.get_intentions() for intent in intentions: print " ", intent try: from com.sri.ai.spark.model.task import IntentionStructure from com.sri.ai.spark.model.util import TextModel print "" print "Task structure:" print "--------------------" structure = IntentionStructure(agent, agent._intention_structure) print TextModel.getIntentionStructureModel(structure) except ImportError: pass #XXX:TODO:there is an excessive number of e_d_m_l calls here. #talk to dm to see if this is necessary elif next == "get predicates": ensure_default_module_loaded(agent) names = get_local_predicates(agent) _print_sym_list(agent, "displaying local predicates", names) elif next == "get functions": ensure_default_module_loaded(agent) names = get_local_functions(agent) _print_sym_list(agent, "displaying local functions", names) elif next == "get tasks": ensure_default_module_loaded(agent) names = get_local_tasks(agent) _print_sym_list(agent, "displaying actions", names) elif next == "get all predicates": ensure_default_module_loaded(agent) names = get_all_predicates(agent) _print_sym_list(agent, "displaying all predicates", names) elif next == "get all functions": ensure_default_module_loaded(agent) names = get_all_functions(agent) _print_sym_list(agent, "displaying all functions", names) elif next == "get all tasks": ensure_default_module_loaded(agent) names = get_all_tasks(agent) _print_sym_list(agent, "displaying actions", names) else: print "Invalid command: don't know how to get '%s'" % next[4:] elif next.startswith("debugon "): if next == "debugon oaa": M.spark__io__oaa.debug.on() elif next == "debugon pubsub": M.iris__events.debug.on() elif next == "debugon tir": M.task_manager__tir.debug.on() else: print "Invalid command: don't know how to debugon '%s'" % next[ 8:] elif next.startswith("debugoff "): if next == "debugoff oaa": M.spark__io__oaa.debug.off() elif next == "debugoff pubsub": M.iris__events.debug.off() elif next == "debugoff tir": M.task_manager__tir.debug.off() else: print "Invalid command: don't know how to debugoff '%s'" % next[ 9:] elif next.startswith("debug"): debug_arg = next[5:].strip() id_num = None if debug_arg != "": try: id_num = string.atoi(debug_arg) except AnyException: errid = NEWPM.displayError() NEWPM.pm(id_num) elif next == "python": runPyConsole() elif next == "" and get_step_controller().step(): pass elif next == "": print "" #ignoring blank input line" # Remaining commands require the kb lock (don't block!) else: # We have the KB lock if next.startswith("test "): agent.test(next[4:], get_modname()) elif next.startswith("eval ") or next.startswith("evaluate "): term = next[5:] if next.startswith("evaluate "): term = term[4:] result = agent.eval(term, get_modname()) print "->", value_str(result) elif next.startswith("run "): term = next[4:] print "synchronously running command" ext = agent.run(term, get_modname()) ext.thread_event.wait() if ext.result is SUCCESS: print "SUCCEEDED" else: print "FAILED", ext.result elif next.startswith("addfact "): print "adding fact to KB" agent.run("[conclude: " + next[7:] + "]", get_modname()) elif next.startswith("removefact "): print "adding fact to KB" agent.run("[retract: " + next[10:] + "]", get_modname()) elif next.startswith("unload "): # drop module modname = next[6:].strip() agent.unload_modpath(Symbol(modname)) elif next.startswith("["): print "running command" agent.run(next, get_modname()) else: print "ignoring unrecognized request:", next except AnyException: errid = NEWPM.displayError() return False
# - no errors thus far if opts.getboolean(SPARK, EXECUTE_MAIN) and not exitcode: err = execute_spark_main(runAgent, moduleArgs) if err is not None: raise err #pass to outside handler #if not (opts.get(SPARK, 'loadOnly') or exitcode): if not exitcode: interpreter_loop(opts.get(SPARK, SCRIPT_FILENAME) or None, opts.getboolean(SPARK, SPARK_CLI), opts.get(SPARK, LOG_FILENAME) or None) elif init_result == None or init_result == 0: exitcode = EC_LOAD_ERROR except AnyException, e: errid = NEWPM.displayError() import traceback traceback.print_exc() if opts.getboolean(SPARK, DEBUGGER_MODE): import pdb pdb.post_mortem(sys.exc_info()[2]) #NEWPM.pm(errid) return e #pass up to Java # Workaround for bugs in OAA 2.3.2 mod = sys.modules.get("spark.io.oaa") if mod: # disconnect/stop OAA if it has been loaded mod.oaaStop(runAgent) # Try to kill off rogue OAA 2.3.2 thread try:
if len(argv) > 1: if argv[1] == "-test": new_agent() modules = get_files() generate_indexes(modules) doc_module(default_module) generate_all_symbols_indexes(modules) return new_agent() modules = get_files() print "====================", modules generate_indexes(modules) print "====================" for mod in modules: print "====================" doc_module(mod) #generate all symbols must run last generate_all_symbols_indexes(modules) if __name__ == "__main__": try: print "main(%r)" % sys.argv main(sys.argv) except: errid = NEWPM.displayError() sys.exit()