Esempio n. 1
0
    def _compile_additional_info(self, file):
        """
        Compiles the constraint code into a callable method.
        @param file: The output file.
        """
        
        # Attribute constraints code
        for v in self.node_iter():
            for attr in self.vs[v].attribute_names():
                if not self.vs[v][attr]:
                    # There is no action for this attribute
                    continue
                if Himesis.is_RAM_attribute(attr):
                    file.write('''
    def %s(self, attr_value, this):
%s

''' % (self.get_attr_constraint_name(v, attr), misc.indent_text(EpsilonParser().to_python(self.vs[v][attr]), 2)))
        
        # The constraint code
        code = 'return True'
        if Himesis.Constants.MT_CONSTRAINT in self.attributes() and self[Himesis.Constants.MT_CONSTRAINT]:
            code = EpsilonParser().to_python(self[Himesis.Constants.MT_CONSTRAINT])
        file.write('''
    def constraint(self, PreNode, graph):
        """
            Executable constraint code. 
            @param PreNode: Function taking an integer as parameter
                            and returns the node corresponding to that label.
        """
%s
''' %  misc.indent_text(code, 2))
Esempio n. 2
0
    def _compile_additional_info(self, file):
        """
        Compiles the execute and action codes into callable methods as well as the pointer to the pre-condition pattern. 
        @param file: The output file
        """
        if self.is_compiled:
            # self.pre is a Himesis graph
            file.write('''
        from %s import %s
        self.pre = %s()
    ''' % tuple([self.pre.__class__.__name__] * 3))
        else:
            # self.pre = "precondition name"
            file.write('''
        from %s import %s
        self.pre = %s()
    ''' % tuple([Himesis.standardize_name(self.pre)] * 3))
        
        # Attributes action code
        for v in self.node_iter():
            for attr in self.vs[v].attribute_names():
                if not self.vs[v][attr]:
                    # There is no action for this attribute
                    continue
                if Himesis.is_RAM_attribute(attr):
                    code = self.vs[v][attr]
                    if self.attribute_is_specified(code):
                        file.write('''
    def %s(self, attr_value, PreNode, graph):
%s

''' % (self.get_attr_action_name(v, attr), misc.indent_text(EpsilonParser().to_python(code), 2)))
        
        # The action code
        code = 'pass'
        if Himesis.Constants.MT_ACTION in self.attributes() and self[Himesis.Constants.MT_ACTION]:
            code = EpsilonParser().to_python(self[Himesis.Constants.MT_ACTION])
        file.write('''
    def action(self, PostNode, graph):
        """
            Executable constraint code. 
            @param PostNode: Function taking an integer as parameter
                             and returns the node corresponding to that label.
        """
%s
''' % misc.indent_text(code, 2))
        
        # The execute method
        self.set_execute_body()
        assert(len(self.execute_body) > 0, 'The execute method has no body')
        self.execute_parameters = ['packet', 'match'] 
        self.execute_doc = '''"""
    Transforms the current match of the packet according to the rule %s.
    Pivots are also assigned, if any.
    @param packet: The input packet.
    @param match: The match to rewrite.
"""
'''
        super(HimesisPostConditionPattern, self)._compile_additional_info(file)
Esempio n. 3
0
    def _compile_additional_info(self, file):
        s = ''
        if self.execute_body:
            s = '''
    def execute(self'''
            if self.execute_parameters:
                params = ', %s' * len(self.execute_parameters)
                s += params % tuple(self.execute_parameters)
            s += '''):
%s
    '''
            file.write(s % indent_text(self.execute_doc + self.execute_body, 2))