Search in sources :

Example 6 with Activation

use of org.drools.core.spi.Activation in project drools by kiegroup.

the class KnowledgeBuilderTest method testSerializable.

@Test
public void testSerializable() 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) );");
    // check that packageDescr is serializable
    final PackageDescr back = (PackageDescr) SerializationHelper.serializeObject(packageDescr);
    assertNotNull(back);
    assertEquals("p1", back.getName());
    builder.addPackage(packageDescr);
    InternalKnowledgePackage pkg = builder.getPackage(packageDescr.getName());
    final RuleImpl rule = pkg.getRule("rule-1");
    assertLength(0, builder.getErrors().getErrors());
    InternalKnowledgePackage newPkg = SerializationHelper.serializeObject(pkg);
    final RuleImpl newRule = newPkg.getRule("rule-1");
    InternalKnowledgeBase kBase = (InternalKnowledgeBase) KnowledgeBaseFactory.newKnowledgeBase();
    // It's been serialised so we have to simulate the re-wiring process
    newPkg.getDialectRuntimeRegistry().onAdd(kBase.getRootClassLoader());
    newPkg.getDialectRuntimeRegistry().onBeforeExecute();
    kBase.getGlobals().put("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(), newRule, newRule.getLhs(), 0, new BuildContext(kBase)));
    final Activation activation = new MockActivation(newRule, 0, newRule.getLhs(), tuple);
    final DefaultKnowledgeHelper knowledgeHelper = new org.drools.core.base.DefaultKnowledgeHelper(((StatefulKnowledgeSessionImpl) workingMemory));
    knowledgeHelper.setActivation(activation);
    newRule.getConsequence().evaluate(knowledgeHelper, ((StatefulKnowledgeSessionImpl) workingMemory));
    assertEquals(new Integer(1), map.get("value"));
}
Also used : GlobalDescr(org.drools.compiler.lang.descr.GlobalDescr) HashMap(java.util.HashMap) AndDescr(org.drools.compiler.lang.descr.AndDescr) DefaultKnowledgeHelper(org.drools.core.base.DefaultKnowledgeHelper) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) Activation(org.drools.core.spi.Activation) BuildContext(org.drools.core.reteoo.builder.BuildContext) StatefulKnowledgeSessionImpl(org.drools.core.impl.StatefulKnowledgeSessionImpl) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) 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) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode) CompositeObjectSinkAdapterTest(org.drools.core.reteoo.CompositeObjectSinkAdapterTest) Test(org.junit.Test)

Example 7 with Activation

use of org.drools.core.spi.Activation 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 8 with Activation

use of org.drools.core.spi.Activation in project drools by kiegroup.

the class ParallelRuleEvaluator method evaluateAndFire.

@Override
public int evaluateAndFire(AgendaFilter filter, int fireCount, int fireLimit, InternalAgendaGroup group) {
    this.filter = filter;
    this.fireCount = fireCount;
    this.fireLimit = fireLimit;
    Activation[] activations = group.getActivations();
    for (Activation activation : activations) {
        RuleAgendaItem item = (RuleAgendaItem) activation;
        int index = item.getPartition().getParallelEvaluationSlot();
        RuleEvaluatorCallable evaluator = evaluators[index];
        evaluator.enqueue(item);
        if (!evaluator.running) {
            evaluator.running = true;
            results[index] = Completion.service.submit(evaluator);
        }
    }
    int localFireCount = 0;
    for (int i = 0; i < evaluatorsNr; i++) {
        if (results[i] != null) {
            try {
                evaluators[i].enqueue(POISON_PILL);
                localFireCount += results[i].get();
            } catch (Exception e) {
                throw new RuntimeException(e);
            } finally {
                results[i] = null;
            }
        }
    }
    return localFireCount;
}
Also used : RuleAgendaItem(org.drools.core.phreak.RuleAgendaItem) Activation(org.drools.core.spi.Activation)

Example 9 with Activation

