Search in sources :

Example 36 with KnowledgeBaseImpl

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());
}
Also used : Rete(org.drools.core.reteoo.Rete) FactHandle(org.kie.api.runtime.rule.FactHandle) Memory(org.drools.core.common.Memory) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) QueryElementNode(org.drools.core.reteoo.QueryElementNode) ArrayList(java.util.ArrayList) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) LeftTupleSink(org.drools.core.reteoo.LeftTupleSink) KieHelper(org.kie.internal.utils.KieHelper) KnowledgeBaseImpl(org.drools.core.impl.KnowledgeBaseImpl) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode) Test(org.junit.Test)

Example 37 with KnowledgeBaseImpl

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());
}
Also used : DefaultKieBaseEventListener(org.kie.api.event.kiebase.DefaultKieBaseEventListener) KieBase(org.kie.api.KieBase) KieBaseEventListener(org.kie.api.event.kiebase.KieBaseEventListener) DefaultKieBaseEventListener(org.kie.api.event.kiebase.DefaultKieBaseEventListener) KnowledgeBaseImpl(org.drools.core.impl.KnowledgeBaseImpl) Test(org.junit.Test)

Example 38 with KnowledgeBaseImpl

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_);
}
Also used : Import(org.kie.soup.project.datamodel.imports.Import) VerifyRuleFired(org.drools.workbench.models.testscenarios.shared.VerifyRuleFired) VerifyField(org.drools.workbench.models.testscenarios.shared.VerifyField) ExecutionTrace(org.drools.workbench.models.testscenarios.shared.ExecutionTrace) KnowledgeBaseImpl(org.drools.core.impl.KnowledgeBaseImpl) Scenario(org.drools.workbench.models.testscenarios.shared.Scenario) FieldData(org.drools.workbench.models.testscenarios.shared.FieldData) FactData(org.drools.workbench.models.testscenarios.shared.FactData) ProjectClassLoader(org.drools.core.common.ProjectClassLoader) KieSession(org.kie.api.runtime.KieSession) Expectation(org.drools.workbench.models.testscenarios.shared.Expectation) VerifyFact(org.drools.workbench.models.testscenarios.shared.VerifyFact) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 39 with KnowledgeBaseImpl

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;
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) KnowledgeBaseImpl(org.drools.core.impl.KnowledgeBaseImpl) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase)

Example 40 with KnowledgeBaseImpl

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());
}
Also used : Rete(org.drools.core.reteoo.Rete) FactHandle(org.kie.api.runtime.rule.FactHandle) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) KieHelper(org.kie.internal.utils.KieHelper) KnowledgeBaseImpl(org.drools.core.impl.KnowledgeBaseImpl) AlphaNode(org.drools.core.reteoo.AlphaNode) LeftTuple(org.drools.core.reteoo.LeftTuple) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode) Test(org.junit.Test)

Aggregations

KnowledgeBaseImpl (org.drools.core.impl.KnowledgeBaseImpl)45 Test (org.junit.Test)31 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)12 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)11 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)11 ByteArrayResource (org.drools.core.io.impl.ByteArrayResource)10 Resource (org.kie.api.io.Resource)10 LogicalTypeInconsistencyException (org.drools.core.factmodel.traits.LogicalTypeInconsistencyException)9 TraitFactory (org.drools.core.factmodel.traits.TraitFactory)9 TraitProxy (org.drools.core.factmodel.traits.TraitProxy)9 TraitableBean (org.drools.core.factmodel.traits.TraitableBean)9 ClassPathResource (org.drools.core.io.impl.ClassPathResource)9 KieBase (org.kie.api.KieBase)9 FactType (org.kie.api.definition.type.FactType)9 RuleTerminalNode (org.drools.core.reteoo.RuleTerminalNode)8 KieSession (org.kie.api.runtime.KieSession)8 RuleBaseConfiguration (org.drools.core.RuleBaseConfiguration)6 WorkingMemory (org.drools.core.WorkingMemory)6 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)6 KieHelper (org.kie.internal.utils.KieHelper)6