use of org.drools.core.reteoo.ObjectTypeNode in project drools by kiegroup.
the class PropertySpecificTest method testComplexBetaSharedAlphaWithWatchesRemoveR1.
@Test
public void testComplexBetaSharedAlphaWithWatchesRemoveR1() {
String rule1 = "$b : B( b == 15) @watch(i) A( a == 10, b == 15 ) @watch(c)";
String rule2 = "$b : B( b == 15) @watch(j) A( a == 10, i == 20 ) @watch(s)";
String rule3 = "$b : B( c == 15) @watch(k) A( a == 10, i == 20, b == 10 ) @watch(j)";
KieBase kbase = getKnowledgeBase(rule1, rule2, rule3);
InternalWorkingMemory wm = ((InternalWorkingMemory) kbase.newKieSession());
kbase.removeRule("org.drools.compiler.integrationtests", "r0");
ObjectTypeNode otn = getObjectTypeNode(kbase, "A");
assertNotNull(otn);
Class classType = ((ClassObjectType) otn.getObjectType()).getClassType();
List<String> sp = getSettableProperties(wm, otn);
AlphaNode alphaNode1 = (AlphaNode) otn.getObjectSinkPropagator().getSinks()[0];
assertEquals(calculatePositiveMask(classType, list("a"), sp), alphaNode1.getDeclaredMask());
assertEquals(calculatePositiveMask(classType, list("a", "i", "b", "s", "j"), sp), alphaNode1.getInferredMask());
// first share
AlphaNode alphaNode1_1 = (AlphaNode) alphaNode1.getObjectSinkPropagator().getSinks()[0];
assertEquals(calculatePositiveMask(classType, list("i"), sp), alphaNode1_1.getDeclaredMask());
assertEquals(calculatePositiveMask(classType, list("a", "i", "b", "s", "j"), sp), alphaNode1_1.getInferredMask());
BetaNode betaNode1 = (BetaNode) alphaNode1_1.getObjectSinkPropagator().getSinks()[1];
assertEquals(calculatePositiveMask(classType, list("s"), sp), betaNode1.getRightDeclaredMask());
assertEquals(calculatePositiveMask(classType, list("a", "i", "s"), sp), betaNode1.getRightInferredMask());
assertEquals(calculatePositiveMask(classType, list("j"), sp), betaNode1.getLeftDeclaredMask());
assertEquals(calculatePositiveMask(classType, list("b", "j"), sp), betaNode1.getLeftInferredMask());
// second split, third alpha
AlphaNode alphaNode1_2 = (AlphaNode) alphaNode1_1.getObjectSinkPropagator().getSinks()[0];
assertEquals(calculatePositiveMask(classType, list("b"), sp), alphaNode1_2.getDeclaredMask());
assertEquals(calculatePositiveMask(classType, list("a", "b", "i", "j"), sp), alphaNode1_2.getInferredMask());
BetaNode betaNode3 = (BetaNode) alphaNode1_2.getObjectSinkPropagator().getSinks()[0];
assertEquals(calculatePositiveMask(classType, list("j"), sp), betaNode3.getRightDeclaredMask());
assertEquals(calculatePositiveMask(classType, list("a", "i", "b", "j"), sp), betaNode3.getRightInferredMask());
assertEquals(calculatePositiveMask(classType, list("k"), sp), betaNode3.getLeftDeclaredMask());
assertEquals(calculatePositiveMask(classType, list("c", "k"), sp), betaNode3.getLeftInferredMask());
}
use of org.drools.core.reteoo.ObjectTypeNode in project drools by kiegroup.
the class PropertySpecificTest method test2DifferentAlphaWatchBeforeSameBeta.
@Test
public void test2DifferentAlphaWatchBeforeSameBeta() {
String rule1 = "B(a == 15) @watch(b) C()";
String rule2 = "B(a == 15) @watch(c) C()";
KieBase kbase = getKnowledgeBase(rule1, rule2);
InternalWorkingMemory wm = ((InternalWorkingMemory) kbase.newKieSession());
ObjectTypeNode otnB = getObjectTypeNode(kbase, "B");
List<String> sp = getSettableProperties(wm, otnB);
Class classType = ((ClassObjectType) otnB.getObjectType()).getClassType();
AlphaNode alphaNode = (AlphaNode) otnB.getObjectSinkPropagator().getSinks()[0];
assertEquals(calculatePositiveMask(classType, list("a"), sp), alphaNode.getDeclaredMask());
assertEquals(calculatePositiveMask(classType, list("a", "b", "c"), sp), alphaNode.getInferredMask());
ObjectTypeNode otnC = getObjectTypeNode(kbase, "C");
BetaNode betaNodeC1 = (BetaNode) otnC.getObjectSinkPropagator().getSinks()[0];
BetaNode betaNodeC2 = (BetaNode) otnC.getObjectSinkPropagator().getSinks()[1];
LeftInputAdapterNode lia1 = (LeftInputAdapterNode) alphaNode.getObjectSinkPropagator().getSinks()[0];
assertSame(betaNodeC1, lia1.getSinkPropagator().getSinks()[0]);
LeftInputAdapterNode lia2 = (LeftInputAdapterNode) alphaNode.getObjectSinkPropagator().getSinks()[1];
assertSame(betaNodeC2, lia2.getSinkPropagator().getSinks()[0]);
assertEquals(EmptyBitMask.get(), betaNodeC1.getRightDeclaredMask());
assertEquals(EmptyBitMask.get(), betaNodeC1.getRightInferredMask());
assertEquals(calculatePositiveMask(classType, list("b"), sp), betaNodeC1.getLeftDeclaredMask());
assertEquals(calculatePositiveMask(classType, list("a", "b"), sp), betaNodeC1.getLeftInferredMask());
assertEquals(EmptyBitMask.get(), betaNodeC2.getRightDeclaredMask());
assertEquals(EmptyBitMask.get(), betaNodeC2.getRightInferredMask());
assertEquals(calculatePositiveMask(classType, list("c"), sp), betaNodeC2.getLeftDeclaredMask());
assertEquals(calculatePositiveMask(classType, list("a", "c"), sp), betaNodeC2.getLeftInferredMask());
kbase.removeRule("org.drools.compiler.integrationtests", "r0");
assertEquals(calculatePositiveMask(classType, list("a"), sp), alphaNode.getDeclaredMask());
assertEquals(calculatePositiveMask(classType, list("a", "c"), sp), alphaNode.getInferredMask());
assertEquals(1, lia2.getSinkPropagator().getSinks().length);
BetaNode betaNodeC = (BetaNode) lia2.getSinkPropagator().getSinks()[0];
assertEquals(EmptyBitMask.get(), betaNodeC2.getRightDeclaredMask());
assertEquals(EmptyBitMask.get(), betaNodeC2.getRightInferredMask());
assertEquals(calculatePositiveMask(classType, list("c"), sp), betaNodeC2.getLeftDeclaredMask());
assertEquals(calculatePositiveMask(classType, list("a", "c"), sp), betaNodeC2.getLeftInferredMask());
}
use of org.drools.core.reteoo.ObjectTypeNode in project drools by kiegroup.
the class IndexingTest method testFullFastIteratorResume.
@Test(timeout = 10000)
public void testFullFastIteratorResume() 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) {
}
@Override
public void rowDeleted(Row row) {
}
@Override
public void rowUpdated(Row row) {
}
});
Map<String, InternalFactHandle> peeps = new HashMap<String, InternalFactHandle>();
Person p = new Person("x0", 100);
InternalFactHandle fh = (InternalFactHandle) wm.insert(p);
peeps.put(p.getName(), fh);
for (int i = 1; i < 100; i++) {
p = new Person("x" + i, 101);
fh = (InternalFactHandle) wm.insert(p);
wm.fireAllRules();
peeps.put(p.getName(), fh);
}
List<RightTuple> list = new ArrayList<RightTuple>(100);
FastIterator it = n.getRightIterator(bm.getRightTupleMemory());
for (RightTuple rt = n.getFirstRightTuple(null, bm.getRightTupleMemory(), null, it); rt != null; rt = (RightTuple) it.next(rt)) {
list.add(rt);
}
assertEquals(100, list.size());
// check we can resume from each entry in the list above.
for (int i = 0; i < 100; i++) {
RightTuple rightTuple = list.get(i);
// resumes from the current rightTuple
it = n.getRightIterator(bm.getRightTupleMemory(), rightTuple);
int j = i + 1;
for (RightTuple rt = (RightTuple) it.next(rightTuple); rt != null; rt = (RightTuple) it.next(rt)) {
assertSame(list.get(j), rt);
j++;
}
}
}
use of org.drools.core.reteoo.ObjectTypeNode in project drools by kiegroup.
the class IndexingTest method testIndexingOnQueryUnification.
@Test(timeout = 10000)
public void testIndexingOnQueryUnification() throws Exception {
String str = "";
str += "package org.drools.compiler.test \n";
str += "import org.drools.compiler.Person \n";
str += "query peeps( String $name, String $likes, String $street) \n";
str += " $p : Person( $name := name, $likes := likes, $street := address.street ) \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;
}
}
InternalWorkingMemory wm = ((StatefulKnowledgeSessionImpl) kbase.newKieSession());
AlphaNode alphanode = (AlphaNode) node.getObjectSinkPropagator().getSinks()[0];
LeftInputAdapterNode liaNode = (LeftInputAdapterNode) alphanode.getObjectSinkPropagator().getSinks()[0];
// $p2
JoinNode j = (JoinNode) liaNode.getSinkPropagator().getSinks()[0];
TripleNonIndexSkipBetaConstraints c = (TripleNonIndexSkipBetaConstraints) j.getRawConstraints();
// assertEquals( "$name", ((VariableConstraint)c.getConstraint()).getRequiredDeclarations()[0].getIdentifier() );
assertTrue(c.isIndexed());
BetaMemory bm = (BetaMemory) wm.getNodeMemory(j);
assertTrue(bm.getLeftTupleMemory() instanceof TupleIndexHashTable);
assertTrue(bm.getRightTupleMemory() instanceof TupleIndexHashTable);
}
use of org.drools.core.reteoo.ObjectTypeNode in project drools by kiegroup.
the class IndexingTest method testBuildsIndexedAlphaNodes.
@Test(timeout = 10000)
public void testBuildsIndexedAlphaNodes() {
String drl = "";
drl += "package org.drools.compiler.test\n";
drl += "import org.drools.compiler.Person\n";
drl += "rule test1\n";
drl += "when\n";
drl += " Person(name == \"Mark\", age == 37)\n";
drl += " Person(name == \"Mark\", happy == true)\n";
drl += "then\n";
drl += "end\n";
KieBase kbase = loadKnowledgeBaseFromString(drl);
ObjectTypeNode otn = getObjectTypeNode(kbase, Person.class);
InternalWorkingMemory wm = ((StatefulKnowledgeSessionImpl) kbase.newKieSession());
AlphaNode alphaNode1 = (AlphaNode) otn.getObjectSinkPropagator().getSinks()[0];
CompositeObjectSinkAdapter sinkAdapter = (CompositeObjectSinkAdapter) alphaNode1.getObjectSinkPropagator();
ObjectSinkNodeList hashableSinks = sinkAdapter.getHashableSinks();
assertNotNull(hashableSinks);
assertEquals(2, hashableSinks.size());
AlphaNode alphaNode2 = (AlphaNode) alphaNode1.getObjectSinkPropagator().getSinks()[0];
assertSame(hashableSinks.getFirst(), alphaNode2);
AlphaNode alphaNode3 = (AlphaNode) alphaNode1.getObjectSinkPropagator().getSinks()[1];
assertSame(hashableSinks.getLast(), alphaNode3);
}
Aggregations