Search in sources :

Example 1 with ObjectFieldImpl

use of org.drools.core.base.field.ObjectFieldImpl in project drools by kiegroup.

the class CompositeObjectSinkAdapterTest method testDoubleAlphaWithBeta.

@Test
public void testDoubleAlphaWithBeta() {
    final CompositeObjectSinkAdapter ad = new CompositeObjectSinkAdapter();
    final MvelConstraint lit = new MvelConstraintTestUtil("type == \"stilton\"", new ObjectFieldImpl("stilton"), new MockExtractor());
    final AlphaNode al = new AlphaNode(buildContext.getNextId(), lit, new MockObjectSource(0), buildContext);
    ad.addObjectSink(al);
    assertNull(ad.otherSinks);
    assertNotNull(ad.hashedFieldIndexes);
    assertEquals(1, ad.hashableSinks.size());
    assertEquals(al, ad.getSinks()[0]);
    final MvelConstraint lit2 = new MvelConstraintTestUtil("type == \"cheddar\"", new ObjectFieldImpl("cheddar"), new MockExtractor());
    final AlphaNode al2 = new AlphaNode(buildContext.getNextId(), lit2, new MockObjectSource(buildContext.getNextId()), buildContext);
    ad.addObjectSink(al2);
    assertNull(ad.otherSinks);
    assertEquals(2, ad.hashableSinks.size());
    assertEquals(al, ad.getSinks()[0]);
    assertEquals(al2, ad.getSinks()[1]);
    // add a beta, just for good measure, make sure it leaves others alone
    final MockBetaNode beta = new MockBetaNode(buildContext.getNextId(), new MockBetaNode(), new MockObjectSource(), buildContext);
    ad.addObjectSink(beta);
    assertNotNull(ad.otherSinks);
    assertEquals(2, ad.hashableSinks.size());
    assertEquals(1, ad.otherSinks.size());
    assertEquals(beta, ad.otherSinks.getFirst());
    ad.removeObjectSink(beta);
    assertNull(ad.otherSinks);
    assertEquals(2, ad.hashableSinks.size());
}
Also used : MvelConstraint(org.drools.core.rule.constraint.MvelConstraint) ObjectFieldImpl(org.drools.core.base.field.ObjectFieldImpl) MvelConstraintTestUtil(org.drools.core.rule.MvelConstraintTestUtil) Test(org.junit.Test)

Example 2 with ObjectFieldImpl

use of org.drools.core.base.field.ObjectFieldImpl in project drools by kiegroup.

the class CompositeObjectSinkAdapterTest method testSingleAlpha.

@Test
public void testSingleAlpha() {
    final CompositeObjectSinkAdapter ad = new CompositeObjectSinkAdapter();
    final MvelConstraint lit = new MvelConstraintTestUtil("type == \"stilton\"", new ObjectFieldImpl("stilton"), new MockExtractor());
    final AlphaNode al = new AlphaNode(buildContext.getNextId(), lit, new MockObjectSource(0), buildContext);
    ad.addObjectSink(al);
    assertNull(ad.otherSinks);
    assertNotNull(ad.hashedFieldIndexes);
    assertEquals(1, ad.hashableSinks.size());
    assertEquals(al, ad.getSinks()[0]);
    ad.removeObjectSink(al);
    assertNull(ad.otherSinks);
    assertNull(ad.hashableSinks);
}
Also used : MvelConstraint(org.drools.core.rule.constraint.MvelConstraint) ObjectFieldImpl(org.drools.core.base.field.ObjectFieldImpl) MvelConstraintTestUtil(org.drools.core.rule.MvelConstraintTestUtil) Test(org.junit.Test)

Example 3 with ObjectFieldImpl

use of org.drools.core.base.field.ObjectFieldImpl in project drools by kiegroup.

the class CompositeObjectSinkAdapterTest method testTripleAlpha.

@Test
public void testTripleAlpha() {
    final CompositeObjectSinkAdapter ad = new CompositeObjectSinkAdapter();
    InternalReadAccessor extractor = store.getReader(Cheese.class, "type");
    final MvelConstraint lit = new MvelConstraintTestUtil("type == \"stilton\"", new ObjectFieldImpl("stilton"), new MockExtractor());
    final AlphaNode al = new AlphaNode(buildContext.getNextId(), lit, new MockObjectSource(buildContext.getNextId()), buildContext);
    ad.addObjectSink(al);
    assertNull(ad.otherSinks);
    assertNotNull(ad.hashedFieldIndexes);
    assertEquals(1, ad.hashableSinks.size());
    assertEquals(al, ad.getSinks()[0]);
    final MvelConstraint lit2 = new MvelConstraintTestUtil("type == \"cheddar\"", new ObjectFieldImpl("cheddar"), new MockExtractor());
    final AlphaNode al2 = new AlphaNode(buildContext.getNextId(), lit2, new MockObjectSource(buildContext.getNextId()), buildContext);
    ad.addObjectSink(al2);
    assertNull(ad.hashedSinkMap);
    assertEquals(2, ad.hashableSinks.size());
    final MvelConstraint lit3 = new MvelConstraintTestUtil("type == \"stinky\"", new ObjectFieldImpl("stinky"), new MockExtractor());
    final AlphaNode al3 = new AlphaNode(buildContext.getNextId(), lit3, new MockObjectSource(buildContext.getNextId()), buildContext);
    ad.addObjectSink(al3);
    // this should now be nicely hashed.
    assertNotNull(ad.hashedSinkMap);
    assertNull(ad.hashableSinks);
    // now remove one, check the hashing is undone
    ad.removeObjectSink(al2);
    assertNotNull(ad.hashableSinks);
    assertEquals(2, ad.hashableSinks.size());
    assertNull(ad.hashedSinkMap);
}
Also used : InternalReadAccessor(org.drools.core.spi.InternalReadAccessor) MvelConstraint(org.drools.core.rule.constraint.MvelConstraint) ObjectFieldImpl(org.drools.core.base.field.ObjectFieldImpl) MvelConstraintTestUtil(org.drools.core.rule.MvelConstraintTestUtil) Test(org.junit.Test)

