示例#1
0
def parsePredicate(predicateExpr):
    """
    _parsePredicate_

    Parse, convert predicate expression string into a
    predicate object tree
    """
    exprParser = ExpressionParser(predicateExpr)
    try:
        glyphs = exprParser.parse()
    except StandardError, ex:
        msg = "Error parsing predicate expression:\n"
        msg += str(predicateExpr)
        msg += "Error: %s" % str(ex)
        raise IMProvException(msg, ClassInstance = exprParser,
                              Expression = predicateExpr)
示例#2
0
    def loadFunction(self):
        """
        _loadFunction_

        Load the function matching the symbol, raising an
        exception if it is not found
        """
        self._FunctionRef = PredicateFunctions.getPredicateFunction(
            self.symbol
            )
        if self._FunctionRef == None:
            msg = "Unknown Function: %s\n" % self.symbol
            msg += "Unable to find a function named %s\n" % self.symbol
            raise IMProvException(msg, ClassInstance = self,
                                  MissingFunction = self.symbol)
        return
示例#3
0
    def __call__(self, node):
        """
        _operator()_

        Evaluate this expression on the node provided,
        if the operator is set, then evaluate the operator,
        else, evaluate the left hand side value itself
        """
        if self.operator != None:
            try:
                return self.operatorFunction(
                    self.leftHandSide(node),
                    self.rightHandSide(node))
            except StandardError, ex:
                msg = "Error evaluating operator %s:\n" % self.operator.symbol
                msg += str(ex)
                raise IMProvException(msg, ClassInstance = self)
示例#4
0
    def addNode(self, node):
        """
        _addNode_

        Add a child node to this node

        Args --

        - *node* : Instance of IMProvNode to be added
        as child
        """
        if not isinstance(node, IMProvNode):
            msg = "Value is not an IMProvNode instance"
            raise IMProvException(msg, ClassInstance=self, Value=node)

        self.children.append(node)
        self[node.name] = node
        return
示例#5
0
    def loadOperator(self):
        """
        _loadOperator_

        Get the matching operator implementation for this operator
        raise an error if no match
        
        """
        self._FunctionRef = PredicateOperators.getPredicateOperator(
            self.symbol
            )
        if self._FunctionRef == None:
            msg = "Unknown Operator: %s\n" % self.symbol
            msg += "Unable to find implementation for operator: "
            msg += "%s\n" % self.symbol
            raise IMProvException(msg, ClassInstance = self,
                                  MissingOperator = self.symbol)
        return
示例#6
0
    def __call__(self, node):
        """
        _operator()_

        Define operation of a FunctionTerm on a node,
        this looks the appropriate function from the
        Predicate Function Implementation table by matching the
        function name to the appropriate function, so that it can be evaluated
        on a node.
        """
        try:
            return self._FunctionRef(node, *self.args)
        except StandardError, ex:
            msg = "Error evaluating function: %s\n" % self.symbol
            msg += "With Arguments: %s\n" % ( self.args, ) 
            msg += str(ex)
            raise IMProvException(msg, ClassInstance = self,
                                  PredicateFunction = self.symbol)
示例#7
0
 def glyphType(self, glyph):
     """
     Evaluate the type of glyph and return a 
     type value for the glyph that matches the types
     stored in this object
     """
     if glyph == "[":
         return "OpenPredicate"
     if glyph == "]":
         return "ClosePredicate"
     if glyph == "(":
         return "OpenParen"
     if glyph == ")":
         return "CloseParen"
     if self._MatchOperator.match(glyph):
         return "Operator"
     if self._MatchValue.match(glyph):
         return "Value"
     if self._MatchFunc.match(glyph):
         return "Function"
     msg = "Badly formed token: %s" % glyph
     raise IMProvException(msg, ClassInstance = self,
                           Token = glyph) 
示例#8
0
class GlyphParser:
    """
    _GlyphParser_

    Event driven parser that stores an event for each glyph type

    """
    _MatchFunc = re.compile("^[a-zA-Z0-9_]+$")
    _MatchValue = re.compile("^\"[a-zA-Z0-9_]+\"$")
    _MatchOperator = re.compile("(^==$)|(^!=$)|(^>=$)|(^<=$)|(^\&\&$)|(^\|\|$)")
    
    def __init__(self):
        pass
    
        
    def glyphType(self, glyph):
        """
        Evaluate the type of glyph and return a 
        type value for the glyph that matches the types
        stored in this object
        """
        if glyph == "[":
            return "OpenPredicate"
        if glyph == "]":
            return "ClosePredicate"
        if glyph == "(":
            return "OpenParen"
        if glyph == ")":
            return "CloseParen"
        if self._MatchOperator.match(glyph):
            return "Operator"
        if self._MatchValue.match(glyph):
            return "Value"
        if self._MatchFunc.match(glyph):
            return "Function"
        msg = "Badly formed token: %s" % glyph
        raise IMProvException(msg, ClassInstance = self,
                              Token = glyph) 

    def __call__(self, glyphs):
        """
        _operator()_

        process a list of glyphs and convert them into
        and Expression tree
        
        """
        self.validateGlyphs(glyphs)
        newGlyphs = self.reduceFunctions(glyphs)
        newGlyphs = self.reduceValues(newGlyphs)
        newGlyphs = self.reduceOperators(newGlyphs)
        return self.createTree(newGlyphs)

    def validateGlyphs(self, glyphs):
        """
        _validateGlyphs_

        Basic validation of the predicate expressions

        """
        exprStr = ""
        parenStack = []
        for glyph in glyphs:
            exprStr += " %s " % glyph
            try:
                gType = self.glyphType(glyph)
            except IMProvException, ex:
                exprStr += "<<< Error"
                ex.addInfo(ErrorLocation = exprStr)
                raise ex
            if gType == "OpenParen":
                parenStack.append("(")
            if gType == "CloseParen":
                parenStack.pop()
        if parenStack != []:
            msg = "Mismatched Parentheses in Predicate expression:\n"
            msg += exprStr
            raise IMProvException(msg, ClassInstance = self,
                                  Expression = exprStr)
        return
示例#9
0
    predicate object tree
    """
    exprParser = ExpressionParser(predicateExpr)
    try:
        glyphs = exprParser.parse()
    except StandardError, ex:
        msg = "Error parsing predicate expression:\n"
        msg += str(predicateExpr)
        msg += "Error: %s" % str(ex)
        raise IMProvException(msg, ClassInstance = exprParser,
                              Expression = predicateExpr)
    
    glyphParser = GlyphParser()
    try:
        return glyphParser(exprParser.glyphs)
    except IMProvException, ex:
        ex.addInfo(Expression = predicateExpr)
        raise ex
    except StandardError, ex:
        msg = "Error parsing predicate expression:\n"
        msg += str(predicateExpr)
        msg += "Error: %s" % str(ex)
        raise IMProvException(msg, ClassInstance = glyphParser,
                              Expression = predicateExpr)