Search in sources :

Example 6 with DefaultKnowledgeHelper

use of org.drools.core.base.DefaultKnowledgeHelper in project drools by kiegroup.

the class KnowledgeBuilderTest method testReload.

@Test
public void testReload() throws Exception {
    final KnowledgeBuilderImpl builder = new KnowledgeBuilderImpl();
    final PackageDescr packageDescr = new PackageDescr("p1");
    final RuleDescr ruleDescr = new RuleDescr("rule-1");
    packageDescr.addRule(ruleDescr);
    final AndDescr lhs = new AndDescr();
    ruleDescr.setLhs(lhs);
    packageDescr.addGlobal(new GlobalDescr("map", "java.util.Map"));
    ruleDescr.setConsequence("map.put(\"value\", new Integer(1) );");
    builder.addPackage(packageDescr);
    InternalKnowledgePackage pkg = builder.getPackage(packageDescr.getName());
    RuleImpl rule = pkg.getRule("rule-1");
    assertLength(0, builder.getErrors().getErrors());
    InternalKnowledgeBase kBase = KnowledgeBaseFactory.newKnowledgeBase();
    kBase.addGlobal("map", Map.class);
    final KieSession workingMemory = kBase.newKieSession();
    final HashMap map = new HashMap();
    workingMemory.setGlobal("map", map);
    final LeftTupleImpl tuple = new MockTuple(new HashMap());
    tuple.setLeftTupleSink(new RuleTerminalNode(1, new CompositeObjectSinkAdapterTest.MockBetaNode(), rule, rule.getLhs(), 0, new BuildContext(kBase)));
    final Activation activation = new MockActivation(rule, 0, rule.getLhs(), tuple);
    DefaultKnowledgeHelper knowledgeHelper = new org.drools.core.base.DefaultKnowledgeHelper(((StatefulKnowledgeSessionImpl) workingMemory));
    knowledgeHelper.setActivation(activation);
    rule.getConsequence().evaluate(knowledgeHelper, ((StatefulKnowledgeSessionImpl) workingMemory));
    assertEquals(new Integer(1), map.get("value"));
    ruleDescr.setConsequence("map.put(\"value\", new Integer(2) );");
    pkg.removeRule(rule);
    // Make sure the compiled classes are also removed
    assertEquals(0, ((JavaDialectRuntimeData) pkg.getDialectRuntimeRegistry().getDialectData("java")).getStore().size());
    builder.addPackage(packageDescr);
    pkg = builder.getPackage(packageDescr.getName());
    rule = pkg.getRule("rule-1");
    knowledgeHelper = new org.drools.core.base.DefaultKnowledgeHelper(((StatefulKnowledgeSessionImpl) workingMemory));
    knowledgeHelper.setActivation(activation);
    rule.getConsequence().evaluate(knowledgeHelper, ((StatefulKnowledgeSessionImpl) workingMemory));
    assertEquals(new Integer(2), map.get("value"));
}
Also used : GlobalDescr(org.drools.compiler.lang.descr.GlobalDescr) HashMap(java.util.HashMap) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) Activation(org.drools.core.spi.Activation) LeftTupleImpl(org.drools.core.reteoo.LeftTupleImpl) KieSession(org.kie.api.runtime.KieSession) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode) AndDescr(org.drools.compiler.lang.descr.AndDescr) DefaultKnowledgeHelper(org.drools.core.base.DefaultKnowledgeHelper) DefaultKnowledgeHelper(org.drools.core.base.DefaultKnowledgeHelper) JavaDialectRuntimeData(org.drools.core.rule.JavaDialectRuntimeData) BuildContext(org.drools.core.reteoo.builder.BuildContext) StatefulKnowledgeSessionImpl(org.drools.core.impl.StatefulKnowledgeSessionImpl) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) CompositeObjectSinkAdapterTest(org.drools.core.reteoo.CompositeObjectSinkAdapterTest) Test(org.junit.Test)

Example 7 with DefaultKnowledgeHelper

use of org.drools.core.base.DefaultKnowledgeHelper in project drools by kiegroup.

the class PhreakRuleTerminalNode method doLeftTupleInsert.

