Ejemplo n.º 1
0
def evalUpdate(graph, update):
    """

    http://www.w3.org/TR/sparql11-update/#updateLanguage

    'A request is a sequence of operations [...] Implementations MUST
    ensure that operations of a single request are executed in a
    fashion that guarantees the same effects as executing them in
    lexical order.

    Operations all result either in success or failure.

    If multiple operations are present in a single request, then a
    result of failure from any operation MUST abort the sequence of
    operations, causing the subsequent operations to be ignored.'

    This will return None on success and raise Exceptions on error

    """

    for u in update:

        ctx = QueryContext(graph)
        ctx.prologue=u.prologue

        try:
            if u.name == 'Load':
                evalLoad(ctx, u)
            elif u.name == 'Clear':
                evalClear(ctx, u)
            elif u.name == 'Drop':
                # rdflib does not record empty graphs, so clear == drop
                evalClear(ctx, u)
            elif u.name == 'Create':
                evalCreate(ctx, u)
            elif u.name == 'Add':
                evalAdd(ctx, u)
            elif u.name == 'Move':
                evalMove(ctx, u)
            elif u.name == 'Copy':
                evalCopy(ctx, u)
            elif u.name == 'InsertData':
                evalInsertData(ctx, u)
            elif u.name == 'DeleteData':
                evalDeleteData(ctx, u)
            elif u.name == 'DeleteWhere':
                evalDeleteWhere(ctx, u)
            elif u.name == 'Modify':
                evalModify(ctx, u)
            else:
                raise Exception('Unknown update operation: %s' % (u,))
        except:
            if not u.silent:
                raise
Ejemplo n.º 2
0
def _eval(e, ctx=None):
    if not ctx:
        ctx = QueryContext()
    try:
        r = e.eval(ctx)
        if isinstance(r, SPARQLError):
            print r
            return False
        return r
    except SPARQLError:
        return False
Ejemplo n.º 3
0
def test_comparisons_var():

    ctx = QueryContext()
    ctx[Variable('x')] = Literal(2)

    eq(bool(_eval(_translate((p.Expression.parseString('?x<3')[0])), ctx)), True)
    eq(bool(_eval(_translate((p.Expression.parseString('?x<3.0')[0])), ctx)), True)
    eq(bool(_eval(_translate((p.Expression.parseString('?x<3e0')[0])), ctx)), True)

    eq(bool(_eval(_translate((p.Expression.parseString('?x<2.1')[0])), ctx)), True)
    eq(bool(_eval(_translate((p.Expression.parseString('?x<21e-1')[0])), ctx)), True)

    eq(bool(_eval(_translate((p.Expression.parseString('?x=2.0')[0])), ctx)), True)
    eq(bool(_eval(_translate((p.Expression.parseString('?x=2e0')[0])), ctx)), True)

    eq(bool(_eval(_translate((p.Expression.parseString('?x="cake"')[0])), ctx)), False)

    ctx = QueryContext()
    ctx[Variable('x')] = Literal(4)

    eq(bool(_eval(_translate((p.Expression.parseString('?x<3')[0])), ctx)), False)
    eq(bool(_eval(_translate((p.Expression.parseString('?x<3.0')[0])), ctx)), False)
    eq(bool(_eval(_translate((p.Expression.parseString('?x<3e0')[0])), ctx)), False)
Ejemplo n.º 4
0
def test_arithmetic_var():
    ctx = QueryContext()
    ctx[Variable('x')] = Literal(2)

    eq(_eval(_translate((p.Expression.parseString('2+?x')[0])), ctx).value, 4)

    eq(_eval(_translate((p.Expression.parseString('?x+3')[0])), ctx).value, 5)
    eq(_eval(_translate((p.Expression.parseString('3-?x')[0])), ctx).value, 1)

    eq(_eval(_translate((p.Expression.parseString('?x*3')[0])), ctx).value, 6)
    eq(_eval(_translate((p.Expression.parseString('4/?x')[0])), ctx).value, 2)

    eq(_eval(_translate((p.Expression.parseString('?x+?x+?x')[0])), ctx).value, 6)
    eq(_eval(_translate((p.Expression.parseString('?x-?x+?x')[0])), ctx).value, 2)
    eq(_eval(_translate((p.Expression.parseString('(?x-?x)+?x')[0])), ctx).value, 2)
    eq(_eval(_translate((p.Expression.parseString('?x-(?x+?x)')[0])), ctx).value, -2)

    eq(_eval(_translate((p.Expression.parseString('?x*?x*?x')[0])), ctx).value, 8)
    eq(_eval(_translate((p.Expression.parseString('4/?x*?x')[0])), ctx).value, 4)
    eq(_eval(_translate((p.Expression.parseString('8/4*?x')[0])), ctx).value, 4)
    eq(_eval(_translate((p.Expression.parseString('8/(4*?x)')[0])), ctx).value, 1)
    eq(_eval(_translate((p.Expression.parseString('(?x/?x)*?x')[0])), ctx).value, 2)
    eq(_eval(_translate((p.Expression.parseString('4/(?x*?x)')[0])), ctx).value, 1)
