Search in sources :

Example 21 with InternalWorkingMemory

use of org.drools.core.common.InternalWorkingMemory in project drools by kiegroup.

the class KieBaseUpdater method run.

@Override
public void run() {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(ctx.kBase, ctx.newKM.getBuilderConfiguration(ctx.newKieBaseModel));
    KnowledgeBuilderImpl pkgbuilder = (KnowledgeBuilderImpl) kbuilder;
    CompositeKnowledgeBuilder ckbuilder = kbuilder.batch();
    boolean shouldRebuild = applyResourceChanges(pkgbuilder, ckbuilder);
    // remove resources first
    for (ResourceChangeSet rcs : ctx.cs.getChanges().values()) {
        if (rcs.getChangeType() == ChangeType.REMOVED) {
            String resourceName = rcs.getResourceName();
            if (!resourceName.endsWith(".properties") && isFileInKBase(ctx.currentKM, ctx.currentKieBaseModel, resourceName)) {
                pkgbuilder.removeObjectsGeneratedFromResource(ctx.currentKM.getResource(resourceName));
            }
        }
    }
    // remove all ObjectTypeNodes for the modified classes
    if (ctx.modifyingUsedClass) {
        for (Class<?> cls : ctx.modifiedClasses) {
            clearInstancesOfModifiedClass(cls);
        }
    }
    if (shouldRebuild) {
        // readd unchanged dsl files to the kbuilder
        for (String dslFile : ctx.unchangedResources) {
            if (isFileInKBase(ctx.newKM, ctx.newKieBaseModel, dslFile)) {
                ctx.newKM.addResourceToCompiler(ckbuilder, ctx.newKieBaseModel, dslFile);
            }
        }
        rebuildAll(pkgbuilder, ckbuilder);
    }
    ctx.kBase.setResolvedReleaseId(ctx.newReleaseId);
    for (InternalWorkingMemory wm : ctx.kBase.getWorkingMemories()) {
        wm.notifyWaitOnRest();
    }
}
Also used : InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) CompositeKnowledgeBuilder(org.kie.internal.builder.CompositeKnowledgeBuilder) CompositeKnowledgeBuilder(org.kie.internal.builder.CompositeKnowledgeBuilder) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) ResourceChangeSet(org.kie.internal.builder.ResourceChangeSet)

Example 22 with InternalWorkingMemory

use of org.drools.core.common.InternalWorkingMemory in project drools by kiegroup.

the class AddRuleTest method testPopulatedSharedLiaNode.

@Test
public void testPopulatedSharedLiaNode() throws Exception {
    InternalKnowledgeBase kbase1 = buildKnowledgeBase("r1", "   A() B(1;) C() D() E()\n");
    InternalWorkingMemory wm = ((InternalWorkingMemory) kbase1.newKieSession());
    List list = new ArrayList();
    wm.setGlobal("list", list);
    wm.insert(new A(1));
    wm.insert(new A(2));
    wm.insert(new A(3));
    wm.insert(new B(1));
    wm.insert(new B(2));
    wm.insert(new C(1));
    wm.insert(new D(1));
    wm.insert(new E(1));
    wm.fireAllRules();
    assertEquals(3, list.size());
    kbase1.addPackages(buildKnowledgePackage("r2", "   a : A() B(2;) C() D() E()\n"));
    ObjectTypeNode aotn = getObjectTypeNode(kbase1, A.class);
    LeftInputAdapterNode liaNode = (LeftInputAdapterNode) aotn.getObjectSinkPropagator().getSinks()[0];
    JoinNode bNode1 = (JoinNode) liaNode.getSinkPropagator().getFirstLeftTupleSink();
    JoinNode bNode2 = (JoinNode) liaNode.getSinkPropagator().getLastLeftTupleSink();
    BetaMemory bm = (BetaMemory) wm.getNodeMemory(bNode2);
    SegmentMemory sm = bm.getSegmentMemory();
    assertNotNull(sm.getStagedLeftTuples().getInsertFirst());
    assertNotNull(sm.getStagedLeftTuples().getInsertFirst().getStagedNext());
    assertNotNull(sm.getStagedLeftTuples().getInsertFirst().getStagedNext().getStagedNext());
    assertNull(sm.getStagedLeftTuples().getInsertFirst().getStagedNext().getStagedNext().getStagedNext());
    wm.fireAllRules();
    assertNull(sm.getStagedLeftTuples().getInsertFirst());
    assertEquals(6, list.size());
    assertEquals("r1", ((Match) list.get(0)).getRule().getName());
    assertEquals("r1", ((Match) list.get(1)).getRule().getName());
    assertEquals("r1", ((Match) list.get(2)).getRule().getName());
    assertEquals("r2", ((Match) list.get(3)).getRule().getName());
    assertEquals("r2", ((Match) list.get(4)).getRule().getName());
    assertEquals("r2", ((Match) list.get(5)).getRule().getName());
    List results = new ArrayList();
    results.add(((A) ((Match) list.get(3)).getDeclarationValue("a")).getObject());
    results.add(((A) ((Match) list.get(4)).getDeclarationValue("a")).getObject());
    results.add(((A) ((Match) list.get(5)).getDeclarationValue("a")).getObject());
    assertTrue(results.containsAll(asList(1, 2, 3)));
}
Also used : SegmentMemory(org.drools.core.reteoo.SegmentMemory) JoinNode(org.drools.core.reteoo.JoinNode) ArrayList(java.util.ArrayList) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) BetaMemory(org.drools.core.reteoo.BetaMemory) Match(org.kie.api.runtime.rule.Match) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) ArrayList(java.util.ArrayList) Arrays.asList(java.util.Arrays.asList) List(java.util.List) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode) Test(org.junit.Test)

