[jboss-jira] [JBoss JIRA] (DROOLS-383) switch over String in function

Juan Carlos Garcia (JIRA) issues at jboss.org
Tue Apr 21 07:00:27 EDT 2015


    [ https://issues.jboss.org/browse/DROOLS-383?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13060941#comment-13060941 ] 

Juan Carlos Garcia edited comment on DROOLS-383 at 4/21/15 7:00 AM:
--------------------------------------------------------------------

[~mfusco] [~tari_manga]

This problem also arise while using Enum type (which sounds weird), but what happens is that the eclipse compiler (for some reason) is setting the compliance level setting to 1.4 (default)
http://help.eclipse.org/luna/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2Ftasks%2Ftask-using_batch_compiler.htm

Be aware that (target/source and compliance) settings are differents things.

While researching on this found a least known configuration property *"org.eclipse.jdt.core.compiler.compliance"* which actually fix this problem.

The link below gave some  clues, about this
http://maven.40175.n5.nabble.com/JDT-compiler-compliance-level-as-a-parameter-for-eclipse-eclipse-target-of-maven-eclipse-plugin-td5715018.html

Basically is just matter to patch: /drools-compiler/src/main/java/org/drools/compiler/commons/jci/compilers/EclipseJavaCompilerSettings.java

{code}
public static final String CompilerOptions_OPTION_Compliance = "org.eclipse.jdt.core.compiler.compliance"; //$NON-NLS-1$

....
....
Map toNativeSettings() {
    final Map map = new HashMap(defaultEclipseSettings);

    map.put(CompilerOptions_OPTION_SuppressWarnings, isWarnings()?CompilerOptions_GENERATE:CompilerOptions_DO_NOT_GENERATE);
    map.put(CompilerOptions_OPTION_ReportDeprecation, isDeprecations()?CompilerOptions_GENERATE:CompilerOptions_DO_NOT_GENERATE);
    map.put(CompilerOptions_OPTION_TargetPlatform, toNativeVersion(getTargetVersion()));
    map.put(CompilerOptions_OPTION_Source, toNativeVersion(getSourceVersion()));
    //set the compliance level
    map.put(CompilerOptions_OPTION_Compliance, toNativeVersion(getSourceVersion()));
    //
    map.put(CompilerOptions_OPTION_Encoding, getSourceEncoding());

    return map;
}
{code}

I will submit a pull request via GitHub so this fix makes it into the upstream.



was (Author: jcgarciam):
[~mfusco] [~tari_manga]

This problem also arise while using Enum type (which sounds weird), but what happens is that the eclipse compiler (for some reason) is setting the compliance level setting to 1.4 (default)
http://help.eclipse.org/luna/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2Ftasks%2Ftask-using_batch_compiler.htm

Be aware that (target/source and compliance) settings are differents things.

While researching on this found a least known configuration property *"org.eclipse.jdt.core.compiler.compliance"* which actually fix this problem.

basically is just matter to patch: /drools-compiler/src/main/java/org/drools/compiler/commons/jci/compilers/EclipseJavaCompilerSettings.java

{code}
public static final String CompilerOptions_OPTION_Compliance = "org.eclipse.jdt.core.compiler.compliance"; //$NON-NLS-1$

....
....
Map toNativeSettings() {
    final Map map = new HashMap(defaultEclipseSettings);

    map.put(CompilerOptions_OPTION_SuppressWarnings, isWarnings()?CompilerOptions_GENERATE:CompilerOptions_DO_NOT_GENERATE);
    map.put(CompilerOptions_OPTION_ReportDeprecation, isDeprecations()?CompilerOptions_GENERATE:CompilerOptions_DO_NOT_GENERATE);
    map.put(CompilerOptions_OPTION_TargetPlatform, toNativeVersion(getTargetVersion()));
    map.put(CompilerOptions_OPTION_Source, toNativeVersion(getSourceVersion()));
    //set the compliance level
    map.put(CompilerOptions_OPTION_Compliance, toNativeVersion(getSourceVersion()));
    //
    map.put(CompilerOptions_OPTION_Encoding, getSourceEncoding());

    return map;
}
{code}

I will submit a pull request via GitHub so this fix makes it into the upstream.


> 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: Mark Proctor
>         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



--
This message was sent by Atlassian JIRA
(v6.3.11#6341)


More information about the jboss-jira mailing list