use of org.drools.mvel.java.JavaAnalysisResult in project drools by kiegroup.
the class AbstractASMConsequenceBuilder method consequenceContext.
private Map<String, Object> consequenceContext(RuleBuildContext context, String consequenceName) {
String className = consequenceName + "Consequence";
Map<String, Declaration> decls = context.getDeclarationResolver().getDeclarations(context.getRule(), consequenceName);
JavaAnalysisResult analysis = JavaRuleBuilderHelper.createJavaAnalysisResult(context, consequenceName, decls);
if (analysis == null) {
// not possible to get the analysis results
return null;
}
// this will fix modify, retract, insert, update, entrypoints and channels
try {
String fixedConsequence = KnowledgeHelperFixer.fix(AsmUtil.fixBlockDescr(context, analysis, decls));
return JavaRuleBuilderHelper.createConsequenceContext(context, consequenceName, className, fixedConsequence, decls, analysis.getBoundIdentifiers());
} catch (MissingDependencyException e) {
context.addError(new MissingDependencyError(context.getRuleDescr().getResource(), e));
}
return null;
}
use of org.drools.mvel.java.JavaAnalysisResult 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();
}
}
use of org.drools.mvel.java.JavaAnalysisResult 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());
}
}
use of org.drools.mvel.java.JavaAnalysisResult 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();
}
}
use of org.drools.mvel.java.JavaAnalysisResult 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();
}
}
Aggregations