Ejemplo n.º 5
0
def evalUpdate(graph, update, initBindings=None):
    """

    http://www.w3.org/TR/sparql11-update/#updateLanguage

    'A request is a sequence of operations [...] Implementations MUST
    ensure that operations of a single request are executed in a
    fashion that guarantees the same effects as executing them in
    lexical order.

    Operations all result either in success or failure.

    If multiple operations are present in a single request, then a
    result of failure from any operation MUST abort the sequence of
    operations, causing the subsequent operations to be ignored.'

    This will return None on success and raise Exceptions on error

    """

    for u in update:

        ctx = QueryContext(graph)
        ctx.prologue = u.prologue

        if initBindings:
            for k, v in initBindings.iteritems():
                if not isinstance(k, Variable):
                    k = Variable(k)
                ctx[k] = v
            ctx.push()  # nescessary?

        try:
            if u.name == 'Load':
                evalLoad(ctx, u)
            elif u.name == 'Clear':
                evalClear(ctx, u)
            elif u.name == 'Drop':
                # rdflib does not record empty graphs, so clear == drop
                evalClear(ctx, u)
            elif u.name == 'Create':
                evalCreate(ctx, u)
            elif u.name == 'Add':
                evalAdd(ctx, u)
            elif u.name == 'Move':
                evalMove(ctx, u)
            elif u.name == 'Copy':
                evalCopy(ctx, u)
            elif u.name == 'InsertData':
                evalInsertData(ctx, u)
            elif u.name == 'DeleteData':
                evalDeleteData(ctx, u)
            elif u.name == 'DeleteWhere':
                evalDeleteWhere(ctx, u)
            elif u.name == 'Modify':
                evalModify(ctx, u)
            else:
                raise Exception('Unknown update operation: %s' % (u, ))
        except:
            if not u.silent:
                raise
Ejemplo n.º 6
0
def evalQuery(graph, query, initBindings, base=None):
    ctx = QueryContext(graph)

    ctx.prologue = query.prologue

    if initBindings:
        for k, v in initBindings.iteritems():
            if not isinstance(k, Variable):
                k = Variable(k)
            ctx[k] = v
        ctx.push()  # nescessary?

    main = query.algebra

    # import pdb; pdb.set_trace()
    if main.datasetClause:
        if ctx.dataset is None:
            raise Exception("Non-conjunctive-graph doesn't know about " +
                            "graphs! Try a query without FROM (NAMED).")

        ctx = ctx.clone()  # or push/pop?

        firstDefault = False
        for d in main.datasetClause:
            if d.default:

                if firstDefault:
                    # replace current default graph
                    dg = ctx.dataset.get_context(BNode())
                    ctx.pushGraph(dg)
                    firstDefault = True

                ctx.load(d.default, default=True)

            elif d.named:
                g = d.named
                ctx.load(g, default=False)

    return evalPart(ctx, main)
Ejemplo n.º 7
0
def evalQuery(graph, query, initBindings, base=None):
    ctx = QueryContext(graph)

    ctx.prologue = query.prologue

    if initBindings:
        for k, v in initBindings.iteritems():
            if not isinstance(k, Variable):
                k = Variable(k)
            ctx[k] = v
        ctx.push()  # nescessary?

    main = query.algebra

    # import pdb; pdb.set_trace()
    if main.datasetClause:
        if ctx.dataset is None:
            raise Exception(
                "Non-conjunctive-graph doesn't know about " +
                "graphs! Try a query without FROM (NAMED).")

        ctx = ctx.clone()  # or push/pop?

        firstDefault = False
        for d in main.datasetClause:
            if d.default:

                if firstDefault:
                    # replace current default graph
                    dg = ctx.dataset.get_context(BNode())
                    ctx.pushGraph(dg)
                    firstDefault = True

                ctx.load(d.default, default=True)

            elif d.named:
                g = d.named
                ctx.load(g, default=False)

    return evalPart(ctx, main)