Example 23 with InternalWorkingMemory

use of org.drools.core.common.InternalWorkingMemory in project drools by kiegroup.

the class AddRuleTest method testPopulatedMultipleShares.

@Test
public void testPopulatedMultipleShares() throws Exception {
    InternalKnowledgeBase kbase1 = buildKnowledgeBase("r1", "   A(1;)  A(2;) B(1;) B(2;) C(1;) D() E()\n");
    InternalWorkingMemory wm = ((InternalWorkingMemory) kbase1.newKieSession());
    List list = new ArrayList();
    wm.setGlobal("list", list);
    wm.insert(new A(1));
    wm.insert(new A(2));
    wm.insert(new A(2));
    wm.insert(new A(3));
    wm.insert(new B(1));
    wm.insert(new B(2));
    wm.insert(new C(1));
    wm.insert(new C(2));
    wm.insert(new D(1));
    wm.insert(new E(1));
    wm.fireAllRules();
    assertEquals(2, list.size());
    kbase1.addPackages(buildKnowledgePackage("r2", "   A(1;)  A(2;) B(1;) B(2;) C(2;) D() E()\n"));
    kbase1.addPackages(buildKnowledgePackage("r3", "   A(1;)  A(3;) B(1;) B(2;) C(2;) D() E()\n"));
    wm.fireAllRules();
    System.out.println(list);
    assertEquals(5, list.size());
    assertEquals("r1", ((Match) list.get(0)).getRule().getName());
    assertEquals("r1", ((Match) list.get(1)).getRule().getName());
    // only one A3
    assertEquals("r3", ((Match) list.get(2)).getRule().getName());
    assertEquals("r2", ((Match) list.get(3)).getRule().getName());
    assertEquals("r2", ((Match) list.get(4)).getRule().getName());
}
Also used : InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) Arrays.asList(java.util.Arrays.asList) List(java.util.List) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Match(org.kie.api.runtime.rule.Match) Test(org.junit.Test)

Example 24 with InternalWorkingMemory

use of org.drools.core.common.InternalWorkingMemory in project drools by kiegroup.

the class AddRuleTest method testSplitTwoBeforeCreatedSegment.

