示例#1
0
文件: resume.py 项目: jbalint/spark
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)
示例#2
0
文件: resume.py 项目: jbalint/spark
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)
示例#3
0
文件: resume.py 项目: jbalint/spark
def resume_kb(agent, persistKbPath=None):
    if not _resumeState:
        return
    if persistKbPath is None:
        if not AGENT_KB_MAP.has_key(agent.name):
            raise Exception(
                "unable to locate a persisted knowledge base for agent '%s'" %
                agent.name)
        persistKbPath = AGENT_KB_MAP[agent.name]
    if not os.path.exists(persistKbPath):
        raise Exception(
            "persisted knowledge base [%s] missing for agent '%s'" %
            (persistKbPath, agent.name))

    console("(persist) Agent '%s' persisted KB: [%s]", agent.name,
            persistKbPath)

    if os.path.exists(persistKbPath + ERRORS_SUFFIX):
        kbErrFile = open(persistKbPath + ERRORS_SUFFIX, 'rb')
        persistedErrorText = kbErrFile.read()
        kbErrFile.close()
        console("\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n"+\
                "(persist) Agent '%s'\n"%agent.name+\
                "The agent reported the following errors when it saved its\n"+\
                "knowledge base:\n"+\
                "\n"+\
                "-----------------------------------------------------\n"+\
                persistedErrorText+\
                "\-----------------------------------------------------\n"+\
                "SPARK can continue loading, but some facts may be lost or corrupt.\n")
        if _isInteractive:
            #TODO: eliminate uses of raw_input
            option = raw_input(
                "Type 'q' to quit loading SPARK or <enter> to continue: ")
            if option.startswith('q'):
                sys.exit(-1)
            console("Continuing SPARK agent resume")

    from cStringIO import StringIO

    failedConcludes = []
    #bogusValues = []
    diffIo = StringIO()

    missingPredsList = []  #only used to make warning messages to user unique
    loadFactsFromFile(agent, persistKbPath, failedConcludes, diffIo,
                      missingPredsList)

    # Due to is_resume_block_concludes() we cannot just assert a new incarnation
    # incarnation = agent.factslist0(P_PersistenceIncarnation)[0][0]
    # agent.addfact(P_PersistenceIncarnation, (incarnation+1,))
    # Instead we rely on knowledge of exactly how it is implemented
    info = agent.getInfo(P_PersistenceIncarnation)
    incarnation = info.keys()[0]

    # Get the current incarnation directory for the agent before
    # bumping the incarnation number.
    incarnation_dir = get_persist_incarnation_dir(agent.name, incarnation)

    del info[incarnation]
    info[incarnation + 1] = [(incarnation + 1, )]

    # Load all the relevant SPU persistence files
    loadedFileObjectsFacts = agent.factslist0(P_LoadedFileObjects)

    from spark.internal.init import is_persist_state
    from spark.internal.persist import ieFact, writeSPUPersistIEFacts
    for lfofact in loadedFileObjectsFacts:  # for each LoadedFileObject fact
        # load in the appropriate SPU persist file
        spufilename = lfofact[0]
        spuindex = lfofact[1]
        name = 'kb2.%08d' % spuindex
        filename = os.path.join(incarnation_dir, name)
        facts = loadFactsFromFile(agent, filename, failedConcludes, diffIo,
                                  missingPredsList)
        # If also persisting, rewrite the file with new objectids
        if is_persist_state():
            iefacts = [ieFact(agent, fact.functor, fact) for fact in facts]
            writeSPUPersistIEFacts(agent, spufilename, spuindex, iefacts)

    diffVal = diffIo.getvalue()
    if diffVal and len(diffVal) > 0:
        console("\n-------------------------------------------------------------------\n"+\
                "(persist) Agent '%s'\n"%agent.name+\
                "The resumed state of the agent knowledge base will be modified.\n"+\
                "These modifications may represent changes in connections to \n"+\
                "external entities or other dynamic state that has changed. These\n"+\
                "changes are considered normal, though they may interfere with\n"+\
                "success of ongoing and future agent actions.\n"+\
                "\n"+\
                "The following is a summary of these modifications:\n"+\
                "\n"+\
                diffVal+"\n"+\
                "-------------------------------------------------------------------\n")

#     if bogusValues:
#         console_error("\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n"+\
#                       "(persist) Agent '%s'\n"%agent.name+\
#                       "The Agent Knowledge Base contained invalid values.\n"+\
#                       "SPARK can continue loading, but some values may be lost or corrupt.\n"+\
#                       "\n"+\
#                       "The following is a list of the corrupt data:\n")
#         for b in bogusValues:
#             #TODO: should append to a StringIO so that we don't get the ERROR: prefix repeatedly
#             console("%s\n", b)
#         if _isInteractive:
#             #TODO: can't use raw_input anymore
#             option = raw_input("Type 'q' to quit loading SPARK or <enter> to continue: ")
#             if option.startswith('q'):
#                 sys.exit(-1)
#             console("Continuing SPARK agent resume")

