Search in sources :

Example 66 with RuleImpl

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

the class CepEspTest method testDelayingNot.

@Test(timeout = 10000)
public void testDelayingNot() throws Exception {
    // read in the source
    KieBaseConfiguration conf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    conf.setOption(EventProcessingOption.STREAM);
    final KieBase kbase = loadKnowledgeBase(conf, "test_CEP_DelayingNot.drl");
    KieSessionConfiguration sconf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    sconf.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
    KieSession wm = createKnowledgeSession(kbase, sconf);
    final RuleImpl rule = (RuleImpl) kbase.getRule("org.drools.compiler", "Delaying Not");
    assertEquals(10000, ((DurationTimer) rule.getTimer()).getDuration());
    final List results = new ArrayList();
    wm.setGlobal("results", results);
    SessionPseudoClock clock = (SessionPseudoClock) wm.getSessionClock();
    clock.advanceTime(10, TimeUnit.SECONDS);
    StockTickInterface st1O = new StockTick(1, "DROO", 100, clock.getCurrentTime());
    EventFactHandle st1 = (EventFactHandle) wm.insert(st1O);
    wm.fireAllRules();
    // should not fire, because it must wait 10 seconds
    assertEquals(0, results.size());
    clock.advanceTime(5, TimeUnit.SECONDS);
    EventFactHandle st2 = (EventFactHandle) wm.insert(new StockTick(1, "DROO", 80, clock.getCurrentTime()));
    wm.fireAllRules();
    // should still not fire, because it must wait 5 more seconds, and st2 has lower price (80)
    assertEquals(0, results.size());
    // assert new data
    wm.fireAllRules();
    clock.advanceTime(6, TimeUnit.SECONDS);
    wm.fireAllRules();
    // should fire, because waited for 10 seconds and no other event arrived with a price increase
    assertEquals(1, results.size());
    assertEquals(st1O, results.get(0));
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) StockTickInterface(org.drools.compiler.StockTickInterface) StockTick(org.drools.compiler.StockTick) SessionPseudoClock(org.kie.api.time.SessionPseudoClock) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) EventFactHandle(org.drools.core.common.EventFactHandle) KieSession(org.kie.api.runtime.KieSession) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) ArrayList(java.util.ArrayList) List(java.util.List) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) Test(org.junit.Test)

Example 67 with RuleImpl

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

the class AnnotationsTest method testNestedAnnotations.

@Test
public void testNestedAnnotations() {
    String drl = "package org.drools.test; " + "import " + Outer.class.getName().replace("$", ".") + "; " + "import " + Inner.class.getName().replace("$", ".") + "; " + "rule Foo " + "when " + "  String() @Outer( value = @Inner( text = \"world\" ) ) " + "then " + "end ";
    KieHelper helper = new KieHelper();
    helper.addContent(drl, ResourceType.DRL);
    Pattern p = ((Pattern) ((RuleImpl) helper.build().getRule("org.drools.test", "Foo")).getLhs().getChildren().get(0));
    Map<String, AnnotationDefinition> defs = p.getAnnotations();
    assertEquals(1, defs.size());
    AnnotationDefinition outer = defs.get(Outer.class.getName().replace("$", "."));
    assertNotNull(outer);
    Object val = outer.getPropertyValue("value");
    assertNotNull(val);
    assertTrue(val instanceof AnnotationDefinition);
    AnnotationDefinition inner = (AnnotationDefinition) val;
    assertEquals("world", inner.getPropertyValue("text"));
}
Also used : Pattern(org.drools.core.rule.Pattern) AnnotationDefinition(org.drools.core.factmodel.AnnotationDefinition) KieHelper(org.kie.internal.utils.KieHelper) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) Test(org.junit.Test)

Example 68 with RuleImpl

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

the class AnnotationsTest method testNestedAnnotationsWithMultiplicity.

@Test
public void testNestedAnnotationsWithMultiplicity() {
    String drl = "package org.drools.test; " + "import " + Outer.class.getName().replace("$", ".") + "; " + "import " + Inner.class.getName().replace("$", ".") + "; " + "rule Foo " + "when " + "  String() @Outer( values = { @Inner( text = \"hello\" ), @Inner( text = \"world\" ) } ) " + "then " + "end ";
    KieHelper helper = new KieHelper();
    helper.addContent(drl, ResourceType.DRL);
    assertEquals(0, helper.verify().getMessages().size());
    Pattern p = ((Pattern) ((RuleImpl) helper.build().getRule("org.drools.test", "Foo")).getLhs().getChildren().get(0));
    Map<String, AnnotationDefinition> defs = p.getAnnotations();
    assertEquals(1, defs.size());
    AnnotationDefinition outer = defs.get(Outer.class.getName().replace("$", "."));
    assertNotNull(outer);
    Object val = outer.getPropertyValue("values");
    assertNotNull(val);
    assertTrue(val instanceof AnnotationDefinition[]);
}
Also used : Pattern(org.drools.core.rule.Pattern) AnnotationDefinition(org.drools.core.factmodel.AnnotationDefinition) KieHelper(org.kie.internal.utils.KieHelper) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) Test(org.junit.Test)

Example 69 with RuleImpl

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

the class DefaultTemplateRuleBaseTest method testSimpleTemplate.

