use of org.drools.core.rule.MvelConstraintTestUtil in project drools by kiegroup.
the class RightTupleIndexHashTableIteratorTest 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() + " " + declaration.getIdentifier();
return new MvelConstraintTestUtil(expression, declaration, extractor);
}
use of org.drools.core.rule.MvelConstraintTestUtil in project drools by kiegroup.
the class LeftLeftTupleIndexHashTableIteratorTest 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() + " " + declaration.getIdentifier();
return new MvelConstraintTestUtil(expression, declaration, extractor);
}
use of org.drools.core.rule.MvelConstraintTestUtil in project drools by kiegroup.
the class AlphaNodeTest method testLiteralConstraintAssertObjectWithoutMemory.
@Test
public void testLiteralConstraintAssertObjectWithoutMemory() throws Exception {
InternalKnowledgeBase kBase = (InternalKnowledgeBase) KnowledgeBaseFactory.newKnowledgeBase();
BuildContext buildContext = new BuildContext(kBase);
buildContext.setRule(new RuleImpl("test"));
StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) kBase.newKieSession();
final RuleImpl rule = new RuleImpl("test-rule");
PropagationContextFactory pctxFactory = kBase.getConfiguration().getComponentFactory().getPropagationContextFactory();
final PropagationContext context = pctxFactory.createPropagationContext(0, PropagationContext.Type.INSERTION, null, null, null);
final MockObjectSource source = new MockObjectSource(buildContext.getNextId());
final ClassFieldReader extractor = store.getReader(Cheese.class, "type");
final FieldValue field = FieldFactory.getInstance().getFieldValue("cheddar");
final MvelConstraint constraint = new MvelConstraintTestUtil("type == \"cheddar\"", field, extractor);
// With Memory
final AlphaNode alphaNode = new AlphaNode(buildContext.getNextId(), constraint, source, // no memory
buildContext);
final MockObjectSink sink = new MockObjectSink();
alphaNode.addObjectSink(sink);
final Cheese cheddar = new Cheese("cheddar", 5);
final DefaultFactHandle f0 = (DefaultFactHandle) ksession.insert(cheddar);
// check sink is empty
assertLength(0, sink.getAsserted());
// object should assert as it passes text
alphaNode.assertObject(f0, context, ksession);
assertEquals(1, sink.getAsserted().size());
Object[] list = (Object[]) sink.getAsserted().get(0);
assertSame(cheddar, ksession.getObject((DefaultFactHandle) list[0]));
final Cheese stilton = new Cheese("stilton", 6);
final DefaultFactHandle f1 = new DefaultFactHandle(1, stilton);
// object should NOT assert as it does not pass test
alphaNode.assertObject(f1, context, ksession);
assertLength(1, sink.getAsserted());
list = (Object[]) sink.getAsserted().get(0);
assertSame(cheddar, ksession.getObject((DefaultFactHandle) list[0]));
}
use of org.drools.core.rule.MvelConstraintTestUtil 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());
}
use of org.drools.core.rule.MvelConstraintTestUtil in project drools by kiegroup.
the class CompositeObjectSinkAdapterTest method testTripleAlphaCharacterConstraint.
@Test
public void testTripleAlphaCharacterConstraint() {
final CompositeObjectSinkAdapter ad = new CompositeObjectSinkAdapter();
InternalReadAccessor extractor = store.getReader(Cheese.class, "charType");
final MvelConstraint lit = new MvelConstraintTestUtil("charType == 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("charType == 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("charType == 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.setCharType('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.setCharType('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);
}
Aggregations