Search in sources :

Example 26 with RuleImpl

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();
}
Also used : RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl)

Example 27 with RuleImpl

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);
}
Also used : WorkingMemory(org.drools.core.WorkingMemory) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) ArrayList(java.util.ArrayList) KieHelper(org.kie.internal.utils.KieHelper) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Salience(org.drools.core.spi.Salience) KieSession(org.kie.api.runtime.KieSession) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KnowledgeHelper(org.drools.core.spi.KnowledgeHelper) Rule(org.kie.api.definition.rule.Rule) InternalFactHandle(org.drools.core.common.InternalFactHandle) Person(org.drools.compiler.Person) Test(org.junit.Test)

Example 28 with RuleImpl

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);
}
Also used : RuleInvokerErrorHandler(org.drools.compiler.builder.impl.errors.RuleInvokerErrorHandler) Wireable(org.drools.core.spi.Wireable) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) JavaDialectRuntimeData(org.drools.core.rule.JavaDialectRuntimeData) RuleClassBuilder(org.drools.compiler.rule.builder.RuleClassBuilder) LineMappings(org.drools.core.rule.LineMappings) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) RuleErrorHandler(org.drools.compiler.builder.impl.errors.RuleErrorHandler) BaseDescr(org.drools.compiler.lang.descr.BaseDescr) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap)

Example 29 with RuleImpl

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;
}
Also used : RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl)

Example 30 with RuleImpl

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.");
    }
}
Also used : WorkingMemory(org.drools.core.WorkingMemory) Consequence(org.drools.core.spi.Consequence) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) JavaDialectRuntimeData(org.drools.core.rule.JavaDialectRuntimeData) ClassFieldAccessorCache(org.drools.core.base.ClassFieldAccessorCache) Command(org.kie.api.command.Command) KnowledgeHelper(org.drools.core.spi.KnowledgeHelper) KieSession(org.kie.api.runtime.KieSession) KnowledgePackageImpl(org.drools.core.definitions.impl.KnowledgePackageImpl) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) Test(org.junit.Test)

Aggregations

RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)107 Test (org.junit.Test)51 Pattern (org.drools.core.rule.Pattern)30 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)28 RuleDescr (org.drools.compiler.lang.descr.RuleDescr)21 WorkingMemory (org.drools.core.WorkingMemory)21 KnowledgePackageImpl (org.drools.core.definitions.impl.KnowledgePackageImpl)18 BuildContext (org.drools.core.reteoo.builder.BuildContext)16 KnowledgeHelper (org.drools.core.spi.KnowledgeHelper)16 Consequence (org.drools.core.spi.Consequence)15 GroupElement (org.drools.core.rule.GroupElement)14 ClassObjectType (org.drools.core.base.ClassObjectType)13 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)13 IOException (java.io.IOException)12 ObjectInput (java.io.ObjectInput)12 ObjectOutput (java.io.ObjectOutput)12 PackageDescr (org.drools.compiler.lang.descr.PackageDescr)12 RuleTerminalNode (org.drools.core.reteoo.RuleTerminalNode)12 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)11 KnowledgeBaseImpl (org.drools.core.impl.KnowledgeBaseImpl)11