Search in sources :

Example 11 with JavaBlockDescr

use of org.drools.compiler.rule.builder.dialect.java.parser.JavaBlockDescr 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);
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) BoundIdentifiers(org.drools.compiler.compiler.BoundIdentifiers) BitMask(org.drools.core.util.bitmask.BitMask) JavaBlockDescr(org.drools.compiler.rule.builder.dialect.java.parser.JavaBlockDescr) Test(org.junit.Test)

Example 12 with JavaBlockDescr

use of org.drools.compiler.rule.builder.dialect.java.parser.JavaBlockDescr 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();
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Cheese(org.drools.compiler.Cheese) BoundIdentifiers(org.drools.compiler.compiler.BoundIdentifiers) RecognitionException(org.antlr.runtime.RecognitionException) JavaBlockDescr(org.drools.compiler.rule.builder.dialect.java.parser.JavaBlockDescr) Test(org.junit.Test)

Aggregations

JavaBlockDescr (org.drools.compiler.rule.builder.dialect.java.parser.JavaBlockDescr)12 HashMap (java.util.HashMap)11 BoundIdentifiers (org.drools.compiler.compiler.BoundIdentifiers)11 ArrayList (java.util.ArrayList)10 Test (org.junit.Test)10 Cheese (org.drools.compiler.Cheese)4 BitMask (org.drools.core.util.bitmask.BitMask)3 RecognitionException (org.antlr.runtime.RecognitionException)2 JavaCatchBlockDescr (org.drools.compiler.rule.builder.dialect.java.parser.JavaCatchBlockDescr)1 JavaElseBlockDescr (org.drools.compiler.rule.builder.dialect.java.parser.JavaElseBlockDescr)1 JavaFinalBlockDescr (org.drools.compiler.rule.builder.dialect.java.parser.JavaFinalBlockDescr)1 JavaForBlockDescr (org.drools.compiler.rule.builder.dialect.java.parser.JavaForBlockDescr)1 JavaIfBlockDescr (org.drools.compiler.rule.builder.dialect.java.parser.JavaIfBlockDescr)1 JavaThrowBlockDescr (org.drools.compiler.rule.builder.dialect.java.parser.JavaThrowBlockDescr)1 JavaTryBlockDescr (org.drools.compiler.rule.builder.dialect.java.parser.JavaTryBlockDescr)1 JavaWhileBlockDescr (org.drools.compiler.rule.builder.dialect.java.parser.JavaWhileBlockDescr)1 MVELDialect (org.drools.compiler.rule.builder.dialect.mvel.MVELDialect)1 Macro (org.mvel2.Macro)1 MacroProcessor (org.mvel2.MacroProcessor)1