]
Petr Široký resolved DROOLS-383.
--------------------------------
Assignee: Petr Široký (was: Mark Proctor)
Fix Version/s: 6.3.0.Beta2
Resolution: Done
switch over String in function
------------------------------
Key: DROOLS-383
URL:
https://issues.jboss.org/browse/DROOLS-383
Project: Drools
Issue Type: Bug
Affects Versions: 6.0.0.Final
Reporter: Matteo Mortari
Assignee: Petr Široký
Fix For: 6.3.0.Beta2
Attachments: 20131219.drools6scratch-switchoverstring.zip
*Executive Summary*: if I try to implement switch over String as per Java 1.7, inside of
DRL function, compilation of DRL fails, but the error message would suggest that actually
switch over String should work.
*Steps to reproduce*
Have a DRL file with function with String parameter and inside the function a switch over
the parameter, for example
{code:title=acme.drl|borderStyle=solid}
rule "Stupid rule over String"
when
String()
then
System.out.println("Hello world");
end
function void theTest(String input) {
switch(input) {
case "Hello World" :
System.out.println("yep");
break;
default :
System.out.println("uh");
break;
}
}
{code}
Have simple code to compile DRL for default KieBase, for example snippet:
{code:title=App.java|borderStyle=solid}
String input = "Hello World";
// Java 1.7
switch(input) {
case "Hello World" :
System.out.println("yep");
break;
default :
System.out.println("uh");
break;
}
System.out.println( "Hello World!" );
KieServices ks = KieServices.Factory.get();
KieContainer kContainer = ks.getKieClasspathContainer();
KieBase kieBase = kContainer.getKieBase();
for ( KiePackage kp : kieBase.getKiePackages() ) {
for (Rule rule : kp.getRules()) {
System.out.println("kp" + kp + " rule " + rule.getName());
}
}
{code}
Console would log DRL compile fails with error:
{noformat}
Exception in thread "main" java.lang.RuntimeException: Error while creating
KieBase[Message [id=1, level=ERROR, path=acme.drl, line=-1, column=0
text=Error importing : 'defaultpkg.TheTest.theTest'], Message [id=2,
level=ERROR, path=acme.drl, line=8, column=0
text=[ function theTesttheTest (line:8): Cannot switch on a value of type String. Only
convertible int values, strings or enum constants are permitted
]], Message [id=3, level=ERROR, path=acme.drl, line=1, column=0
text=Rule Compilation error The import defaultpkg.TheTest cannot be resolved]]
{noformat}
Please notice the 'strings' bit in the error message : {{Cannot switch on a value
of type String. Only convertible int values, *strings* or enum constants are permitted}}
Therefore would suggest it would compile a switch over String, but for some reason it is
failing to recognize that input parameter is indeed a String?
Thank you
Ciao
MM