Search in sources :

Example 1 with RuleBase

use of org.drools.core.impl.RuleBase in project drools by kiegroup.

the class IncrementalCompilationTest method testRuleRemoval.

@Test
public void testRuleRemoval() {
    final String drl1 = "package org.drools.compiler\n" + "import " + Message.class.getCanonicalName() + ";\n" + "rule R1 when\n" + "   $m : Message()\n" + "then\n" + "end\n";
    final String drl2 = "rule R2 when\n" + "   $m : Message( message == \"Hi Universe\" )\n" + "then\n" + "end\n";
    final String drl3 = "rule R3 when\n" + "   $m : Message( message == \"Hello World\" )\n" + "then\n" + "end\n";
    final KieServices ks = KieServices.Factory.get();
    // Create an in-memory jar for version 1.0.0
    final ReleaseId releaseId1 = ks.newReleaseId("org.kie", "test-upgrade", "1.0.0");
    KieUtil.getKieModuleFromDrls(releaseId1, kieBaseTestConfiguration, drl1 + drl2 + drl3);
    // Create a session and fire rules
    final KieContainer kc = ks.newKieContainer(releaseId1);
    KiePackage kpkg = kc.getKieBase().getKiePackage("org.drools.compiler");
    assertEquals(3, kpkg.getRules().size());
    Map<String, Rule> rules = rulestoMap(kpkg.getRules());
    assertNotNull(rules.get("R1"));
    assertNotNull(rules.get("R2"));
    assertNotNull(rules.get("R3"));
    final RuleTerminalNode rtn1_1 = (RuleTerminalNode) ((RuleBase) kc.getKieBase()).getReteooBuilder().getTerminalNodes("org.drools.compiler.R1")[0];
    final RuleTerminalNode rtn3_1 = (RuleTerminalNode) ((RuleBase) kc.getKieBase()).getReteooBuilder().getTerminalNodes("org.drools.compiler.R3")[0];
    // Create a new jar for version 1.1.0
    final ReleaseId releaseId2 = ks.newReleaseId("org.kie", "test-upgrade", "1.1.0");
    KieUtil.getKieModuleFromDrls(releaseId2, kieBaseTestConfiguration, drl1 + drl3);
    // try to update the container to version 1.1.0
    kc.updateToVersion(releaseId2);
    final RuleBase rb_2 = ((RuleBase) kc.getKieBase());
    final RuleTerminalNode rtn1_2 = (RuleTerminalNode) rb_2.getReteooBuilder().getTerminalNodes("org.drools.compiler.R1")[0];
    final RuleTerminalNode rtn3_2 = (RuleTerminalNode) rb_2.getReteooBuilder().getTerminalNodes("org.drools.compiler.R3")[0];
    assertNull(rb_2.getReteooBuilder().getTerminalNodes("org.drools.compiler.R2"));
    assertSame(rtn3_1, rtn3_2);
    assertSame(rtn1_1, rtn1_2);
    kpkg = kc.getKieBase().getKiePackage("org.drools.compiler");
    assertEquals(2, kpkg.getRules().size());
    rules = rulestoMap(kpkg.getRules());
    assertNotNull(rules.get("R1"));
    assertNull(rules.get("R2"));
    assertNotNull(rules.get("R3"));
}
Also used : KiePackage(org.kie.api.definition.KiePackage) KieServices(org.kie.api.KieServices) ReleaseId(org.kie.api.builder.ReleaseId) Rule(org.kie.api.definition.rule.Rule) RuleBase(org.drools.core.impl.RuleBase) KieContainer(org.kie.api.runtime.KieContainer) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode) Test(org.junit.Test)

Example 2 with RuleBase

use of org.drools.core.impl.RuleBase 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 = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, str);
    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 = ((RuleBase) 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) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) RuleBase(org.drools.core.impl.RuleBase) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode) Test(org.junit.Test)

Example 3 with RuleBase

use of org.drools.core.impl.RuleBase in project drools by kiegroup.

the class NodePositionInPathTest method test.

