Search in sources :

Example 31 with BoundIdentifiers

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);
}
Also used : BoundIdentifiers(org.drools.compiler.compiler.BoundIdentifiers) Test(org.junit.Test)

Example 32 with BoundIdentifiers

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();
    }
}
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)

Example 33 with BoundIdentifiers

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);
}
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 34 with BoundIdentifiers

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();
    }
}
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

BoundIdentifiers (org.drools.compiler.compiler.BoundIdentifiers)34 Declaration (org.drools.core.rule.Declaration)17 ArrayList (java.util.ArrayList)16 HashMap (java.util.HashMap)15 Test (org.junit.Test)12 JavaBlockDescr (org.drools.compiler.rule.builder.dialect.java.parser.JavaBlockDescr)11 AnalysisResult (org.drools.compiler.compiler.AnalysisResult)9 MVELDialectRuntimeData (org.drools.core.rule.MVELDialectRuntimeData)8 DescrBuildError (org.drools.compiler.compiler.DescrBuildError)7 MVELCompilationUnit (org.drools.core.base.mvel.MVELCompilationUnit)7 Cheese (org.drools.compiler.Cheese)4 MVELAnalysisResult (org.drools.compiler.rule.builder.dialect.mvel.MVELAnalysisResult)4 TypeDeclaration (org.drools.core.rule.TypeDeclaration)4 MvelConstraint (org.drools.core.rule.constraint.MvelConstraint)4 Constraint (org.drools.core.spi.Constraint)4 HashSet (java.util.HashSet)3 Map (java.util.Map)3 RecognitionException (org.antlr.runtime.RecognitionException)3 MVELDialect (org.drools.compiler.rule.builder.dialect.mvel.MVELDialect)3 EvalDescr (org.drools.compiler.lang.descr.EvalDescr)2