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"));
}
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"));
}
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;
}
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;
}
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;
}
Aggregations