use of org.drools.core.reteoo.ObjectTypeNode in project drools by kiegroup.
the class MVELTest method testNewConstructor.
@Test
public void testNewConstructor() {
final String str = "" + "package org.drools.compiler.test \n" + "import " + Person.class.getCanonicalName() + "\n" + "import " + Address.class.getCanonicalName() + "\n" + "global java.util.List list \n" + "rule \"show\" \n" + "when \n" + " $m : Person( address == new Address('s1')) \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.setAddress(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;
}
}
final AlphaNode alphanode = (AlphaNode) node.getObjectSinkPropagator().getSinks()[0];
final AlphaNodeFieldConstraint constraint = alphanode.getConstraint();
if (constraint instanceof MvelConstraint) {
assertTrue(((MvelConstraint) constraint).getFieldExtractor() instanceof ClassFieldReader);
final FieldValue r = ((MvelConstraint) constraint).getField();
assertEquals(p.getAddress(), r.getValue());
}
}
use of org.drools.core.reteoo.ObjectTypeNode in project drools by kiegroup.
the class MemoryLeakTest method testStagedTupleLeak.
@Test
public void testStagedTupleLeak() throws Exception {
// BZ-1056599
String str = "rule R1 when\n" + " $i : Integer()\n" + "then\n" + " insertLogical( $i.toString() );\n" + "end\n" + "\n" + "rule R2 when\n" + " $i : Integer()\n" + "then\n" + " delete( $i );\n" + "end\n" + "\n" + "rule R3 when\n" + " $l : Long()\n" + " $s : String( this == $l.toString() )\n" + "then\n" + "end\n";
KieBase kbase = new KieHelper().addContent(str, ResourceType.DRL).build();
KieSession ksession = kbase.newKieSession();
for (int i = 0; i < 10; i++) {
ksession.insert(i);
ksession.fireAllRules();
}
Rete rete = ((KnowledgeBaseImpl) kbase).getRete();
JoinNode joinNode = null;
for (ObjectTypeNode otn : rete.getObjectTypeNodes()) {
if (String.class == otn.getObjectType().getValueType().getClassType()) {
joinNode = (JoinNode) otn.getObjectSinkPropagator().getSinks()[0];
break;
}
}
assertNotNull(joinNode);
InternalWorkingMemory wm = (InternalWorkingMemory) ksession;
BetaMemory memory = (BetaMemory) wm.getNodeMemory(joinNode);
TupleSets<RightTuple> stagedRightTuples = memory.getStagedRightTuples();
assertNull(stagedRightTuples.getDeleteFirst());
assertNull(stagedRightTuples.getInsertFirst());
}
use of org.drools.core.reteoo.ObjectTypeNode in project drools by kiegroup.
the class CepEspTest method testEventExpiration2.
@Test(timeout = 10000)
public void testEventExpiration2() throws Exception {
// read in the source
KieBaseConfiguration kbc = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
kbc.setOption(EventProcessingOption.STREAM);
KieBase kbase = loadKnowledgeBase(kbc, "test_CEP_EventExpiration2.drl");
Map<ObjectType, ObjectTypeNode> objectTypeNodes = ((KnowledgeBaseImpl) kbase).getRete().getObjectTypeNodes(EntryPointId.DEFAULT);
ObjectTypeNode node = objectTypeNodes.get(new ClassObjectType(StockTick.class));
assertNotNull(node);
// the expiration policy @expires(10m) should override the temporal operator usage
assertEquals(TimeIntervalParser.parse("10m")[0] + 1, node.getExpirationOffset());
}
use of org.drools.core.reteoo.ObjectTypeNode in project drools by kiegroup.
the class AlphaNetworkModifyTest method testModifyWithLiaToAcc.
@Test
public void testModifyWithLiaToAcc() {
// technically you can't have a modify with InitialFactImpl
// But added test for completeness
String str = "";
str += "package org.simple \n";
str += "import " + Person.class.getCanonicalName() + "\n";
str += "import " + Cheese.class.getCanonicalName() + "\n";
str += "import " + Cat.class.getCanonicalName() + "\n";
str += "global java.util.List list \n";
str += "rule x1 \n";
str += "when \n";
str += " Object() from accumulate( $p : Person() and Cheese(), collectList( $p ) )\n";
str += " Person() \n";
str += "then \n";
str += "end \n";
str += "rule x2 \n";
str += "when \n";
str += " Object() from accumulate( $ch : Cheese(), collectList( $ch ) )\n";
str += " Person() \n";
str += "then \n";
str += "end \n";
str += "rule x3 \n";
str += "when \n";
str += " Object() from accumulate( $ch : Cheese(), collectList( $ch ) )\n";
str += " Person() \n";
str += "then \n";
str += "end \n";
str += "rule x4 \n";
str += "when \n";
str += " Object() from accumulate( $ch : Cheese(), collectList( $ch ) )\n";
str += " Person() \n";
str += "then \n";
str += "end \n";
KieBase kbase = loadKnowledgeBaseFromString(str);
KieSession wm = kbase.newKieSession();
wm.fireAllRules();
ObjectTypeNode otnInit = getObjectTypeNode(kbase, "InitialFactImpl");
LeftInputAdapterNode liaNode = (LeftInputAdapterNode) otnInit.getObjectSinkPropagator().getSinks()[0];
LeftTupleSink[] sinks = liaNode.getSinkPropagator().getSinks();
assertEquals(0, sinks[0].getLeftInputOtnId().getId());
assertEquals(1, sinks[1].getLeftInputOtnId().getId());
assertEquals(2, sinks[2].getLeftInputOtnId().getId());
ObjectTypeNode otnPerson = getObjectTypeNode(kbase, "Person");
ObjectTypeNode otnCheese = getObjectTypeNode(kbase, "Cheese");
assertEquals(0, otnPerson.getOtnIdCounter());
assertEquals(0, otnCheese.getOtnIdCounter());
wm.insert(new Person());
wm.insert(new Cheese());
wm.fireAllRules();
assertEquals(3, otnPerson.getOtnIdCounter());
assertEquals(2, otnCheese.getOtnIdCounter());
}
use of org.drools.core.reteoo.ObjectTypeNode in project drools by kiegroup.
the class PatternBuilder method attachObjectTypeNode.
private void attachObjectTypeNode(final BuildContext context, final BuildUtils utils, final Pattern pattern) {
boolean objectMemory = context.isObjectTypeNodeMemoryEnabled();
ObjectType objectType = pattern.getObjectType();
if (pattern.getObjectType() instanceof ClassObjectType) {
// Is this the query node, if so we don't want any memory
if (DroolsQuery.class == ((ClassObjectType) pattern.getObjectType()).getClassType()) {
context.setTupleMemoryEnabled(false);
context.setObjectTypeNodeMemoryEnabled(false);
}
}
ObjectTypeNode otn = context.getComponentFactory().getNodeFactoryService().buildObjectTypeNode(context.getNextId(), (EntryPointNode) context.getObjectSource(), objectType, context);
if (objectType.isEvent() && EventProcessingOption.STREAM.equals(context.getKnowledgeBase().getConfiguration().getEventProcessingMode())) {
ExpirationSpec expirationSpec = getExpirationForType(context, objectType);
if (expirationSpec.offset != NEVER_EXPIRES && expirationSpec.hard) {
// hard expiration is set, so use it
otn.setExpirationOffset(expirationSpec.offset);
} else {
// otherwise calculate it based on behaviours and temporal constraints
long offset = NEVER_EXPIRES;
for (Behavior behavior : pattern.getBehaviors()) {
if (behavior.getExpirationOffset() != NEVER_EXPIRES) {
offset = Math.max(behavior.getExpirationOffset(), offset);
}
}
// if there's no implicit expiration uses the (eventually set) soft one
if (offset == NEVER_EXPIRES && !expirationSpec.hard) {
offset = expirationSpec.offset;
}
long distance = context.getExpirationOffset(pattern);
if (distance == NEVER_EXPIRES) {
// it means the rules have no temporal constraints, or
// the constraints require events to be hold forever. In this
// case, we allow type declarations to override the implicit
// expiration offset by defining an expiration policy with the
// @expires tag
otn.setExpirationOffset(offset);
} else {
otn.setExpirationOffset(Math.max(distance, offset));
}
}
}
context.setObjectSource(utils.attachNode(context, otn));
context.setObjectTypeNodeMemoryEnabled(objectMemory);
}
Aggregations