use of org.drools.core.definitions.rule.impl.RuleImpl in project drools by kiegroup.
the class DefeasibleBeliefSet method add.
public void add(M newDep) {
newDep.setStatus(resolveStatus(newDep));
RuleImpl rule = newDep.getLogicalDependency().getJustifier().getRule();
// first iterate to see if this new dep is defeated. If it's defeated, it can no longer impacts any deps
// if we checked what it defeats, and later this was defeated, we would have undo action. So we do the cheaper work first.
boolean wasDefeated = false;
for (DefeasibleMode<M> existingDep = rootUndefeated; existingDep != null; existingDep = existingDep.getNext()) {
wasDefeated = checkIsDefeated(newDep, rule, existingDep);
if (wasDefeated) {
existingDep.addDefeated(newDep);
break;
}
}
if (!wasDefeated) {
M stagedDeps = null;
for (M existingDep = rootUndefeated; existingDep != null; ) {
M next = existingDep.getNext();
if (checkIsDefeated(existingDep, existingDep.getLogicalDependency().getJustifier().getRule(), newDep)) {
// fist remove it from the undefeated list
removeUndefeated(existingDep);
newDep.addDefeated(existingDep);
if (existingDep.getRootDefeated() != null) {
// build up the list of staged deps, that will need to be reprocessed
if (stagedDeps == null) {
stagedDeps = existingDep.getRootDefeated();
} else {
stagedDeps.setPrevious(existingDep.getTailDefeated());
stagedDeps = existingDep.getRootDefeated();
}
}
existingDep.clearDefeated();
}
existingDep = next;
}
addUndefeated(newDep);
// now process the staged
reprocessDefeated(stagedDeps);
}
updateStatus();
}
use of org.drools.core.definitions.rule.impl.RuleImpl in project drools by kiegroup.
the class Misc2Test method testCustomDynamicSalience.
@Test
public void testCustomDynamicSalience() {
String drl = "package org.drools.test; " + "import " + Person.class.getName() + "; " + "global java.util.List list; " + "rule A " + "when " + " $person : Person( name == 'a' ) " + "then" + " list.add( $person.getAge() ); " + "end " + "rule B " + "when " + " $person : Person( name == 'b' ) " + "then" + " list.add( $person.getAge() ); " + "end " + "";
KieHelper helper = new KieHelper();
helper.addContent(drl, ResourceType.DRL);
KieSession session = helper.build().newKieSession();
List<Integer> list = new ArrayList<Integer>();
session.setGlobal("list", list);
for (Rule r : session.getKieBase().getKiePackage("org.drools.test").getRules()) {
((RuleImpl) r).setSalience(new Salience() {
@Override
public int getValue(KnowledgeHelper khelper, Rule rule, WorkingMemory workingMemory) {
if (khelper == null) {
return 0;
}
InternalFactHandle h = (InternalFactHandle) khelper.getMatch().getFactHandles().get(0);
return ((Person) h.getObject()).getAge();
}
@Override
public int getValue() {
throw new IllegalStateException("Should not have been called...");
}
@Override
public boolean isDynamic() {
return true;
}
});
}
session.insert(new Person("a", 1));
session.insert(new Person("a", 5));
session.insert(new Person("a", 3));
session.insert(new Person("b", 4));
session.insert(new Person("b", 2));
session.insert(new Person("b", 6));
session.fireAllRules();
assertEquals(Arrays.asList(6, 5, 4, 3, 2, 1), list);
}
use of org.drools.core.definitions.rule.impl.RuleImpl in project drools by kiegroup.
the class JavaDialect method addRule.
/**
* This will add the rule for compiling later on.
* It will not actually call the compiler
*/
public void addRule(final RuleBuildContext context) {
final RuleImpl rule = context.getRule();
final RuleDescr ruleDescr = context.getRuleDescr();
RuleClassBuilder classBuilder = context.getDialect().getRuleClassBuilder();
String ruleClass = classBuilder.buildRule(context);
// return if there is no ruleclass name;
if (ruleClass == null) {
return;
}
// The compilation result is for the entire rule, so difficult to associate with any descr
addClassCompileTask(this.pkg.getName() + "." + ruleDescr.getClassName(), ruleDescr, ruleClass, this.src, new RuleErrorHandler(ruleDescr, rule, "Rule Compilation error"));
JavaDialectRuntimeData data = (JavaDialectRuntimeData) this.pkg.getDialectRuntimeRegistry().getDialectData(ID);
for (Map.Entry<String, String> invokers : context.getInvokers().entrySet()) {
final String className = invokers.getKey();
// Check if an invoker - returnvalue, predicate, eval or consequence has been associated
// If so we add it to the PackageCompilationData as it will get wired up on compilation
final Object invoker = context.getInvokerLookup(className);
if (invoker instanceof Wireable) {
data.putInvoker(className, (Wireable) invoker);
}
final String text = invokers.getValue();
final BaseDescr descr = context.getDescrLookup(className);
addClassCompileTask(className, descr, text, this.src, new RuleInvokerErrorHandler(descr, rule, "Unable to generate rule invoker."));
}
// setup the line mappins for this rule
final String name = this.pkg.getName() + "." + StringUtils.ucFirst(ruleDescr.getClassName());
final LineMappings mapping = new LineMappings(name);
mapping.setStartLine(ruleDescr.getConsequenceLine());
mapping.setOffset(ruleDescr.getConsequenceOffset());
this.pkg.getDialectRuntimeRegistry().getLineMappings().put(name, mapping);
}
use of org.drools.core.definitions.rule.impl.RuleImpl in project drools by kiegroup.
the class RuleDescr method toRule.
public RuleImpl toRule() {
RuleImpl rule = new RuleImpl(name);
rule.setResource(getResource());
return rule;
}
use of org.drools.core.definitions.rule.impl.RuleImpl in project drools by kiegroup.
the class DisposeCommandPublicAPITest method testDisposeCommand.
@Test
public void testDisposeCommand() {
InternalKnowledgeBase kBase;
RuleImpl rule;
InternalKnowledgePackage pkg;
kBase = KnowledgeBaseFactory.newKnowledgeBase();
pkg = new KnowledgePackageImpl("org.droos.test");
pkg.setClassFieldAccessorCache(new ClassFieldAccessorCache(Thread.currentThread().getContextClassLoader()));
JavaDialectRuntimeData data = new JavaDialectRuntimeData();
data.onAdd(pkg.getDialectRuntimeRegistry(), kBase.getRootClassLoader());
pkg.getDialectRuntimeRegistry().setDialectData("java", data);
rule = new RuleImpl("Test");
rule.setDialect("java");
rule.setConsequence(new Consequence() {
public void evaluate(KnowledgeHelper knowledgeHelper, WorkingMemory workingMemory) throws Exception {
}
public String getName() {
return "default";
}
});
pkg.addRule(rule);
kBase.addPackage(pkg);
KieSession session = kBase.newKieSession();
Command dispose = KieServices.Factory.get().getCommands().newDispose();
session.insert("whatever");
session.fireAllRules();
session.execute(dispose);
try {
session.insert("whatever");
} catch (Exception e) {
Assert.assertEquals(e.getMessage(), "Illegal method call. This session was previously disposed.");
}
}
Aggregations