Example 4 with ObjectFieldImpl

use of org.drools.core.base.field.ObjectFieldImpl in project drools by kiegroup.

the class CompositeObjectSinkAdapterTest method testPropagationWithNullValue.

@Test
public void testPropagationWithNullValue() {
    final CompositeObjectSinkAdapter ad = new CompositeObjectSinkAdapter();
    InternalReadAccessor extractor = store.getReader(Cheese.class, "type");
    final MvelConstraint lit1 = new MvelConstraintTestUtil("type == \"stilton\"", new ObjectFieldImpl("stilton"), new MockExtractor());
    final AlphaNode al1 = new AlphaNode(buildContext.getNextId(), lit1, new MockObjectSource(buildContext.getNextId()), buildContext);
    final MvelConstraint lit2 = new MvelConstraintTestUtil("type == \"brie\"", new ObjectFieldImpl("brie"), new MockExtractor());
    final AlphaNode al2 = new AlphaNode(buildContext.getNextId(), lit2, new MockObjectSource(buildContext.getNextId()), buildContext);
    final MvelConstraint lit3 = new MvelConstraintTestUtil("type == \"muzzarela\"", new ObjectFieldImpl("muzzarela"), new MockExtractor());
    final AlphaNode al3 = new AlphaNode(buildContext.getNextId(), lit3, new MockObjectSource(buildContext.getNextId()), buildContext);
    ad.addObjectSink(al1);
    ad.addObjectSink(al2);
    ad.addObjectSink(al3);
    InternalFactHandle handle = new ReteooFactHandleFactory().newFactHandle(new Cheese(), null, null, new DisconnectedWorkingMemoryEntryPoint("DEFAULT"));
    try {
        ad.propagateAssertObject(handle, null, null);
    } catch (RuntimeException e) {
        fail("Not supposed to throw any exception: " + e.getMessage());
    }
}
Also used : DisconnectedWorkingMemoryEntryPoint(org.drools.core.common.DisconnectedWorkingMemoryEntryPoint) MvelConstraint(org.drools.core.rule.constraint.MvelConstraint) Cheese(org.drools.core.test.model.Cheese) MvelConstraintTestUtil(org.drools.core.rule.MvelConstraintTestUtil) InternalReadAccessor(org.drools.core.spi.InternalReadAccessor) ObjectFieldImpl(org.drools.core.base.field.ObjectFieldImpl) InternalFactHandle(org.drools.core.common.InternalFactHandle) Test(org.junit.Test)

Example 5 with ObjectFieldImpl

use of org.drools.core.base.field.ObjectFieldImpl in project drools by kiegroup.

the class FieldFactory method getFieldValue.