public static void doLeftTupleInsert(TerminalNode rtnNode, RuleExecutor executor, InternalAgenda agenda, RuleAgendaItem ruleAgendaItem, int salienceInt, Salience salience, LeftTuple leftTuple) {
    PropagationContext pctx = leftTuple.getPropagationContext();
    pctx = RuleTerminalNode.findMostRecentPropagationContext(leftTuple, pctx);
    if (rtnNode.getRule().isNoLoop() && rtnNode.equals(pctx.getTerminalNodeOrigin())) {
        return;
    }
    InternalWorkingMemory wm = agenda.getWorkingMemory();
    if (salience != null) {
        salienceInt = salience.getValue(new DefaultKnowledgeHelper((AgendaItem) leftTuple, wm), rtnNode.getRule(), wm);
    }
    RuleTerminalNodeLeftTuple rtnLeftTuple = (RuleTerminalNodeLeftTuple) leftTuple;
    agenda.createAgendaItem(rtnLeftTuple, salienceInt, pctx, ruleAgendaItem, ruleAgendaItem.getAgendaGroup());
    EventSupport es = (EventSupport) wm;
    es.getAgendaEventSupport().fireActivationCreated(rtnLeftTuple, wm);
    if (rtnNode.getRule().isLockOnActive() && leftTuple.getPropagationContext().getType() != PropagationContext.Type.RULE_ADDITION) {
        long handleRecency = pctx.getFactHandle().getRecency();
        InternalAgendaGroup agendaGroup = executor.getRuleAgendaItem().getAgendaGroup();
        if (blockedByLockOnActive(rtnNode.getRule(), pctx, handleRecency, agendaGroup)) {
            es.getAgendaEventSupport().fireActivationCancelled(rtnLeftTuple, wm, MatchCancelledCause.FILTER);
            return;
        }
    }
    if (agenda.getActivationsFilter() != null && !agenda.getActivationsFilter().accept(rtnLeftTuple, wm, rtnNode)) {
        // only relevant for seralization, to not refire Matches already fired
        return;
    }
    agenda.addItemToActivationGroup(rtnLeftTuple);
    executor.addLeftTuple(leftTuple);
    // increased here, decreased in Agenda's cancelActivation and fireActivation
    leftTuple.increaseActivationCountForEvents();
    if (!rtnNode.isFireDirect() && executor.isDeclarativeAgendaEnabled()) {
        agenda.insertAndStageActivation(rtnLeftTuple);
    }
}
Also used : InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) InternalAgendaGroup(org.drools.core.common.InternalAgendaGroup) PropagationContext(org.drools.core.spi.PropagationContext) EventSupport(org.drools.core.common.EventSupport) DefaultKnowledgeHelper(org.drools.core.base.DefaultKnowledgeHelper) RuleTerminalNodeLeftTuple(org.drools.core.reteoo.RuleTerminalNodeLeftTuple)

Aggregations

DefaultKnowledgeHelper (org.drools.core.base.DefaultKnowledgeHelper)7 StatefulKnowledgeSessionImpl (org.drools.core.impl.StatefulKnowledgeSessionImpl)5 LeftTupleImpl (org.drools.core.reteoo.LeftTupleImpl)5 Test (org.junit.Test)5 HashMap (java.util.HashMap)4 RuleDescr (org.drools.compiler.lang.descr.RuleDescr)4 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)4 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)4 CompositeObjectSinkAdapterTest (org.drools.core.reteoo.CompositeObjectSinkAdapterTest)4 RuleTerminalNode (org.drools.core.reteoo.RuleTerminalNode)4 PackageDescr (org.drools.compiler.lang.descr.PackageDescr)3 AgendaItem (org.drools.core.common.AgendaItem)3 AgendaItemImpl (org.drools.core.common.AgendaItemImpl)3 InternalFactHandle (org.drools.core.common.InternalFactHandle)3 BuildContext (org.drools.core.reteoo.builder.BuildContext)3 Declaration (org.drools.core.rule.Declaration)3 Cheese (org.drools.compiler.Cheese)2 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)2 DialectCompiletimeRegistry (org.drools.compiler.compiler.DialectCompiletimeRegistry)2 AndDescr (org.drools.compiler.lang.descr.AndDescr)2