use of org.drools.core.common.InternalFactHandle in project drools by kiegroup.
the class FieldConstraintTest method testLiteralConstraint.
/**
* <pre>
*
* ( Cheese (type "cheddar") )
*
* </pre>
*
* This is currently the same as using a ReturnValueConstraint just that it
* doesn't need any requiredDeclarations
*/
@Test
public void testLiteralConstraint() {
InternalKnowledgeBase kBase = (InternalKnowledgeBase) KnowledgeBaseFactory.newKnowledgeBase();
StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) kBase.newKieSession();
final ClassFieldReader extractor = store.getReader(Cheese.class, "type");
final MvelConstraint constraint = new MvelConstraintTestUtil("type == \"cheddar\"", FieldFactory.getInstance().getFieldValue("cheddar"), extractor);
final Cheese cheddar = new Cheese("cheddar", 5);
final InternalFactHandle cheddarHandle = (InternalFactHandle) ksession.insert(cheddar);
// check constraint
assertTrue(constraint.isAllowed(cheddarHandle, ksession));
final Cheese stilton = new Cheese("stilton", 5);
final InternalFactHandle stiltonHandle = (InternalFactHandle) ksession.insert(stilton);
// check constraint
assertFalse(constraint.isAllowed(stiltonHandle, ksession));
}
use of org.drools.core.common.InternalFactHandle in project drools by kiegroup.
the class FieldIndexEntryTest method testTwoEntries.
@Test
public void testTwoEntries() {
final ClassFieldReader extractor = store.getReader(Cheese.class, "type");
final FieldIndex fieldIndex = new FieldIndex(extractor, null, MvelConstraint.INDEX_EVALUATOR);
final SingleIndex singleIndex = new SingleIndex(new FieldIndex[] { fieldIndex }, 1);
Tuple tuple = new RightTupleImpl(new DefaultFactHandle(1, new Cheese("stilton", 10)));
final TupleList index = new AbstractHashTable.SingleIndexTupleList(singleIndex, tuple, "stilton".hashCode(), false);
final Cheese stilton1 = new Cheese("stilton", 35);
final InternalFactHandle h1 = new DefaultFactHandle(1, stilton1);
final Cheese stilton2 = new Cheese("stilton", 59);
final InternalFactHandle h2 = new DefaultFactHandle(2, stilton2);
RightTuple h1RightTuple = new RightTupleImpl(h1, null);
RightTuple h2RightTuple = new RightTupleImpl(h2, null);
// test add
index.add(h1RightTuple);
index.add(h2RightTuple);
assertEquals(h1, index.getFirst().getFactHandle());
assertEquals(h2, ((RightTuple) index.getFirst().getNext()).getFactHandle());
// test get
assertEquals(h1, index.get(h1).getFactHandle());
assertEquals(h2, index.get(h2).getFactHandle());
// test removal for combinations
// remove first
index.remove(h2RightTuple);
assertEquals(h1RightTuple.getFactHandle(), index.getFirst().getFactHandle());
// remove second
index.add(h2RightTuple);
index.remove(h1RightTuple);
assertEquals(h2RightTuple.getFactHandle(), index.getFirst().getFactHandle());
// check index type does not change, as this fact is removed
stilton1.setType("cheddar");
}
use of org.drools.core.common.InternalFactHandle in project drools by kiegroup.
the class NotNodeLeftTuple method getAccumulatedObjects.
@Override
public Collection<Object> getAccumulatedObjects() {
if (NodeTypeEnums.ExistsNode != getTupleSink().getType()) {
return Collections.emptyList();
}
BetaNode betaNode = ((BetaNode) getTupleSink());
BetaConstraints constraints = betaNode.getRawConstraints();
InternalWorkingMemory wm = getFactHandle().getEntryPoint().getInternalWorkingMemory();
BetaMemory bm = (BetaMemory) wm.getNodeMemory((MemoryFactory) getTupleSink());
TupleMemory rtm = bm.getRightTupleMemory();
FastIterator it = betaNode.getRightIterator(rtm);
ContextEntry[] contextEntry = bm.getContext();
constraints.updateFromTuple(contextEntry, wm, this);
Collection<Object> result = new ArrayList<>();
for (RightTuple rightTuple = betaNode.getFirstRightTuple(this, rtm, null, it); rightTuple != null; ) {
RightTuple nextRight = (RightTuple) it.next(rightTuple);
InternalFactHandle fh = rightTuple.getFactHandleForEvaluation();
if (constraints.isAllowedCachedLeft(contextEntry, fh)) {
result.add(fh.getObject());
}
rightTuple = nextRight;
}
return result;
}
use of org.drools.core.common.InternalFactHandle in project drools by kiegroup.
the class WindowNode method assertObject.
public void assertObject(final InternalFactHandle factHandle, final PropagationContext pctx, final InternalWorkingMemory workingMemory) {
EventFactHandle evFh = (EventFactHandle) factHandle;
for (AlphaNodeFieldConstraint constraint : constraints) {
if (!constraint.isAllowed(evFh, workingMemory)) {
return;
}
}
RightTuple rightTuple = new RightTupleImpl(evFh, this);
rightTuple.setPropagationContext(pctx);
// this is cloned, as we need to separate the child RightTuple references
InternalFactHandle clonedFh = evFh.cloneAndLink();
rightTuple.setContextObject(clonedFh);
// process the behavior
final WindowMemory memory = workingMemory.getNodeMemory(this);
if (!behavior.assertFact(memory.behaviorContext, clonedFh, pctx, workingMemory)) {
return;
}
this.sink.propagateAssertObject(clonedFh, pctx, workingMemory);
}
use of org.drools.core.common.InternalFactHandle in project drools by kiegroup.
the class IndexingTest method testIndexingOnQueryUnificationWithNot.
@Test(timeout = 10000)
public void testIndexingOnQueryUnificationWithNot() throws Exception {
String str = "";
str += "package org.drools.compiler.test \n";
str += "import org.drools.compiler.Person \n";
str += "query peeps( String $name, int $age ) \n";
str += " not $p2 : Person( $name := name, age > $age ) \n";
str += "end\n";
KieBase kbase = loadKnowledgeBaseFromString(str);
List<ObjectTypeNode> nodes = ((KnowledgeBaseImpl) kbase).getRete().getObjectTypeNodes();
ObjectTypeNode node = null;
for (ObjectTypeNode n : nodes) {
if (((ClassObjectType) n.getObjectType()).getClassType() == DroolsQuery.class) {
node = n;
break;
}
}
StatefulKnowledgeSessionImpl wm = ((StatefulKnowledgeSessionImpl) kbase.newKieSession());
AlphaNode alphanode = (AlphaNode) node.getObjectSinkPropagator().getSinks()[0];
LeftInputAdapterNode liaNode = (LeftInputAdapterNode) alphanode.getObjectSinkPropagator().getSinks()[0];
NotNode n = (NotNode) liaNode.getSinkPropagator().getSinks()[0];
DoubleNonIndexSkipBetaConstraints c = (DoubleNonIndexSkipBetaConstraints) n.getRawConstraints();
// assertEquals( "$name", ((VariableConstraint)c.getConstraint()).getRequiredDeclarations()[0].getIdentifier() );
assertTrue(c.isIndexed());
BetaMemory bm = (BetaMemory) wm.getNodeMemory(n);
System.out.println(bm.getLeftTupleMemory().getClass());
System.out.println(bm.getRightTupleMemory().getClass());
assertTrue(bm.getLeftTupleMemory() instanceof TupleIndexHashTable);
assertTrue(bm.getRightTupleMemory() instanceof TupleIndexHashTable);
final Map<String, Integer> map = new HashMap<String, Integer>();
map.put("inserted", new Integer(0));
map.put("deleted", new Integer(0));
map.put("updated", new Integer(0));
wm.openLiveQuery("peeps", new Object[] { Variable.v, 99 }, new ViewChangedEventListener() {
@Override
public void rowInserted(Row row) {
System.out.println("inserted");
Integer integer = map.get("inserted");
map.put("inserted", integer.intValue() + 1);
}
@Override
public void rowDeleted(Row row) {
System.out.println("deleted");
Integer integer = map.get("deleted");
map.put("deleted", integer.intValue() + 1);
}
@Override
public void rowUpdated(Row row) {
System.out.println("updated");
Integer integer = map.get("updated");
map.put("updated", integer.intValue() + 1);
}
});
System.out.println("inserted: " + map.get("inserted"));
System.out.println("deleted: " + map.get("deleted"));
System.out.println("updated: " + map.get("updated"));
Map<String, InternalFactHandle> peeps = new HashMap<String, InternalFactHandle>();
Person p = null;
InternalFactHandle fh = null;
int max = 3;
// 1 matched, prior to any insertions
assertEquals(1, map.get("inserted").intValue());
assertEquals(0, map.get("deleted").intValue());
assertEquals(0, map.get("updated").intValue());
// x0 is the blocker
for (int i = 0; i < max; i++) {
p = new Person("x" + i, 100);
fh = (InternalFactHandle) wm.insert(p);
wm.fireAllRules();
peeps.put(p.getName(), fh);
}
// insertions case 1 deletion
assertEquals(1, map.get("inserted").intValue());
assertEquals(1, map.get("deleted").intValue());
assertEquals(0, map.get("updated").intValue());
// each x is blocker in turn up to x99
for (int i = 0; i < (max - 1); i++) {
fh = peeps.get("x" + i);
p = (Person) fh.getObject();
p.setAge(90);
wm.update(fh, p);
wm.fireAllRules();
// make sure this doesn't change
assertEquals("i=" + i, 1, map.get("inserted").intValue());
}
// no change
assertEquals(1, map.get("inserted").intValue());
assertEquals(1, map.get("deleted").intValue());
assertEquals(0, map.get("updated").intValue());
// x99 is still the blocker, everything else is just added
for (int i = 0; i < (max - 1); i++) {
fh = peeps.get("x" + i);
p = (Person) fh.getObject();
p.setAge(102);
wm.update(fh, p);
wm.fireAllRules();
// make sure this doesn't change
assertEquals("i=" + i, 1, map.get("inserted").intValue());
}
// no change
assertEquals(1, map.get("inserted").intValue());
assertEquals(1, map.get("deleted").intValue());
assertEquals(0, map.get("updated").intValue());
// x99 is still the blocker
for (int i = (max - 2); i >= 0; i--) {
fh = peeps.get("x" + i);
p = (Person) fh.getObject();
p.setAge(90);
wm.update(fh, p);
wm.fireAllRules();
// make sure this doesn't change
assertEquals("i=" + i, 1, map.get("inserted").intValue());
}
// move x99, should no longer be a blocker, now it can increase
fh = peeps.get("x" + (max - 1));
p = (Person) fh.getObject();
p.setAge(90);
wm.update(fh, p);
wm.fireAllRules();
assertEquals(2, map.get("inserted").intValue());
}
Aggregations