@Test
public void test() {
    String drl = "rule R1 when\n" + "    Integer()\n" + "    exists( Integer() and Integer() )\n" + "    String()\n" + "then\n" + "end\n" + "rule R2 when\n" + "    Integer()\n" + "    exists( Integer() and String() )\n" + "then\n" + "end\n";
    KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, drl);
    ReteDumper.dumpRete(((RuleBase) kbase));
    Rete rete = ((RuleBase) kbase).getRete();
    LeftInputAdapterNode liaNode = null;
    for (ObjectTypeNode otn : rete.getObjectTypeNodes()) {
        Class<?> otnType = ((ClassObjectType) otn.getObjectType()).getClassType();
        if (Integer.class == otnType) {
            liaNode = (LeftInputAdapterNode) otn.getObjectSinkPropagator().getSinks()[0];
        }
    }
    assertEquals(0, liaNode.getPathIndex());
    LeftTupleSink[] liaSinks = liaNode.getSinkPropagator().getSinks();
    BetaNode join1 = (BetaNode) liaSinks[0];
    assertEquals(1, join1.getPathIndex());
    ExistsNode ex1 = (ExistsNode) liaSinks[1];
    assertEquals(1, ex1.getPathIndex());
    BetaNode join2 = (BetaNode) ex1.getSinkPropagator().getSinks()[0];
    assertEquals(2, join2.getPathIndex());
    RuleTerminalNode rtn1 = (RuleTerminalNode) join2.getSinkPropagator().getSinks()[0];
    assertEquals(3, rtn1.getPathIndex());
    ExistsNode ex2 = (ExistsNode) liaSinks[2];
    assertEquals(1, ex2.getPathIndex());
    RuleTerminalNode rtn2 = (RuleTerminalNode) ex2.getSinkPropagator().getSinks()[0];
    assertEquals(2, rtn2.getPathIndex());
    BetaNode join3 = (BetaNode) join1.getSinkPropagator().getSinks()[0];
    assertEquals(2, join3.getPathIndex());
    RightInputAdapterNode ria1 = (RightInputAdapterNode) join3.getSinkPropagator().getSinks()[0];
    assertEquals(3, ria1.getPathIndex());
    BetaNode join4 = (BetaNode) join1.getSinkPropagator().getSinks()[1];
    assertEquals(2, join4.getPathIndex());
    RightInputAdapterNode ria2 = (RightInputAdapterNode) join4.getSinkPropagator().getSinks()[0];
    assertEquals(3, ria2.getPathIndex());
    LeftTupleNode[] rtn1PathNodes = rtn1.getPathNodes();
    assertEquals(4, rtn1PathNodes.length);
    checkNodePosition(rtn1PathNodes, liaNode);
    checkNodePosition(rtn1PathNodes, ex1);
    checkNodePosition(rtn1PathNodes, join2);
    checkNodePosition(rtn1PathNodes, rtn1);
    LeftTupleNode[] rtn2PathNodes = rtn2.getPathNodes();
    assertEquals(3, rtn2PathNodes.length);
    checkNodePosition(rtn2PathNodes, liaNode);
    checkNodePosition(rtn2PathNodes, ex2);
    checkNodePosition(rtn2PathNodes, rtn2);
    LeftTupleNode[] ria1PathNodes = ria1.getPathNodes();
    assertEquals(4, ria1PathNodes.length);
    checkNodePosition(ria1PathNodes, liaNode);
    checkNodePosition(ria1PathNodes, join1);
    checkNodePosition(ria1PathNodes, join3);
    checkNodePosition(ria1PathNodes, ria1);
    LeftTupleNode[] ria2PathNodes = ria2.getPathNodes();
    assertEquals(4, ria2PathNodes.length);
    checkNodePosition(ria2PathNodes, liaNode);
    checkNodePosition(ria2PathNodes, join1);
    checkNodePosition(ria2PathNodes, join4);
    checkNodePosition(ria2PathNodes, ria2);
}
Also used : Rete(org.drools.core.reteoo.Rete) BetaNode(org.drools.core.reteoo.BetaNode) ClassObjectType(org.drools.core.base.ClassObjectType) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) LeftTupleSink(org.drools.core.reteoo.LeftTupleSink) ExistsNode(org.drools.core.reteoo.ExistsNode) KieBase(org.kie.api.KieBase) LeftTupleNode(org.drools.core.reteoo.LeftTupleNode) RuleBase(org.drools.core.impl.RuleBase) RightInputAdapterNode(org.drools.core.reteoo.RightInputAdapterNode) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode) Test(org.junit.Test)

Example 4 with RuleBase

use of org.drools.core.impl.RuleBase in project drools by kiegroup.

the class PatternTest method testSelfJoinAndNotWithIndex.

