Ejemplo n.º 1
0
 def appendParameter(self,p):
     """
     Appends a parameter to the list of parameters of this module.
     """
     if Utilities.isFloat(p):
         self.params.append(float(p))
     else:
         self.params.append(p)
    def parseString(self, text, defines):
        """
        @param text: Text string that is transformed into condition.
        @type text: str

        @param defines: Global defines
        @type defines: dictionary
        """
        conditionType = ''
        conditionParamChar = None
        conditionOperatorString = None
        conditionValue = None

        #print("PARSING :'" + str(text) + "'")
        if text.strip() == "*":  # '*' Stands for always
            conditionType = '*'
            conditionValue = 1

        elif Utilities.isFloat(text):
            # If the text is a float, then this condition is a probability value [0,1]
            # Example production ---  A : 0.2 -> B
            conditionType = '#'   # '#' Stands for probability
            conditionValue = float(text)

        else:
            # Otherwise, this condition is a check on a parameter
            # Example production ---  A(x) : x > 1 -> B
            # The input text is in the form: 'x>1'
            conditionType = 'P'
            conditionOperatorString = ""
            for i in range(len(text)):
                c = text[i]
                if i == 0:
                    # The first character is the parameter to be checked
                    conditionParamChar = c
                elif (c not in ('=') and c not in ParametricProduction.ops):
                    # We finished the operator, we are building the value
                    if c.isdigit():
                        # We are checking against a constant
                        conditionValue = float(text[i:len(text)])
                    else:
                        # We are checking against a global defined parameter
                        #@note: THIS WILL WORK ONLY IF THE GLOBAL DEFINES ARE CREATED BEFORE THE PRODUCTIONS!
                        definedParam = text[i:len(text)]
                        if definedParam in defines:
                            self.conditionValue = defines[definedParam]
                    break
                else:
                    # We are building the operator
                    conditionOperatorString += c

        self.type = conditionType
        self.parameter = conditionParamChar
        self.operator = conditionOperatorString
        self.value = conditionValue