use of org.drools.compiler.lang.descr.OrDescr in project drools by kiegroup.
the class DRL5Parser method lhsPatternBind.
/**
* lhsPatternBind := label?
* ( LEFT_PAREN lhsPattern (OR lhsPattern)* RIGHT_PAREN
* | lhsPattern )
*
* @param ce
* @return
* @throws RecognitionException
*/
@SuppressWarnings("unchecked")
private BaseDescr lhsPatternBind(PatternContainerDescrBuilder<?, ?> ce, final boolean allowOr) throws RecognitionException {
PatternDescrBuilder<?> pattern = null;
CEDescrBuilder<?, OrDescr> or = null;
BaseDescr result = null;
Token first = input.LT(1);
pattern = helper.start((DescrBuilder<?, ?>) ce, PatternDescrBuilder.class, null);
if (pattern != null) {
result = pattern.getDescr();
}
String label = null;
boolean isUnification = false;
if (input.LA(1) == DRL5Lexer.ID && input.LA(2) == DRL5Lexer.COLON && !helper.validateCEKeyword(1)) {
label = label(DroolsEditorType.IDENTIFIER_PATTERN);
if (state.failed)
return null;
} else if (input.LA(1) == DRL5Lexer.ID && input.LA(2) == DRL5Lexer.UNIFY && !helper.validateCEKeyword(1)) {
label = unif(DroolsEditorType.IDENTIFIER_PATTERN);
if (state.failed)
return null;
isUnification = true;
}
if (input.LA(1) == DRL5Lexer.LEFT_PAREN) {
try {
match(input, DRL5Lexer.LEFT_PAREN, null, null, DroolsEditorType.SYMBOL);
if (state.failed)
return null;
if (helper.validateCEKeyword(1)) {
failMismatchedTokenException();
// in case it is backtracking
return null;
}
lhsPattern(pattern, label, isUnification);
if (state.failed)
return null;
if (allowOr && helper.validateIdentifierKey(DroolsSoftKeywords.OR) && ce instanceof CEDescrBuilder) {
if (state.backtracking == 0) {
// this is necessary because of the crappy bind with multi-pattern OR syntax
or = ((CEDescrBuilder<DescrBuilder<?, ?>, OrDescr>) ce).or();
result = or.getDescr();
helper.end(PatternDescrBuilder.class, pattern);
helper.start(or, CEDescrBuilder.class, null);
// adjust real or starting token:
helper.setStart(or, first);
// remove original pattern from the parent CE child list:
((ConditionalElementDescr) ce.getDescr()).getDescrs().remove(pattern.getDescr());
// add pattern to the OR instead
or.getDescr().addDescr(pattern.getDescr());
}
while (helper.validateIdentifierKey(DroolsSoftKeywords.OR)) {
match(input, DRL5Lexer.ID, DroolsSoftKeywords.OR, null, DroolsEditorType.KEYWORD);
if (state.failed)
return null;
pattern = helper.start(or, PatternDescrBuilder.class, null);
// new pattern, same binding
lhsPattern(pattern, label, isUnification);
if (state.failed)
return null;
helper.end(PatternDescrBuilder.class, pattern);
}
}
match(input, DRL5Lexer.RIGHT_PAREN, null, null, DroolsEditorType.SYMBOL);
if (state.failed)
return null;
} finally {
if (or != null) {
helper.end(CEDescrBuilder.class, or);
} else {
helper.end(PatternDescrBuilder.class, pattern);
}
}
} else {
try {
lhsPattern(pattern, label, isUnification);
if (state.failed)
return null;
} finally {
helper.end(PatternDescrBuilder.class, pattern);
}
}
return result;
}
use of org.drools.compiler.lang.descr.OrDescr in project drools by kiegroup.
the class DRL5Parser method lhsOr.
/**
* lhsOr := LEFT_PAREN OR lhsAnd+ RIGHT_PAREN
* | lhsAnd (OR lhsAnd)*
*
* @param ce
* @param allowOr
* @throws RecognitionException
*/
private BaseDescr lhsOr(final CEDescrBuilder<?, ?> ce, boolean allowOr) throws RecognitionException {
BaseDescr result = null;
if (allowOr && input.LA(1) == DRL5Lexer.LEFT_PAREN && helper.validateLT(2, DroolsSoftKeywords.OR)) {
// prefixed OR
CEDescrBuilder<?, OrDescr> or = null;
if (state.backtracking == 0) {
or = ce.or();
result = or.getDescr();
helper.start(or, CEDescrBuilder.class, null);
}
try {
match(input, DRL5Lexer.LEFT_PAREN, null, null, DroolsEditorType.SYMBOL);
if (state.failed)
return null;
match(input, DRL5Lexer.ID, DroolsSoftKeywords.OR, null, DroolsEditorType.KEYWORD);
if (state.failed)
return null;
while (input.LA(1) == DRL5Lexer.AT) {
// annotation*
annotation(or);
if (state.failed)
return null;
}
if (state.backtracking == 0) {
helper.emit(Location.LOCATION_LHS_BEGIN_OF_CONDITION_AND_OR);
}
while (input.LA(1) != DRL5Lexer.RIGHT_PAREN) {
lhsAnd(or, allowOr);
if (state.failed)
return null;
}
match(input, DRL5Lexer.RIGHT_PAREN, null, null, DroolsEditorType.SYMBOL);
if (state.failed)
return null;
} finally {
if (state.backtracking == 0) {
helper.end(CEDescrBuilder.class, or);
}
}
} else {
// infix OR
// create an OR anyway, as if it is not an OR we remove it later
CEDescrBuilder<?, OrDescr> or = null;
if (state.backtracking == 0) {
or = ce.or();
result = or.getDescr();
helper.start(or, CEDescrBuilder.class, null);
}
try {
lhsAnd(or, allowOr);
if (state.failed)
return null;
if (allowOr && (helper.validateIdentifierKey(DroolsSoftKeywords.OR) || input.LA(1) == DRL5Lexer.DOUBLE_PIPE)) {
while (helper.validateIdentifierKey(DroolsSoftKeywords.OR) || input.LA(1) == DRL5Lexer.DOUBLE_PIPE) {
if (input.LA(1) == DRL5Lexer.DOUBLE_PIPE) {
match(input, DRL5Lexer.DOUBLE_PIPE, null, null, DroolsEditorType.SYMBOL);
} else {
match(input, DRL5Lexer.ID, DroolsSoftKeywords.OR, null, DroolsEditorType.KEYWORD);
}
if (state.failed)
return null;
while (input.LA(1) == DRL5Lexer.AT) {
// annotation*
annotation(or);
if (state.failed)
return null;
}
if (state.backtracking == 0) {
helper.emit(Location.LOCATION_LHS_BEGIN_OF_CONDITION_AND_OR);
}
lhsAnd(or, allowOr);
if (state.failed)
return null;
}
} else if (allowOr) {
if (state.backtracking == 0) {
// if no OR present, then remove it and add children to parent
((ConditionalElementDescr) ce.getDescr()).getDescrs().remove(or.getDescr());
for (BaseDescr base : or.getDescr().getDescrs()) {
((ConditionalElementDescr) ce.getDescr()).addDescr(base);
}
result = ce.getDescr();
}
}
} finally {
if (state.backtracking == 0) {
helper.end(CEDescrBuilder.class, or);
}
}
}
return result;
}
use of org.drools.compiler.lang.descr.OrDescr in project drools by kiegroup.
the class CEDescrBuilderImpl method or.
/**
* {@inheritDoc}
*/
public CEDescrBuilder<CEDescrBuilder<P, T>, OrDescr> or() {
OrDescr orDescr = new OrDescr();
((ConditionalElementDescr) descr).addDescr(orDescr);
return new CEDescrBuilderImpl<CEDescrBuilder<P, T>, OrDescr>(this, orDescr);
}
use of org.drools.compiler.lang.descr.OrDescr in project drools by kiegroup.
the class RuleParserTest method testOrBindingWithBrackets.
@Test
public void testOrBindingWithBrackets() throws Exception {
final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "or_binding_with_brackets.drl");
assertEquals(1, pkg.getRules().size());
final RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
assertEquals(1, rule.getLhs().getDescrs().size());
assertEquals(1, rule.getLhs().getDescrs().size());
final OrDescr or = (OrDescr) rule.getLhs().getDescrs().get(0);
assertEquals(2, or.getDescrs().size());
// first fact
final PatternDescr firstFact = (PatternDescr) or.getDescrs().get(0);
assertEquals("Person", firstFact.getObjectType());
assertEquals("foo", firstFact.getIdentifier());
// second "option"
final PatternDescr secondFact = (PatternDescr) or.getDescrs().get(0);
assertEquals("Person", secondFact.getObjectType());
assertEquals("foo", secondFact.getIdentifier());
assertEqualsIgnoreWhitespace("System.out.println( \"Mark and Michael\" + bar );", (String) rule.getConsequence());
}
use of org.drools.compiler.lang.descr.OrDescr in project drools by kiegroup.
the class RuleParserTest method testOrBindingComplex.
/**
* test basic foo : Fact() || Fact() stuff binding to an "or"
*/
@Test
public void testOrBindingComplex() throws Exception {
final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "or_binding_complex.drl");
assertEquals(1, pkg.getRules().size());
final RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
assertEquals(1, rule.getLhs().getDescrs().size());
assertEquals(1, rule.getLhs().getDescrs().size());
final OrDescr or = (OrDescr) rule.getLhs().getDescrs().get(0);
assertEquals(2, or.getDescrs().size());
// first fact
final PatternDescr firstFact = (PatternDescr) or.getDescrs().get(0);
assertEquals("Person", firstFact.getObjectType());
assertEquals("foo", firstFact.getIdentifier());
// second "option"
final PatternDescr secondFact = (PatternDescr) or.getDescrs().get(1);
assertEquals("Person", secondFact.getObjectType());
assertEquals(1, secondFact.getConstraint().getDescrs().size());
assertEquals("foo", secondFact.getIdentifier());
assertEqualsIgnoreWhitespace("System.out.println( \"Mark and Michael\" + bar );", (String) rule.getConsequence());
}
Aggregations