#print "RESUMED KB", persistKbPath
    return failedConcludes
示例#4
0
文件: resume.py 项目: jbalint/spark
def resume_kb(agent, persistKbPath=None):
    if not _resumeState:
        return
    if persistKbPath is None:
        if not AGENT_KB_MAP.has_key(agent.name):
            raise Exception("unable to locate a persisted knowledge base for agent '%s'"%agent.name)
        persistKbPath = AGENT_KB_MAP[agent.name]
    if not os.path.exists(persistKbPath):
        raise Exception("persisted knowledge base [%s] missing for agent '%s'"%(persistKbPath, agent.name))
     
    console("(persist) Agent '%s' persisted KB: [%s]", agent.name, persistKbPath)
 
    if os.path.exists(persistKbPath+ERRORS_SUFFIX):
        kbErrFile = open(persistKbPath+ERRORS_SUFFIX, 'rb')
        persistedErrorText = kbErrFile.read()
        kbErrFile.close()
        console("\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n"+\
                "(persist) Agent '%s'\n"%agent.name+\
                "The agent reported the following errors when it saved its\n"+\
                "knowledge base:\n"+\
                "\n"+\
                "-----------------------------------------------------\n"+\
                persistedErrorText+\
                "\-----------------------------------------------------\n"+\
                "SPARK can continue loading, but some facts may be lost or corrupt.\n")
        if _isInteractive:
            #TODO: eliminate uses of raw_input
            option = raw_input("Type 'q' to quit loading SPARK or <enter> to continue: ")
            if option.startswith('q'):
                sys.exit(-1)
            console("Continuing SPARK agent resume")
     
    from cStringIO import StringIO        
 
    failedConcludes = []
    #bogusValues = []
    diffIo = StringIO()

    missingPredsList = [] #only used to make warning messages to user unique
    loadFactsFromFile(agent, persistKbPath, failedConcludes, diffIo, missingPredsList)

    # Due to is_resume_block_concludes() we cannot just assert a new incarnation
    # incarnation = agent.factslist0(P_PersistenceIncarnation)[0][0]
    # agent.addfact(P_PersistenceIncarnation, (incarnation+1,))
    # Instead we rely on knowledge of exactly how it is implemented
    info = agent.getInfo(P_PersistenceIncarnation)
    incarnation = info.keys()[0]

    # Get the current incarnation directory for the agent before
    # bumping the incarnation number.
    incarnation_dir = get_persist_incarnation_dir(agent.name, incarnation)
                                                  
    del info[incarnation]
    info[incarnation+1] = [(incarnation+1,)]

    # Load all the relevant SPU persistence files
    loadedFileObjectsFacts = agent.factslist0(P_LoadedFileObjects)
    
    from spark.internal.init import is_persist_state
    from spark.internal.persist import ieFact, writeSPUPersistIEFacts
    for lfofact in loadedFileObjectsFacts: # for each LoadedFileObject fact
        # load in the appropriate SPU persist file
        spufilename = lfofact[0]
        spuindex = lfofact[1]
        name = 'kb2.%08d'%spuindex
        filename = os.path.join(incarnation_dir, name)
        facts = loadFactsFromFile(agent, filename, failedConcludes, diffIo, missingPredsList)
        # If also persisting, rewrite the file with new objectids
        if is_persist_state():
            iefacts = [ieFact(agent, fact.functor, fact) for fact in facts]
            writeSPUPersistIEFacts(agent, spufilename, spuindex, iefacts)

    
    diffVal = diffIo.getvalue()
    if diffVal and len(diffVal) > 0:
        console("\n-------------------------------------------------------------------\n"+\
                "(persist) Agent '%s'\n"%agent.name+\
                "The resumed state of the agent knowledge base will be modified.\n"+\
                "These modifications may represent changes in connections to \n"+\
                "external entities or other dynamic state that has changed. These\n"+\
                "changes are considered normal, though they may interfere with\n"+\
                "success of ongoing and future agent actions.\n"+\
                "\n"+\
                "The following is a summary of these modifications:\n"+\
                "\n"+\
                diffVal+"\n"+\
                "-------------------------------------------------------------------\n")
     
#     if bogusValues:
#         console_error("\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n"+\
#                       "(persist) Agent '%s'\n"%agent.name+\
#                       "The Agent Knowledge Base contained invalid values.\n"+\
#                       "SPARK can continue loading, but some values may be lost or corrupt.\n"+\
#                       "\n"+\
#                       "The following is a list of the corrupt data:\n")
#         for b in bogusValues:
#             #TODO: should append to a StringIO so that we don't get the ERROR: prefix repeatedly
#             console("%s\n", b)
#         if _isInteractive:
#             #TODO: can't use raw_input anymore
#             option = raw_input("Type 'q' to quit loading SPARK or <enter> to continue: ")
#             if option.startswith('q'):
#                 sys.exit(-1)
#             console("Continuing SPARK agent resume")
             
    #print "RESUMED KB", persistKbPath
    return failedConcludes