Search in sources :

Example 1 with BooleanCell

use of org.knime.core.data.def.BooleanCell in project knime-core by knime.

the class ExpressionFactory method xor.

/**
 * {@inheritDoc}
 */
@Override
public Expression xor(final List<Expression> boolExpressions) {
    if (boolExpressions.size() < 1) {
        throw new IllegalStateException("xor requires at least one argument.");
    }
    final boolean allIsConstant = checkBooleansAndConstant(boolExpressions);
    return new Expression.Base(boolExpressions) {

        /**
         * {@inheritDoc}
         */
        @Override
        public DataType getOutputType() {
            return BooleanCell.TYPE;
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public List<DataType> getInputArgs() {
            return Collections.singletonList(BooleanCell.TYPE);
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public ExpressionValue evaluate(final DataRow row, final VariableProvider provider) {
            ExpressionValue val = boolExpressions.get(0).evaluate(row, provider);
            DataCell valCell = val.getValue();
            assert !valCell.isMissing();
            if (valCell.isMissing()) {
                return new ExpressionValue(valCell, EMPTY_MAP);
            } else if (!(valCell instanceof BooleanValue)) {
                throw new IllegalStateException("Not a boolean value in row '" + row.getKey() + "': " + valCell.getType());
            }
            BooleanCell ret = (BooleanCell) BooleanCellFactory.create(((BooleanValue) valCell).getBooleanValue());
            Map<String, Map<String, String>> matchedObjects = val.getMatchedObjects();
            for (int i = 1; i < boolExpressions.size(); ++i) {
                Expression boolExpression = boolExpressions.get(i);
                ExpressionValue v = boolExpression.evaluate(row, provider);
                DataCell cell = v.getValue();
                if (cell.isMissing()) {
                    return new ExpressionValue(cell, EMPTY_MAP);
                } else if (cell instanceof BooleanValue) {
                    BooleanValue bool = (BooleanValue) cell;
                    matchedObjects = Util.mergeObjects(matchedObjects, v.getMatchedObjects());
                    ret = (BooleanCell) BooleanCellFactory.create(ret.getBooleanValue() ^ bool.getBooleanValue());
                } else {
                    throw new IllegalStateException("Not a boolean value: " + v.getValue());
                }
            }
            return new ExpressionValue(ret, matchedObjects);
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public boolean isConstant() {
            return allIsConstant;
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public String toString() {
            return "xor(" + boolExpressions + ")";
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public ASTType getTreeType() {
            return ASTType.Xor;
        }
    };
}
Also used : BooleanCell(org.knime.core.data.def.BooleanCell) DataRow(org.knime.core.data.DataRow) BooleanValue(org.knime.core.data.BooleanValue) DataType(org.knime.core.data.DataType) DataCell(org.knime.core.data.DataCell) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with BooleanCell

use of org.knime.core.data.def.BooleanCell in project knime-core by knime.

the class ExpressionFactoryTest method testIn.

/**
 * Test method for {@link ExpressionFactory#in(Expression, Expression)} .
 */
@Test
public void testIn() {
    final BooleanCell trueCell = BooleanCell.TRUE;
    final BooleanCell falseCell = BooleanCell.FALSE;
    testForValues(m_factory.in(m_mockBoolExpression, m_factory.list(Arrays.asList(m_mockBoolExpression, m_mockBoolExpressionOther))), trueCell, trueCell, trueCell, trueCell, trueCell);
    testForValues(m_factory.in(m_mockBoolExpressionOther, m_factory.list(Arrays.asList(m_mockBoolExpression))), trueCell, falseCell, falseCell, trueCell, falseCell);
    testForValues(m_factory.in(m_mockBoolExpression, m_factory.list(Arrays.asList(m_mockBoolExpressionOther))), trueCell, falseCell, falseCell, trueCell, falseCell);
}
Also used : BooleanCell(org.knime.core.data.def.BooleanCell) Test(org.junit.Test)

Example 3 with BooleanCell

use of org.knime.core.data.def.BooleanCell in project knime-core by knime.

the class ExpressionFactoryTest method testList.

/**
 * Test method for {@link ExpressionFactory#list(java.util.List)} .
 */
@Test
public void testList() {
    final BooleanCell trueCell = BooleanCell.TRUE;
    final BooleanCell falseCell = BooleanCell.FALSE;
    final ListCell trueList = CollectionCellFactory.createListCell(Arrays.asList(trueCell));
    final ListCell falseList = CollectionCellFactory.createListCell(Arrays.asList(falseCell));
    final ListCell missingCellList = CollectionCellFactory.createListCell(Arrays.asList(DataType.getMissingCell()));
    testForValues(m_factory.list(Arrays.asList(m_mockBoolExpression)), trueList, falseList, trueList, falseList, missingCellList);
    testForValues(m_factory.list(Arrays.asList(m_mockBoolExpression, m_mockBoolExpressionOther)), CollectionCellFactory.createListCell(Arrays.asList(trueCell, trueCell)), CollectionCellFactory.createListCell(Arrays.asList(falseCell, trueCell)), CollectionCellFactory.createListCell(Arrays.asList(trueCell, falseCell)), CollectionCellFactory.createListCell(Arrays.asList(falseCell, falseCell)), CollectionCellFactory.createListCell(Arrays.asList(DataType.getMissingCell(), trueCell)));
}
Also used : ListCell(org.knime.core.data.collection.ListCell) BooleanCell(org.knime.core.data.def.BooleanCell) Test(org.junit.Test)

Example 4 with BooleanCell

use of org.knime.core.data.def.BooleanCell in project knime-core by knime.

the class ExpressionFactoryTest method testOr.

/**
 * Test method for {@link ExpressionFactory#or(List)} .
 */
@Test
public void testOr() {
    final Expression orSingle = m_factory.or(Arrays.asList(m_mockBoolExpression));
    final BooleanCell trueCell = BooleanCell.TRUE, falseCell = BooleanCell.FALSE;
    // DataCell missing = DataType.getMissingCell();
    testForValues(orSingle, trueCell, falseCell, trueCell, falseCell);
    final Expression orSame = m_factory.or(Arrays.asList(m_mockBoolExpression, m_mockBoolExpression));
    testForValues(orSame, trueCell, falseCell, trueCell, falseCell);
    final Expression orMixed = m_factory.or(Arrays.asList(m_mockBoolExpression, m_mockBoolExpressionOther));
    testForValues(orMixed, trueCell, trueCell, trueCell, falseCell);
}
Also used : BooleanCell(org.knime.core.data.def.BooleanCell) Test(org.junit.Test)

Example 5 with BooleanCell

use of org.knime.core.data.def.BooleanCell in project knime-core by knime.

the class ExpressionFactoryTest method testXor.

/**
 * Test method for {@link ExpressionFactory#xor(List)} .
 */
@Test
public void testXor() {
    final Expression xorSingle = m_factory.xor(Arrays.asList(m_mockBoolExpression));
    final BooleanCell trueCell = BooleanCell.TRUE, falseCell = BooleanCell.FALSE;
    // DataCell missing = DataType.getMissingCell();
    testForValues(xorSingle, trueCell, falseCell, trueCell, falseCell);
    final Expression xorSame = m_factory.xor(Arrays.asList(m_mockBoolExpression, m_mockBoolExpression));
    testForValues(xorSame, falseCell, falseCell, falseCell, falseCell);
    final Expression xorMixed = m_factory.xor(Arrays.asList(m_mockBoolExpression, m_mockBoolExpressionOther));
    testForValues(xorMixed, falseCell, trueCell, trueCell, falseCell);
}
Also used : BooleanCell(org.knime.core.data.def.BooleanCell) Test(org.junit.Test)

Aggregations

BooleanCell (org.knime.core.data.def.BooleanCell)8 Test (org.junit.Test)7 HashMap (java.util.HashMap)1 Map (java.util.Map)1 BooleanValue (org.knime.core.data.BooleanValue)1 DataCell (org.knime.core.data.DataCell)1 DataRow (org.knime.core.data.DataRow)1 DataType (org.knime.core.data.DataType)1 ListCell (org.knime.core.data.collection.ListCell)1