Esempio n. 1
0
def mapToOperator(expr, prolog, combinationArg=None, constraint=False):
    """
    Reduces certain expressions (operator expressions, function calls, terms,
    and combinator expressions) into strings of their Python equivalent
    """
    if prolog.DEBUG:
        print(('''mapToOperator:\n\texpr=%s,\n\ttype=%s,\n\tconstr=%s.\n''' %
               (expr, type(expr), constraint)))

    combinationInvokation = combinationArg and '(%s)' % combinationArg or ""

    if isinstance(expr, ListRedirect):
        expr = expr.reduce()

    if isinstance(expr, UnaryOperator):
        return UnaryOperatorMapping[type(expr)] % (mapToOperator(
            expr.argument, prolog, combinationArg, constraint=constraint))

    elif isinstance(expr, BinaryOperator):
        return BinaryOperatorMapping[type(expr)] % (
            mapToOperator(
                expr.left, prolog, combinationArg, constraint=constraint),
            mapToOperator(
                expr.right, prolog, combinationArg,
                constraint=constraint), combinationInvokation)

    elif isinstance(expr, (Variable, Unbound)):  # GJH the only use of Unbound
        if constraint:
            return """operators.EBV(rdflib.Variable("%s"))%s""" % (
                expr.n3(), combinationInvokation)
        else:
            return '"?%s"' % expr

    elif isinstance(expr, ParsedREGEXInvocation):
        return 'operators.regex(%s, %s%s)%s' % (
            mapToOperator(
                expr.arg1, prolog, combinationArg, constraint=constraint),
            mapToOperator(
                expr.arg2, prolog, combinationArg, constraint=constraint),
            expr.arg3 and ',"' + str(expr.arg3) + '"'
            or '', combinationInvokation)

    elif isinstance(expr, BuiltinFunctionCall):
        normBuiltInName = FUNCTION_NAMES[expr.name].lower()
        normBuiltInName = CAMEL_CASE_BUILTINS.get(
            normBuiltInName, 'operators.' + normBuiltInName)
        return "%s(%s)%s" % (normBuiltInName, ",".join([
            mapToOperator(i, prolog, combinationArg, constraint=constraint)
            for i in expr.arguments
        ]), combinationInvokation)

    elif isinstance(expr, ParsedDatatypedLiteral):
        lit = Literal(expr.value, datatype=convertTerm(expr.dataType, prolog))
        if constraint:
            return """operators.EBV(%r)%s""" % (lit, combinationInvokation)
        else:
            return repr(lit)

    elif isinstance(expr, (Literal, URIRef)):
        return repr(expr)

    elif isinstance(expr, QName):

        if expr[:2] == '_:':
            return repr(BNode(expr[2:]))
        else:
            return "'%s'" % convertTerm(expr, prolog)

    elif isinstance(expr, str):
        return "'%s'" % convertTerm(expr, prolog)

    elif isinstance(expr, ParsedAdditiveExpressionList):
        return 'Literal(%s)' % (operators.addOperator([
            mapToOperator(
                item, prolog, combinationArg='i', constraint=constraint)
            for item in expr
        ], combinationArg))

    elif isinstance(expr, FunctionCall):

        if isinstance(expr.name, QName):
            fUri = convertTerm(expr.name, prolog)

        if fUri in XSDToPython:
            return "operators.XSDCast(%s, '%s')%s" % (mapToOperator(
                expr.arguments[0],
                prolog,
                combinationArg='i',
                constraint=constraint), fUri, combinationInvokation)

        # @@FIXME The hook for extension functions goes here
        if fUri not in prolog.extensionFunctions:
            import warnings
            warnings.warn(
                "Use of unregistered extension function: %s" % (fUri),
                UserWarning, 1)
        else:
            raise NotImplemented(
                "Extension Mechanism hook not yet completely hooked up..")

    else:

        if isinstance(expr, ListRedirect):
            expr = expr.reduce()
            if expr.pyBooleanOperator:
                return expr.pyBooleanOperator.join([
                    mapToOperator(i, prolog, constraint=constraint)
                    for i in expr
                ])

        raise Exception("What do i do with %s (a %s)?" %
                        (expr, type(expr).__name__))
