use of org.drools.drl.ast.descr.BaseDescr in project drools by kiegroup.
the class Query method buildPattern.
@Override
public void buildPattern() {
NameExpr queryCall = new NameExpr(toQueryDef(pattern.getObjectType()));
MethodCallExpr callCall = new MethodCallExpr(queryCall, QUERY_INVOCATION_CALL);
callCall.addArgument("" + !pattern.isQuery());
if (!constraintDescrs.isEmpty()) {
List<QueryParameter> queryParams = packageModel.queryVariables(queryName);
if (queryParams.size() != constraintDescrs.size()) {
context.addCompilationError(new InvalidExpressionErrorResult("Wrong number of argument invoking query '" + queryName + "'"));
return;
}
Expression[] queryArgs = new Expression[queryParams.size()];
for (int i = 0; i < constraintDescrs.size(); i++) {
BaseDescr baseDescr = constraintDescrs.get(i);
String itemText = baseDescr.getText();
boolean isPositional = baseDescr instanceof ExprConstraintDescr && ((ExprConstraintDescr) baseDescr).getType() == ExprConstraintDescr.Type.POSITIONAL;
boolean isBinding = baseDescr instanceof BindingDescr || itemText.contains(":");
if ((!isPositional) && (!isBinding)) {
// error, can't have non binding slots.
context.addCompilationError(new InvalidExpressionErrorResult("Query's must use positional or bindings, not field constraints: " + itemText));
} else if (isPositional && isBinding) {
// error, can't have positional binding slots.
context.addCompilationError(new InvalidExpressionErrorResult("Query's can't use positional bindings: " + itemText));
}
int colonPos = itemText.indexOf(':');
if (colonPos > 0) {
String bindingId = itemText.substring(0, colonPos).trim();
String paramName = itemText.substring(colonPos + 1).trim();
for (int j = 0; j < queryParams.size(); j++) {
if (queryParams.get(j).getName().equals(paramName)) {
addQueryArg(queryParams, queryArgs, bindingId, j);
break;
} else if (queryParams.get(j).getName().equals(bindingId)) {
addQueryArg(queryParams, queryArgs, paramName, j);
break;
}
}
} else {
addQueryArg(queryParams, queryArgs, itemText, i);
}
}
for (Expression queryArg : queryArgs) {
callCall.addArgument(queryArg);
}
}
context.addExpression(callCall);
}
use of org.drools.drl.ast.descr.BaseDescr in project drools by kiegroup.
the class DRL6StrictParser method declare.
/* ------------------------------------------------------------------------------------------------
* DECLARE STATEMENT
* ------------------------------------------------------------------------------------------------ */
/**
* declare := DECLARE
* | (ENTRY-POINT) => entryPointDeclaration
* | (WINDOW) => windowDeclaration
* | (TRAIT) => typeDeclaration (trait)
* | (ENUM) => enumDeclaration
* | typeDeclaration (class)
* END
*
* @return
* @throws org.antlr.runtime.RecognitionException
*/
public BaseDescr declare(PackageDescrBuilder pkg) throws RecognitionException {
BaseDescr declaration = null;
try {
DeclareDescrBuilder declare = helper.start(pkg, DeclareDescrBuilder.class, null);
// 'declare'
match(input, DRL6Lexer.ID, DroolsSoftKeywords.DECLARE, null, DroolsEditorType.KEYWORD);
if (state.failed)
return null;
if (helper.validateIdentifierKey(DroolsSoftKeywords.ENTRY)) {
// entry point declaration
declaration = entryPointDeclaration(declare);
} else if (helper.validateIdentifierKey(DroolsSoftKeywords.WINDOW)) {
// window declaration
declaration = windowDeclaration(declare);
} else if (helper.validateIdentifierKey(DroolsSoftKeywords.TRAIT)) {
// trait type declaration
// 'trait'
match(input, DRL6Lexer.ID, DroolsSoftKeywords.TRAIT, null, DroolsEditorType.KEYWORD);
if (state.failed)
return null;
declaration = typeDeclaration(declare, true);
} else if (helper.validateIdentifierKey(DroolsSoftKeywords.ENUM)) {
match(input, DRL6Lexer.ID, DroolsSoftKeywords.ENUM, null, DroolsEditorType.KEYWORD);
if (state.failed)
return null;
declaration = enumDeclaration(declare);
} else {
// class type declaration
declaration = typeDeclaration(declare, false);
}
} catch (RecognitionException re) {
reportError(re);
}
return declaration;
}
use of org.drools.drl.ast.descr.BaseDescr in project drools by kiegroup.
the class DRL6StrictParser method lhsOr.
/**
* lhsOr := LEFT_PAREN OR lhsAnd+ RIGHT_PAREN
* | lhsAnd (OR lhsAnd)*
*
* @param ce
* @param allowOr
* @throws org.antlr.runtime.RecognitionException
*/
private BaseDescr lhsOr(final CEDescrBuilder<?, ?> ce, boolean allowOr) throws RecognitionException {
BaseDescr result = null;
if (allowOr && input.LA(1) == DRL6Lexer.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, DRL6Lexer.LEFT_PAREN, null, null, DroolsEditorType.SYMBOL);
if (state.failed)
return null;
match(input, DRL6Lexer.ID, DroolsSoftKeywords.OR, null, DroolsEditorType.KEYWORD);
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) {
lhsAnd(or, 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, 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) == DRL6Lexer.DOUBLE_PIPE)) {
while (helper.validateIdentifierKey(DroolsSoftKeywords.OR) || input.LA(1) == DRL6Lexer.DOUBLE_PIPE) {
if (input.LA(1) == DRL6Lexer.DOUBLE_PIPE) {
match(input, DRL6Lexer.DOUBLE_PIPE, null, null, DroolsEditorType.SYMBOL);
} else {
match(input, DRL6Lexer.ID, DroolsSoftKeywords.OR, null, DroolsEditorType.KEYWORD);
}
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.drl.ast.descr.BaseDescr in project drools by kiegroup.
the class DRL6StrictParser method lhsExpression.
/**
* lhsExpression := lhsOr*
*
* @param lhs
* @throws org.antlr.runtime.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) != DRL6Lexer.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.drl.ast.descr.BaseDescr in project drools by kiegroup.
the class DRL6StrictParser method lhsAccumulate.
/**
* lhsAccumulate := (ACCUMULATE|ACC) LEFT_PAREN lhsAnd (COMMA|SEMICOLON)
* accumulateFunctionBinding (COMMA accumulateFunctionBinding)*
* (SEMICOLON constraints)?
* RIGHT_PAREN SEMICOLON?
*
* @param ce
* @return
* @throws org.antlr.runtime.RecognitionException
*/
private BaseDescr lhsAccumulate(PatternContainerDescrBuilder<?, ?> ce) throws RecognitionException {
PatternDescrBuilder<?> pattern = null;
BaseDescr result = null;
pattern = helper.start((DescrBuilder<?, ?>) ce, PatternDescrBuilder.class, null);
if (pattern != null) {
result = pattern.getDescr();
}
try {
if (state.backtracking == 0) {
pattern.type("Object[]");
pattern.isQuery(false);
// might have to add the implicit bindings as well
}
AccumulateDescrBuilder<?> accumulate = helper.start(pattern, AccumulateDescrBuilder.class, null);
try {
if (helper.validateIdentifierKey(DroolsSoftKeywords.ACCUMULATE)) {
match(input, DRL6Lexer.ID, DroolsSoftKeywords.ACCUMULATE, null, DroolsEditorType.KEYWORD);
} else {
// might be using the short mnemonic
match(input, DRL6Lexer.ID, DroolsSoftKeywords.ACC, null, DroolsEditorType.KEYWORD);
}
if (state.failed)
return null;
if (state.backtracking == 0 && input.LA(1) != DRL6Lexer.EOF) {
helper.emit(Location.LOCATION_LHS_FROM_ACCUMULATE);
}
match(input, DRL6Lexer.LEFT_PAREN, null, null, DroolsEditorType.SYMBOL);
if (state.failed)
return null;
CEDescrBuilder<?, AndDescr> source = accumulate.source();
try {
helper.start(source, CEDescrBuilder.class, null);
lhsAnd(source, false);
if (state.failed)
return null;
if (source.getDescr() != null && source.getDescr() instanceof ConditionalElementDescr) {
ConditionalElementDescr root = (ConditionalElementDescr) source.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]);
}
}
} finally {
helper.end(CEDescrBuilder.class, source);
}
if (input.LA(1) == DRL6Lexer.COMMA) {
match(input, DRL6Lexer.COMMA, null, null, DroolsEditorType.SYMBOL);
if (state.failed)
return null;
} else if (input.LA(-1) != DRL6Lexer.SEMICOLON) {
// lhsUnary will consume an optional SEMICOLON, so we need to check if it was consumed already
// or if we must fail consuming it now
match(input, DRL6Lexer.SEMICOLON, null, null, DroolsEditorType.SYMBOL);
if (state.failed)
return null;
}
// accumulate functions
accumulateFunctionBinding(accumulate);
if (state.failed)
return null;
while (input.LA(1) == DRL6Lexer.COMMA) {
match(input, DRL6Lexer.COMMA, null, null, DroolsEditorType.SYMBOL);
if (state.failed)
return null;
accumulateFunctionBinding(accumulate);
if (state.failed)
return null;
}
if (input.LA(1) == DRL6Lexer.SEMICOLON) {
match(input, DRL6Lexer.SEMICOLON, null, null, DroolsEditorType.SYMBOL);
if (state.failed)
return null;
constraints(pattern);
}
match(input, DRL6Lexer.RIGHT_PAREN, null, null, DroolsEditorType.SYMBOL);
if (state.failed)
return null;
} finally {
helper.end(AccumulateDescrBuilder.class, accumulate);
if (state.backtracking == 0 && input.LA(1) != DRL6Lexer.EOF) {
helper.emit(Location.LOCATION_LHS_BEGIN_OF_CONDITION);
}
}
} finally {
helper.end(PatternDescrBuilder.class, pattern);
}
if (input.LA(1) == DRL6Lexer.SEMICOLON) {
match(input, DRL6Lexer.SEMICOLON, null, null, DroolsEditorType.SYMBOL);
if (state.failed)
return null;
}
return result;
}
Aggregations