def restore(self):
        print "Restoring Profile."
        data = State.get_data(self.version, self.StateID)
        q = False
        for inputsize in data:
            try:
                algdata = inputsize["Algorithm"]
            except:

                inputsize = {
                    "ResourceVariables": self.variables,
                    "InputParameters": self.parameters,
                    "Algorithm": {}
                }
                State.checkpoint(self.version, self.StateID, data)
                break
            if algdata != None and algdata != []:

                if inputsize["InputParameters"] == self.parameters:
                    q = True
                    self.strategy.update_state(algdata)
                    #if profiling with the input size was finished go to the next one
                    if not self.strategy.stop_condition():
                        break
        if not q:
            return []
        return data
    def restore(self):
        #load state
        data = State.get_data(self.version, self.StateID)

        for inputsize in data:
            try:
                algdata = inputsize["Methodology"]
            except:
                inputsize = {
                    "Input": self.newinput,
                    "BenchmarkInput": self.benchmark_input,
                    "Methodology": {},
                }
                State.checkpoint(self.version, self.StateID, data)
                continue

            if not (algdata in [None, {}, []]):
                self.benchmark_input = inputsize["BenchmarkInput"]
                self.benchmark_solutions = None
                for sol in self.profiling_solutions:
                    if sol["Input"] == self.benchmark_input:
                        self.benchmark_solutions = sol["Configurations"]

                if not self.benchmark_solutions:
                    #the benchmark input from the save file doesn't have any solution explored
                    self.benchmark_input = self.profiling_solutions[-1][
                        "Input"]
                    self.benchmark_solutions = self.profiling_solutions[-1][
                        "Configurations"]

                self.strategy.update_state(algdata)
                #if profiling with the input size was finished go to the next one
                if not self.strategy.stop_condition():
                    break
        return data
    def save_state(self):
        print "Save state to file."
        data = State.get_data(self.version, self.StateID)
        algdata = self.strategy.get_state()
        info = {
            "Input": self.newinput,
            "BenchmarkInput": self.benchmark_input,
            "Methodology": algdata
        }
        q = False
        for inputsize in data:
            if inputsize["Input"] == self.newinput:
                inputsize.update(info)
                q = True
        if not q:
            data.append(info)

        State.checkpoint(self.version, self.StateID, data)
    def save_state(self):
        print "Save state to file."
        data = State.get_data(self.version, self.StateID)
        algdata = self.strategy.get_state()
        info = {
            "ResourceVariables": self.variables,
            "InputParameters": self.parameters,
            "Algorithm": algdata
        }

        q = False
        for inputsize in data:
            if inputsize["InputParameters"] == self.parameters:
                inputsize.update(info)
                q = True
        if not q:
            data.append(info)

        State.checkpoint(self.version, self.StateID, data)
    def model_version(v):
        version = ".".join(map(lambda s: str(s), v))
        #LOOK FOR PROFILE DATA
        profile_dir = os.path.abspath(
            os.path.join(os.path.dirname(__file__), "../profiles"))
        data_file = "%s/%s-%s.pf" % (profile_dir, Controller.application.Name,
                                     version)

        State.load(version, data_file)

        current_state = State.get_current_state(version)

        if current_state == 0:
            print "Profiling state."
            #profiling with static input phase
            #get the first small input size to profile with
            parameters = VariableMapper(
                Controller.application.getParameterVariableMap(
                )).lower_bound_values()
            #start profiler to create a model
            Profiler.StateID = current_state
            profiler = Profiler(Controller.application,
                                version,
                                parameters=parameters)
            try:
                profiler.run()
            except:
                traceback.print_exc()
                print 'Profiler interrupted. Exiting'
                return False, None
            State.change_state(version)
            current_state = State.get_current_state(version)
            State.checkpoint(version, current_state)

        #get the input size from the SLO for which to make prediction
        input_size = Controller.application.getExecutionParameters(
            Controller.slo.ExecutionArguments)
        modeller = None
        if current_state >= 1:
            print "Modelling state."
            #profiling variable input
            #modelling state - use function to predict
            Extrapolator.StateID = current_state
            variables, solutions_identified_in_profiling = Profiler(
                Controller.application, version).get_explored_solutions()

            print len(solutions_identified_in_profiling)

            modeller = Extrapolator(Controller.application, version, variables,
                                    solutions_identified_in_profiling,
                                    input_size)
            try:
                modeller.run()
            except:
                traceback.print_exc()
                print 'Modeller interrupted. Exiting'
                return False, None
            State.change_state(version)
            current_state = State.get_current_state(version)

        print "Current state", current_state
        State.save(version)
        #retrieve the model - tuple (function,constraints)
        model = modeller.get_model()
        return True, model