Search in sources :

Example 1 with LiteralExpr

use of org.sirix.service.xml.xpath.expr.LiteralExpr in project sirix by sirixdb.

the class PipelineBuilder method addPredicate.

/**
 * Adds a predicate to the pipeline.
 *
 * @param pRtx transaction to operate with
 */
public void addPredicate(final XdmNodeReadTrx pRtx) {
    assert getPipeStack().size() >= 2;
    final Axis predicate = getPipeStack().pop().getExpr();
    if (predicate instanceof LiteralExpr) {
        predicate.hasNext();
        // if is numeric literal -> abbrev for position()
        final int type = pRtx.getTypeKey();
        if (type == pRtx.keyForName("xs:integer") || type == pRtx.keyForName("xs:double") || type == pRtx.keyForName("xs:float") || type == pRtx.keyForName("xs:decimal")) {
            throw new IllegalStateException("function fn:position() is not implemented yet.");
        // getExpression().add(
        // new PosFilter(transaction, (int)
        // Double.parseDouble(transaction
        // .getValue())));
        // return; // TODO: YES! it is dirty!
        // AtomicValue pos =
        // new AtomicValue(mTransaction.getItem().getRawValue(),
        // mTransaction
        // .keyForName("xs:integer"));
        // long position = mTransaction.getItemList().addItem(pos);
        // mPredicate.reset(mTransaction.getItem().getKey());
        // IAxis function =
        // new FNPosition(mTransaction, new ArrayList<IAxis>(),
        // FuncDef.POS.getMin(), FuncDef.POS
        // .getMax(),
        // mTransaction.keyForName(FuncDef.POS.getReturnType()));
        // IAxis expectedPos = new LiteralExpr(mTransaction, position);
        // 
        // mPredicate = new ValueComp(mTransaction, function,
        // expectedPos, CompKind.EQ);
        }
    }
    getExpression().add(new PredicateFilterAxis(pRtx, predicate));
}
Also used : LiteralExpr(org.sirix.service.xml.xpath.expr.LiteralExpr) ModOpAxis(org.sirix.service.xml.xpath.operators.ModOpAxis) RangeAxis(org.sirix.service.xml.xpath.expr.RangeAxis) AddOpAxis(org.sirix.service.xml.xpath.operators.AddOpAxis) ExceptAxis(org.sirix.service.xml.xpath.expr.ExceptAxis) Axis(org.sirix.api.Axis) MulOpAxis(org.sirix.service.xml.xpath.operators.MulOpAxis) UnionAxis(org.sirix.service.xml.xpath.expr.UnionAxis) IfAxis(org.sirix.service.xml.xpath.expr.IfAxis) IntersectAxis(org.sirix.service.xml.xpath.expr.IntersectAxis) IDivOpAxis(org.sirix.service.xml.xpath.operators.IDivOpAxis) DupFilterAxis(org.sirix.service.xml.xpath.filter.DupFilterAxis) PredicateFilterAxis(org.sirix.axis.filter.PredicateFilterAxis) VariableAxis(org.sirix.service.xml.xpath.expr.VariableAxis) ForAxis(org.sirix.axis.ForAxis) SequenceAxis(org.sirix.service.xml.xpath.expr.SequenceAxis) FilterAxis(org.sirix.axis.filter.FilterAxis) SubOpAxis(org.sirix.service.xml.xpath.operators.SubOpAxis) DivOpAxis(org.sirix.service.xml.xpath.operators.DivOpAxis) PredicateFilterAxis(org.sirix.axis.filter.PredicateFilterAxis)

Example 2 with LiteralExpr

use of org.sirix.service.xml.xpath.expr.LiteralExpr in project sirix by sirixdb.

the class AbstractComparator method hasNext.

@Override
public final boolean hasNext() {
    resetToLastKey();
    if (mIsFirst) {
        mIsFirst = false;
        // TODO: why?
        if (!(mOperand1 instanceof LiteralExpr)) {
            mOperand1.reset(getTrx().getNodeKey());
        }
        // TODO: why?
        if (!(mOperand2 instanceof LiteralExpr)) {
            mOperand2.reset(getTrx().getNodeKey());
        }
        /*
       * Evaluates the comparison. First atomizes both operands and then executes the comparison on
       * them. At the end, the transaction is set to the retrieved result item.
       */
        if (mOperand1.hasNext()) {
            mKey = mOperand1.next();
            try {
                // atomize operands
                final AtomicValue[] operandOne = atomize(mOperand1);
                if (mOperand2.hasNext()) {
                    mKey = mOperand2.next();
                    final AtomicValue[] operandTwo = atomize(mOperand2);
                    hook(operandOne, operandTwo);
                    try {
                        // get comparison result
                        final boolean resultValue = compare(operandOne, operandTwo);
                        final AtomicValue result = new AtomicValue(resultValue);
                        // add retrieved AtomicValue to item list
                        final int itemKey = getTrx().getItemList().addItem(result);
                        mKey = itemKey;
                    } catch (SirixXPathException e) {
                        throw new RuntimeException(e);
                    }
                    return true;
                }
            } catch (final SirixXPathException exc) {
                throw new RuntimeException(exc);
            }
        }
    }
    // return empty sequence or function called more than once
    resetToStartKey();
    return false;
}
Also used : SirixXPathException(org.sirix.exception.SirixXPathException) LiteralExpr(org.sirix.service.xml.xpath.expr.LiteralExpr) AtomicValue(org.sirix.service.xml.xpath.AtomicValue)

Example 3 with LiteralExpr

use of org.sirix.service.xml.xpath.expr.LiteralExpr in project sirix by sirixdb.

the class NodeCompTest method testCompare.

