use of org.drools.core.reteoo.AlphaNode 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.AlphaNode 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.AlphaNode 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.AlphaNode 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);
}
use of org.drools.core.reteoo.AlphaNode in project drools by kiegroup.
the class MVELTest method testMapAccessorWithStaticFieldAccess.
@Test
public void testMapAccessorWithStaticFieldAccess() {
final String str = "" + "package org.drools.compiler.test \n" + "import " + Person.class.getCanonicalName() + "\n" + "import " + Address.class.getCanonicalName() + "\n" + "import " + TestEnum.class.getCanonicalName() + "\n" + "global java.util.List list \n" + "rule \"show\" \n" + "when \n" + " $m : Person( namedAddresses[TestEnum.ONE] == new Address('s1'), namedAddresses[TestEnum.ONE].street == new Address('s1').street ) \n" + "then \n" + " list.add('r1'); \n" + "end \n";
final KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL);
if (kbuilder.hasErrors()) {
fail(kbuilder.getErrors().toString());
}
final InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(kbuilder.getKnowledgePackages());
final KieSession ksession = createKnowledgeSession(kbase);
final List list = new ArrayList();
ksession.setGlobal("list", list);
final Person p = new Person("yoda");
p.getNamedAddresses().put(TestEnum.ONE, new Address("s1"));
ksession.insert(p);
ksession.fireAllRules();
assertEquals("r1", list.get(0));
// Check it was built with MVELReturnValueExpression constraint
final List<ObjectTypeNode> nodes = ((InternalKnowledgeBase) kbase).getRete().getObjectTypeNodes();
ObjectTypeNode node = null;
for (final ObjectTypeNode n : nodes) {
if (((ClassObjectType) n.getObjectType()).getClassType() == Person.class) {
node = n;
break;
}
}
AlphaNode alphanode = (AlphaNode) node.getObjectSinkPropagator().getSinks()[0];
AlphaNodeFieldConstraint constraint = alphanode.getConstraint();
if (constraint instanceof MvelConstraint) {
assertTrue(((MvelConstraint) alphanode.getConstraint()).getFieldExtractor() instanceof MVELObjectClassFieldReader);
assertEquals(new Address("s1"), ((MvelConstraint) alphanode.getConstraint()).getField().getValue());
}
alphanode = (AlphaNode) alphanode.getObjectSinkPropagator().getSinks()[0];
constraint = alphanode.getConstraint();
if (constraint instanceof MvelConstraint) {
assertTrue(((MvelConstraint) alphanode.getConstraint()).getFieldExtractor() instanceof MVELObjectClassFieldReader);
assertEquals(new Address("s1").getStreet(), ((MvelConstraint) alphanode.getConstraint()).getField().getValue());
}
}
Aggregations