use of org.drools.drl.ast.descr.AndDescr in project drools by kiegroup.
the class KnowledgeBuilderTest method testPredicate.
@Test
public void testPredicate() throws Exception {
final KnowledgeBuilderImpl builder = new KnowledgeBuilderImpl();
final PackageDescr packageDescr = new PackageDescr("p1");
final RuleDescr ruleDescr = new RuleDescr("rule-1");
packageDescr.addRule(ruleDescr);
final AndDescr lhs = new AndDescr();
ruleDescr.setLhs(lhs);
final PatternDescr pattern = new PatternDescr(Cheese.class.getName(), "stilton");
lhs.addDescr(pattern);
final BindingDescr fieldBindingDescr = new BindingDescr("x", "price");
pattern.addConstraint(fieldBindingDescr);
final BindingDescr fieldBindingDescr2 = new BindingDescr("y", "price");
pattern.addConstraint(fieldBindingDescr2);
packageDescr.addGlobal(new GlobalDescr("map", "java.util.Map"));
final ExprConstraintDescr predicate = new ExprConstraintDescr("eval(( ( Integer )map.get( new Integer(x) )).intValue() == y)");
pattern.addConstraint(predicate);
ruleDescr.setConsequence("update(stilton);");
builder.addPackage(packageDescr);
assertLength(0, builder.getErrors().getErrors());
}
use of org.drools.drl.ast.descr.AndDescr in project drools by kiegroup.
the class KnowledgeBuilderTest method testDuplicateDeclaration.
// TODO we now allow bindings on declarations, so update the test for this
@Test
// TODO we now allow bindings on declarations, so update the test for this
@Ignore
public void testDuplicateDeclaration() {
final KnowledgeBuilderImpl builder = new KnowledgeBuilderImpl();
final PackageDescr packageDescr = new PackageDescr("p1");
final RuleDescr ruleDescr = new RuleDescr("rule-1");
packageDescr.addRule(ruleDescr);
final AndDescr lhs = new AndDescr();
ruleDescr.setLhs(lhs);
final PatternDescr pattern1 = new PatternDescr(Cheese.class.getName());
lhs.addDescr(pattern1);
final BindingDescr fieldBindingDescr = new BindingDescr("$type", "type");
final FieldConstraintDescr literalDescr = new FieldConstraintDescr("type");
literalDescr.addRestriction(new LiteralRestrictionDescr("==", "stilton"));
pattern1.addConstraint(fieldBindingDescr);
pattern1.addConstraint(literalDescr);
final PatternDescr pattern2 = new PatternDescr(Cheese.class.getName());
lhs.addDescr(pattern2);
pattern2.addConstraint(fieldBindingDescr);
ruleDescr.setConsequence("update(stilton);");
builder.addPackage(packageDescr);
assertLength(2, builder.getErrors().getErrors());
}
use of org.drools.drl.ast.descr.AndDescr in project drools by kiegroup.
the class KnowledgeBuilderTest method createReturnValueRule.
private void createReturnValueRule(final PackageDescr packageDescr, final String expression) {
final RuleDescr ruleDescr = new RuleDescr("rule-1");
packageDescr.addRule(ruleDescr);
final AndDescr lhs = new AndDescr();
ruleDescr.setLhs(lhs);
final PatternDescr pattern = new PatternDescr(Cheese.class.getName(), "stilton");
lhs.addDescr(pattern);
BindingDescr fieldBindingDescr = new BindingDescr("x", "price");
pattern.addConstraint(fieldBindingDescr);
fieldBindingDescr = new BindingDescr("y", "price");
pattern.addConstraint(fieldBindingDescr);
packageDescr.addGlobal(new GlobalDescr("map", "java.util.Map"));
pattern.addConstraint(new ExprConstraintDescr("price == (" + expression + ")"));
ruleDescr.setConsequence("update(stilton);");
}
use of org.drools.drl.ast.descr.AndDescr in project drools by kiegroup.
the class KnowledgeBuilderTest method testAnd.
@Test
public void testAnd() throws Exception {
final KnowledgeBuilderImpl builder = new KnowledgeBuilderImpl();
final RuleImpl rule = createRule(new AndDescr(), builder, "update(stilton);");
assertLength(0, builder.getErrors().getErrors());
final GroupElement lhs = rule.getLhs();
assertLength(1, lhs.getChildren());
final GroupElement and = (GroupElement) lhs.getChildren().get(0);
assertLength(1, and.getChildren());
final Pattern pattern = (Pattern) and.getChildren().get(0);
}
use of org.drools.drl.ast.descr.AndDescr in project drools by kiegroup.
the class DRL6Parser method lhsAnd.
/**
* lhsAnd := LEFT_PAREN AND lhsUnary+ RIGHT_PAREN
* | lhsUnary (AND lhsUnary)*
*
* @param ce
* @throws org.antlr.runtime.RecognitionException
*/
private BaseDescr lhsAnd(final CEDescrBuilder<?, ?> ce, boolean allowOr) throws RecognitionException {
BaseDescr result = null;
if (input.LA(1) == DRL6Lexer.LEFT_PAREN && helper.validateLT(2, DroolsSoftKeywords.AND)) {
// prefixed AND
CEDescrBuilder<?, AndDescr> and = null;
if (state.backtracking == 0) {
and = ce.and();
result = ce.getDescr();
helper.start(and, CEDescrBuilder.class, null);
}
try {
match(input, DRL6Lexer.LEFT_PAREN, null, null, DroolsEditorType.SYMBOL);
if (state.failed)
return null;
match(input, DRL6Lexer.ID, DroolsSoftKeywords.AND, null, DroolsEditorType.KEYWORD);
if (state.failed)
return null;
while (input.LA(1) == DRL6Lexer.AT) {
// annotation*
annotation(and);
if (state.failed)
return null;
}
if (state.backtracking == 0) {
helper.emit(Location.LOCATION_LHS_BEGIN_OF_CONDITION_AND_OR);
}
while (input.LA(1) != DRL6Lexer.RIGHT_PAREN) {
lhsUnary(and, allowOr);
if (state.failed)
return null;
}
match(input, DRL6Lexer.RIGHT_PAREN, null, null, DroolsEditorType.SYMBOL);
if (state.failed)
return null;
} finally {
if (state.backtracking == 0) {
helper.end(CEDescrBuilder.class, and);
}
}
} else {
// infix AND
// create an AND anyway, since if it is not an AND we remove it later
CEDescrBuilder<?, AndDescr> and = null;
if (state.backtracking == 0) {
and = ce.and();
result = and.getDescr();
helper.start(and, CEDescrBuilder.class, null);
}
try {
lhsUnary(and, allowOr);
if (state.failed)
return null;
if (helper.validateIdentifierKey(DroolsSoftKeywords.AND) || input.LA(1) == DRL6Lexer.DOUBLE_AMPER) {
while (helper.validateIdentifierKey(DroolsSoftKeywords.AND) || input.LA(1) == DRL6Lexer.DOUBLE_AMPER) {
if (input.LA(1) == DRL6Lexer.DOUBLE_AMPER) {
match(input, DRL6Lexer.DOUBLE_AMPER, null, null, DroolsEditorType.SYMBOL);
} else {
match(input, DRL6Lexer.ID, DroolsSoftKeywords.AND, null, DroolsEditorType.KEYWORD);
}
if (state.failed)
return null;
while (input.LA(1) == DRL6Lexer.AT) {
// annotation*
annotation(and);
if (state.failed)
return null;
}
if (state.backtracking == 0) {
helper.emit(Location.LOCATION_LHS_BEGIN_OF_CONDITION_AND_OR);
}
lhsUnary(and, allowOr);
if (state.failed)
return null;
}
} else {
if (state.backtracking == 0 && and.getDescr().getDescrs().size() < 2) {
// if no AND present, then remove it and add children to parent
((ConditionalElementDescr) ce.getDescr()).getDescrs().remove(and.getDescr());
for (BaseDescr base : and.getDescr().getDescrs()) {
((ConditionalElementDescr) ce.getDescr()).addDescr(base);
}
result = ce.getDescr();
}
}
} finally {
if (state.backtracking == 0) {
helper.end(CEDescrBuilder.class, and);
}
}
}
return result;
}
Aggregations