Search in sources :

Example 6 with BoundIdentifiers

use of org.drools.compiler.compiler.BoundIdentifiers in project drools by kiegroup.

the class JavaConsequenceBuilderTest method testFixModifyBlocks.

@Test
public void testFixModifyBlocks() throws Exception {
    String consequence = " System.out.println(\"this is a test\");\n " + " Cheese c1 = $cheese;\n" + " try { \r\n" + "     modify( c1 ) { setPrice( 10 ), \n" + "                    setOldPrice( age ) }\n " + "     Cheese c4 = $cheese;\n" + "     try { \n" + "         modify( c4 ) { setPrice( 10 ), setOldPrice( age ) }\n " + "     } catch (java.lang.Exception e) {\n" + "         modify( c1 ) { setPrice( 10 ), setOldPrice( age ) }\n " + "     } finally {\n " + "         Cheese c3 = $cheese;\n" + "         modify( c3 ) { setPrice( 10 ), setOldPrice( age ) }\n " + "    }\n" + " } catch (java.lang.Exception e) {\n" + "     Cheese c2 = $cheese;\n" + "     modify( c2 ) { setPrice( 10 ), setOldPrice( age ) }\n " + " } finally {\n " + "     Cheese c3 = $cheese;\n" + "     modify( c3 ) { setPrice( 10 ), setOldPrice( age ) }\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);
    analysis.setBoundIdentifiers(bindings);
    context.getKnowledgeBuilder().getTypeDeclaration(Cheese.class).setPropertyReactive(true);
    String fixed = fixBlockDescr(context, analysis, context.getDeclarationResolver().getDeclarations(context.getRule()), descrs);
    String expected = " System.out.println(\"this is a test\");\r\n" + "  Cheese c1 = $cheese;\r\n" + " try { \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__, new org.drools.core.util.bitmask.LongBitMask(12L), org.drools.compiler.Cheese.class ); }\r\n" + "      Cheese c4 = $cheese;\r\n" + "     try { \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__, new org.drools.core.util.bitmask.LongBitMask(12L), org.drools.compiler.Cheese.class ); }\r\n" + "      } catch (java.lang.Exception e) {\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" + "      } finally {\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__, new org.drools.core.util.bitmask.LongBitMask(12L), org.drools.compiler.Cheese.class ); }\r\n" + "     }\r\n" + " } catch (java.lang.Exception e) {\r\n" + "     Cheese c2 = $cheese;\r\n" + "     { org.drools.compiler.Cheese __obj__ = ( c2 ); 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" + "  } finally {\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__, new org.drools.core.util.bitmask.LongBitMask(12L), org.drools.compiler.Cheese.class ); }\r\n" + " }\r\n" + " { $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" + "  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 );
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Cheese(org.drools.compiler.Cheese) BoundIdentifiers(org.drools.compiler.compiler.BoundIdentifiers) JavaBlockDescr(org.drools.compiler.rule.builder.dialect.java.parser.JavaBlockDescr) Test(org.junit.Test)

Example 7 with BoundIdentifiers

use of org.drools.compiler.compiler.BoundIdentifiers in project drools by kiegroup.

the class JavaConsequenceBuilderTest method testForBlocks.

@Test
public void testForBlocks() throws Exception {
    String consequence = " System.out.println(\"this is a test\");\n " + "int i = 0;\n" + " for ( Cheese c1 = $cheese; i < 10;i++ )     { \r\n" + "     modify( c1 ) { setPrice( 10 ), \n" + "                    setOldPrice( age ) }\n " + "     Cheese c4 = $cheese;\n" + "     for ( Cheese item : new ArrayList<Cheese>() ) {" + "         modify( $cheese ) { setPrice( 10 ), setOldPrice( age ) }\n " + "      }\n" + " } \n " + " for ( ; ; ) modify( (Cheese) $cheese ) { setPrice( 10 ), setOldPrice( age ) }\n " + " for ( Cheese item : new ArrayList<Cheese>() ) modify( $cheese ) { setPrice( 10 ), setOldPrice( age ) }\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" + " int i = 0;\r\n" + " for ( Cheese c1 = $cheese; i < 10;i++ )     { \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" + "     for ( Cheese item : new ArrayList<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" + "       }\r\n" + " } \r\n" + "  for ( ; ; ) { org.drools.compiler.Cheese __obj__ = ( (Cheese) $cheese ); 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(), java.lang.Object.class ); }\r\n" + "  for ( Cheese item : new ArrayList<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" + " \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) JavaBlockDescr(org.drools.compiler.rule.builder.dialect.java.parser.JavaBlockDescr) Test(org.junit.Test)

Example 8 with BoundIdentifiers

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

Example 9 with BoundIdentifiers

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

Example 10 with BoundIdentifiers

use of org.drools.compiler.compiler.BoundIdentifiers in project drools by kiegroup.

the class QueryElementBuilder method analyzeExpression.

private AnalysisResult analyzeExpression(RuleBuildContext context, BaseDescr base, String expression) {
    Map<String, Declaration> decls = context.getDeclarationResolver().getDeclarations(context.getRule());
    Map<String, Class<?>> declarationClasses = DeclarationScopeResolver.getDeclarationClasses(decls);
    BoundIdentifiers boundIds = new BoundIdentifiers(declarationClasses, context);
    return context.getDialect().analyzeBlock(context, base, expression, boundIds);
}
Also used : Declaration(org.drools.core.rule.Declaration) BoundIdentifiers(org.drools.compiler.compiler.BoundIdentifiers)

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