Search in sources :

Example 1 with JavaExprAnalyzer

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

the class JavaConsequenceBuilderPRAlwaysTest method testFixExitPointsReferences.

@Test
public void testFixExitPointsReferences() {
    String consequence = " System.out.println(\"this is a test\");\n " + " exitPoints[\"foo\"].insert( new Cheese() );\n " + " System.out.println(\"we are done with exitPoints\");\n ";
    setupTest(consequence, new HashMap<String, Object>());
    try {
        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.getExitPoint(\"foo\").insert( new Cheese() );\n " + " System.out.println(\"we are done with exitPoints\");\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 2 with JavaExprAnalyzer

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

the class JavaExprAnalyzerTest method testAnalyzeBlock.

@Test
public void testAnalyzeBlock() {
    JavaExprAnalyzer analyzer = new JavaExprAnalyzer();
    String codeBlock = "int x;\n" + "Cheese cheese = new Cheese();\n" + "for( Iterator it = list.iterator(); it.hasNext(); ) {\n" + "    int shouldNotBeIncluded = 1;\n" + "}\n" + "{\n" + "    String anotherNonTopLevelVar = \"test\";\n" + "}\n" + "double thisIsAGoodVar = 0;\n" + "method();\n";
    try {
        JavaAnalysisResult analysis = analyzer.analyzeBlock(codeBlock, new BoundIdentifiers(new HashMap<String, Class<?>>(), null));
        Set<String> vars = analysis.getLocalVariables();
        assertEquals(3, vars.size());
        assertTrue(vars.contains("x"));
        assertTrue(vars.contains("cheese"));
        assertTrue(vars.contains("thisIsAGoodVar"));
    } catch (RecognitionException e) {
        e.printStackTrace();
        fail("Not supposed to raise exception: " + e.getMessage());
    }
}
Also used : JavaAnalysisResult(org.drools.mvel.java.JavaAnalysisResult) JavaExprAnalyzer(org.drools.mvel.java.JavaExprAnalyzer) HashMap(java.util.HashMap) RecognitionException(org.antlr.runtime.RecognitionException) BoundIdentifiers(org.drools.compiler.compiler.BoundIdentifiers) Test(org.junit.Test)

Example 3 with JavaExprAnalyzer

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

the class JavaConsequenceBuilderTest method testFixExitPointsReferences.

@Test
public void testFixExitPointsReferences() {
    String consequence = " System.out.println(\"this is a test\");\n " + " exitPoints[\"foo\"].insert( new Cheese() );\n " + " System.out.println(\"we are done with exitPoints\");\n ";
    setupTest(consequence, new HashMap<String, Object>());
    try {
        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.getExitPoint(\"foo\").insert( new Cheese() );\n " + " System.out.println(\"we are done with exitPoints\");\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 4 with JavaExprAnalyzer

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

the class JavaConsequenceBuilderTest 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 5 with JavaExprAnalyzer

use of org.drools.mvel.java.JavaExprAnalyzer 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)

Aggregations

BoundIdentifiers (org.drools.compiler.compiler.BoundIdentifiers)6 JavaAnalysisResult (org.drools.mvel.java.JavaAnalysisResult)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)4 ImportDeclaration (org.drools.core.rule.ImportDeclaration)4 HashMap (java.util.HashMap)1 JavaAccumulateBuilder (org.drools.mvel.java.JavaAccumulateBuilder)1