use of org.drools.compiler.compiler.BoundIdentifiers in project drools by kiegroup.
the class JavaAccumulateBuilderTest method testFixInitCode.
@Test
public void testFixInitCode() throws Exception {
JavaExprAnalyzer analyzer = new JavaExprAnalyzer();
JavaAccumulateBuilder builder = new JavaAccumulateBuilder();
String code = "int x = 0;";
String expected = "x = 0;";
BoundIdentifiers bindings = new BoundIdentifiers(new HashMap<String, Class<?>>(), null);
JavaAnalysisResult analysis = analyzer.analyzeBlock(code, bindings);
String result = builder.fixInitCode(analysis, code);
assertEquals(expected, result);
code = "$anExternalVar.method(); \nint aVar = 0, anotherVar=10 ;Integer bla = new Integer( 25);functionCall();\n";
expected = "$anExternalVar.method(); \naVar = 0;anotherVar=10;bla = new Integer( 25);functionCall();\n";
;
analysis = analyzer.analyzeBlock(code, bindings);
result = builder.fixInitCode(analysis, code);
assertEquals(expected, result);
code = "$anExternalVar.method(); String[] aVar = new String[] { \"a\", \"b\" }, anotherVar=new String[] { someStringVar } ;final Integer bla = new Integer( 25);functionCall();\n";
expected = "$anExternalVar.method(); aVar = new String[] { \"a\", \"b\" };anotherVar=new String[] { someStringVar };bla = new Integer( 25);functionCall();\n";
analysis = analyzer.analyzeBlock(code, bindings);
result = builder.fixInitCode(analysis, code);
assertEquals(expected, result);
}
use of org.drools.compiler.compiler.BoundIdentifiers 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();
}
}
use of org.drools.compiler.compiler.BoundIdentifiers in project drools by kiegroup.
the class JavaConsequenceBuilderPRAlwaysTest 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()));
List<String> cheeseAccessibleProperties = ClassUtils.getAccessibleProperties(Cheese.class);
BitMask priceOldPrice = PropertySpecificUtil.calculatePositiveMask(Cheese.class, Arrays.asList("price", "oldPrice"), cheeseAccessibleProperties);
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__, " + priceOldPrice.getInstancingStatement() + ", 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__, " + priceOldPrice.getInstancingStatement() + ", 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__, " + priceOldPrice.getInstancingStatement() + ", org.drools.compiler.Cheese.class ); }\r\n" + " { $cheese.setPrice( 10 ); $cheese.setOldPrice( age ); drools.update( $cheese__Handle__, " + priceOldPrice.getInstancingStatement() + ", 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__, " + priceOldPrice.getInstancingStatement() + ", 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.compiler.BoundIdentifiers in project drools by kiegroup.
the class JavaConsequenceBuilderTest 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