use of org.drools.core.rule.MvelConstraintTestUtil 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);
}
use of org.drools.core.rule.MvelConstraintTestUtil in project drools by kiegroup.
the class CompositeObjectSinkAdapterTest method testTripleAlphaObjectCharacterConstraint.
@Test
public void testTripleAlphaObjectCharacterConstraint() {
final CompositeObjectSinkAdapter ad = new CompositeObjectSinkAdapter();
InternalReadAccessor extractor = store.getReader(Cheese.class, "charObjectType");
final MvelConstraint lit = new MvelConstraintTestUtil("charObjectType == 65", new LongFieldImpl(65), extractor);
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("charObjectType == 66", new LongFieldImpl(66), extractor);
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("charObjectType == 67", new LongFieldImpl(67), extractor);
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);
// test propagation
Cheese cheese = new Cheese();
cheese.setCharObjectType('B');
CompositeObjectSinkAdapter.HashKey hashKey = new CompositeObjectSinkAdapter.HashKey();
// should find this
hashKey.setValue(extractor.getIndex(), cheese, extractor);
ObjectSink sink = (ObjectSink) ad.hashedSinkMap.get(hashKey);
assertSame(al2, sink);
// should not find this one
cheese.setCharObjectType('X');
hashKey.setValue(extractor.getIndex(), cheese, extractor);
sink = (ObjectSink) ad.hashedSinkMap.get(hashKey);
assertNull(sink);
// now remove one, check the hashing is undone
ad.removeObjectSink(al2);
assertNotNull(ad.hashableSinks);
assertEquals(2, ad.hashableSinks.size());
assertNull(ad.hashedSinkMap);
}
use of org.drools.core.rule.MvelConstraintTestUtil 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);
}
use of org.drools.core.rule.MvelConstraintTestUtil 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());
}
}
use of org.drools.core.rule.MvelConstraintTestUtil in project drools by kiegroup.
the class BaseBetaConstraintsTest method getConstraint.
protected BetaNodeFieldConstraint getConstraint(String identifier, Operator operator, String fieldName, Class clazz) {
ClassFieldAccessorStore store = new ClassFieldAccessorStore();
store.setClassFieldAccessorCache(new ClassFieldAccessorCache(Thread.currentThread().getContextClassLoader()));
store.setEagerWire(true);
InternalReadAccessor extractor = store.getReader(clazz, fieldName);
Declaration declaration = new Declaration(identifier, extractor, new Pattern(0, new ClassObjectType(clazz)));
String expression = fieldName + " " + operator.getOperatorString() + " " + identifier;
return new MvelConstraintTestUtil(expression, operator.getOperatorString(), declaration, extractor);
}
Aggregations