use of org.drools.core.spi.Activation in project drools by kiegroup.

the class ObjectTypeConfigurationRegistry method getObjectTypeConf.

/**
 * Returns the ObjectTypeConfiguration object for the given object or
 * creates a new one if none is found in the cache
 */
public ObjectTypeConf getObjectTypeConf(EntryPointId entrypoint, Object object) {
    // first see if it's a ClassObjectTypeConf
    Object key;
    if (object instanceof Activation) {
        key = ClassObjectType.Match_ObjectType.getClassType();
    } else if (object instanceof Fact) {
        key = ((Fact) object).getFactTemplate().getName();
    } else {
        key = object.getClass();
    }
    ObjectTypeConf objectTypeConf = this.typeConfMap.get(key);
    // it doesn't exist, so create it.
    if (objectTypeConf == null) {
        if (object instanceof Fact) {
            objectTypeConf = new FactTemplateTypeConf(entrypoint, ((Fact) object).getFactTemplate(), this.kBase);
        } else {
            objectTypeConf = new ClassObjectTypeConf(entrypoint, (Class<?>) key, this.kBase);
        }
        ObjectTypeConf existing = this.typeConfMap.putIfAbsent(key, objectTypeConf);
        if (existing != null) {
            // Raced, take the (now) existing.
            objectTypeConf = existing;
        }
    }
    return objectTypeConf;
}
Also used : FactTemplateTypeConf(org.drools.core.reteoo.FactTemplateTypeConf) ObjectTypeConf(org.drools.core.reteoo.ObjectTypeConf) ClassObjectTypeConf(org.drools.core.reteoo.ClassObjectTypeConf) ClassObjectTypeConf(org.drools.core.reteoo.ClassObjectTypeConf) Activation(org.drools.core.spi.Activation) Fact(org.drools.core.facttemplates.Fact)

Example 10 with Activation

use of org.drools.core.spi.Activation in project drools by kiegroup.

the class ActivationIterator method next.

public Object next() {
    Activation acc = null;
    if (this.currentTuple != null) {
        Object obj = currentTuple.getContextObject();
        acc = obj == Boolean.TRUE ? null : (Activation) obj;
        currentTuple = leftTupleIter.next();
        while (currentTuple == null && (node = (TerminalNode) nodeIter.next()) != null) {
            if (!(node instanceof RuleTerminalNode)) {
                continue;
            }
            leftTupleIter = LeftTupleIterator.iterator(wm, node);
            this.currentTuple = leftTupleIter.next();
        }
    }
    return acc;
}
Also used : Activation(org.drools.core.spi.Activation) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode) TerminalNode(org.drools.core.reteoo.TerminalNode) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode)

Aggregations

Activation (org.drools.core.spi.Activation)22 RuleAgendaItem (org.drools.core.phreak.RuleAgendaItem)6 Test (org.junit.Test)4 RuleTerminalNode (org.drools.core.reteoo.RuleTerminalNode)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Iterator (java.util.Iterator)2 AndDescr (org.drools.compiler.lang.descr.AndDescr)2 GlobalDescr (org.drools.compiler.lang.descr.GlobalDescr)2 PackageDescr (org.drools.compiler.lang.descr.PackageDescr)2 RuleDescr (org.drools.compiler.lang.descr.RuleDescr)2 DefaultKnowledgeHelper (org.drools.core.base.DefaultKnowledgeHelper)2 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)2 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)2 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)2 StatefulKnowledgeSessionImpl (org.drools.core.impl.StatefulKnowledgeSessionImpl)2 RuleExecutor (org.drools.core.phreak.RuleExecutor)2 CompositeObjectSinkAdapterTest (org.drools.core.reteoo.CompositeObjectSinkAdapterTest)2 LeftTupleImpl (org.drools.core.reteoo.LeftTupleImpl)2 ObjectTypeConf (org.drools.core.reteoo.ObjectTypeConf)2