use of org.drools.compiler.rule.builder.dialect.java.parser.JavaLocalDeclarationDescr in project drools by kiegroup.
the class JavaParser method forStatement.
// $ANTLR end "ifStatement"
// $ANTLR start "forStatement"
// src/main/resources/org/drools/compiler/semantics/java/parser/Java.g:801:1: forStatement options {k=3; } : x= 'for' y= '(' ( ( ( variableModifier )* type id= Identifier z= ':' expression ) | ( ( forInit )? z= ';' ( expression )? ';' ( forUpdate )? ) ) ')' bs= statement ;
public final void forStatement() throws RecognitionException {
VarDecl_stack.push(new VarDecl_scope());
int forStatement_StartIndex = input.index();
Token x = null;
Token y = null;
Token id = null;
Token z = null;
ParserRuleReturnScope bs = null;
ParserRuleReturnScope variableModifier4 = null;
ParserRuleReturnScope type5 = null;
JavaForBlockDescr fd = null;
increaseLevel();
VarDecl_stack.peek().descr = new JavaLocalDeclarationDescr();
try {
if (state.backtracking > 0 && alreadyParsedRule(input, 86)) {
return;
}
// src/main/resources/org/drools/compiler/semantics/java/parser/Java.g:813:5: (x= 'for' y= '(' ( ( ( variableModifier )* type id= Identifier z= ':' expression ) | ( ( forInit )? z= ';' ( expression )? ';' ( forUpdate )? ) ) ')' bs= statement )
// src/main/resources/org/drools/compiler/semantics/java/parser/Java.g:814:5: x= 'for' y= '(' ( ( ( variableModifier )* type id= Identifier z= ':' expression ) | ( ( forInit )? z= ';' ( expression )? ';' ( forUpdate )? ) ) ')' bs= statement
{
x = (Token) match(input, 86, FOLLOW_86_in_forStatement3409);
if (state.failed)
return;
y = (Token) match(input, 36, FOLLOW_36_in_forStatement3413);
if (state.failed)
return;
if (state.backtracking == 0) {
fd = new JavaForBlockDescr();
fd.setStart(((CommonToken) x).getStartIndex());
pushContainerBlockDescr(fd, true);
fd.setStartParen(((CommonToken) y).getStartIndex());
}
// src/main/resources/org/drools/compiler/semantics/java/parser/Java.g:819:5: ( ( ( variableModifier )* type id= Identifier z= ':' expression ) | ( ( forInit )? z= ';' ( expression )? ';' ( forUpdate )? ) )
int alt118 = 2;
alt118 = dfa118.predict(input);
switch(alt118) {
case 1:
// src/main/resources/org/drools/compiler/semantics/java/parser/Java.g:820:9: ( ( variableModifier )* type id= Identifier z= ':' expression )
{
// src/main/resources/org/drools/compiler/semantics/java/parser/Java.g:820:9: ( ( variableModifier )* type id= Identifier z= ':' expression )
// src/main/resources/org/drools/compiler/semantics/java/parser/Java.g:820:11: ( variableModifier )* type id= Identifier z= ':' expression
{
// src/main/resources/org/drools/compiler/semantics/java/parser/Java.g:820:11: ( variableModifier )*
loop114: while (true) {
int alt114 = 2;
int LA114_0 = input.LA(1);
if ((LA114_0 == 58 || LA114_0 == 83)) {
alt114 = 1;
}
switch(alt114) {
case 1:
// src/main/resources/org/drools/compiler/semantics/java/parser/Java.g:820:13: variableModifier
{
pushFollow(FOLLOW_variableModifier_in_forStatement3449);
variableModifier4 = variableModifier();
state._fsp--;
if (state.failed)
return;
if (state.backtracking == 0) {
VarDecl_stack.peek().descr.updateStart(((CommonToken) (variableModifier4 != null ? (variableModifier4.start) : null)).getStartIndex() - 1);
VarDecl_stack.peek().descr.addModifier((variableModifier4 != null ? input.toString(variableModifier4.start, variableModifier4.stop) : null));
}
}
break;
default:
break loop114;
}
}
pushFollow(FOLLOW_type_in_forStatement3488);
type5 = type();
state._fsp--;
if (state.failed)
return;
if (state.backtracking == 0) {
VarDecl_stack.peek().descr.updateStart(((CommonToken) (type5 != null ? (type5.start) : null)).getStartIndex() - 1);
VarDecl_stack.peek().descr.setType((type5 != null ? input.toString(type5.start, type5.stop) : null));
VarDecl_stack.peek().descr.setEnd(((CommonToken) (type5 != null ? (type5.stop) : null)).getStopIndex());
}
id = (Token) match(input, Identifier, FOLLOW_Identifier_in_forStatement3514);
if (state.failed)
return;
if (state.backtracking == 0) {
JavaLocalDeclarationDescr.IdentifierDescr ident = new JavaLocalDeclarationDescr.IdentifierDescr();
ident.setIdentifier((id != null ? id.getText() : null));
ident.setStart(((CommonToken) id).getStartIndex() - 1);
ident.setEnd(((CommonToken) id).getStopIndex());
VarDecl_stack.peek().descr.addIdentifier(ident);
}
z = (Token) match(input, 51, FOLLOW_51_in_forStatement3541);
if (state.failed)
return;
pushFollow(FOLLOW_expression_in_forStatement3543);
expression();
state._fsp--;
if (state.failed)
return;
if (state.backtracking == 0) {
fd.setInitEnd(((CommonToken) z).getStartIndex());
}
}
}
break;
case 2:
// src/main/resources/org/drools/compiler/semantics/java/parser/Java.g:845:9: ( ( forInit )? z= ';' ( expression )? ';' ( forUpdate )? )
{
// src/main/resources/org/drools/compiler/semantics/java/parser/Java.g:845:9: ( ( forInit )? z= ';' ( expression )? ';' ( forUpdate )? )
// src/main/resources/org/drools/compiler/semantics/java/parser/Java.g:845:10: ( forInit )? z= ';' ( expression )? ';' ( forUpdate )?
{
// src/main/resources/org/drools/compiler/semantics/java/parser/Java.g:845:10: ( forInit )?
int alt115 = 2;
int LA115_0 = input.LA(1);
if (((LA115_0 >= CharacterLiteral && LA115_0 <= DecimalLiteral) || LA115_0 == FloatingPointLiteral || (LA115_0 >= HexLiteral && LA115_0 <= Identifier) || (LA115_0 >= OctalLiteral && LA115_0 <= StringLiteral) || LA115_0 == 29 || LA115_0 == 36 || (LA115_0 >= 40 && LA115_0 <= 41) || (LA115_0 >= 44 && LA115_0 <= 45) || LA115_0 == 53 || LA115_0 == 58 || LA115_0 == 65 || LA115_0 == 67 || (LA115_0 >= 70 && LA115_0 <= 71) || LA115_0 == 77 || (LA115_0 >= 79 && LA115_0 <= 80) || (LA115_0 >= 82 && LA115_0 <= 83) || LA115_0 == 85 || LA115_0 == 92 || LA115_0 == 94 || (LA115_0 >= 97 && LA115_0 <= 98) || LA115_0 == 105 || LA115_0 == 108 || LA115_0 == 111 || LA115_0 == 115 || LA115_0 == 118 || LA115_0 == 126)) {
alt115 = 1;
}
switch(alt115) {
case 1:
// src/main/resources/org/drools/compiler/semantics/java/parser/Java.g:845:10: forInit
{
pushFollow(FOLLOW_forInit_in_forStatement3579);
forInit();
state._fsp--;
if (state.failed)
return;
}
break;
}
z = (Token) match(input, 52, FOLLOW_52_in_forStatement3584);
if (state.failed)
return;
// src/main/resources/org/drools/compiler/semantics/java/parser/Java.g:845:25: ( expression )?
int alt116 = 2;
int LA116_0 = input.LA(1);
if (((LA116_0 >= CharacterLiteral && LA116_0 <= DecimalLiteral) || LA116_0 == FloatingPointLiteral || (LA116_0 >= HexLiteral && LA116_0 <= Identifier) || (LA116_0 >= OctalLiteral && LA116_0 <= StringLiteral) || LA116_0 == 29 || LA116_0 == 36 || (LA116_0 >= 40 && LA116_0 <= 41) || (LA116_0 >= 44 && LA116_0 <= 45) || LA116_0 == 53 || LA116_0 == 65 || LA116_0 == 67 || (LA116_0 >= 70 && LA116_0 <= 71) || LA116_0 == 77 || (LA116_0 >= 79 && LA116_0 <= 80) || LA116_0 == 82 || LA116_0 == 85 || LA116_0 == 92 || LA116_0 == 94 || (LA116_0 >= 97 && LA116_0 <= 98) || LA116_0 == 105 || LA116_0 == 108 || LA116_0 == 111 || LA116_0 == 115 || LA116_0 == 118 || LA116_0 == 126)) {
alt116 = 1;
}
switch(alt116) {
case 1:
// src/main/resources/org/drools/compiler/semantics/java/parser/Java.g:845:25: expression
{
pushFollow(FOLLOW_expression_in_forStatement3586);
expression();
state._fsp--;
if (state.failed)
return;
}
break;
}
match(input, 52, FOLLOW_52_in_forStatement3589);
if (state.failed)
return;
// src/main/resources/org/drools/compiler/semantics/java/parser/Java.g:845:41: ( forUpdate )?
int alt117 = 2;
int LA117_0 = input.LA(1);
if (((LA117_0 >= CharacterLiteral && LA117_0 <= DecimalLiteral) || LA117_0 == FloatingPointLiteral || (LA117_0 >= HexLiteral && LA117_0 <= Identifier) || (LA117_0 >= OctalLiteral && LA117_0 <= StringLiteral) || LA117_0 == 29 || LA117_0 == 36 || (LA117_0 >= 40 && LA117_0 <= 41) || (LA117_0 >= 44 && LA117_0 <= 45) || LA117_0 == 53 || LA117_0 == 65 || LA117_0 == 67 || (LA117_0 >= 70 && LA117_0 <= 71) || LA117_0 == 77 || (LA117_0 >= 79 && LA117_0 <= 80) || LA117_0 == 82 || LA117_0 == 85 || LA117_0 == 92 || LA117_0 == 94 || (LA117_0 >= 97 && LA117_0 <= 98) || LA117_0 == 105 || LA117_0 == 108 || LA117_0 == 111 || LA117_0 == 115 || LA117_0 == 118 || LA117_0 == 126)) {
alt117 = 1;
}
switch(alt117) {
case 1:
// src/main/resources/org/drools/compiler/semantics/java/parser/Java.g:845:41: forUpdate
{
pushFollow(FOLLOW_forUpdate_in_forStatement3591);
forUpdate();
state._fsp--;
if (state.failed)
return;
}
break;
}
if (state.backtracking == 0) {
fd.setInitEnd(((CommonToken) z).getStartIndex());
}
}
}
break;
}
match(input, 37, FOLLOW_37_in_forStatement3637);
if (state.failed)
return;
pushFollow(FOLLOW_statement_in_forStatement3641);
bs = statement();
state._fsp--;
if (state.failed)
return;
if (state.backtracking == 0) {
fd.setTextStart(((CommonToken) (bs != null ? (bs.start) : null)).getStartIndex());
fd.setEnd(((CommonToken) (bs != null ? (bs.stop) : null)).getStopIndex());
popContainerBlockDescr();
}
}
if (state.backtracking == 0) {
addLocalDeclaration(VarDecl_stack.peek().descr);
decreaseLevel();
}
} catch (RecognitionException re) {
reportError(re);
recover(input, re);
} finally {
// do for sure before leaving
if (state.backtracking > 0) {
memoize(input, 86, forStatement_StartIndex);
}
VarDecl_stack.pop();
}
}
use of org.drools.compiler.rule.builder.dialect.java.parser.JavaLocalDeclarationDescr in project drools by kiegroup.
the class JavaParser method forControl.
// $ANTLR end "moreStatementExpressions"
// $ANTLR start "forControl"
// src/main/resources/org/drools/compiler/semantics/java/parser/Java.g:1040:1: forControl options {k=3; } : ( forVarControl | ( forInit )? ';' ( expression )? ';' ( forUpdate )? );
public final void forControl() throws RecognitionException {
VarDecl_stack.push(new VarDecl_scope());
int forControl_StartIndex = input.index();
increaseLevel();
VarDecl_stack.peek().descr = new JavaLocalDeclarationDescr();
try {
if (state.backtracking > 0 && alreadyParsedRule(input, 100)) {
return;
}
// src/main/resources/org/drools/compiler/semantics/java/parser/Java.g:1051:5: ( forVarControl | ( forInit )? ';' ( expression )? ';' ( forUpdate )? )
int alt135 = 2;
alt135 = dfa135.predict(input);
switch(alt135) {
case 1:
// src/main/resources/org/drools/compiler/semantics/java/parser/Java.g:1051:7: forVarControl
{
pushFollow(FOLLOW_forVarControl_in_forControl4558);
forVarControl();
state._fsp--;
if (state.failed)
return;
}
break;
case 2:
// src/main/resources/org/drools/compiler/semantics/java/parser/Java.g:1052:7: ( forInit )? ';' ( expression )? ';' ( forUpdate )?
{
// src/main/resources/org/drools/compiler/semantics/java/parser/Java.g:1052:7: ( forInit )?
int alt132 = 2;
int LA132_0 = input.LA(1);
if (((LA132_0 >= CharacterLiteral && LA132_0 <= DecimalLiteral) || LA132_0 == FloatingPointLiteral || (LA132_0 >= HexLiteral && LA132_0 <= Identifier) || (LA132_0 >= OctalLiteral && LA132_0 <= StringLiteral) || LA132_0 == 29 || LA132_0 == 36 || (LA132_0 >= 40 && LA132_0 <= 41) || (LA132_0 >= 44 && LA132_0 <= 45) || LA132_0 == 53 || LA132_0 == 58 || LA132_0 == 65 || LA132_0 == 67 || (LA132_0 >= 70 && LA132_0 <= 71) || LA132_0 == 77 || (LA132_0 >= 79 && LA132_0 <= 80) || (LA132_0 >= 82 && LA132_0 <= 83) || LA132_0 == 85 || LA132_0 == 92 || LA132_0 == 94 || (LA132_0 >= 97 && LA132_0 <= 98) || LA132_0 == 105 || LA132_0 == 108 || LA132_0 == 111 || LA132_0 == 115 || LA132_0 == 118 || LA132_0 == 126)) {
alt132 = 1;
}
switch(alt132) {
case 1:
// src/main/resources/org/drools/compiler/semantics/java/parser/Java.g:1052:7: forInit
{
pushFollow(FOLLOW_forInit_in_forControl4566);
forInit();
state._fsp--;
if (state.failed)
return;
}
break;
}
match(input, 52, FOLLOW_52_in_forControl4569);
if (state.failed)
return;
// src/main/resources/org/drools/compiler/semantics/java/parser/Java.g:1052:20: ( expression )?
int alt133 = 2;
int LA133_0 = input.LA(1);
if (((LA133_0 >= CharacterLiteral && LA133_0 <= DecimalLiteral) || LA133_0 == FloatingPointLiteral || (LA133_0 >= HexLiteral && LA133_0 <= Identifier) || (LA133_0 >= OctalLiteral && LA133_0 <= StringLiteral) || LA133_0 == 29 || LA133_0 == 36 || (LA133_0 >= 40 && LA133_0 <= 41) || (LA133_0 >= 44 && LA133_0 <= 45) || LA133_0 == 53 || LA133_0 == 65 || LA133_0 == 67 || (LA133_0 >= 70 && LA133_0 <= 71) || LA133_0 == 77 || (LA133_0 >= 79 && LA133_0 <= 80) || LA133_0 == 82 || LA133_0 == 85 || LA133_0 == 92 || LA133_0 == 94 || (LA133_0 >= 97 && LA133_0 <= 98) || LA133_0 == 105 || LA133_0 == 108 || LA133_0 == 111 || LA133_0 == 115 || LA133_0 == 118 || LA133_0 == 126)) {
alt133 = 1;
}
switch(alt133) {
case 1:
// src/main/resources/org/drools/compiler/semantics/java/parser/Java.g:1052:20: expression
{
pushFollow(FOLLOW_expression_in_forControl4571);
expression();
state._fsp--;
if (state.failed)
return;
}
break;
}
match(input, 52, FOLLOW_52_in_forControl4574);
if (state.failed)
return;
// src/main/resources/org/drools/compiler/semantics/java/parser/Java.g:1052:36: ( forUpdate )?
int alt134 = 2;
int LA134_0 = input.LA(1);
if (((LA134_0 >= CharacterLiteral && LA134_0 <= DecimalLiteral) || LA134_0 == FloatingPointLiteral || (LA134_0 >= HexLiteral && LA134_0 <= Identifier) || (LA134_0 >= OctalLiteral && LA134_0 <= StringLiteral) || LA134_0 == 29 || LA134_0 == 36 || (LA134_0 >= 40 && LA134_0 <= 41) || (LA134_0 >= 44 && LA134_0 <= 45) || LA134_0 == 53 || LA134_0 == 65 || LA134_0 == 67 || (LA134_0 >= 70 && LA134_0 <= 71) || LA134_0 == 77 || (LA134_0 >= 79 && LA134_0 <= 80) || LA134_0 == 82 || LA134_0 == 85 || LA134_0 == 92 || LA134_0 == 94 || (LA134_0 >= 97 && LA134_0 <= 98) || LA134_0 == 105 || LA134_0 == 108 || LA134_0 == 111 || LA134_0 == 115 || LA134_0 == 118 || LA134_0 == 126)) {
alt134 = 1;
}
switch(alt134) {
case 1:
// src/main/resources/org/drools/compiler/semantics/java/parser/Java.g:1052:36: forUpdate
{
pushFollow(FOLLOW_forUpdate_in_forControl4576);
forUpdate();
state._fsp--;
if (state.failed)
return;
}
break;
}
}
break;
}
if (state.backtracking == 0) {
addLocalDeclaration(VarDecl_stack.peek().descr);
decreaseLevel();
}
} catch (RecognitionException re) {
reportError(re);
recover(input, re);
} finally {
// do for sure before leaving
if (state.backtracking > 0) {
memoize(input, 100, forControl_StartIndex);
}
VarDecl_stack.pop();
}
}
use of org.drools.compiler.rule.builder.dialect.java.parser.JavaLocalDeclarationDescr in project drools by kiegroup.
the class DialectUtil method rewriteDescr.
private static boolean rewriteDescr(final RuleBuildContext context, final String originalCode, final MVELDialect mvel, final StringBuilder consequence, final JavaBlockDescr d, final BoundIdentifiers bindings, final Map<String, Declaration> decls) {
if (d.getEnd() == 0) {
// do nothing, it was incorrectly parsed, but this error should be picked up else where
return false;
}
boolean typeSafety = context.isTypesafe();
// we have to analyse in dynamic mode for now, as we cannot safely determine all input vars
context.setTypesafe(false);
Map<String, Class<?>> localTypes = d.getInputs();
if (d.getInScopeLocalVars() != null && !d.getInScopeLocalVars().isEmpty()) {
localTypes = new HashMap<String, Class<?>>(d.getInputs() != null ? d.getInputs() : Collections.EMPTY_MAP);
for (JavaLocalDeclarationDescr local : d.getInScopeLocalVars()) {
// these are variables declared in the code itself that are in the scope for this expression
try {
Class<?> type = context.getDialect("java").getPackageRegistry().getTypeResolver().resolveType(local.getRawType());
for (IdentifierDescr id : local.getIdentifiers()) {
localTypes.put(id.getIdentifier(), type);
}
} catch (ClassNotFoundException e) {
context.addError(new DescrBuildError(context.getRuleDescr(), context.getParentDescr(), null, "Unable to resolve type " + local.getRawType() + ":\n" + e.getMessage()));
}
}
}
MVELAnalysisResult mvelAnalysis = (MVELAnalysisResult) mvel.analyzeBlock(context, d.getTargetExpression(), bindings, localTypes, "drools", KnowledgeHelper.class);
context.setTypesafe(typeSafety);
if (mvelAnalysis == null) {
// something bad happened, issue already logged in errors
return false;
}
Class ret = mvelAnalysis.getReturnType();
if (ret == null) {
// not possible to evaluate expression return value
context.addError(new DescrBuildError(context.getParentDescr(), context.getRuleDescr(), originalCode, "Unable to determine the resulting type of the expression: " + d.getTargetExpression() + "\n"));
return false;
}
// adding modify expression
String retString = ClassUtils.canonicalName(ret);
String declrString;
if (d.getTargetExpression().charAt(0) == '(') {
declrString = d.getTargetExpression().substring(1, d.getTargetExpression().length() - 1).trim();
} else {
declrString = d.getTargetExpression();
}
String obj = declrString;
Declaration declr = decls.get(declrString);
consequence.append("{ ");
if (declr == null) {
obj = "__obj__";
consequence.append(retString);
consequence.append(" ");
consequence.append(obj);
consequence.append(" = ");
consequence.append(d.getTargetExpression());
consequence.append("; ");
}
if (declr == null || declr.isInternalFact()) {
consequence.append("org.kie.api.runtime.rule.FactHandle ");
consequence.append(obj);
consequence.append("__Handle2__ = drools.getFactHandle(");
consequence.append(obj);
consequence.append(");");
}
// the following is a hack to preserve line breaks.
String originalBlock = originalCode.substring(d.getStart() - 1, d.getEnd());
switch(d.getType()) {
case MODIFY:
rewriteModifyDescr(context, d, originalBlock, consequence, declr, obj);
break;
case UPDATE:
rewriteUpdateDescr(context, d, consequence, declr, obj);
break;
case DELETE:
rewriteDeleteDescr(context, d, consequence, declr, obj);
break;
}
return declr != null;
}
use of org.drools.compiler.rule.builder.dialect.java.parser.JavaLocalDeclarationDescr in project drools by kiegroup.
the class DialectUtil method findDeclarationClass.
private static Class<?> findDeclarationClass(RuleBuildContext context, JavaBlockDescr d, String statement) {
Class<?> inputClass = d.getInputs() == null ? null : d.getInputs().get(statement);
if (inputClass != null) {
return inputClass;
}
List<JavaLocalDeclarationDescr> localDeclarationDescrs = d.getInScopeLocalVars();
if (localDeclarationDescrs == null) {
return null;
}
String className = null;
for (JavaLocalDeclarationDescr localDeclr : localDeclarationDescrs) {
for (IdentifierDescr idDescr : localDeclr.getIdentifiers()) {
if (statement.equals(idDescr.getIdentifier())) {
className = localDeclr.getType();
break;
}
}
if (className != null) {
break;
}
}
return findClassByName(context, className);
}
use of org.drools.compiler.rule.builder.dialect.java.parser.JavaLocalDeclarationDescr in project drools by kiegroup.
the class JavaAccumulateBuilder method fixInitCode.
protected String fixInitCode(JavaAnalysisResult analysis, final String originalCode) {
TreeSet<JavaLocalDeclarationDescr> locals = new TreeSet<JavaLocalDeclarationDescr>(new Comparator<JavaLocalDeclarationDescr>() {
public int compare(JavaLocalDeclarationDescr o1, JavaLocalDeclarationDescr o2) {
return o1.getStart() - o2.getStart();
}
});
for (JavaLocalDeclarationDescr local : analysis.getLocalVariablesMap().values()) {
locals.add(local);
}
StringBuilder initCode = new StringBuilder();
int lastAdded = 0;
for (JavaLocalDeclarationDescr d : locals) {
// adding chunk
initCode.append(originalCode.substring(lastAdded, d.getStart()));
lastAdded = d.getEnd();
// adding variable initializations
for (JavaLocalDeclarationDescr.IdentifierDescr id : d.getIdentifiers()) {
initCode.append(originalCode.substring(id.getStart(), id.getEnd()));
initCode.append(";");
lastAdded = id.getEnd();
while (lastAdded < originalCode.length() && (Character.isWhitespace(originalCode.charAt(lastAdded)) || originalCode.charAt(lastAdded) == ';')) {
lastAdded++;
}
}
}
initCode.append(originalCode.substring(lastAdded));
return initCode.toString();
}
Aggregations