Search in sources :

Example 86 with RuleImpl

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

the class RuleTest method testTimeMachine.

@Test
public void testTimeMachine() {
    SessionConfiguration conf = SessionConfiguration.newInstance();
    conf.setClockType(ClockType.PSEUDO_CLOCK);
    WorkingMemory wm = (WorkingMemory) new KnowledgeBaseImpl("x", null).newKieSession(conf, null);
    final Calendar future = Calendar.getInstance();
    ((PseudoClockScheduler) wm.getSessionClock()).setStartupTime(future.getTimeInMillis());
    final RuleImpl rule = new RuleImpl("myrule");
    rule.setEnabled(EnabledBoolean.ENABLED_TRUE);
    assertTrue(rule.isEffective(null, new RuleTerminalNode(), wm));
    future.setTimeInMillis(future.getTimeInMillis() + 100000000);
    rule.setDateEffective(future);
    assertFalse(rule.isEffective(null, new RuleTerminalNode(), wm));
    ((PseudoClockScheduler) wm.getSessionClock()).advanceTime(1000000000000L, TimeUnit.MILLISECONDS);
    assertTrue(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) SessionConfiguration(org.drools.core.SessionConfiguration) PseudoClockScheduler(org.drools.core.time.impl.PseudoClockScheduler) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode) Test(org.junit.Test)

Example 87 with RuleImpl

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

the class RuleTest method testGetSalienceValue.

@Test
public void testGetSalienceValue() {
    final RuleImpl rule = new RuleImpl("myrule");
    final int salienceValue = 100;
    Salience salience = new SalienceInteger(salienceValue);
    rule.setSalience(salience);
    assertEquals(salienceValue, rule.getSalienceValue());
    assertFalse(rule.isSalienceDynamic());
}
Also used : Salience(org.drools.core.spi.Salience) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) SalienceInteger(org.drools.core.base.SalienceInteger) Test(org.junit.Test)

Example 88 with RuleImpl

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

the class RuleTest method testIsSalienceDynamic.

@Test
public void testIsSalienceDynamic() {
    final RuleImpl rule = new RuleImpl("myrule");
    Salience salience = new MVELSalienceExpression();
    rule.setSalience(salience);
    assertTrue(rule.isSalienceDynamic());
}
Also used : Salience(org.drools.core.spi.Salience) MVELSalienceExpression(org.drools.core.base.mvel.MVELSalienceExpression) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) Test(org.junit.Test)

Example 89 with RuleImpl

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

the class RuleTest method testDateExpires.

@Test
public void testDateExpires() throws Exception {
    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.setDateExpires(earlier);
    assertFalse(rule.isEffective(null, new RuleTerminalNode(), wm));
    final Calendar later = Calendar.getInstance();
    later.setTimeInMillis(later.getTimeInMillis() + 100000000);
    rule.setDateExpires(later);
    assertTrue(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 90 with RuleImpl

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

the class RuleBuilder method buildMetaAttributes.

public static void buildMetaAttributes(final RuleBuildContext context) {
    RuleImpl rule = context.getRule();
    for (String metaAttr : context.getRuleDescr().getAnnotationNames()) {
        AnnotationDescr ad = context.getRuleDescr().getAnnotation(metaAttr);
        String adFqn = ad.getFullyQualifiedName();
        if (adFqn != null) {
            AnnotationDefinition annotationDefinition;
            try {
                annotationDefinition = AnnotationDefinition.build(context.getDialect().getTypeResolver().resolveType(adFqn), ad.getValueMap(), context.getDialect().getTypeResolver());
            } catch (NoSuchMethodException e) {
                throw new RuntimeException(e);
            } catch (ClassNotFoundException e) {
                throw new RuntimeException(e);
            }
            if (annotationDefinition.getValues().size() == 1 && annotationDefinition.getValues().containsKey(AnnotationDescr.VALUE)) {
                rule.addMetaAttribute(metaAttr, annotationDefinition.getPropertyValue(AnnotationDescr.VALUE));
            } else {
                Map<String, Object> map = new HashMap<String, Object>(annotationDefinition.getValues().size());
                for (String key : annotationDefinition.getValues().keySet()) {
                    map.put(key, annotationDefinition.getPropertyValue(key));
                }
                rule.addMetaAttribute(metaAttr, map);
            }
        } else {
            if (ad.hasValue()) {
                if (ad.getValues().size() == 1) {
                    rule.addMetaAttribute(metaAttr, resolveValue(ad.getSingleValueAsString()));
                } else {
                    rule.addMetaAttribute(metaAttr, ad.getValueMap());
                }
            } else {
                rule.addMetaAttribute(metaAttr, null);
            }
        }
    }
}
Also used : AnnotationDefinition(org.drools.core.factmodel.AnnotationDefinition) HashMap(java.util.HashMap) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) AnnotationDescr(org.drools.compiler.lang.descr.AnnotationDescr)

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