use of org.drools.compiler.rule.builder.dialect.java.parser.JavaBlockDescr in project drools by kiegroup.
the class JavaConsequenceBuilderTest method testWhileBlocks.
@Test
public void testWhileBlocks() throws Exception {
String consequence = " System.out.println(\"this is a test\");\n " + " Cheese c1 = $cheese;\n" + " while ( c1 == $cheese ) { \r\n" + " modify( c1 ) { setPrice( 10 ), \n" + " setOldPrice( age ) }\n " + " Cheese c4 = $cheese;\n" + " while ( true ) { \n" + " modify( c4 ) { setPrice( 10 ), setOldPrice( age ) }\n " + " }" + " } \n " + " Cheese c3 = $cheese;\n" + " while ( c4 == $cheese ) modify( $cheese ) { setPrice( 10 ), setOldPrice( age ) }\n " + " modify( $cheese ) { setPrice( 10 ), setOldPrice( age ) }\n " + " System.out.println(\"we are done\");\n " + " while (true) { System.out.println(1);}\n" + " modify( $cheese ) { setPrice( 10 ), setOldPrice( age ) }\n " + " System.out.println(\"we are done\");\n ";
setupTest("", new HashMap<String, Object>());
ruleDescr.setConsequence(consequence);
JavaExprAnalyzer analyzer = new JavaExprAnalyzer();
Map<String, Class<?>> declrCls = new HashMap<String, Class<?>>();
declrCls.put("$cheese", Cheese.class);
JavaAnalysisResult analysis = (JavaAnalysisResult) analyzer.analyzeBlock((String) ruleDescr.getConsequence(), new BoundIdentifiers(declrCls, null));
BoundIdentifiers bindings = new BoundIdentifiers(new HashMap(), null);
bindings.getDeclrClasses().put("$cheese", Cheese.class);
bindings.getDeclrClasses().put("age", int.class);
// Set the inputs for each container, this is needed for modifes when the target context is the result of an expression
List<JavaBlockDescr> descrs = new ArrayList<JavaBlockDescr>();
setContainerBlockInputs(context, descrs, analysis.getBlockDescrs(), consequence, bindings, new HashMap(), 0);
String fixed = fixBlockDescr(context, analysis, context.getDeclarationResolver().getDeclarations(context.getRule()));
String expected = " System.out.println(\"this is a test\");\r\n" + " Cheese c1 = $cheese;\r\n" + " while ( c1 == $cheese ) { \r\n" + " { org.drools.compiler.Cheese __obj__ = ( c1 ); org.kie.api.runtime.rule.FactHandle __obj____Handle2__ = drools.getFactHandle(__obj__);__obj__.setPrice( 10 ); \r\n" + "__obj__.setOldPrice( age ); drools.update( __obj____Handle2__, org.drools.core.util.bitmask.AllSetButLastBitMask.get(), org.drools.compiler.Cheese.class ); }\r\n" + " Cheese c4 = $cheese;\r\n" + " while ( true ) { \r\n" + " { org.drools.compiler.Cheese __obj__ = ( c4 ); org.kie.api.runtime.rule.FactHandle __obj____Handle2__ = drools.getFactHandle(__obj__);__obj__.setPrice( 10 ); __obj__.setOldPrice( age ); drools.update( __obj____Handle2__, org.drools.core.util.bitmask.AllSetButLastBitMask.get(), org.drools.compiler.Cheese.class ); }\r\n" + " } } \r\n" + " Cheese c3 = $cheese;\r\n" + " while ( c4 == $cheese ) { $cheese.setPrice( 10 ); $cheese.setOldPrice( age ); drools.update( $cheese__Handle__, org.drools.core.util.bitmask.AllSetButLastBitMask.get(), org.drools.compiler.Cheese.class ); }\r\n" + " { $cheese.setPrice( 10 ); $cheese.setOldPrice( age ); drools.update( $cheese__Handle__, org.drools.core.util.bitmask.AllSetButLastBitMask.get(), org.drools.compiler.Cheese.class ); }\r\n" + " System.out.println(\"we are done\");\r\n" + " while (true) { System.out.println(1);}\r\n" + " { $cheese.setPrice( 10 ); $cheese.setOldPrice( age ); drools.update( $cheese__Handle__, org.drools.core.util.bitmask.AllSetButLastBitMask.get(), org.drools.compiler.Cheese.class ); }\r\n" + " System.out.println(\"we are done\");\r\n" + " \r\n" + "";
assertNotNull(context.getErrors().toString(), fixed);
assertEqualsIgnoreSpaces(expected, fixed);
}
use of org.drools.compiler.rule.builder.dialect.java.parser.JavaBlockDescr in project drools by kiegroup.
the class JavaConsequenceBuilderTest method testIfElseBlocks.
@Test
public void testIfElseBlocks() throws Exception {
String consequence = " System.out.println(\"this is a test\");\n " + " Cheese c1 = $cheese;\n" + " if( c1 == $cheese ) { \r\n" + " modify( c1 ) { setPrice( 10 ), \n" + " setOldPrice( age ) }\n " + " Cheese c4 = $cheese;\n" + " if ( true ) { \n" + " modify( c4 ) { setPrice( 10 ), setOldPrice( age ) }\n " + " } else if (1==2) {\n" + " modify( c1 ) { setPrice( 10 ), setOldPrice( age ) }\n " + " } else {\n " + " Cheese c3 = $cheese;\n" + " modify( $cheese ) { setPrice( 10 ), setOldPrice( age ) }\n " + " }\n" + " } else {\n " + " Cheese c3 = $cheese;\n" + " modify( c3 ) { setPrice( 10 ), setOldPrice( age ) }\n " + " if ( c4 == $cheese ) modify( $cheese ) { setPrice( 10 ), setOldPrice( age ) }\n " + " else modify( $cheese ) { setPrice( 12 ) }\n " + "}\n" + " modify( $cheese ) { setPrice( 10 ), setOldPrice( age ) }\n " + " System.out.println(\"we are done\");\n ";
setupTest("", new HashMap<String, Object>());
ruleDescr.setConsequence(consequence);
JavaExprAnalyzer analyzer = new JavaExprAnalyzer();
Map<String, Class<?>> declrCls = new HashMap<String, Class<?>>();
declrCls.put("$cheese", Cheese.class);
JavaAnalysisResult analysis = (JavaAnalysisResult) analyzer.analyzeBlock((String) ruleDescr.getConsequence(), new BoundIdentifiers(declrCls, null));
BoundIdentifiers bindings = new BoundIdentifiers(new HashMap(), null);
bindings.getDeclrClasses().put("$cheese", Cheese.class);
bindings.getDeclrClasses().put("age", int.class);
// Set the inputs for each container, this is needed for modifes when the target context is the result of an expression
List<JavaBlockDescr> descrs = new ArrayList<JavaBlockDescr>();
setContainerBlockInputs(context, descrs, analysis.getBlockDescrs(), consequence, bindings, new HashMap(), 0);
String fixed = fixBlockDescr(context, analysis, context.getDeclarationResolver().getDeclarations(context.getRule()));
String expected = " System.out.println(\"this is a test\");\r\n" + " Cheese c1 = $cheese;\r\n" + " if( c1 == $cheese ) { \r\n" + " { org.drools.compiler.Cheese __obj__ = ( c1 ); org.kie.api.runtime.rule.FactHandle __obj____Handle2__ = drools.getFactHandle(__obj__);__obj__.setPrice( 10 ); \r\n" + "__obj__.setOldPrice( age ); drools.update( __obj____Handle2__, org.drools.core.util.bitmask.AllSetButLastBitMask.get(), org.drools.compiler.Cheese.class ); }\r\n" + " Cheese c4 = $cheese;\r\n" + " if ( true ) { \r\n" + " { org.drools.compiler.Cheese __obj__ = ( c4 ); org.kie.api.runtime.rule.FactHandle __obj____Handle2__ = drools.getFactHandle(__obj__);__obj__.setPrice( 10 ); __obj__.setOldPrice( age ); drools.update( __obj____Handle2__, org.drools.core.util.bitmask.AllSetButLastBitMask.get(), org.drools.compiler.Cheese.class ); }\r\n" + " } else if (1==2) {\r\n" + " { org.drools.compiler.Cheese __obj__ = ( c1 ); org.kie.api.runtime.rule.FactHandle __obj____Handle2__ = drools.getFactHandle(__obj__);__obj__.setPrice( 10 ); __obj__.setOldPrice( age ); drools.update( __obj____Handle2__, org.drools.core.util.bitmask.AllSetButLastBitMask.get(), org.drools.compiler.Cheese.class ); }\r\n" + " } else {\r\n" + " Cheese c3 = $cheese;\r\n" + " { $cheese.setPrice( 10 ); $cheese.setOldPrice( age ); drools.update( $cheese__Handle__, org.drools.core.util.bitmask.AllSetButLastBitMask.get(), org.drools.compiler.Cheese.class ); }\r\n" + " }\r\n" + " } else {\r\n" + " Cheese c3 = $cheese;\r\n" + " { org.drools.compiler.Cheese __obj__ = ( c3 ); org.kie.api.runtime.rule.FactHandle __obj____Handle2__ = drools.getFactHandle(__obj__);__obj__.setPrice( 10 ); __obj__.setOldPrice( age ); drools.update( __obj____Handle2__, org.drools.core.util.bitmask.AllSetButLastBitMask.get(), org.drools.compiler.Cheese.class ); }\r\n" + " if ( c4 == $cheese ) { $cheese.setPrice( 10 ); $cheese.setOldPrice( age ); drools.update( $cheese__Handle__, org.drools.core.util.bitmask.AllSetButLastBitMask.get(), org.drools.compiler.Cheese.class ); }\r\n" + " else { $cheese.setPrice( 12 ); drools.update( $cheese__Handle__, org.drools.core.util.bitmask.AllSetButLastBitMask.get(), org.drools.compiler.Cheese.class ); }\r\n" + " }\r\n" + " { $cheese.setPrice( 10 ); $cheese.setOldPrice( age ); drools.update( $cheese__Handle__, org.drools.core.util.bitmask.AllSetButLastBitMask.get(), org.drools.compiler.Cheese.class ); }\r\n" + " System.out.println(\"we are done\");\r\n" + " \r\n";
assertNotNull(context.getErrors().toString(), fixed);
assertEqualsIgnoreSpaces(expected, fixed);
// System.out.println( "=============================" );
// System.out.println( ruleDescr.getConsequence() );
// System.out.println( "=============================" );
// System.out.println( fixed );
}
use of org.drools.compiler.rule.builder.dialect.java.parser.JavaBlockDescr in project drools by kiegroup.
the class DialectUtil method fixBlockDescr.
public static String fixBlockDescr(final RuleBuildContext context, final JavaAnalysisResult analysis, Map<String, Declaration> decls, List<JavaBlockDescr> blocks) {
MVELDialect mvel = (MVELDialect) context.getDialect("mvel");
String originalCode = analysis.getAnalyzedExpr();
BoundIdentifiers bindings = analysis.getBoundIdentifiers();
// sorting exit points for correct order iteration
Collections.sort(blocks, new Comparator<JavaBlockDescr>() {
public int compare(JavaBlockDescr o1, JavaBlockDescr o2) {
return o1.getStart() - o2.getStart();
}
});
StringBuilder consequence = new StringBuilder();
int lastAdded = 0;
for (JavaBlockDescr block : blocks) {
if (block.getEnd() == 0 || block.getEnd() > originalCode.length()) {
// do nothing, it was incorrectly parsed, but this error should be picked up else where
continue;
}
// adding chunk
consequence.append(originalCode.substring(lastAdded, block.getStart() - 1));
lastAdded = block.getEnd();
switch(block.getType()) {
case MODIFY:
case UPDATE:
case DELETE:
rewriteDescr(context, originalCode, mvel, consequence, block, bindings, decls);
break;
case ENTRY:
case EXIT:
case CHANNEL:
rewriteInterfacePoint(context, originalCode, consequence, (JavaInterfacePointsDescr) block);
break;
case INSERT:
parseInsertDescr(context, block);
default:
consequence.append(originalCode.substring(block.getStart() - 1, lastAdded));
}
}
consequence.append(originalCode.substring(lastAdded));
return consequence.toString();
}
use of org.drools.compiler.rule.builder.dialect.java.parser.JavaBlockDescr in project drools by kiegroup.
the class DialectUtil method setContainerBlockInputs.
/**
* This code is not currently used, it's commented out in method caller. This is because we couldn't
* get this to work and will have to wait until MVEL supports genercs (mdp).
*/
public static void setContainerBlockInputs(RuleBuildContext context, List<JavaBlockDescr> descrs, JavaContainerBlockDescr parentBlock, String originalCode, BoundIdentifiers bindings, Map<String, Class<?>> parentVars, int offset) {
StringBuilder consequence = new StringBuilder();
int lastAdded = 0;
// strip blocks, so we can analyse this block with MVEL
for (JavaBlockDescr block : parentBlock.getJavaBlockDescrs()) {
if (block.getEnd() == 0) {
// do nothing, it was incorrectly parsed, but this error should be picked up else where
continue;
}
if (block.getType() == JavaBlockDescr.BlockType.TRY) {
// adding previous chunk up to the start of this block
consequence.append(originalCode.substring(lastAdded, block.getStart() - 1 - offset));
JavaTryBlockDescr tryDescr = (JavaTryBlockDescr) block;
if (tryDescr.getFinal() != null) {
lastAdded = tryDescr.getFinal().getEnd() - offset;
} else {
lastAdded = tryDescr.getCatches().get(tryDescr.getCatches().size() - 1).getEnd() - offset;
}
stripTryDescr(originalCode, consequence, (JavaTryBlockDescr) block, offset);
} else if (block.getType() == JavaBlockDescr.BlockType.THROW) {
// adding previous chunk up to the start of this block
consequence.append(originalCode.substring(lastAdded, block.getStart() - 1 - offset));
JavaThrowBlockDescr throwBlock = (JavaThrowBlockDescr) block;
addWhiteSpaces(originalCode, consequence, throwBlock.getStart() - offset, throwBlock.getTextStart() - offset);
consequence.append(originalCode.substring(throwBlock.getTextStart() - offset - 1, throwBlock.getEnd() - 1 - offset)).append(";");
lastAdded = throwBlock.getEnd() - offset;
} else if (block.getType() == JavaBlockDescr.BlockType.IF) {
// adding previous chunk up to the start of this block
consequence.append(originalCode.substring(lastAdded, block.getStart() - 1 - offset));
JavaIfBlockDescr ifDescr = (JavaIfBlockDescr) block;
lastAdded = ifDescr.getEnd() - offset;
stripBlockDescr(originalCode, consequence, ifDescr, offset);
} else if (block.getType() == JavaBlockDescr.BlockType.ELSE) {
// adding previous chunk up to the start of this block
consequence.append(originalCode.substring(lastAdded, block.getStart() - 1 - offset));
JavaElseBlockDescr elseDescr = (JavaElseBlockDescr) block;
lastAdded = elseDescr.getEnd() - offset;
stripBlockDescr(originalCode, consequence, elseDescr, offset);
} else if (block.getType() == JavaBlockDescr.BlockType.WHILE) {
// adding previous chunk up to the start of this block
consequence.append(originalCode.substring(lastAdded, block.getStart() - 1 - offset));
JavaWhileBlockDescr whileDescr = (JavaWhileBlockDescr) block;
lastAdded = whileDescr.getEnd() - offset;
stripBlockDescr(originalCode, consequence, whileDescr, offset);
} else if (block.getType() == JavaBlockDescr.BlockType.FOR) {
// adding previous chunk up to the start of this block
consequence.append(originalCode.substring(lastAdded, block.getStart() - 1 - offset));
JavaForBlockDescr forDescr = (JavaForBlockDescr) block;
lastAdded = forDescr.getEnd() - offset;
stripBlockDescr(originalCode, consequence, forDescr, offset);
}
}
consequence.append(originalCode.substring(lastAdded));
// We need to do this as MVEL doesn't recognise "modify"
MacroProcessor macroProcessor = new MacroProcessor();
Map<String, Macro> macros = new HashMap<String, Macro>(MVELConsequenceBuilder.macros);
macros.put("modify", new Macro() {
public String doMacro() {
return "with ";
}
});
macroProcessor.setMacros(macros);
String mvelCode = macroProcessor.parse(consequence.toString());
Map<String, Class<?>> inputs = getInputs(context, mvelCode, bindings, parentVars);
inputs.putAll(parentVars);
parentBlock.setInputs(inputs);
// set inputs for current container blocks to be rewritten
for (JavaBlockDescr block : parentBlock.getJavaBlockDescrs()) {
if (block.getType() == JavaBlockDescr.BlockType.TRY) {
JavaTryBlockDescr tryBlock = (JavaTryBlockDescr) block;
setContainerBlockInputs(context, descrs, tryBlock, originalCode.substring(tryBlock.getTextStart() - offset, tryBlock.getEnd() - 1 - offset), bindings, inputs, tryBlock.getTextStart());
for (JavaCatchBlockDescr catchBlock : tryBlock.getCatches()) {
setContainerBlockInputs(context, descrs, catchBlock, catchBlock.getClause() + "=null;" + originalCode.substring(catchBlock.getTextStart() - offset, catchBlock.getEnd() - 1 - offset), bindings, inputs, tryBlock.getTextStart());
}
if (tryBlock.getFinal() != null) {
JavaFinalBlockDescr finalBlock = tryBlock.getFinal();
setContainerBlockInputs(context, descrs, finalBlock, originalCode.substring(finalBlock.getTextStart() - offset, finalBlock.getEnd() - 1 - offset), bindings, inputs, tryBlock.getTextStart());
}
} else if (block.getType() == JavaBlockDescr.BlockType.IF) {
JavaIfBlockDescr ifBlock = (JavaIfBlockDescr) block;
int adjustBlock = (originalCode.charAt(ifBlock.getTextStart() - offset - 1) == '{') ? 0 : 1;
setContainerBlockInputs(context, descrs, ifBlock, originalCode.substring(ifBlock.getTextStart() - offset + adjustBlock, ifBlock.getEnd() - 1 - offset - adjustBlock), bindings, inputs, ifBlock.getTextStart());
} else if (block.getType() == JavaBlockDescr.BlockType.ELSE) {
JavaElseBlockDescr elseBlock = (JavaElseBlockDescr) block;
int adjustBlock = (originalCode.charAt(elseBlock.getTextStart() - offset - 1) == '{') ? 0 : 1;
setContainerBlockInputs(context, descrs, elseBlock, originalCode.substring(elseBlock.getTextStart() - offset + adjustBlock, elseBlock.getEnd() - 1 - offset - adjustBlock), bindings, inputs, elseBlock.getTextStart());
} else if (block.getType() == JavaBlockDescr.BlockType.WHILE) {
JavaWhileBlockDescr whileBlock = (JavaWhileBlockDescr) block;
int adjustBlock = (originalCode.charAt(whileBlock.getTextStart() - offset - 1) == '{') ? 0 : 1;
setContainerBlockInputs(context, descrs, whileBlock, originalCode.substring(whileBlock.getTextStart() - offset + adjustBlock, whileBlock.getEnd() - 1 - offset - adjustBlock), bindings, inputs, whileBlock.getTextStart());
} else if (block.getType() == JavaBlockDescr.BlockType.FOR) {
JavaForBlockDescr forBlock = (JavaForBlockDescr) block;
int adjustBlock = (originalCode.charAt(forBlock.getTextStart() - offset - 1) == '{') ? 0 : 1;
setContainerBlockInputs(context, descrs, forBlock, originalCode.substring(forBlock.getStartParen() - offset, forBlock.getInitEnd() - offset) + originalCode.substring(forBlock.getTextStart() - offset + adjustBlock, forBlock.getEnd() - 1 - offset - adjustBlock), bindings, inputs, forBlock.getTextStart() - (forBlock.getInitEnd() - forBlock.getStartParen()));
} else {
// each block to be rewritten now knows it's own variables
block.setInputs(inputs);
descrs.add(block);
}
}
}
use of org.drools.compiler.rule.builder.dialect.java.parser.JavaBlockDescr in project drools by kiegroup.
the class JavaConsequenceBuilderPRAlwaysTest method testFixThrows.
@Test
public void testFixThrows() {
String consequence = " modify( $cheese ) { setPrice( 10 ), setOldPrice( age ) }\n " + " throw new java.lang.RuntimeException(\"xxx\");\n " + " Cheese c1 = $cheese;\n" + " modify( c1 ) { setPrice( 10 ), setOldPrice( age ) }\n ";
setupTest("", new HashMap<String, Object>());
try {
ruleDescr.setConsequence(consequence);
JavaExprAnalyzer analyzer = new JavaExprAnalyzer();
Map<String, Class<?>> declrCls = new HashMap<String, Class<?>>();
declrCls.put("$cheese", Cheese.class);
JavaAnalysisResult analysis = (JavaAnalysisResult) analyzer.analyzeBlock((String) ruleDescr.getConsequence(), new BoundIdentifiers(declrCls, null));
BoundIdentifiers bindings = new BoundIdentifiers(new HashMap(), null);
bindings.getDeclrClasses().put("$cheese", Cheese.class);
bindings.getDeclrClasses().put("age", int.class);
// Set the inputs for each container, this is needed for modifes when the target context is the result of an expression
List<JavaBlockDescr> descrs = new ArrayList<JavaBlockDescr>();
setContainerBlockInputs(context, descrs, analysis.getBlockDescrs(), consequence, bindings, new HashMap(), 0);
context.getKnowledgeBuilder().getTypeDeclaration(Cheese.class).setPropertyReactive(true);
String fixed = fixBlockDescr(context, analysis, context.getDeclarationResolver().getDeclarations(context.getRule()));
String expected = " { $cheese.setPrice( 10 ); $cheese.setOldPrice( age ); drools.update( $cheese__Handle__, new org.drools.core.util.bitmask.LongBitMask(12L), org.drools.compiler.Cheese.class ); }\r\n" + " throw new java.lang.RuntimeException(\"xxx\");\r\n" + " Cheese c1 = $cheese;\r\n" + " { org.drools.compiler.Cheese __obj__ = ( c1 ); org.kie.api.runtime.rule.FactHandle __obj____Handle2__ = drools.getFactHandle(__obj__);__obj__.setPrice( 10 ); __obj__.setOldPrice( age ); drools.update( __obj____Handle2__, new org.drools.core.util.bitmask.LongBitMask(12L), org.drools.compiler.Cheese.class ); }\r\n" + " \r\n" + "";
assertNotNull(context.getErrors().toString(), fixed);
assertEqualsIgnoreSpaces(expected, fixed);
} catch (RecognitionException e) {
e.printStackTrace();
}
}
Aggregations