use of org.drools.kiesession.session.StatefulKnowledgeSessionImpl in project drools by kiegroup.
the class KieContainerImpl method createKieSessionsPool.
StatefulSessionPool createKieSessionsPool(String kSessionName, KieSessionConfiguration conf, Environment env, int initialSize, boolean stateless) {
KieSessionModel kSessionModel = kSessionName != null ? getKieSessionModel(kSessionName) : findKieSessionModel(false);
if (kSessionModel == null) {
log.error("Unknown KieSession name: " + kSessionName);
return null;
}
InternalKnowledgeBase kBase = (InternalKnowledgeBase) getKieBaseFromKieSessionModel(kSessionModel);
return kBase == null ? null : new StatefulSessionPool(kBase, initialSize, () -> {
SessionConfiguration sessConf = conf != null ? (SessionConfiguration) conf : kBase.getSessionConfiguration();
StatefulKnowledgeSessionImpl kSession = stateless ? ((StatefulKnowledgeSessionImpl) RuntimeComponentFactory.get().createStatefulSession(kBase, env, sessConf, false)).setStateless(true) : (StatefulKnowledgeSessionImpl) kBase.newKieSession(sessConf, env);
registerNewKieSession(kSessionModel, kBase, kSession);
return kSession;
});
}
use of org.drools.kiesession.session.StatefulKnowledgeSessionImpl in project drools by kiegroup.
the class FieldConstraintTest method testPredicateConstraint.
/**
* <pre>
*
* (Cheese (price ?price1 )
* (Cheese (price ?price2&:(= ?price2 (* 2 ?price1) )
*
* </pre>
*/
@Test
public void testPredicateConstraint() {
InternalKnowledgeBase kBase = KnowledgeBaseFactory.newKnowledgeBase();
;
StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) kBase.newKieSession();
final InternalReadAccessor priceExtractor = store.getReader(Cheese.class, "price");
Pattern pattern = new Pattern(0, new ClassObjectType(Cheese.class));
// Bind the extractor to a decleration
// Declarations know the pattern they derive their value form
final Declaration price1Declaration = new Declaration("price1", priceExtractor, pattern);
pattern = new Pattern(1, new ClassObjectType(Cheese.class));
// Bind the extractor to a decleration
// Declarations know the pattern they derive their value form
final Declaration price2Declaration = new Declaration("price2", priceExtractor, pattern);
final PredicateExpression evaluator = new PredicateExpression() {
private static final long serialVersionUID = 510l;
public boolean evaluate(InternalFactHandle handle, Tuple tuple, Declaration[] previousDeclarations, Declaration[] localDeclarations, ReteEvaluator reteEvaluator, Object context) {
int price1 = previousDeclarations[0].getIntValue(reteEvaluator, tuple.getObject(previousDeclarations[0]));
int price2 = localDeclarations[0].getIntValue(reteEvaluator, handle.getObject());
return (price2 == (price1 * 2));
}
public Object createContext() {
return null;
}
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
}
public void writeExternal(ObjectOutput out) throws IOException {
}
};
final PredicateConstraint constraint1 = new PredicateConstraint(evaluator, new Declaration[] { price1Declaration }, new Declaration[] { price2Declaration });
final Cheese cheddar0 = new Cheese("cheddar", 5);
final InternalFactHandle f0 = (InternalFactHandle) ksession.insert(cheddar0);
LeftTupleImpl tuple = new LeftTupleImpl(f0, null, true);
final Cheese cheddar1 = new Cheese("cheddar", 10);
final InternalFactHandle f1 = (InternalFactHandle) ksession.insert(cheddar1);
tuple = new LeftTupleImpl(tuple, new RightTupleImpl(f1, null), null, true);
final PredicateContextEntry context = (PredicateContextEntry) constraint1.createContextEntry();
context.updateFromTuple(ksession, tuple);
assertTrue(constraint1.isAllowedCachedLeft(context, f1));
}
use of org.drools.kiesession.session.StatefulKnowledgeSessionImpl 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 = KnowledgeBaseFactory.newKnowledgeBase();
;
StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) kBase.newKieSession();
final ClassFieldReader extractor = store.getReader(Cheese.class, "type");
AlphaNodeFieldConstraint constraint = ConstraintTestUtil.createCheeseTypeEqualsConstraint(extractor, "cheddar", useLambdaConstraint);
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.kiesession.session.StatefulKnowledgeSessionImpl in project drools by kiegroup.
the class NotTest method getBlockerFactHandle.
private InternalFactHandle getBlockerFactHandle(KieSession ksession) {
ObjectTypeNode otn = getObjectTypeNode(ksession.getKieBase(), Person.class);
BetaNode notNode = (BetaNode) ((AlphaNode) otn.getSinks()[0]).getSinks()[0];
StatefulKnowledgeSessionImpl ksessionImpl = (StatefulKnowledgeSessionImpl) ksession;
NodeMemories nodeMemories = ksessionImpl.getNodeMemories();
BetaMemory betaMemory = (BetaMemory) nodeMemories.getNodeMemory(notNode, ksessionImpl);
TupleMemory rightTupleMemory = betaMemory.getRightTupleMemory();
Tuple[] tuples = (Tuple[]) rightTupleMemory.toArray();
for (int i = 0; i < tuples.length; i++) {
RightTupleImpl tuple = (RightTupleImpl) tuples[i];
if (tuple.getBlocked() != null) {
return tuple.getFactHandle();
}
}
fail("Cannot find blocker in BetaMemory");
return null;
}
use of org.drools.kiesession.session.StatefulKnowledgeSessionImpl in project drools by kiegroup.
the class SegmentMemoryPrototypeTest method testSessionReset.
@Test
public void testSessionReset() {
String str = "import " + Person.class.getCanonicalName() + "\n" + "import " + Address.class.getCanonicalName() + "\n" + "import java.util.List;\n" + "\n" + "rule R1 when\n" + " $i : Integer()\n" + " String( length == $i )\n" + " Long()\n" + "then end\n" + "rule R2 when\n" + " $i : Integer()\n" + " String( length == $i )\n" + " Boolean()\n" + "then end";
KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, str);
List<TerminalNode> terminalNodes = ReteDumper.collectRete(kbase).stream().filter(TerminalNode.class::isInstance).map(TerminalNode.class::cast).collect(toList());
StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) kbase.newKieSession();
ksession.insert(4);
ksession.insert(4L);
ksession.insert(true);
ksession.insert("test");
assertEquals(2, ksession.fireAllRules());
assertTrue(terminalNodes.stream().map(ksession::getNodeMemory).map(PathMemory.class::cast).allMatch(PathMemory::isRuleLinked));
ksession.reset();
assertFalse(terminalNodes.stream().map(ksession::getNodeMemory).map(PathMemory.class::cast).anyMatch(PathMemory::isRuleLinked));
ksession.insert(4);
ksession.insert(4L);
ksession.insert("test");
assertEquals(1, ksession.fireAllRules());
}
Aggregations