use of org.drools.compiler.lang.descr.AttributeDescr in project drools by kiegroup.
the class DRL6Parser method attribute.
/**
* attribute :=
* salience
* | enabled
* | ( NO-LOOP
* | AUTO-FOCUS
* | LOCK-ON-ACTIVE
* | REFRACT
* | DIRECT
* ) BOOLEAN?
* | ( AGENDA-GROUP
* | ACTIVATION-GROUP
* | RULEFLOW-GROUP
* | DATE-EFFECTIVE
* | DATE-EXPIRES
* | DIALECT
* ) STRING
* | CALENDARS STRING (COMMA STRING)*
* | TIMER ( DECIMAL | chunk_(_) )
* | DURATION ( DECIMAL | chunk_(_) )
*
* The above syntax is not quite how this is parsed, because the soft keyword
* is determined by look-ahead and passed on to one of the x-Attribute methods
* (booleanAttribute, stringAttribute, stringListAttribute, intOrChunkAttribute)
* which will actually gobble the tokens.
*
* @return
*/
public AttributeDescr attribute(AttributeSupportBuilder<?> as) {
AttributeDescr attribute = null;
try {
if (state.backtracking == 0 && input.LA(1) != DRL6Lexer.EOF) {
helper.emit(Location.LOCATION_RULE_HEADER_KEYWORD);
}
if (helper.validateIdentifierKey(DroolsSoftKeywords.SALIENCE)) {
attribute = salience(as);
} else if (helper.validateIdentifierKey(DroolsSoftKeywords.ENABLED)) {
attribute = enabled(as);
} else if (helper.validateIdentifierKey(DroolsSoftKeywords.NO) && helper.validateLT(2, "-") && helper.validateLT(3, DroolsSoftKeywords.LOOP)) {
attribute = booleanAttribute(as, new String[] { DroolsSoftKeywords.NO, "-", DroolsSoftKeywords.LOOP });
} else if (helper.validateIdentifierKey(DroolsSoftKeywords.AUTO) && helper.validateLT(2, "-") && helper.validateLT(3, DroolsSoftKeywords.FOCUS)) {
attribute = booleanAttribute(as, new String[] { DroolsSoftKeywords.AUTO, "-", DroolsSoftKeywords.FOCUS });
} else if (helper.validateIdentifierKey(DroolsSoftKeywords.LOCK) && helper.validateLT(2, "-") && helper.validateLT(3, DroolsSoftKeywords.ON) && helper.validateLT(4, "-") && helper.validateLT(5, DroolsSoftKeywords.ACTIVE)) {
attribute = booleanAttribute(as, new String[] { DroolsSoftKeywords.LOCK, "-", DroolsSoftKeywords.ON, "-", DroolsSoftKeywords.ACTIVE });
} else if (helper.validateIdentifierKey(DroolsSoftKeywords.REFRACT)) {
attribute = booleanAttribute(as, new String[] { DroolsSoftKeywords.REFRACT });
} else if (helper.validateIdentifierKey(DroolsSoftKeywords.DIRECT)) {
attribute = booleanAttribute(as, new String[] { DroolsSoftKeywords.DIRECT });
} else if (helper.validateIdentifierKey(DroolsSoftKeywords.AGENDA) && helper.validateLT(2, "-") && helper.validateLT(3, DroolsSoftKeywords.GROUP)) {
attribute = stringAttribute(as, new String[] { DroolsSoftKeywords.AGENDA, "-", DroolsSoftKeywords.GROUP });
} else if (helper.validateIdentifierKey(DroolsSoftKeywords.ACTIVATION) && helper.validateLT(2, "-") && helper.validateLT(3, DroolsSoftKeywords.GROUP)) {
attribute = stringAttribute(as, new String[] { DroolsSoftKeywords.ACTIVATION, "-", DroolsSoftKeywords.GROUP });
} else if (helper.validateIdentifierKey(DroolsSoftKeywords.RULEFLOW) && helper.validateLT(2, "-") && helper.validateLT(3, DroolsSoftKeywords.GROUP)) {
attribute = stringAttribute(as, new String[] { DroolsSoftKeywords.RULEFLOW, "-", DroolsSoftKeywords.GROUP });
} else if (helper.validateIdentifierKey(DroolsSoftKeywords.DATE) && helper.validateLT(2, "-") && helper.validateLT(3, DroolsSoftKeywords.EFFECTIVE)) {
attribute = stringAttribute(as, new String[] { DroolsSoftKeywords.DATE, "-", DroolsSoftKeywords.EFFECTIVE });
attribute.setType(AttributeDescr.Type.DATE);
} else if (helper.validateIdentifierKey(DroolsSoftKeywords.DATE) && helper.validateLT(2, "-") && helper.validateLT(3, DroolsSoftKeywords.EXPIRES)) {
attribute = stringAttribute(as, new String[] { DroolsSoftKeywords.DATE, "-", DroolsSoftKeywords.EXPIRES });
attribute.setType(AttributeDescr.Type.DATE);
} else if (helper.validateIdentifierKey(DroolsSoftKeywords.DIALECT)) {
attribute = stringAttribute(as, new String[] { DroolsSoftKeywords.DIALECT });
} else if (helper.validateIdentifierKey(DroolsSoftKeywords.CALENDARS)) {
attribute = stringListAttribute(as, new String[] { DroolsSoftKeywords.CALENDARS });
} else if (helper.validateIdentifierKey(DroolsSoftKeywords.TIMER)) {
attribute = intOrChunkAttribute(as, new String[] { DroolsSoftKeywords.TIMER });
} else if (helper.validateIdentifierKey(DroolsSoftKeywords.DURATION)) {
attribute = intOrChunkAttribute(as, new String[] { DroolsSoftKeywords.DURATION });
}
if (state.backtracking == 0) {
helper.emit(Location.LOCATION_RULE_HEADER);
}
} catch (RecognitionException re) {
reportError(re);
}
return attribute;
}
use of org.drools.compiler.lang.descr.AttributeDescr in project drools by kiegroup.
the class RuleBuilderTest method testBuildDurationExpression.
@Test
public void testBuildDurationExpression() throws Exception {
// creates mock objects
final RuleBuildContext context = mock(RuleBuildContext.class);
final RuleImpl rule = mock(RuleImpl.class);
// creates input object
final RuleDescr ruleDescr = new RuleDescr("my rule");
ruleDescr.addAttribute(new AttributeDescr("duration", "( 1h30m )"));
ruleDescr.addAttribute(new AttributeDescr("calendars", "[\"cal1\", \"cal2\"]"));
// defining expectations on the mock object
when(context.getRule()).thenReturn(rule);
when(context.getRuleDescr()).thenReturn(ruleDescr);
// calling the build method
RuleBuilder.buildAttributes(context);
// check expectations
verify(rule).setTimer(new IntervalTimer(null, null, -1, TimeUtils.parseTimeString("1h30m"), 0));
verify(rule).setCalendars(new String[] { "cal1", "cal2" });
}
use of org.drools.compiler.lang.descr.AttributeDescr in project drools by kiegroup.
the class RuleBuilderTest method testBuild.
@Test
public void testBuild() throws Exception {
final DrlParser parser = new DrlParser(LanguageLevelOption.DRL5);
final KnowledgeBuilderImpl kBuilder = new KnowledgeBuilderImpl();
kBuilder.addPackage(new PackageDescr("org.drools"));
InternalKnowledgePackage pkg = kBuilder.getPackage("org.drools");
final PackageDescr pkgDescr = parser.parse(new InputStreamReader(getClass().getResourceAsStream("nestedConditionalElements.drl")));
// just checking there is no parsing errors
assertFalse(parser.getErrors().toString(), parser.hasErrors());
pkg.addGlobal("results", List.class);
final RuleDescr ruleDescr = pkgDescr.getRules().get(0);
final String ruleClassName = "RuleClassName.java";
ruleDescr.setClassName(ruleClassName);
ruleDescr.addAttribute(new AttributeDescr("dialect", "java"));
kBuilder.addPackage(pkgDescr);
assertTrue(kBuilder.getErrors().toString(), kBuilder.getErrors().isEmpty());
final RuleImpl rule = kBuilder.getPackage("org.drools.compiler").getRule("test nested CEs");
assertEquals("There should be 2 rule level declarations", 2, rule.getDeclarations().size());
// second GE should be a not
final GroupElement not = (GroupElement) rule.getLhs().getChildren().get(1);
assertTrue(not.isNot());
// not has no outer declarations
assertTrue(not.getOuterDeclarations().isEmpty());
assertEquals(1, not.getInnerDeclarations().size());
assertTrue(not.getInnerDeclarations().keySet().contains("$state"));
// second not
final GroupElement not2 = (GroupElement) ((GroupElement) not.getChildren().get(0)).getChildren().get(1);
assertTrue(not2.isNot());
// not has no outer declarations
assertTrue(not2.getOuterDeclarations().isEmpty());
assertEquals(1, not2.getInnerDeclarations().size());
assertTrue(not2.getInnerDeclarations().keySet().contains("$likes"));
}
use of org.drools.compiler.lang.descr.AttributeDescr in project drools by kiegroup.
the class MVELSalienceBuilderTest method setUp.
@Before
public void setUp() throws Exception {
InternalKnowledgePackage pkg = new KnowledgePackageImpl("pkg1");
final RuleDescr ruleDescr = new RuleDescr("rule 1");
ruleDescr.addAttribute(new AttributeDescr("salience", "(p.age + 20)/2"));
ruleDescr.setConsequence("");
KnowledgeBuilderImpl pkgBuilder = new KnowledgeBuilderImpl(pkg);
DialectCompiletimeRegistry dialectRegistry = pkgBuilder.getPackageRegistry(pkg.getName()).getDialectCompiletimeRegistry();
MVELDialect mvelDialect = (MVELDialect) dialectRegistry.getDialect("mvel");
context = new RuleBuildContext(pkgBuilder, ruleDescr, dialectRegistry, pkg, mvelDialect);
final InstrumentedDeclarationScopeResolver declarationResolver = new InstrumentedDeclarationScopeResolver();
final ObjectType personObjeectType = new ClassObjectType(Person.class);
final Pattern pattern = new Pattern(0, personObjeectType);
final PatternExtractor extractor = new PatternExtractor(personObjeectType);
final Declaration declaration = new Declaration("p", extractor, pattern);
final Map<String, Declaration> map = new HashMap<String, Declaration>();
map.put("p", declaration);
declarationResolver.setDeclarations(map);
context.setDeclarationResolver(declarationResolver);
kBase = KnowledgeBaseFactory.newKnowledgeBase();
SalienceBuilder salienceBuilder = new MVELSalienceBuilder();
salienceBuilder.build(context);
((MVELSalienceExpression) context.getRule().getSalience()).compile((MVELDialectRuntimeData) context.getPkg().getDialectRuntimeRegistry().getDialectData("mvel"));
}
use of org.drools.compiler.lang.descr.AttributeDescr in project drools by kiegroup.
the class RuleParserTest method testAttributes.
@Test
public void testAttributes() throws Exception {
final RuleDescr rule = (RuleDescr) parseResource("rule", "rule_attributes.drl");
assertEquals("simple_rule", rule.getName());
assertEqualsIgnoreWhitespace("bar();", (String) rule.getConsequence());
final Map<String, AttributeDescr> attrs = rule.getAttributes();
assertEquals(6, attrs.size());
AttributeDescr at = (AttributeDescr) attrs.get("salience");
assertEquals("salience", at.getName());
assertEquals("42", at.getValue());
at = (AttributeDescr) attrs.get("agenda-group");
assertEquals("agenda-group", at.getName());
assertEquals("my_group", at.getValue());
at = (AttributeDescr) attrs.get("no-loop");
assertEquals("no-loop", at.getName());
assertEquals("true", at.getValue());
at = (AttributeDescr) attrs.get("duration");
assertEquals("duration", at.getName());
assertEquals("42", at.getValue());
at = (AttributeDescr) attrs.get("activation-group");
assertEquals("activation-group", at.getName());
assertEquals("my_activation_group", at.getValue());
at = (AttributeDescr) attrs.get("lock-on-active");
assertEquals("lock-on-active", at.getName());
assertEquals("true", at.getValue());
}
Aggregations