Search in sources :

Example 56 with RuleImpl

use of org.drools.core.definitions.rule.impl.RuleImpl in project drools by kiegroup.

the class RuleTest method testDateEffectiveExpires.

@Test
public void testDateEffectiveExpires() {
    WorkingMemory wm = (WorkingMemory) new KnowledgeBaseImpl("x", null).newKieSession();
    final RuleImpl rule = new RuleImpl("myrule");
    final Calendar past = Calendar.getInstance();
    past.setTimeInMillis(10);
    final Calendar future = Calendar.getInstance();
    future.setTimeInMillis(future.getTimeInMillis() + 100000000);
    rule.setDateEffective(past);
    rule.setDateExpires(future);
    assertTrue(rule.isEffective(null, new RuleTerminalNode(), wm));
    rule.setDateExpires(past);
    assertFalse(rule.isEffective(null, new RuleTerminalNode(), wm));
    rule.setDateExpires(future);
    rule.setDateEffective(future);
    assertFalse(rule.isEffective(null, new RuleTerminalNode(), wm));
}
Also used : WorkingMemory(org.drools.core.WorkingMemory) Calendar(java.util.Calendar) KnowledgeBaseImpl(org.drools.core.impl.KnowledgeBaseImpl) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode) Test(org.junit.Test)

Example 57 with RuleImpl

use of org.drools.core.definitions.rule.impl.RuleImpl in project drools by kiegroup.

the class RuleTest method testDateEffective.

@Test
public void testDateEffective() {
    WorkingMemory wm = (WorkingMemory) new KnowledgeBaseImpl("x", null).newKieSession();
    final RuleImpl rule = new RuleImpl("myrule");
    assertTrue(rule.isEffective(null, new RuleTerminalNode(), wm));
    final Calendar earlier = Calendar.getInstance();
    earlier.setTimeInMillis(10);
    rule.setDateEffective(earlier);
    assertTrue(rule.isEffective(null, new RuleTerminalNode(), wm));
    final Calendar later = Calendar.getInstance();
    later.setTimeInMillis(later.getTimeInMillis() + 100000000);
    assertTrue(later.after(Calendar.getInstance()));
    rule.setDateEffective(later);
    assertFalse(rule.isEffective(null, new RuleTerminalNode(), wm));
}
Also used : WorkingMemory(org.drools.core.WorkingMemory) Calendar(java.util.Calendar) KnowledgeBaseImpl(org.drools.core.impl.KnowledgeBaseImpl) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode) Test(org.junit.Test)

Example 58 with RuleImpl

use of org.drools.core.definitions.rule.impl.RuleImpl in project drools by kiegroup.

the class ReteooBuilder method removeRules.

public synchronized void removeRules(Collection<RuleImpl> rulesToBeRemoved) {
    // reset working memories for potential propagation
    InternalWorkingMemory[] workingMemories = this.kBase.getWorkingMemories();
    for (RuleImpl rule : rulesToBeRemoved) {
        if (rule.hasChildren() && !rulesToBeRemoved.containsAll(rule.getChildren())) {
            throw new RuntimeException("Cannot remove parent rule " + rule + " without having removed all its chikdren");
        }
        final RuleRemovalContext context = new RuleRemovalContext(rule);
        context.setKnowledgeBase(kBase);
        BaseNode[] rulesTerminalNodes = rules.remove(rule.getFullyQualifiedName());
        if (rulesTerminalNodes == null) {
            // there couldn't be any rule to be removed if it comes from a broken drl
            continue;
        }
        for (BaseNode node : rulesTerminalNodes) {
            removeTerminalNode(context, (TerminalNode) node, workingMemories);
        }
        if (rule.isQuery()) {
            this.queries.remove(rule.getName());
        }
        if (rule.getParent() != null && !rulesToBeRemoved.contains(rule.getParent())) {
            rule.getParent().removeChild(rule);
        }
    }
}
Also used : InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) BaseNode(org.drools.core.common.BaseNode) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl)

Example 59 with RuleImpl

use of org.drools.core.definitions.rule.impl.RuleImpl in project drools by kiegroup.

the class QueryTerminalNodeVisitor method doVisit.

@Override
protected void doVisit(NetworkNode node, Stack<NetworkNode> nodeStack, StatefulKnowledgeSessionInfo info) {
    QueryTerminalNode rtn = (QueryTerminalNode) node;
    RuleImpl rule = rtn.getRule();
    // first thing, associate all nodes belonging to this rule
    for (NetworkNode snode : nodeStack) {
        info.assign(snode, rule);
    }
// Query Terminal Nodes no longer have memory
// TODO delete this
// final LinkedList<?> memory = (LinkedList<?>) info.getSession().getNodeMemory( rtn );
// final DefaultNodeInfo ni = (DefaultNodeInfo) info.getNodeInfo( node );
// ni.setMemoryEnabled( true );
// ni.setTupleMemorySize( memory.size() );
}
Also used : QueryTerminalNode(org.drools.core.reteoo.QueryTerminalNode) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) NetworkNode(org.drools.core.common.NetworkNode)

Example 60 with RuleImpl

use of org.drools.core.definitions.rule.impl.RuleImpl in project drools by kiegroup.

the class RuleTerminalNodeVisitor method doVisit.

@Override
protected void doVisit(NetworkNode node, Stack<NetworkNode> nodeStack, StatefulKnowledgeSessionInfo info) {
    RuleTerminalNode rtn = (RuleTerminalNode) node;
    RuleImpl rule = rtn.getRule();
    // first thing, associate all nodes belonging to this rule
    for (NetworkNode snode : nodeStack) {
        info.assign(snode, rule);
    }
    final DefaultNodeInfo ni = (DefaultNodeInfo) info.getNodeInfo(node);
    ni.setMemoryEnabled(false);
}
Also used : RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) NetworkNode(org.drools.core.common.NetworkNode) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode)

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