@SuppressWarnings("unchecked")
@Test
public void testSimpleTemplate() throws Exception {
    TemplateContainer tc = new TemplateContainer() {

        private Column[] columns = new Column[] { new LongColumn("column1"), new LongColumn("column2"), new StringColumn("column3") };

        public Column[] getColumns() {
            return columns;
        }

        public String getHeader() {
            return null;
        }

        public Map<String, RuleTemplate> getTemplates() {
            Map<String, RuleTemplate> templates = new HashMap<String, RuleTemplate>();
            RuleTemplate ruleTemplate = new RuleTemplate("template1", this);
            ruleTemplate.addColumn("column1 == 10");
            ruleTemplate.addColumn("column2 < 5 || > 20");
            ruleTemplate.addColumn("column3 == \"xyz\"");
            templates.put("template1", ruleTemplate);
            return templates;
        }

        public Column getColumn(String name) {
            return columns[Integer.parseInt(name.substring(6)) - 1];
        }
    };
    DefaultTemplateRuleBase ruleBase = new DefaultTemplateRuleBase(tc);
    InternalKnowledgePackage[] packages = ((KnowledgeBaseImpl) ruleBase.newStatefulSession().getKieBase()).getPackages();
    assertEquals(1, packages.length);
    Map<String, String> globals = packages[0].getGlobals();
    assertEquals(DefaultGenerator.class.getName(), globals.get("generator"));
    Collection<org.kie.api.definition.rule.Rule> rules = packages[0].getRules();
    assertEquals(1, rules.size());
    assertEquals("template1", rules.iterator().next().getName());
    GroupElement lhs = ((RuleImpl) rules.iterator().next()).getLhs();
    // when
    // r : Row()
    // column1 : Column(name == "column1")
    // exists LongCell(row == r, column == column1, value == 10)
    // column2 : Column(name == "column2")
    // exists LongCell(row == r, column == column2, value < 5 | > 20)
    // column3 : Column(name == "column3")
    // exists StringCell(row == r, column == column3, value == "xyz")
    assertEquals(7, lhs.getChildren().size());
    org.drools.core.rule.Pattern pattern = (org.drools.core.rule.Pattern) lhs.getChildren().get(1);
    assertEquals(1, pattern.getConstraints().size());
    Constraint constraint = pattern.getConstraints().get(0);
    GroupElement exists = (GroupElement) lhs.getChildren().get(2);
    pattern = (org.drools.core.rule.Pattern) exists.getChildren().get(0);
    assertEquals(3, pattern.getConstraints().size());
    IndexableConstraint vconstraint = (IndexableConstraint) pattern.getConstraints().get(1);
    assertEquals(Column.class, vconstraint.getFieldIndex().getExtractor().getExtractToClass());
    assertEquals("column1", vconstraint.getRequiredDeclarations()[0].getIdentifier());
    pattern = (org.drools.core.rule.Pattern) lhs.getChildren().get(3);
    assertEquals(1, pattern.getConstraints().size());
    constraint = pattern.getConstraints().get(0);
    exists = (GroupElement) lhs.getChildren().get(4);
    pattern = (org.drools.core.rule.Pattern) exists.getChildren().get(0);
    assertEquals(3, pattern.getConstraints().size());
    vconstraint = (IndexableConstraint) pattern.getConstraints().get(1);
    assertEquals(Column.class, vconstraint.getFieldIndex().getExtractor().getExtractToClass());
    assertEquals("column2", vconstraint.getRequiredDeclarations()[0].getIdentifier());
    pattern = (org.drools.core.rule.Pattern) lhs.getChildren().get(5);
    assertEquals(1, pattern.getConstraints().size());
    constraint = pattern.getConstraints().get(0);
    exists = (GroupElement) lhs.getChildren().get(6);
    pattern = (org.drools.core.rule.Pattern) exists.getChildren().get(0);
    assertEquals(3, pattern.getConstraints().size());
    vconstraint = (IndexableConstraint) pattern.getConstraints().get(1);
    assertEquals(Column.class, vconstraint.getFieldIndex().getExtractor().getExtractToClass());
    assertEquals("column3", vconstraint.getRequiredDeclarations()[0].getIdentifier());
}
Also used : HashMap(java.util.HashMap) Constraint(org.drools.core.spi.Constraint) IndexableConstraint(org.drools.core.rule.IndexableConstraint) GroupElement(org.drools.core.rule.GroupElement) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) IndexableConstraint(org.drools.core.rule.IndexableConstraint) KnowledgeBaseImpl(org.drools.core.impl.KnowledgeBaseImpl) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) Test(org.junit.Test)

Example 70 with RuleImpl

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

the class ConditionalBranchBuilder method getLastPattern.

private Pattern getLastPattern(RuleBuildContext context) {
    GroupElement ge = (GroupElement) context.getDeclarationResolver().peekBuildStack();
    Pattern lastPattern = getLastPattern(ge.getChildren());
    if (lastPattern == null) {
        RuleImpl parent = context.getRule().getParent();
        if (parent != null) {
            lastPattern = getLastPattern(parent.getLhs().getChildren());
        }
    }
    return lastPattern;
}
Also used : Pattern(org.drools.core.rule.Pattern) GroupElement(org.drools.core.rule.GroupElement) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl)

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