use of org.drools.compiler.lang.descr.BaseDescr in project drools by kiegroup.
the class DRL5Parser method constraint.
/**
* constraint := nestedConstraint | conditionalOrExpression
* @param pattern
* @throws RecognitionException
*/
private void constraint(PatternDescrBuilder<?> pattern, boolean positional, String prefix) throws RecognitionException {
if (speculateNestedConstraint()) {
nestedConstraint(pattern, prefix);
return;
}
if (state.backtracking == 0) {
helper.emit(Location.LOCATION_LHS_INSIDE_CONDITION_START);
}
int first = input.index();
// resetting
exprParser.getHelper().setHasOperator(false);
try {
exprParser.conditionalOrExpression();
} finally {
if (state.backtracking == 0) {
if (input.LA(1) == DRL5Lexer.ID && input.LA(2) == DRL5Lexer.EOF) {
helper.emit(Location.LOCATION_LHS_INSIDE_CONDITION_ARGUMENT);
} else if (input.LA(1) != DRL5Lexer.EOF) {
helper.emit(Location.LOCATION_LHS_INSIDE_CONDITION_END);
} else if (!lastTokenWasWhiteSpace()) {
int location = getCurrentLocation();
if (location == Location.LOCATION_LHS_INSIDE_CONDITION_END) {
helper.emit(Location.LOCATION_LHS_INSIDE_CONDITION_ARGUMENT);
} else if (input.get(input.index()).getType() != DRL5Lexer.EOF) {
helper.emit(Location.LOCATION_LHS_INSIDE_CONDITION_START);
}
} else if (getCurrentLocation() == Location.LOCATION_LHS_INSIDE_CONDITION_START && !exprParser.getHelper().getHasOperator() && lastTokenWasWhiteSpace() && input.LA(1) == DRL5Lexer.EOF && input.LA(-1) == DRL5Lexer.ID) {
helper.emit(Location.LOCATION_LHS_INSIDE_CONDITION_OPERATOR);
}
}
}
if (state.failed)
return;
if (state.backtracking == 0 && input.index() > first) {
// expression consumed something
int last = input.LT(-1).getTokenIndex();
String expr = toExpression(prefix, first, last);
pattern.constraint(expr, positional);
BaseDescr constrDescr = pattern.getDescr().getDescrs().get(pattern.getDescr().getDescrs().size() - 1);
constrDescr.setLocation(input.get(first).getLine(), input.get(first).getCharPositionInLine());
constrDescr.setEndLocation(input.get(last).getLine(), input.get(last).getCharPositionInLine());
constrDescr.setStartCharacter(((CommonToken) input.get(first)).getStartIndex());
constrDescr.setEndCharacter(((CommonToken) input.get(last)).getStopIndex());
}
}
use of org.drools.compiler.lang.descr.BaseDescr in project drools by kiegroup.
the class DRL5Parser method lhsExpression.
/**
* lhsExpression := lhsOr*
*
* @param lhs
* @throws RecognitionException
*/
private void lhsExpression(CEDescrBuilder<?, AndDescr> lhs) throws RecognitionException {
helper.start(lhs, CEDescrBuilder.class, null);
if (state.backtracking == 0) {
helper.emit(Location.LOCATION_LHS_BEGIN_OF_CONDITION);
}
try {
while (input.LA(1) != DRL5Lexer.EOF && !helper.validateIdentifierKey(DroolsSoftKeywords.THEN) && !helper.validateIdentifierKey(DroolsSoftKeywords.END)) {
if (state.backtracking == 0) {
helper.emit(Location.LOCATION_LHS_BEGIN_OF_CONDITION);
}
lhsOr(lhs, true);
if (lhs.getDescr() != null && lhs.getDescr() instanceof ConditionalElementDescr) {
ConditionalElementDescr root = (ConditionalElementDescr) lhs.getDescr();
BaseDescr[] descrs = root.getDescrs().toArray(new BaseDescr[root.getDescrs().size()]);
root.getDescrs().clear();
for (int i = 0; i < descrs.length; i++) {
root.addOrMerge(descrs[i]);
}
}
if (state.failed)
return;
}
} finally {
helper.end(CEDescrBuilder.class, lhs);
}
}
use of org.drools.compiler.lang.descr.BaseDescr in project drools by kiegroup.
the class RuleParserTest method testForall.
@Test
public void testForall() throws Exception {
final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "forall.drl");
assertEquals(1, pkg.getRules().size());
final RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
assertEquals(1, rule.getLhs().getDescrs().size());
final ForallDescr forall = (ForallDescr) rule.getLhs().getDescrs().get(0);
assertEquals(2, forall.getDescrs().size());
final PatternDescr pattern = forall.getBasePattern();
assertEquals("Person", pattern.getObjectType());
final List<BaseDescr> remaining = forall.getRemainingPatterns();
assertEquals(1, remaining.size());
final PatternDescr cheese = (PatternDescr) remaining.get(0);
assertEquals("Cheese", cheese.getObjectType());
}
use of org.drools.compiler.lang.descr.BaseDescr in project drools by kiegroup.
the class RuleParserTest method testOrWithSpecialBind.
@Test
public void testOrWithSpecialBind() throws Exception {
String source = "rule \"A and (B or C or D)\" \n" + " when \n" + " pdo1 : ParametricDataObject( paramID == 101, stringValue == \"1000\" ) and \n" + " pdo2 :(ParametricDataObject( paramID == 101, stringValue == \"1001\" ) or \n" + " ParametricDataObject( paramID == 101, stringValue == \"1002\" ) or \n" + " ParametricDataObject( paramID == 101, stringValue == \"1003\" )) \n" + " then \n" + " System.out.println( \"Rule: A and (B or C or D) Fired. pdo1: \" + pdo1 + \" pdo2: \"+ pdo2); \n" + "end\n";
PackageDescr pkg = (PackageDescr) parse("compilationUnit", source);
assertFalse(parser.getErrors().toString(), parser.hasErrors());
RuleDescr rule = pkg.getRules().get(0);
AndDescr lhs = rule.getLhs();
assertEquals(2, lhs.getDescrs().size());
PatternDescr pdo1 = (PatternDescr) lhs.getDescrs().get(0);
assertEquals("pdo1", pdo1.getIdentifier());
OrDescr or = (OrDescr) rule.getLhs().getDescrs().get(1);
assertEquals(3, or.getDescrs().size());
for (BaseDescr pdo2 : or.getDescrs()) {
assertEquals("pdo2", ((PatternDescr) pdo2).getIdentifier());
}
}
use of org.drools.compiler.lang.descr.BaseDescr in project drools by kiegroup.
the class GroupElementBuilder method build.
public RuleConditionElement build(final RuleBuildContext context, final BaseDescr descr, final Pattern prefixPattern) {
final ConditionalElementDescr cedescr = (ConditionalElementDescr) descr;
final GroupElement ge = this.newGroupElementFor(descr);
context.getDeclarationResolver().pushOnBuildStack(ge);
if (prefixPattern != null) {
ge.addChild(prefixPattern);
}
// iterate over child descriptors
for (final BaseDescr child : cedescr.getDescrs()) {
// gets child to build
// child.setResource(..) does not seem to be necessary (since builderImpls have already set the resource for all children)
// but leaving it in here to be save
child.setResource(context.getRuleDescr().getResource());
child.setNamespace(context.getRuleDescr().getNamespace());
// gets corresponding builder
final RuleConditionBuilder builder = (RuleConditionBuilder) context.getDialect().getBuilder(child.getClass());
if (builder != null) {
final RuleConditionElement element = builder.build(context, child);
// builder will return null. Ex: ClassNotFound for the pattern type
if (element != null) {
ge.addChild(element);
}
} else {
throw new RuntimeException("BUG: no builder found for descriptor class " + child.getClass());
}
}
context.getDeclarationResolver().popBuildStack();
return ge;
}
Aggregations