@Test
public void testSelfJoinAndNotWithIndex() {
    final String drl = "package org.drools.compiler.integrationtests.drl;\n" + "import " + Person.class.getCanonicalName() + ";\n" + "global java.util.List list\n" + "rule test1\n" + "when\n" + "   $p1 : Person( )\n" + "     not Person( name == $p1.name, age < $p1.age )\n" + "   $p2 : Person( name == $p1.name, likes != $p1.likes, age > $p1.age)\n" + "     not Person( name == $p1.name, likes == $p2.likes, age < $p2.age )\n" + "then\n" + "    System.out.println( $p1 + \":\" + $p2 );\n" + "    list.add( $p1 );\n" + "    list.add( $p2 );\n" + "end\n";
    final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("pattern-test", kieBaseTestConfiguration, drl);
    final KieSession ksession = kbase.newKieSession();
    try {
        final List list = new ArrayList();
        ksession.setGlobal("list", list);
        final Person p0 = new Person("yoda", 0);
        p0.setLikes("cheddar");
        ksession.insert(p0);
        final Person p1 = new Person("darth", 15);
        p1.setLikes("cheddar");
        final FactHandle fh1 = ksession.insert(p1);
        final Person p2 = new Person("darth", 25);
        p2.setLikes("cheddar");
        // creates activation.
        ksession.insert(p2);
        final Person p3 = new Person("darth", 30);
        p3.setLikes("brie");
        ksession.insert(p3);
        ksession.fireAllRules();
        // selects p1 and p3
        if (((RuleBase) kbase).getConfiguration().getAssertBehaviour().equals(RuleBaseConfiguration.AssertBehaviour.IDENTITY)) {
            assertEquals(2, list.size());
            assertSame(p1, list.get(0));
            assertSame(p3, list.get(1));
            p1.setName("yoda");
            // creates activation
            ksession.update(fh1, p1);
            ksession.fireAllRules();
            // now selects p2 and p3
            assertEquals(4, list.size());
            assertSame(p2, list.get(2));
            assertSame(p3, list.get(3));
        } else {
            // Person has equals method based on the Person's name.
            // There are 3 Darths, so 2 of them are not inserted with EQUALITY.
            assertEquals(0, list.size());
            p1.setName("yoda");
            ksession.update(fh1, p1);
            ksession.fireAllRules();
            assertEquals(0, list.size());
        }
    } finally {
        ksession.dispose();
    }
}
Also used : InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) Person(org.drools.testcoverage.common.model.Person) RuleBase(org.drools.core.impl.RuleBase) Test(org.junit.Test)

Example 5 with RuleBase

use of org.drools.core.impl.RuleBase 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 = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, str);
    KieSession ksession = kbase.newKieSession();
    TruthMaintenanceSystemFactoryImpl tms = (TruthMaintenanceSystemFactoryImpl) TruthMaintenanceSystemFactory.get();
    tms.clearEntryPointsMap();
    for (int i = 0; i < 10; i++) {
        ksession.insert(i);
        ksession.fireAllRules();
    }
    Rete rete = ((RuleBase) 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());
    // DROOLS-6809
    assertEquals(1, tms.getEntryPointsMapSize());
    ksession.dispose();
    assertEquals(0, tms.getEntryPointsMapSize());
}
Also used : TruthMaintenanceSystemFactoryImpl(org.drools.tms.TruthMaintenanceSystemFactoryImpl) Rete(org.drools.core.reteoo.Rete) JoinNode(org.drools.core.reteoo.JoinNode) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) BetaMemory(org.drools.core.reteoo.BetaMemory) RightTuple(org.drools.core.reteoo.RightTuple) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) RuleBase(org.drools.core.impl.RuleBase) Test(org.junit.Test)

Aggregations

RuleBase (org.drools.core.impl.RuleBase)21 Test (org.junit.Test)16 KieBase (org.kie.api.KieBase)9 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)6 KieSession (org.kie.api.runtime.KieSession)6 Rete (org.drools.core.reteoo.Rete)5 LeftInputAdapterNode (org.drools.core.reteoo.LeftInputAdapterNode)4 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)3 BuildContext (org.drools.core.reteoo.builder.BuildContext)3 FactHandle (org.kie.api.runtime.rule.FactHandle)3 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 ClassObjectType (org.drools.core.base.ClassObjectType)2 AlphaNode (org.drools.core.reteoo.AlphaNode)2 LeftTupleSink (org.drools.core.reteoo.LeftTupleSink)2 RuleTerminalNode (org.drools.core.reteoo.RuleTerminalNode)2 IndexableConstraint (org.drools.core.rule.IndexableConstraint)2 KieBaseConfiguration (org.kie.api.KieBaseConfiguration)2 ReleaseId (org.kie.api.builder.ReleaseId)2 KiePackage (org.kie.api.definition.KiePackage)2