use of org.drools.core.reteoo.ObjectTypeNode in project drools by kiegroup.
the class ReteDumper method getSinks.
public static Sink[] getSinks(BaseNode node) {
Sink[] sinks = null;
if (node instanceof EntryPointNode) {
EntryPointNode source = (EntryPointNode) node;
Collection<ObjectTypeNode> otns = source.getObjectTypeNodes().values();
sinks = otns.toArray(new Sink[otns.size()]);
} else if (node instanceof ObjectSource) {
ObjectSource source = (ObjectSource) node;
sinks = source.getObjectSinkPropagator().getSinks();
} else if (node instanceof LeftTupleSource) {
LeftTupleSource source = (LeftTupleSource) node;
sinks = source.getSinkPropagator().getSinks();
}
return sinks;
}
use of org.drools.core.reteoo.ObjectTypeNode 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());
}
use of org.drools.core.reteoo.ObjectTypeNode in project drools by kiegroup.
the class IndexingTest method testAlphaNodeSharing.
@Test()
public void testAlphaNodeSharing() {
String drl = "";
drl += "package org.drools.compiler.test\n";
drl += "import " + Person.class.getCanonicalName() + "\n";
drl += "rule r1\n";
drl += "when\n";
drl += " Person(name == \"Mark\")\n";
drl += "then\n";
drl += "end\n";
drl += "rule r2\n";
drl += "when\n";
drl += " Person(name == \"Mark\", age == 40)\n";
drl += "then\n";
drl += "end\n";
drl += "rule r3\n";
drl += "when\n";
drl += " Person(name == \"Mark\", age == 50)\n";
drl += "then\n";
drl += "end\n";
drl += "rule r4\n";
drl += "when\n";
drl += " Person(name == \"John\", age == 60)\n";
drl += "then\n";
drl += "end\n";
KieBase kbase = loadKnowledgeBaseFromString(drl);
Map<String, Rule> rules = rulestoMap(kbase);
ObjectTypeNode otn = getObjectTypeNode(kbase, Person.class);
InternalWorkingMemory wm = ((StatefulKnowledgeSessionImpl) kbase.newKieSession());
assertEquals(2, otn.getObjectSinkPropagator().size());
AlphaNode a1 = (AlphaNode) otn.getObjectSinkPropagator().getSinks()[0];
assertEquals(3, a1.getObjectSinkPropagator().size());
assertEquals(3, a1.getAssociationsSize());
assertTrue(a1.isAssociatedWith(rules.get("r1")));
assertTrue(a1.isAssociatedWith(rules.get("r2")));
assertTrue(a1.isAssociatedWith(rules.get("r3")));
AlphaNode a2 = (AlphaNode) otn.getObjectSinkPropagator().getSinks()[1];
assertEquals(1, a2.getAssociationsSize());
assertEquals(1, a2.getObjectSinkPropagator().size());
assertTrue(a2.isAssociatedWith(rules.get("r4")));
}
use of org.drools.core.reteoo.ObjectTypeNode in project drools by kiegroup.
the class TraitTest method testAlphaNodeSharing.
@Test
public void testAlphaNodeSharing() {
String drl = "package test; " + "import " + Entity.class.getName() + " " + "declare trait Person\n" + " name : String\n" + "end\n" + "rule Init " + "when " + "then " + " don( new Entity(), Person.class ); " + "end\n" + "rule One when" + " $core: Entity( this isA Person ) " + "then " + "end " + "rule Two when" + " $core: Entity( this isA Person ) " + "then " + "end " + "\n";
final KieBase kbase = getKieBaseFromString(drl);
TraitFactory.setMode(mode, kbase);
KieSession kSession = kbase.newKieSession();
assertEquals(3, kSession.fireAllRules());
NamedEntryPoint nep = ((NamedEntryPoint) kSession.getEntryPoint(EntryPointId.DEFAULT.getEntryPointId()));
ObjectTypeNode otn = nep.getEntryPointNode().getObjectTypeNodes().get(new ClassObjectType(Entity.class));
assertNotNull(otn);
assertEquals(1, otn.getObjectSinkPropagator().getSinks().length);
}
use of org.drools.core.reteoo.ObjectTypeNode in project drools by kiegroup.
the class AccumulateTest method testAccumulateWithSameSubnetwork.
// (timeout = 10000)
@Test
public void testAccumulateWithSameSubnetwork() throws Exception {
String rule = "package org.drools.compiler.test;\n" + "import org.drools.compiler.Cheese;\n" + "import org.drools.compiler.Person;\n" + "global java.util.List list; \n" + "rule r1 salience 100 \n" + " when\n" + " $person : Person( name == 'Alice', $likes : likes )\n" + " $total : Number() from accumulate( $p : Person(likes != $likes, $l : likes) and $c : Cheese( type == $l ),\n" + " min($c.getPrice()) )\n" + " then\n" + " list.add( 'r1' + ':' + $total);\n" + "end\n" + "rule r2 \n" + " when\n" + " $person : Person( name == 'Alice', $likes : likes )\n" + " $total : Number() from accumulate( $p : Person(likes != $likes, $l : likes) and $c : Cheese( type == $l ),\n" + " max($c.getPrice()) )\n" + " then\n" + " list.add( 'r2' + ':' + $total);\n" + "end\n" + "";
// read in the source
KieBase kbase = loadKnowledgeBaseFromString(rule);
KieSession wm = createKnowledgeSession(kbase);
List list = new ArrayList();
wm.setGlobal("list", list);
// Check the network formation, to ensure the RiaNode is shared.
ObjectTypeNode cheeseOtn = LinkingTest.getObjectTypeNode(kbase, Cheese.class);
ObjectSink[] oSinks = cheeseOtn.getObjectSinkPropagator().getSinks();
assertEquals(1, oSinks.length);
JoinNode cheeseJoin = (JoinNode) oSinks[0];
LeftTupleSink[] ltSinks = cheeseJoin.getSinkPropagator().getSinks();
assertEquals(1, ltSinks.length);
RightInputAdapterNode rian = (RightInputAdapterNode) ltSinks[0];
// RiaNode is shared, if this has two outputs
assertEquals(2, rian.getObjectSinkPropagator().size());
wm.insert(new Cheese("stilton", 10));
wm.insert(new Person("Alice", "brie"));
wm.insert(new Person("Bob", "stilton"));
wm.fireAllRules();
assertEquals(2, list.size());
assertEquals("r1:10", list.get(0));
assertEquals("r2:10", list.get(1));
}
Aggregations