public FieldValue getFieldValue(Object value, ValueType valueType) {
    FieldValue field = null;
    if (value == null) {
        valueType = ValueType.NULL_TYPE;
    }
    if (valueType == ValueType.NULL_TYPE) {
        field = new ObjectFieldImpl(null);
    } else if (valueType == ValueType.PCHAR_TYPE || valueType == ValueType.CHAR_TYPE) {
        if (value instanceof String && ((String) value).length() == 1) {
            field = new LongFieldImpl(((String) value).charAt(0));
        } else {
            field = new LongFieldImpl(((Character) value).charValue());
        }
    } else if (valueType == ValueType.PBYTE_TYPE || valueType == ValueType.BYTE_TYPE) {
        if (value instanceof String) {
            field = new LongFieldImpl(Byte.parseByte((String) value));
        } else {
            field = new LongFieldImpl(((Number) value).byteValue());
        }
    } else if (valueType == ValueType.PSHORT_TYPE || valueType == ValueType.SHORT_TYPE) {
        if (value instanceof String) {
            try {
                field = new LongFieldImpl(NumberFormat.getInstance().parse((String) value).shortValue());
            } catch (ParseException e) {
                throw new NumberFormatException("Error parsing number '" + value + "'");
            }
        } else {
            field = new LongFieldImpl(((Number) value).shortValue());
        }
    } else if (valueType == ValueType.PINTEGER_TYPE || valueType == ValueType.INTEGER_TYPE) {
        if (value instanceof String) {
            try {
                field = new LongFieldImpl(NumberFormat.getInstance().parse((String) value).intValue());
            } catch (ParseException e) {
                throw new NumberFormatException("Error parsing number '" + value + "'");
            }
        } else {
            field = new LongFieldImpl(((Number) value).intValue());
        }
    } else if (valueType == ValueType.PLONG_TYPE || valueType == ValueType.LONG_TYPE) {
        if (value instanceof String) {
            try {
                field = new LongFieldImpl(NumberFormat.getInstance().parse((String) value).longValue());
            } catch (ParseException e) {
                throw new NumberFormatException("Error parsing number '" + value + "'");
            }
        } else {
            field = new LongFieldImpl(((Number) value).longValue());
        }
    } else if (valueType == ValueType.PFLOAT_TYPE || valueType == ValueType.FLOAT_TYPE) {
        if (value instanceof String) {
            try {
                field = new DoubleFieldImpl(NumberFormat.getInstance().parse((String) value).floatValue());
            } catch (ParseException e) {
                throw new NumberFormatException("Error parsing number '" + value + "'");
            }
        } else {
            field = new DoubleFieldImpl(((Number) value).floatValue());
        }
    } else if (valueType == ValueType.PDOUBLE_TYPE || valueType == ValueType.DOUBLE_TYPE) {
        if (value instanceof String) {
            try {
                field = new DoubleFieldImpl(NumberFormat.getInstance().parse((String) value).doubleValue());
            } catch (ParseException e) {
                throw new NumberFormatException("Error parsing number '" + value + "'");
            }
        } else {
            field = new DoubleFieldImpl(((Number) value).doubleValue());
        }
    } else if (valueType == ValueType.PBOOLEAN_TYPE || valueType == ValueType.BOOLEAN_TYPE) {
        if (value instanceof String) {
            field = new BooleanFieldImpl(Boolean.valueOf((String) value));
        } else {
            field = new BooleanFieldImpl((Boolean) value);
        }
    } else if (valueType == ValueType.STRING_TYPE) {
        field = new ObjectFieldImpl(value.toString());
    } else if (valueType == ValueType.DATE_TYPE) {
        // MN: I think its fine like this, seems to work !
        if (value instanceof String) {
            Date date = DateUtils.parseDate((String) value);
            field = new ObjectFieldImpl(date);
        } else {
            field = new ObjectFieldImpl(value);
        }
    } else if (valueType == ValueType.ARRAY_TYPE) {
        // MN: I think its fine like this.
        field = new ObjectFieldImpl(value);
    } else if (valueType == ValueType.OBJECT_TYPE) {
        field = new ObjectFieldImpl(value);
    } else if (valueType == ValueType.TRAIT_TYPE) {
        field = new ObjectFieldImpl(value);
    } else if (valueType == ValueType.BIG_DECIMAL_TYPE) {
        field = new ObjectFieldImpl(MathUtils.getBigDecimal(value));
    } else if (valueType == ValueType.BIG_INTEGER_TYPE) {
        field = new ObjectFieldImpl(MathUtils.getBigInteger(value));
    } else if (valueType == ValueType.CLASS_TYPE) {
        field = new ClassFieldImpl((Class) value);
    }
    return field;
}
Also used : BooleanFieldImpl(org.drools.core.base.field.BooleanFieldImpl) DoubleFieldImpl(org.drools.core.base.field.DoubleFieldImpl) LongFieldImpl(org.drools.core.base.field.LongFieldImpl) Date(java.util.Date) ClassFieldImpl(org.drools.core.base.field.ClassFieldImpl) ObjectFieldImpl(org.drools.core.base.field.ObjectFieldImpl) FieldValue(org.drools.core.spi.FieldValue) ParseException(java.text.ParseException)

Aggregations

ObjectFieldImpl (org.drools.core.base.field.ObjectFieldImpl)5 MvelConstraintTestUtil (org.drools.core.rule.MvelConstraintTestUtil)4 MvelConstraint (org.drools.core.rule.constraint.MvelConstraint)4 Test (org.junit.Test)4 InternalReadAccessor (org.drools.core.spi.InternalReadAccessor)2 ParseException (java.text.ParseException)1 Date (java.util.Date)1 BooleanFieldImpl (org.drools.core.base.field.BooleanFieldImpl)1 ClassFieldImpl (org.drools.core.base.field.ClassFieldImpl)1 DoubleFieldImpl (org.drools.core.base.field.DoubleFieldImpl)1 LongFieldImpl (org.drools.core.base.field.LongFieldImpl)1 DisconnectedWorkingMemoryEntryPoint (org.drools.core.common.DisconnectedWorkingMemoryEntryPoint)1 InternalFactHandle (org.drools.core.common.InternalFactHandle)1 FieldValue (org.drools.core.spi.FieldValue)1 Cheese (org.drools.core.test.model.Cheese)1