@Test
public void testCompare() throws SirixXPathException {
    AtomicValue[] op1 = { new AtomicValue(2, Type.INTEGER) };
    AtomicValue[] op2 = { new AtomicValue(3, Type.INTEGER) };
    AtomicValue[] op3 = { new AtomicValue(3, Type.INTEGER) };
    assertEquals(false, comparator.compare(op1, op2));
    assertEquals(true, comparator.compare(op3, op2));
    try {
        comparator = new NodeComp(holder.getReader(), new LiteralExpr(holder.getReader(), -2), new LiteralExpr(holder.getReader(), -1), CompKind.PRE);
        comparator.compare(op1, op2);
        fail("Expexcted not yet implemented exception.");
    } catch (IllegalStateException e) {
        assertEquals("Evaluation of node comparisons not possible", e.getMessage());
    }
    try {
        comparator = new NodeComp(holder.getReader(), new LiteralExpr(holder.getReader(), -2), new LiteralExpr(holder.getReader(), -1), CompKind.FO);
        comparator.compare(op1, op2);
        fail("Expexcted not yet implemented exception.");
    } catch (IllegalStateException e) {
        assertEquals("Evaluation of node comparisons not possible", e.getMessage());
    }
}
Also used : LiteralExpr(org.sirix.service.xml.xpath.expr.LiteralExpr) AtomicValue(org.sirix.service.xml.xpath.AtomicValue) Test(org.junit.Test)

Example 4 with LiteralExpr

use of org.sirix.service.xml.xpath.expr.LiteralExpr in project sirix by sirixdb.

the class DivOpAxisTest method testOperate.

@Test
public final void testOperate() throws SirixException {
    AtomicValue item1 = new AtomicValue(1.0, Type.DOUBLE);
    AtomicValue item2 = new AtomicValue(2.0, Type.DOUBLE);
    AbstractAxis op1 = new LiteralExpr(holder.getReader(), holder.getReader().getItemList().addItem(item1));
    AbstractAxis op2 = new LiteralExpr(holder.getReader(), holder.getReader().getItemList().addItem(item2));
    AbstractObAxis axis = new DivOpAxis(holder.getReader(), op1, op2);
    assertEquals(true, axis.hasNext());
    axis.next();
    assertThat(0.5, is(Double.parseDouble(holder.getReader().getValue())));
    assertEquals(holder.getReader().keyForName("xs:double"), holder.getReader().getTypeKey());
    assertEquals(false, axis.hasNext());
}
Also used : AbstractAxis(org.sirix.service.xml.xpath.AbstractAxis) LiteralExpr(org.sirix.service.xml.xpath.expr.LiteralExpr) AtomicValue(org.sirix.service.xml.xpath.AtomicValue) Test(org.junit.Test)

Example 5 with LiteralExpr

use of org.sirix.service.xml.xpath.expr.LiteralExpr in project sirix by sirixdb.

the class IDivOpAxisTest method testOperate.

@Test
public final void testOperate() throws SirixException {
    AtomicValue item1 = new AtomicValue(3.0, Type.DOUBLE);
    AtomicValue item2 = new AtomicValue(2.0, Type.DOUBLE);
    AbstractAxis op1 = new LiteralExpr(holder.getReader(), holder.getReader().getItemList().addItem(item1));
    AbstractAxis op2 = new LiteralExpr(holder.getReader(), holder.getReader().getItemList().addItem(item2));
    AbstractObAxis axis = new IDivOpAxis(holder.getReader(), op1, op2);
    assertEquals(true, axis.hasNext());
    axis.next();
    // note: although getRawValue() returns [1], parseString returns ""
    // assertEquals(1,
    // Integer.parseInt(TypedValue.parseString(holder.getRtx().getRawValue())));
    assertEquals(holder.getReader().keyForName("xs:integer"), holder.getReader().getTypeKey());
    assertEquals(false, axis.hasNext());
}
Also used : AbstractAxis(org.sirix.service.xml.xpath.AbstractAxis) LiteralExpr(org.sirix.service.xml.xpath.expr.LiteralExpr) AtomicValue(org.sirix.service.xml.xpath.AtomicValue) Test(org.junit.Test)

Aggregations

LiteralExpr (org.sirix.service.xml.xpath.expr.LiteralExpr)11 AtomicValue (org.sirix.service.xml.xpath.AtomicValue)10 Test (org.junit.Test)9 AbstractAxis (org.sirix.service.xml.xpath.AbstractAxis)7 Axis (org.sirix.api.Axis)2 SirixXPathException (org.sirix.exception.SirixXPathException)2 DescendantAxis (org.sirix.axis.DescendantAxis)1 ForAxis (org.sirix.axis.ForAxis)1 FilterAxis (org.sirix.axis.filter.FilterAxis)1 PredicateFilterAxis (org.sirix.axis.filter.PredicateFilterAxis)1 ExceptAxis (org.sirix.service.xml.xpath.expr.ExceptAxis)1 IfAxis (org.sirix.service.xml.xpath.expr.IfAxis)1 IntersectAxis (org.sirix.service.xml.xpath.expr.IntersectAxis)1 RangeAxis (org.sirix.service.xml.xpath.expr.RangeAxis)1 SequenceAxis (org.sirix.service.xml.xpath.expr.SequenceAxis)1 UnionAxis (org.sirix.service.xml.xpath.expr.UnionAxis)1 VariableAxis (org.sirix.service.xml.xpath.expr.VariableAxis)1 DupFilterAxis (org.sirix.service.xml.xpath.filter.DupFilterAxis)1 AddOpAxis (org.sirix.service.xml.xpath.operators.AddOpAxis)1 DivOpAxis (org.sirix.service.xml.xpath.operators.DivOpAxis)1