use of org.drools.core.common.AgendaItem in project drools by kiegroup.
the class RuleExecutor method fire.
private int fire(InternalWorkingMemory wm, InternalAgenda agenda, AgendaFilter filter, int fireCount, int fireLimit) {
int localFireCount = 0;
if (!tupleList.isEmpty()) {
if (!fireExitedEarly && isDeclarativeAgendaEnabled()) {
// Network Evaluation can notify meta rules, which should be given a chance to fire first
RuleAgendaItem nextRule = agenda.peekNextRule();
if (!isHigherSalience(nextRule)) {
fireExitedEarly = true;
return localFireCount;
}
}
RuleTerminalNode rtn = (RuleTerminalNode) pmem.getPathEndNode();
RuleImpl rule = rtn.getRule();
Tuple tuple = getNextTuple();
if (rule.isAllMatches()) {
fireConsequenceEvent(wm, agenda, (AgendaItem) tuple, DefaultAgenda.ON_BEFORE_ALL_FIRES_CONSEQUENCE_NAME);
}
Tuple lastTuple = null;
for (; tuple != null; lastTuple = tuple, tuple = getNextTuple()) {
// if the current Rule is no-loop and the origin rule is the same then return
if (cancelAndContinue(wm, rtn, rule, tuple, filter)) {
continue;
}
AgendaItem item = (AgendaItem) tuple;
if (agenda.getActivationsFilter() != null && !agenda.getActivationsFilter().accept(item, wm, rtn)) {
// only relevant for seralization, to not refire Matches already fired
continue;
}
fireActivation(wm, agenda, item);
localFireCount++;
if (rtn.getLeftTupleSource() == null) {
// The activation firing removed this rule from the rule base
break;
}
agenda.flushPropagations();
// dyanmic salience may have updated it, so get again.
int salience = ruleAgendaItem.getSalience();
if (queue != null && !queue.isEmpty() && salience != queue.peek().getSalience()) {
ruleAgendaItem.dequeue();
ruleAgendaItem.setSalience(queue.peek().getSalience());
ruleAgendaItem.getAgendaGroup().add(ruleAgendaItem);
}
if (!rule.isAllMatches()) {
// if firing rule is @All don't give way to other rules
if (haltRuleFiring(fireCount, fireLimit, localFireCount, agenda)) {
// another rule has high priority and is on the agenda, so evaluate it first
break;
}
if (!wm.isSequential()) {
reEvaluateNetwork(agenda);
}
}
}
if (rule.isAllMatches()) {
fireConsequenceEvent(wm, agenda, (AgendaItem) lastTuple, DefaultAgenda.ON_AFTER_ALL_FIRES_CONSEQUENCE_NAME);
}
}
removeRuleAgendaItemWhenEmpty(wm);
fireExitedEarly = false;
return localFireCount;
}
use of org.drools.core.common.AgendaItem in project drools by kiegroup.
the class ProtobufOutputMarshaller method writeAgenda.
private static void writeAgenda(MarshallerWriteContext context, ProtobufMessages.RuleData.Builder _ksb) throws IOException {
InternalWorkingMemory wm = context.wm;
InternalAgenda agenda = wm.getAgenda();
org.drools.core.marshalling.impl.ProtobufMessages.Agenda.Builder _ab = ProtobufMessages.Agenda.newBuilder();
AgendaGroup[] agendaGroups = agenda.getAgendaGroupsMap().values().toArray(new AgendaGroup[agenda.getAgendaGroupsMap().size()]);
Arrays.sort(agendaGroups, AgendaGroupSorter.instance);
for (AgendaGroup ag : agendaGroups) {
AgendaGroupQueueImpl group = (AgendaGroupQueueImpl) ag;
org.drools.core.marshalling.impl.ProtobufMessages.Agenda.AgendaGroup.Builder _agb = ProtobufMessages.Agenda.AgendaGroup.newBuilder();
_agb.setName(group.getName()).setIsActive(group.isActive()).setIsAutoDeactivate(group.isAutoDeactivate()).setClearedForRecency(group.getClearedForRecency()).setHasRuleFlowLister(group.isRuleFlowListener()).setActivatedForRecency(group.getActivatedForRecency());
Map<Long, String> nodeInstances = group.getNodeInstances();
for (Map.Entry<Long, String> entry : nodeInstances.entrySet()) {
org.drools.core.marshalling.impl.ProtobufMessages.Agenda.AgendaGroup.NodeInstance.Builder _nib = ProtobufMessages.Agenda.AgendaGroup.NodeInstance.newBuilder();
_nib.setProcessInstanceId(entry.getKey());
_nib.setNodeInstanceId(entry.getValue());
_agb.addNodeInstance(_nib.build());
}
_ab.addAgendaGroup(_agb.build());
}
org.drools.core.marshalling.impl.ProtobufMessages.Agenda.FocusStack.Builder _fsb = ProtobufMessages.Agenda.FocusStack.newBuilder();
LinkedList<AgendaGroup> focusStack = agenda.getStackList();
for (AgendaGroup group : focusStack) {
_fsb.addGroupName(group.getName());
}
_ab.setFocusStack(_fsb.build());
// serialize all dormant activations
org.drools.core.util.Iterator it = ActivationIterator.iterator(wm);
List<org.drools.core.spi.Activation> dormant = new ArrayList<org.drools.core.spi.Activation>();
for (org.drools.core.spi.Activation item = (org.drools.core.spi.Activation) it.next(); item != null; item = (org.drools.core.spi.Activation) it.next()) {
if (!item.isQueued()) {
dormant.add(item);
}
}
Collections.sort(dormant, ActivationsSorter.INSTANCE);
for (org.drools.core.spi.Activation activation : dormant) {
_ab.addMatch(writeActivation(context, (AgendaItem) activation));
}
// serialize all network evaluator activations
for (Activation activation : agenda.getActivations()) {
if (activation.isRuleAgendaItem()) {
// serialize it
_ab.addRuleActivation(writeActivation(context, (AgendaItem) activation));
}
}
_ksb.setAgenda(_ab.build());
}
use of org.drools.core.common.AgendaItem in project drools by kiegroup.
the class MVELConsequenceBuilderTest method testSimpleExpression.
@Test
public void testSimpleExpression() throws Exception {
PackageDescr pkgDescr = new PackageDescr("pkg1");
KnowledgeBuilderImpl pkgBuilder = new KnowledgeBuilderImpl();
pkgBuilder.addPackage(pkgDescr);
InternalKnowledgePackage pkg = pkgBuilder.getPackageRegistry("pkg1").getPackage();
final RuleDescr ruleDescr = new RuleDescr("rule 1");
ruleDescr.setNamespace("pkg1");
ruleDescr.setConsequence("modify (cheese) {price = 5 };\nretract (cheese)");
DialectCompiletimeRegistry dialectRegistry = pkgBuilder.getPackageRegistry(pkg.getName()).getDialectCompiletimeRegistry();
MVELDialect mvelDialect = (MVELDialect) dialectRegistry.getDialect("mvel");
final RuleBuildContext context = new RuleBuildContext(pkgBuilder, ruleDescr, dialectRegistry, pkg, mvelDialect);
final InstrumentedDeclarationScopeResolver declarationResolver = new InstrumentedDeclarationScopeResolver();
final ObjectType cheeseObjeectType = new ClassObjectType(Cheese.class);
final Pattern pattern = new Pattern(0, cheeseObjeectType, "cheese");
final GroupElement subrule = new GroupElement(GroupElement.AND);
subrule.addChild(pattern);
final Map<String, Declaration> map = new HashMap<String, Declaration>();
map.put("cheese", pattern.getDeclaration());
declarationResolver.setDeclarations(map);
context.setDeclarationResolver(declarationResolver);
final MVELConsequenceBuilder builder = new MVELConsequenceBuilder();
builder.build(context, RuleImpl.DEFAULT_CONSEQUENCE_NAME);
InternalKnowledgeBase kBase = KnowledgeBaseFactory.newKnowledgeBase();
PropagationContextFactory pctxFactory = kBase.getConfiguration().getComponentFactory().getPropagationContextFactory();
kBase.addPackage(pkg);
StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) kBase.newKieSession();
final Cheese cheddar = new Cheese("cheddar", 10);
final InternalFactHandle f0 = (InternalFactHandle) ksession.insert(cheddar);
final LeftTupleImpl tuple = new LeftTupleImpl(f0, null, true);
f0.removeLeftTuple(tuple);
final AgendaItem item = new AgendaItemImpl(0, tuple, 10, pctxFactory.createPropagationContext(1, PropagationContext.Type.DELETION, null, tuple != null ? (TerminalNode) tuple.getTupleSink() : null, null), new RuleTerminalNode(0, new CompositeObjectSinkAdapterTest.MockBetaNode(), context.getRule(), subrule, 0, new BuildContext(kBase)), null);
final DefaultKnowledgeHelper kbHelper = new DefaultKnowledgeHelper(ksession);
kbHelper.setActivation(item);
((MVELConsequence) context.getRule().getConsequence()).compile((MVELDialectRuntimeData) pkgBuilder.getPackageRegistry(pkg.getName()).getDialectRuntimeRegistry().getDialectData("mvel"));
context.getRule().getConsequence().evaluate(kbHelper, ksession);
assertEquals(5, cheddar.getPrice());
}
use of org.drools.core.common.AgendaItem in project drools by kiegroup.
the class MVELConsequenceBuilderTest method testImperativeCodeError.
@Test
public void testImperativeCodeError() throws Exception {
InternalKnowledgePackage pkg = new KnowledgePackageImpl("pkg1");
final RuleDescr ruleDescr = new RuleDescr("rule 1");
ruleDescr.setConsequence("if (cheese.price == 10) { cheese.price = 5; }");
Properties properties = new Properties();
properties.setProperty("drools.dialect.default", "mvel");
KnowledgeBuilderConfigurationImpl cfg1 = new KnowledgeBuilderConfigurationImpl(properties);
KnowledgeBuilderImpl pkgBuilder = new KnowledgeBuilderImpl(pkg, cfg1);
PackageRegistry pkgRegistry = pkgBuilder.getPackageRegistry(pkg.getName());
DialectCompiletimeRegistry dialectRegistry = pkgBuilder.getPackageRegistry(pkg.getName()).getDialectCompiletimeRegistry();
MVELDialect mvelDialect = (MVELDialect) dialectRegistry.getDialect(pkgRegistry.getDialect());
final RuleBuildContext context = new RuleBuildContext(pkgBuilder, ruleDescr, dialectRegistry, pkg, mvelDialect);
final InstrumentedDeclarationScopeResolver declarationResolver = new InstrumentedDeclarationScopeResolver();
final ObjectType cheeseObjeectType = new ClassObjectType(Cheese.class);
final Pattern pattern = new Pattern(0, cheeseObjeectType);
final PatternExtractor extractor = new PatternExtractor(cheeseObjeectType);
final Declaration declaration = new Declaration("cheese", extractor, pattern);
final Map<String, Declaration> map = new HashMap<String, Declaration>();
map.put("cheese", declaration);
declarationResolver.setDeclarations(map);
context.setDeclarationResolver(declarationResolver);
final MVELConsequenceBuilder builder = new MVELConsequenceBuilder();
builder.build(context, RuleImpl.DEFAULT_CONSEQUENCE_NAME);
InternalKnowledgeBase kBase = KnowledgeBaseFactory.newKnowledgeBase();
StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) kBase.newKieSession();
final Cheese cheddar = new Cheese("cheddar", 10);
final InternalFactHandle f0 = (InternalFactHandle) ksession.insert(cheddar);
final LeftTupleImpl tuple = new LeftTupleImpl(f0, null, true);
final AgendaItem item = new AgendaItemImpl(0, tuple, 10, null, null, null);
final DefaultKnowledgeHelper kbHelper = new DefaultKnowledgeHelper(ksession);
kbHelper.setActivation(item);
try {
((MVELConsequence) context.getRule().getConsequence()).compile((MVELDialectRuntimeData) pkgBuilder.getPackageRegistry(pkg.getName()).getDialectRuntimeRegistry().getDialectData("mvel"));
context.getRule().getConsequence().evaluate(kbHelper, ksession);
fail("should throw an exception, as 'if' is not allowed");
} catch (Exception e) {
}
assertEquals(10, cheddar.getPrice());
}
use of org.drools.core.common.AgendaItem in project drools by kiegroup.
the class MVELSalienceBuilderTest method testSimpleExpression.
@Test
public void testSimpleExpression() {
StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) kBase.newKieSession();
final Person p = new Person("mark", "", 31);
final InternalFactHandle f0 = (InternalFactHandle) ksession.insert(p);
final LeftTupleImpl tuple = new LeftTupleImpl(f0, null, true);
RuleTerminalNode rtn = new RuleTerminalNode();
rtn.setSalienceDeclarations(context.getDeclarationResolver().getDeclarations(context.getRule()).values().toArray(new Declaration[1]));
AgendaItem item = new AgendaItemImpl(0, tuple, 0, null, rtn, null);
assertEquals(25, context.getRule().getSalience().getValue(new DefaultKnowledgeHelper(item, ksession), context.getRule(), ksession));
}
Aggregations