Search in sources :

Example 6 with JavaAnalysisResult

use of org.drools.mvel.java.JavaAnalysisResult in project drools by kiegroup.

the class JavaConsequenceBuilderPRAlwaysTest method testFixEntryPointsReferences.

@Test
public void testFixEntryPointsReferences() {
    String consequence = " System.out.println(\"this is a test\");\n " + " entryPoints[\"foo\"].insert( new Cheese() );\n " + " System.out.println(\"we are done with entryPoints\");\n ";
    setupTest("", new HashMap<String, Object>());
    try {
        ruleDescr.setConsequence(consequence);
        JavaExprAnalyzer analyzer = new JavaExprAnalyzer();
        JavaAnalysisResult analysis = (JavaAnalysisResult) analyzer.analyzeBlock((String) ruleDescr.getConsequence(), new BoundIdentifiers(new HashMap<String, Class<?>>(), null));
        String fixed = fixBlockDescr(context, analysis, new HashMap<String, Declaration>());
        String expected = " System.out.println(\"this is a test\");\n " + " drools.getEntryPoint(\"foo\").insert( new Cheese() );\n " + " System.out.println(\"we are done with entryPoints\");\n ";
        // System.out.println( "=============================" );
        // System.out.println( ruleDescr.getConsequence() );
        // System.out.println( "=============================" );
        // System.out.println( fixed );
        assertNotNull(context.getErrors().toString(), fixed);
        assertEqualsIgnoreSpaces(expected, fixed);
    } catch (RecognitionException e) {
        e.printStackTrace();
    }
}
Also used : JavaAnalysisResult(org.drools.mvel.java.JavaAnalysisResult) JavaExprAnalyzer(org.drools.mvel.java.JavaExprAnalyzer) Declaration(org.drools.core.rule.Declaration) ImportDeclaration(org.drools.core.rule.ImportDeclaration) RecognitionException(org.antlr.runtime.RecognitionException) BoundIdentifiers(org.drools.compiler.compiler.BoundIdentifiers) Test(org.junit.Test)

Example 7 with JavaAnalysisResult

use of org.drools.mvel.java.JavaAnalysisResult 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 : JavaAnalysisResult(org.drools.mvel.java.JavaAnalysisResult) JavaExprAnalyzer(org.drools.mvel.java.JavaExprAnalyzer) JavaAccumulateBuilder(org.drools.mvel.java.JavaAccumulateBuilder) BoundIdentifiers(org.drools.compiler.compiler.BoundIdentifiers) Test(org.junit.Test)

Aggregations

JavaAnalysisResult (org.drools.mvel.java.JavaAnalysisResult)7 BoundIdentifiers (org.drools.compiler.compiler.BoundIdentifiers)6 JavaExprAnalyzer (org.drools.mvel.java.JavaExprAnalyzer)6 Test (org.junit.Test)6 RecognitionException (org.antlr.runtime.RecognitionException)5 Declaration (org.drools.core.rule.Declaration)5 ImportDeclaration (org.drools.core.rule.ImportDeclaration)4 HashMap (java.util.HashMap)1 MissingDependencyError (org.drools.compiler.compiler.MissingDependencyError)1 MissingDependencyException (org.drools.core.common.MissingDependencyException)1 JavaAccumulateBuilder (org.drools.mvel.java.JavaAccumulateBuilder)1