use of org.drools.core.impl.KnowledgeBaseImpl in project drools by kiegroup.
the class PassiveQueryTest method testPassiveQueryUsingSegmentPropagator.
@Test
public void testPassiveQueryUsingSegmentPropagator() throws Exception {
String str = "global java.util.List list\n" + "query Q (Integer i)\n" + " String( this == i.toString() )\n" + "end\n" + "rule R1a @Propagation(IMMEDIATE) when\n" + " $i : Integer()\n" + " ?Q( $i; )\n" + "then\n" + " list.add( \"R1a\" );\n" + "end\n" + "rule R1b @Propagation(IMMEDIATE) when\n" + " $i : Integer()\n" + " ?Q( $i; )\n" + " Long( intValue == $i )\n" + "then\n" + " list.add( \"R1b\" );\n" + "end\n" + "rule R2 when\n" + " $i : Integer()\n" + " ?Q( $i; )\n" + "then\n" + " list.add( \"R2\" );\n" + "end\n";
KieBase kbase = new KieHelper().addContent(str, ResourceType.DRL).build();
KieSession ksession = kbase.newKieSession();
List<String> list = new ArrayList<String>();
ksession.setGlobal("list", list);
ksession.insert(1L);
FactHandle fh = ksession.insert(1);
ksession.insert("1");
Rete rete = ((KnowledgeBaseImpl) kbase).getRete();
LeftInputAdapterNode lia = null;
for (ObjectTypeNode otn : rete.getObjectTypeNodes()) {
if (Integer.class == otn.getObjectType().getValueType().getClassType()) {
lia = (LeftInputAdapterNode) otn.getObjectSinkPropagator().getSinks()[0];
break;
}
}
LeftTupleSink[] sinks = lia.getSinkPropagator().getSinks();
QueryElementNode q1 = (QueryElementNode) sinks[0];
QueryElementNode q2 = (QueryElementNode) sinks[1];
InternalWorkingMemory wm = (InternalWorkingMemory) ksession;
wm.flushPropagations();
Memory memory1 = wm.getNodeMemory(q1);
assertTrue(memory1.getSegmentMemory().getStagedLeftTuples().isEmpty());
ksession.fireAllRules();
assertEquals(1, list.size());
assertEquals("R2", list.get(0));
list.clear();
ksession.delete(fh);
ksession.insert(1);
ksession.fireAllRules();
assertEquals(3, list.size());
}
use of org.drools.core.impl.KnowledgeBaseImpl in project drools by kiegroup.
the class Misc2Test method testKnowledgeBaseEventSupportLeak.
@Test
public void testKnowledgeBaseEventSupportLeak() throws Exception {
// JBRULES-3666
KieBase kbase = getKnowledgeBase();
KieBaseEventListener listener = new DefaultKieBaseEventListener();
kbase.addEventListener(listener);
kbase.addEventListener(listener);
assertEquals(1, ((KnowledgeBaseImpl) kbase).getKieBaseEventListeners().size());
kbase.removeEventListener(listener);
assertEquals(0, ((KnowledgeBaseImpl) kbase).getKieBaseEventListeners().size());
}
use of org.drools.core.impl.KnowledgeBaseImpl in project drools by kiegroup.
the class ScenarioRunnerTest method testIntegrationWithDeclaredTypes.
@Test
public void testIntegrationWithDeclaredTypes() throws Exception {
Scenario scenario = new Scenario();
scenario.getImports().addImport(new Import("foo.bar.Coolness"));
FactData[] facts = new FactData[] { new FactData("Coolness", "c", Arrays.<Field>asList(new FieldData("num", "42"), new FieldData("name", "mic")), false) };
scenario.getFixtures().addAll(Arrays.asList(facts));
ExecutionTrace executionTrace = new ExecutionTrace();
scenario.getRules().add("rule1");
scenario.setInclusive(true);
scenario.getFixtures().add(executionTrace);
Expectation[] assertions = new Expectation[2];
assertions[0] = new VerifyFact("c", ls(new VerifyField("num", "42", "==")));
assertions[1] = new VerifyRuleFired("rule1", 1, null);
scenario.getFixtures().addAll(Arrays.asList(assertions));
KieSession ksession = getKieSession("test_rules3.drl");
ClassLoader cl = ((KnowledgeBaseImpl) ksession.getKieBase()).getRootClassLoader();
HashSet<String> imports = new HashSet<String>();
imports.add("foo.bar.*");
assertNotNull(cl.loadClass("foo.bar.Coolness"));
ClassLoader cl_ = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(cl);
// resolver will need to have generated beans in it - possibly using a composite classloader from the package,
// including whatever CL has the generated beans...
ScenarioRunner run = new ScenarioRunner(ksession);
run.run(scenario);
assertEquals(1, executionTrace.getNumberOfRulesFired().intValue());
assertTrue(scenario.wasSuccessful());
Thread.currentThread().setContextClassLoader(cl_);
}
use of org.drools.core.impl.KnowledgeBaseImpl in project drools by kiegroup.
the class CommonTestMethodBase method loadKnowledgeBaseFromString.
protected KieBase loadKnowledgeBaseFromString(KnowledgeBuilderConfiguration config, KieBaseConfiguration kBaseConfig, NodeFactory nodeFactory, String... drlContentStrings) {
KnowledgeBuilder kbuilder = config == null ? KnowledgeBuilderFactory.newKnowledgeBuilder() : KnowledgeBuilderFactory.newKnowledgeBuilder(config);
for (String drlContentString : drlContentStrings) {
kbuilder.add(ResourceFactory.newByteArrayResource(drlContentString.getBytes()), ResourceType.DRL);
}
if (kbuilder.hasErrors()) {
fail(kbuilder.getErrors().toString());
}
if (kBaseConfig == null) {
kBaseConfig = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
}
InternalKnowledgeBase kbase = kBaseConfig == null ? KnowledgeBaseFactory.newKnowledgeBase() : KnowledgeBaseFactory.newKnowledgeBase(kBaseConfig);
if (nodeFactory != null) {
((KnowledgeBaseImpl) kbase).getConfiguration().getComponentFactory().setNodeFactoryProvider(nodeFactory);
}
kbase.addPackages(kbuilder.getKnowledgePackages());
return kbase;
}
use of org.drools.core.impl.KnowledgeBaseImpl in project drools by kiegroup.
the class MemoryLeakTest method testStagedLeftTupleLeak.
@Test
public void testStagedLeftTupleLeak() throws Exception {
// BZ-1058874
String str = "rule R1 when\n" + " String( this == \"this\" )\n" + " String( this == \"that\" )\n" + "then\n" + "end\n";
KieBase kbase = new KieHelper().addContent(str, ResourceType.DRL).build();
KieSession ksession = kbase.newKieSession();
ksession.fireAllRules();
for (int i = 0; i < 10; i++) {
FactHandle fh = ksession.insert("this");
ksession.fireAllRules();
ksession.delete(fh);
ksession.fireAllRules();
}
Rete rete = ((KnowledgeBaseImpl) kbase).getRete();
LeftInputAdapterNode liaNode = null;
for (ObjectTypeNode otn : rete.getObjectTypeNodes()) {
if (String.class == otn.getObjectType().getValueType().getClassType()) {
AlphaNode alphaNode = (AlphaNode) otn.getObjectSinkPropagator().getSinks()[0];
liaNode = (LeftInputAdapterNode) alphaNode.getObjectSinkPropagator().getSinks()[0];
break;
}
}
assertNotNull(liaNode);
InternalWorkingMemory wm = (InternalWorkingMemory) ksession;
LeftInputAdapterNode.LiaNodeMemory memory = (LeftInputAdapterNode.LiaNodeMemory) wm.getNodeMemory(liaNode);
TupleSets<LeftTuple> stagedLeftTuples = memory.getSegmentMemory().getStagedLeftTuples();
assertNull(stagedLeftTuples.getDeleteFirst());
assertNull(stagedLeftTuples.getInsertFirst());
}
Aggregations