@Test
public void testSplitTwoBeforeCreatedSegment() throws Exception {
    InternalKnowledgeBase kbase1 = buildKnowledgeBase("r1", "   A(1;)  A(2;) B(1;) B(2;) C(1;) C(2;) D(1;) D(2;) E(1;) E(2;)\n");
    kbase1.addPackages(buildKnowledgePackage("r2", "   A(1;)  A(2;) B(1;) B(2;) C(1;) C(2;) D(1;) D(2;) E(1;) E(2;)\n"));
    kbase1.addPackages(buildKnowledgePackage("r3", "   A(1;)  A(2;) B(1;) B(2;) C(1;) C(2;) D(1;) D(2;)\n"));
    kbase1.addPackages(buildKnowledgePackage("r4", "   A(1;)  A(2;) B(1;) B(2;) C(1;) C(2;) \n"));
    InternalWorkingMemory wm = ((InternalWorkingMemory) kbase1.newKieSession());
    List list = new ArrayList();
    wm.setGlobal("list", list);
    wm.insert(new E(1));
    wm.insert(new E(2));
    wm.flushPropagations();
    RuleTerminalNode rtn1 = getRtn("org.kie.r1", kbase1);
    RuleTerminalNode rtn2 = getRtn("org.kie.r2", kbase1);
    RuleTerminalNode rtn3 = getRtn("org.kie.r3", kbase1);
    RuleTerminalNode rtn4 = getRtn("org.kie.r4", kbase1);
    PathMemory pm1 = (PathMemory) wm.getNodeMemory(rtn1);
    SegmentMemory[] smems = pm1.getSegmentMemories();
    assertEquals(4, smems.length);
    assertNull(smems[0]);
    assertNull(smems[1]);
    assertNull(smems[3]);
    SegmentMemory sm = smems[2];
    assertEquals(2, sm.getPos());
    assertEquals(4, sm.getSegmentPosMaskBit());
    assertEquals(4, pm1.getLinkedSegmentMask());
    kbase1.addPackages(buildKnowledgePackage("r5", "   A(1;)  A(2;) B(1;) B(2;) \n"));
    smems = pm1.getSegmentMemories();
    assertEquals(5, smems.length);
    assertNull(smems[0]);
    assertNull(smems[1]);
    assertNull(smems[2]);
    sm = smems[3];
    assertEquals(3, sm.getPos());
    assertEquals(8, sm.getSegmentPosMaskBit());
    assertEquals(8, pm1.getLinkedSegmentMask());
    RuleTerminalNode rtn5 = getRtn("org.kie.r5", kbase1);
    PathMemory pm5 = (PathMemory) wm.getNodeMemory(rtn5);
    smems = pm5.getSegmentMemories();
    assertEquals(2, smems.length);
    assertNull(smems[0]);
    assertNull(smems[1]);
}
Also used : InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) SegmentMemory(org.drools.core.reteoo.SegmentMemory) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) Arrays.asList(java.util.Arrays.asList) List(java.util.List) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode) PathMemory(org.drools.core.reteoo.PathMemory) Test(org.junit.Test)

Example 25 with InternalWorkingMemory

use of org.drools.core.common.InternalWorkingMemory in project drools by kiegroup.

the class AddRuleTest method testPopulatedRuleWithEvals.

