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));
}
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"));
}
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[]);
}
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());
}
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;
}
Aggregations