Save/Load Environmnet State

2010-04-12
2013-04-25
  • Vadim Gelman
    Vadim Gelman
    2010-04-12

    Is it possible to preserve Environment state for latter reuse?

    Environment.Save(), Environment.SaveFacts(), Environment.Load().LoadFacts()

    Attempting to do the above got me an Environment with reactivated Agenda, what I really want is the Environment state as it was before it was saved.

     
  • Vadim Gelman
    Vadim Gelman
    2010-04-12

    Code Example:

    def getEngine(sessionId):
        return ClipsRulesEngine(sessionId)
    def saveEngine(engine):
        temp_dir = '/tmp' if isLinux() else 'c:/temp'
        file_name = '%s/rules.bin' % temp_dir
        engine.engine.Save(file_name)
        f = open(file_name)
        rules_state = f.read()
        f.close()
        
        file_name = '%s/facts.bin' % temp_dir
        engine.engine.SaveFacts(file_name)
        f = open(file_name)
        facts_state = f.read()
        f.close()
        engine_state = "%s__rules_facts_sep__%s" % (rules_state, facts_state)
        return engine_state
    def loadEngine(engine_state):
        engine = clips.Environment()
        rules_state = engine_state.split("__rules_facts_sep__")[0]
        facts_state = engine_state.split("__rules_facts_sep__")[1]
        temp_dir = '/tmp' if isLinux() else 'c:/temp'
        file_name = '%s/rules.bin' % temp_dir
        f = open(file_name, 'w')
        f.write(rules_state)
        f.close()
        engine.Load(file_name)
        file_name = '%s/facts.bin' % temp_dir
        f = open(file_name, 'w')
        f.write(facts_state)
        f.close()
        engine.LoadFacts(file_name)
        return engine
    
     
  • Vadim Gelman
    Vadim Gelman
    2010-04-12

    Removing Activations seems to do the trick. Still wondering if somebody had the same requirement and if there is an easy way to achive the same.

    def loadEngine(engine_state):
        engine = clips.Environment()
        engine.Reset()
        rules_state = engine_state.split("__rules_facts_sep__")[0]
        facts_state = engine_state.split("__rules_facts_sep__")[1]
        temp_dir = '/tmp' if isLinux() else 'c:/temp'
        file_name = '%s/rules.bin' % temp_dir
        f = open(file_name, 'w')
        f.write(rules_state)
        f.close()
        engine.Load(file_name)
        file_name = '%s/facts.bin' % temp_dir
        f = open(file_name, 'w')
        f.write(facts_state)
        f.close()
        engine.LoadFacts(file_name)
        initialActivation = engine.InitialActivation()
        _resetActivation(initialActivation)
        if initialActivation: initialActivation.Remove()
        engine.Run()
        
        return engine
    def _resetActivation(activation):
        if activation:
            nextActivation = activation.Next()
            if nextActivation:
                _resetActivation(nextActivation)
            else:
                activation.Remove()
    
     
  • Hi, sorry for my late answer…

    Unfortunately, since the act of saving and retrieving the files is performed directly by the CLIPS library, I'm not aware of a way to save/retrieve the actual state of the CLIPS "machine" as it is in a particular moment - what you want to save is what other environments call "the working memory". CLIPS, for now, only offers to save the environment constructs, and not the machine state. I'm afraid that other tricks might be "unstable" and break when used in particular contexts.

    F.