Esempio n. 2
0
def mapToOperator(expr, prolog, combinationArg=None, constraint=False):
    """
    Reduces certain expressions (operator expressions, function calls, terms,
    and combinator expressions) into strings of their Python equivalent
    """
    if prolog.DEBUG:
        print('''mapToOperator:\n\texpr=%s,\n\ttype=%s,\n\tconstr=%s.\n''' % (
            expr, type(expr), constraint))

    combinationInvokation = combinationArg and '(%s)' % combinationArg or ""

    if isinstance(expr, ListRedirect):
        expr = expr.reduce()

    if isinstance(expr, UnaryOperator):
        return UnaryOperatorMapping[type(expr)] % (
                mapToOperator(expr.argument, prolog, combinationArg,
                                constraint=constraint))

    elif isinstance(expr, BinaryOperator):
        return BinaryOperatorMapping[type(expr)] % (
                mapToOperator(expr.left, prolog, combinationArg,
                                constraint=constraint),
                mapToOperator(expr.right, prolog, combinationArg,
                                constraint=constraint),
                combinationInvokation
                )

    elif isinstance(expr, (Variable, Unbound)):  # GJH the only use of Unbound
        if constraint:
            return """operators.EBV(rdflib.Variable("%s"))%s""" % (
                            expr.n3(), combinationInvokation)
        else:
            return '"?%s"' % expr

    elif isinstance(expr, ParsedREGEXInvocation):
        return 'operators.regex(%s, %s%s)%s' % (
                mapToOperator(expr.arg1, prolog, combinationArg,
                                constraint=constraint),
                mapToOperator(expr.arg2, prolog, combinationArg,
                                constraint=constraint),
                expr.arg3 and ',"' + str(expr.arg3) + '"' or '',
                combinationInvokation
                )

    elif isinstance(expr, BuiltinFunctionCall):
        normBuiltInName = FUNCTION_NAMES[expr.name].lower()
        normBuiltInName = CAMEL_CASE_BUILTINS.get(
                normBuiltInName, 'operators.' + normBuiltInName)
        return "%s(%s)%s" % (
            normBuiltInName,
            ",".join([mapToOperator(i, prolog, combinationArg,
                                    constraint=constraint)
                         for i in expr.arguments]),
            combinationInvokation)

    elif isinstance(expr, ParsedDatatypedLiteral):
        lit = Literal(expr.value, datatype=convertTerm(expr.dataType, prolog))
        if constraint:
            return """operators.EBV(%r)%s""" % (lit, combinationInvokation)
        else:
            return repr(lit)

    elif isinstance(expr, (Literal, URIRef)):
        return repr(expr)

    elif isinstance(expr, QName):

        if expr[:2] == '_:':
            return repr(BNode(expr[2:]))
        else:
            return "'%s'" % convertTerm(expr, prolog)

    elif isinstance(expr, basestring):
        return "'%s'" % convertTerm(expr, prolog)

    elif isinstance(expr, ParsedAdditiveExpressionList):
        return 'Literal(%s)' % (
            operators.addOperator(
                [mapToOperator(item, prolog, combinationArg='i',
                                constraint=constraint)
                        for item in expr],
                combinationArg))

    elif isinstance(expr, FunctionCall):

        if isinstance(expr.name, QName):
            fUri = convertTerm(expr.name, prolog)

        if fUri in XSDToPython:
            return "operators.XSDCast(%s, '%s')%s" % (
             mapToOperator(expr.arguments[0], prolog, combinationArg='i',
                            constraint=constraint),
             fUri,
             combinationInvokation)

        # @@FIXME The hook for extension functions goes here
        if fUri not in prolog.extensionFunctions:
            import warnings
            warnings.warn(
                "Use of unregistered extension function: %s" % (fUri),
                UserWarning, 1)
        else:
            raise NotImplemented(
                "Extension Mechanism hook not yet completely hooked up..")

    else:

        if isinstance(expr, ListRedirect):
            expr = expr.reduce()
            if expr.pyBooleanOperator:
                return expr.pyBooleanOperator.join(
                       [mapToOperator(i, prolog,
                                      constraint=constraint)
                            for i in expr])

        raise Exception("What do i do with %s (a %s)?" % (
                    expr, type(expr).__name__))