@Test
public void testPopulatedRuleWithEvals() throws Exception {
    InternalKnowledgeBase kbase1 = buildKnowledgeBase("r1", "   a:A() B() eval(1==1) eval(1==1) C(1;) \n");
    InternalWorkingMemory wm = ((InternalWorkingMemory) kbase1.newKieSession());
    List list = new ArrayList();
    wm.setGlobal("list", list);
    wm.insert(new A(1));
    wm.insert(new A(2));
    wm.insert(new A(3));
    wm.insert(new B(1));
    wm.insert(new C(1));
    wm.insert(new C(2));
    wm.fireAllRules();
    assertEquals(3, list.size());
    kbase1.addPackages(buildKnowledgePackage("r2", "   a:A() B() eval(1==1) eval(1==1) C(2;) \n"));
    ObjectTypeNode aotn = getObjectTypeNode(kbase1, A.class);
    LeftInputAdapterNode liaNode = (LeftInputAdapterNode) aotn.getObjectSinkPropagator().getSinks()[0];
    JoinNode bNode = (JoinNode) liaNode.getSinkPropagator().getFirstLeftTupleSink();
    EvalConditionNode e1 = (EvalConditionNode) bNode.getSinkPropagator().getFirstLeftTupleSink();
    EvalConditionNode e2 = (EvalConditionNode) e1.getSinkPropagator().getFirstLeftTupleSink();
    JoinNode c1Node = (JoinNode) e2.getSinkPropagator().getFirstLeftTupleSink();
    JoinNode c2Node = (JoinNode) e2.getSinkPropagator().getLastLeftTupleSink();
    LiaNodeMemory lm = (LiaNodeMemory) wm.getNodeMemory(liaNode);
    SegmentMemory sm = lm.getSegmentMemory();
    BetaMemory c1Mem = (BetaMemory) wm.getNodeMemory(c1Node);
    assertSame(sm.getFirst(), c1Mem.getSegmentMemory());
    assertEquals(3, c1Mem.getLeftTupleMemory().size());
    assertEquals(1, c1Mem.getRightTupleMemory().size());
    BetaMemory c2Mem = (BetaMemory) wm.getNodeMemory(c2Node);
    SegmentMemory c2Smem = sm.getFirst().getNext();
    assertSame(c2Smem, c2Mem.getSegmentMemory());
    assertEquals(0, c2Mem.getLeftTupleMemory().size());
    assertEquals(0, c2Mem.getRightTupleMemory().size());
    assertNotNull(c2Smem.getStagedLeftTuples().getInsertFirst());
    assertNotNull(c2Smem.getStagedLeftTuples().getInsertFirst().getStagedNext());
    assertNotNull(c2Smem.getStagedLeftTuples().getInsertFirst().getStagedNext().getStagedNext());
    assertNull(c2Smem.getStagedLeftTuples().getInsertFirst().getStagedNext().getStagedNext().getStagedNext());
    wm.fireAllRules();
    assertEquals(3, c2Mem.getLeftTupleMemory().size());
    assertEquals(1, c2Mem.getRightTupleMemory().size());
    assertNull(c2Smem.getStagedLeftTuples().getInsertFirst());
    assertEquals(6, list.size());
    assertEquals("r1", ((Match) list.get(0)).getRule().getName());
    assertEquals("r1", ((Match) list.get(1)).getRule().getName());
    assertEquals("r1", ((Match) list.get(2)).getRule().getName());
    assertEquals("r2", ((Match) list.get(3)).getRule().getName());
    assertEquals(3, ((A) ((Match) list.get(3)).getDeclarationValue("a")).getObject());
    assertEquals("r2", ((Match) list.get(4)).getRule().getName());
    assertEquals(2, ((A) ((Match) list.get(4)).getDeclarationValue("a")).getObject());
    assertEquals("r2", ((Match) list.get(5)).getRule().getName());
    assertEquals(1, ((A) ((Match) list.get(5)).getDeclarationValue("a")).getObject());
}
Also used : SegmentMemory(org.drools.core.reteoo.SegmentMemory) EvalConditionNode(org.drools.core.reteoo.EvalConditionNode) JoinNode(org.drools.core.reteoo.JoinNode) ArrayList(java.util.ArrayList) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) BetaMemory(org.drools.core.reteoo.BetaMemory) LiaNodeMemory(org.drools.core.reteoo.LeftInputAdapterNode.LiaNodeMemory) Match(org.kie.api.runtime.rule.Match) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) ArrayList(java.util.ArrayList) Arrays.asList(java.util.Arrays.asList) List(java.util.List) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode) Test(org.junit.Test)

Aggregations

InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)147 Test (org.junit.Test)107 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)72 KieBase (org.kie.api.KieBase)53 LeftInputAdapterNode (org.drools.core.reteoo.LeftInputAdapterNode)47 ArrayList (java.util.ArrayList)46 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)45 SegmentMemory (org.drools.core.reteoo.SegmentMemory)42 List (java.util.List)39 RuleTerminalNode (org.drools.core.reteoo.RuleTerminalNode)36 BetaMemory (org.drools.core.reteoo.BetaMemory)33 KieSession (org.kie.api.runtime.KieSession)32 JoinNode (org.drools.core.reteoo.JoinNode)30 PathMemory (org.drools.core.reteoo.PathMemory)29 ClassObjectType (org.drools.core.base.ClassObjectType)26 LiaNodeMemory (org.drools.core.reteoo.LeftInputAdapterNode.LiaNodeMemory)25 FactHandle (org.kie.api.runtime.rule.FactHandle)23 BetaNode (org.drools.core.reteoo.BetaNode)22 KieHelper (org.kie.internal.utils.KieHelper)22 AlphaNode (org.drools.core.reteoo.AlphaNode)19