예제 #1
0
 def get_mdp_body( self, transitions, state_var, rand_var):
     c, eos, eq = self.get_common_vars()
     condition_writer = CodeWriter( self.config )
     i = 0
     for cdf, state, prob in transitions:
         cond = rand_var + " < " + str(cdf) 
         assign_state = state_var + eq + state + eos
         condition_writer.write_comment("Probability of transition: " + str(prob * 100) + "%", 0 )
         if i == 0: condition_writer.write_cond( 0, cond, assign_state )
         else: condition_writer.write_else_if( 0, cond, assign_state )
         i += 1
     return condition_writer.code
예제 #2
0
    def make_code(self):
        c, eos, eq = self.get_common_vars()
        writer = CodeWriter( c )
        # Write out the headers
        self.write_headers( writer )
        # Create the state variables
        state_var, prev_state_var, time_var = self.write_state_vars( writer )
        # Begin optional sections
        indent_level = self.create_optional_sections( writer )

        prev_var_decl = c["prev_var_type"] + prev_state_var + eq + state_var
        writer.write( prev_var_decl  + eos, indent_level)

        # Handle state action logic
        self.create_actions( writer, indent_level, state_var )

        # Create the transtion logic
        if self.is_hlsm():
            self.create_hlsm_transitions( writer, indent_level, state_var )
        elif self.is_mdp():
            self.create_mdp_transitions( writer, indent_level, state_var )


        # Write any extra functions at the end of the state loop
        if "run_at_end" in c:
            for l in c["run_at_end"]: writer.write( l, indent_level )

        # Set transition time
        transition_body = c["time_var"] + eq + c["time_function"] + c["end_var"]
        if on_trans in c: transition_body = [ transition_body, c[on_trans ] ] 
        writer.write_cond( indent_level, prev_state_var + "!=" + state_var, transition_body) 

        # End optional sections
        # End infinite loop 
        if inf_loop in c: writer.write( c["after_func"], indent_level - 1 )
        # End wrapper function
        if wrapper_function in c: writer.write( c["after_func"], 1 )

        self.create_function_stubs( writer )
        self.create_footer( writer )
        return writer.dump()