Last active
November 6, 2020 08:02
-
-
Save tkobayas/578be09bc32641d605ec1cfdf3bb7bde to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Test | |
public void testAccumulateLambdaToString() { | |
SecurityManager origSecurityManager = System.getSecurityManager(); | |
try { | |
System.setSecurityManager(new SecurityManager() { | |
@Override | |
public void checkPermission(Permission perm) { | |
if (perm instanceof RuntimePermission && "accessDeclaredMembers".equals(perm.getName())) { | |
for (StackTraceElement elem : Thread.currentThread().getStackTrace()) { | |
if (elem.toString().contains("LambdaIntrospector.getExpressionHash") || elem.toString().contains("DummyLambdaPrinter.getExpressionHash")) { | |
fail("Introspector getExpressionHash is called : elem = " + elem); | |
} | |
} | |
} | |
} | |
}); | |
String str = | |
"import " + Person.class.getCanonicalName() + ";" + | |
"import " + Result.class.getCanonicalName() + ";" + | |
"rule X when\n" + | |
" accumulate ( $p: Person ( getName().startsWith(\"M\") ); \n" + | |
" $sum : sum($p.getAge()) \n" + | |
" ) \n" + | |
"then\n" + | |
" insert(new Result($sum));\n" + | |
"end"; | |
KieSession ksession = getKieSession(str); | |
RuleImpl rule = (RuleImpl) ksession.getKieBase().getKiePackage("defaultpkg").getRules().iterator().next(); | |
Pattern accumulatePattern = (Pattern) rule.getLhs().getChildren().get(0);// AccumulatePattern | |
SingleAccumulate singleAccumulate = (SingleAccumulate) accumulatePattern.getSource(); | |
Accumulator accumulator = singleAccumulate.getAccumulators()[0]; | |
if (accumulator instanceof LambdaAccumulator.BindingAcc) { | |
LambdaAccumulator.BindingAcc accumulator1 = (LambdaAccumulator.BindingAcc) accumulator; | |
BindingEvaluator bindingEvaluator = accumulator1.getBindingEvaluator(); | |
BindViewItem1 binding = (BindViewItem1) bindingEvaluator.getBinding(); | |
String result = binding.getBindingFunction().toString(); | |
System.out.println("toString : " + result); | |
} | |
} finally { | |
System.setSecurityManager(origSecurityManager); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment